Skip to content

Commit

Permalink
[ESUP-55] adds # and * support and also ability to press multiple but…
Browse files Browse the repository at this point in the history
…tons in a row (#641)

* adds # and * support and also ability to press multiple buttons in a row

* fix tests
  • Loading branch information
ajar98 committed Jul 16, 2024
1 parent 32f0cb4 commit 3dc1d49
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
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

0 comments on commit 3dc1d49

Please sign in to comment.