-
Notifications
You must be signed in to change notification settings - Fork 564
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Only send actual audio to Deepgram (#738)
- Loading branch information
1 parent
7b204f2
commit 893687e
Showing
4 changed files
with
38 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"livekit-plugins-deepgram": patch | ||
--- | ||
|
||
Only send actual audio to Deepgram using a basic audio RMS filter |
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
27 changes: 27 additions & 0 deletions
27
livekit-plugins/livekit-plugins-deepgram/livekit/plugins/deepgram/utils.py
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,27 @@ | ||
import numpy as np | ||
from livekit import rtc | ||
|
||
# This is the magic number during testing that we use to determine if a frame is loud enough | ||
# to possibly contain speech. It's very conservative. | ||
MAGIC_NUMBER_THRESHOLD = 0.004 | ||
|
||
|
||
class BasicAudioEnergyFilter: | ||
def __init__(self, *, cooldown_seconds: float = 1): | ||
self._cooldown_seconds = cooldown_seconds | ||
self._cooldown = cooldown_seconds | ||
|
||
def push_frame(self, frame: rtc.AudioFrame) -> bool: | ||
arr = np.frombuffer(frame.data, dtype=np.int16) | ||
float_arr = arr.astype(np.float32) / 32768.0 | ||
rms = np.sqrt(np.mean(np.square(float_arr))) | ||
if rms > MAGIC_NUMBER_THRESHOLD: | ||
self._cooldown = self._cooldown_seconds | ||
return True | ||
|
||
duration_seconds = frame.samples_per_channel / frame.sample_rate | ||
self._cooldown -= duration_seconds | ||
if self._cooldown > 0: | ||
return True | ||
|
||
return False |
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