Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ESUP-55] adds # and * support and also ability to press multiple buttons in a row #641

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions vocode/streaming/utils/dtmf_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from vocode.streaming.utils.singleton import Singleton

DEFAULT_DTMF_TONE_LENGTH_SECONDS = 0.3
DEFAULT_DTMF_TONE_SILENCE_SECONDS = 0.1
MAX_INT = 32767


Expand All @@ -22,6 +23,8 @@ class KeypadEntry(str, Enum):
EIGHT = "8"
NINE = "9"
ZERO = "0"
POUND = "#"
STAR = "*"


DTMF_FREQUENCIES = {
Expand All @@ -35,6 +38,8 @@ class KeypadEntry(str, Enum):
KeypadEntry.EIGHT: (852, 1336),
KeypadEntry.NINE: (852, 1477),
KeypadEntry.ZERO: (941, 1336),
KeypadEntry.STAR: (941, 1209),
KeypadEntry.POUND: (941, 1477),
}


Expand All @@ -49,6 +54,7 @@ def generate(
sampling_rate: int,
audio_encoding: AudioEncoding,
duration_seconds: float = DEFAULT_DTMF_TONE_LENGTH_SECONDS,
silence_seconds: float = DEFAULT_DTMF_TONE_SILENCE_SECONDS,
) -> bytes:
if (keypad_entry, sampling_rate, audio_encoding) in self.tone_cache:
return self.tone_cache[(keypad_entry, sampling_rate, audio_encoding)]
Expand All @@ -57,6 +63,7 @@ def generate(
tone = np.sin(2 * np.pi * f1 * t) + np.sin(2 * np.pi * f2 * t)
tone = tone / np.max(np.abs(tone)) # Normalize to [-1, 1]
pcm = (tone * MAX_INT).astype(np.int16).tobytes()
pcm += b"\0" * int(silence_seconds * sampling_rate * 2)
if audio_encoding == AudioEncoding.MULAW:
output = audioop.lin2ulaw(pcm, 2)
else:
Expand Down
Loading