-
Notifications
You must be signed in to change notification settings - Fork 397
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #158 from pipecat-ai/use-pyloudnorm-loudness
interruptions: introduce pyloudnorm to compute loudness
- Loading branch information
Showing
5 changed files
with
64 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# | ||
# Copyright (c) 2024, Daily | ||
# | ||
# SPDX-License-Identifier: BSD 2-Clause License | ||
# | ||
|
||
import numpy as np | ||
import pyloudnorm as pyln | ||
|
||
|
||
def normalize_value(value, min_value, max_value): | ||
normalized = (value - min_value) / (max_value - min_value) | ||
normalized_clamped = max(0, min(1, normalized)) | ||
return normalized_clamped | ||
|
||
|
||
def calculate_audio_volume(audio: bytes, sample_rate: int) -> float: | ||
audio_np = np.frombuffer(audio, dtype=np.int16) | ||
audio_float = audio_np.astype(np.float64) | ||
|
||
block_size = audio_np.size / sample_rate | ||
meter = pyln.Meter(sample_rate, block_size=block_size) | ||
loudness = meter.integrated_loudness(audio_float) | ||
|
||
# Loudness goes from -20 to 80 (more or less), where -20 is quiet and 80 is | ||
# loud. | ||
loudness = normalize_value(loudness, -20, 80) | ||
|
||
return loudness | ||
|
||
|
||
def exp_smoothing(value: float, prev_value: float, factor: float) -> float: | ||
return prev_value + factor * (value - prev_value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters