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
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions tests/streaming/action/test_dtmf.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
async def test_vonage_dtmf_press_digits(mocker, mock_env):
action = VonageDTMF(action_config=DTMFVocodeActionConfig())
vonage_uuid = generate_uuid()
digits = "1234"
digits = "1234*#"

vonage_phone_conversation_mock = mocker.MagicMock()
vonage_config = VonageConfig(
Expand Down Expand Up @@ -92,7 +92,7 @@ async def test_twilio_dtmf_press_digits(
mocker, mock_env, mock_twilio_phone_conversation, mock_twilio_output_device: TwilioOutputDevice
):
action = TwilioDTMF(action_config=DTMFVocodeActionConfig())
digits = "1234"
digits = "1234*#"
twilio_sid = "twilio_sid"

action.attach_conversation_state_manager(
Expand Down Expand Up @@ -136,7 +136,7 @@ async def test_twilio_dtmf_failure(
mocker, mock_env, mock_twilio_phone_conversation, mock_twilio_output_device: TwilioOutputDevice
):
action = TwilioDTMF(action_config=DTMFVocodeActionConfig())
digits = "****"
digits = "%%%%"
twilio_sid = "twilio_sid"

action.attach_conversation_state_manager(
Expand Down
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