Listen

Streams audio in real-time over a websocket connection to a third-party websocket server. Stream may be one-way only or bidirectional.

1{
2 "verb": "listen",
3 "url": "wss://myrecorder.example.com/calls",
4 "mixType" : "stereo"
5}

Parameters

actionHook
stringRequired

Webhook to invoke when the listen operation ends.
The information will include the duration of the audio stream and a ‘digits’ property if the recording was terminated by a DTMF key.

bidirectionalAudio.enabled
booleanDefaults to true

If true, enable bidirectional audio.

bidirectionalAudio.sampleRate
number

The sample rate of PCM audio sent back to jambonz over the websocket.

bidirectionalAudio.streaming
booleanDefaults to false

If true, enable streaming of audio from your application back to Jambonz (and the remote caller).

disableBidirectionalAudio
booleanDeprecated

If true, disable bidirectional audio (has the same effect as setting bidirectionalAudio.enabled to false).

finishOnKey
string

The set of digits that can end the listen action if any one of them is detected.

maxLength
number

The maximum length of the listened audio stream, in seconds. The websocket connection will be closed if this duration is reached.

metadata
object

Additional user data to add to the JSON payload sent to the remote server when the WebSocket connection is first established.

mixType
stringDefaults to mono

"mono" (send a single channel), "stereo" (send dual channels of both calls in a bridge), or "mixed" (send audio from both calls in a bridge in a single mixed audio stream).

passDtmf
booleanDefaults to false

If true, any DTMF digits detected from the caller will be passed over the WebSocket as text frames in JSON format.

playBeep
booleanDefaults to false

Whether to play a beep at the start of the listen operation.

sampleRate
numberDefaults to 8000

Sample rate of the PCM audio that will be sent from jambonz to remote server. Allowable values: 8000, 16000, 24000, 48000, or 64000.

timeout
number

The number of seconds of silence that terminates the listen operation.

transcribe
object

A nested transcribe verb.

url
stringRequired

The URL of the remote server to connect to; should be a ws or wss URL.

wsAuth.password
string

HTTP basic auth password to use on the WebSocket connection, if desired.

wsAuth.username
string

HTTP basic auth username to use on the WebSocket connection, if desired.

Audio format

Audio is sent over the websocket in linear 16-bit PCM encoding, using the sample rate specified in the sampleRate property. The audio is sent in binary frames over the websocket connection. The audio sent back from the server is expected to also be linear16 PCM encoded audio, with a sample rate specified in the bidirectionalAudio.sampleRate property.

If the bidirectionalAudio.streaming property is set to true, then the audio sent back from the server should be sent as binary frames over the websocket connection and will be streamed to the caller. Otherwise, audio that is sent back is expected to be sent as JSON text frames containing base64-encoded audio content that will be buffered and then played out to the caller once it is received in full.

Initial metadata

One text frame is sent immediately after the websocket connection is established. This text frame contains a JSON string with all of the call attributes normally sent on an HTTP request (e.g. callSid, etc), plus sampleRate and mixType properties describing the audio sample rate and stream(s). Additional metadata can also be added to this payload using the metadata property as described in the table below. Once the intial text frame containing the metadata has been sent, the remote side should expect to receive only binary frames, containing audio.

Passing DTMF

Any DTMF digits entered by the far end party on the call can optionally be passed to the websocket server as JSON text frames by setting the passDtmf property to true. Each DTMF entry is reported separately in a payload that contains the specific DTMF key that was entered, as well as the duration which is reported in RTP timestamp units. The payload that is sent will look like this:

1{
2 "event": "dtmf",
3 "dtmf": "2",
4 "duration": "1600"
5}

Bidirectional audio

Audio can also be sent back over the websocket to jambonz. This audio, if supplied, will be played out to the caller. (Note: Bidirectional audio is not supported when the listen is nested in the context of a dial verb).

There are two separate modes for bidirectional audio:

  • non-streaming, where you provide a full base64-encoded audio file as JSON text frames
  • streaming, where stream audio as L16 pcm raw audio as binary frames

non-streaming

The far-end websocket server supplies bidirectional audio by sending a JSON text frame over the websocket connection:

1{
2 "type": "playAudio",
3 "data": {
4 "audioContent": "base64-encoded content..",
5 "audioContentType": "raw",
6 "sampleRate": "16000"
7 }
8}

In the example above, raw (headerless) audio is sent. The audio must be 16-bit pcm encoded audio, with a configurable sample rate of either 8000, 16000, 24000, 32000, 48000, or 64000 khz. Alternatively, a wave file format can be supplied by using type “wav” (or “wave”), and in this case no sampleRate property is needed. In all cases, the audio must be base64 encoded when sent over the socket.

If multiple playAudio commands are sent before the first has finished playing they will be queued and played in order. You may have up to 10 queued playAudio commands at any time.

Once a playAudio command has finished playing out the audio, a playDone json text frame will be sent over the websocket connection:

1{
2 "type": "playDone"
3}

A killAudio command can also be sent by the websocket server to stop the playout of audio that was started via a previous playAudio command:

1{
2 "type": "killAudio"
3}

And finally, if the websocket connection wishes to end the listen, it can send a disconnect command:

1{
2 "type": "disconnect"
3}

Streaming

To enable bidirectional audio, you must explicitly enable it in the listen verb with the streaming property as shown below:

1{
2 verb: 'listen',
3 bidirectionalAudio: {
4 enabled: true,
5 streaming: true,
6 sampleRate: 8000
7 }
8}

Your application should then send binary frames of linear-16 pcm raw data with the specified sample rate over the websocket connection. You can specify both the sample rate that you want to receive over the websocket as well as the sample rate that you want to send back audio, and they do not need to be the same. In the example below, we choose to receive 8k sampling but send back 16K sampling. You can send any length of frame and jambonz will buffer the recieved audio to play it out at the correct sample rate, however we reccomend sending a fixed length message (320 bytes at 8Khz, 640 bytes at 16Khz), each sample is 16bit therefore takes 2 bytes so your frames should always be an even number of bytes to ensure the best playback quality

1{
2 verb: 'listen',
3 sampleRate: 8000
4 bidirectionalAudio: {
5 enabled: true,
6 streaming: true,
7 sampleRate: 16000
8 }
9}

Commands

You can send the following commands over the websocket as json frames:

  • disconnect
  • killAudio
  • mark
  • clearMarks

disconnect

1{
2 "type": "disconnect"
3}

This causes the websocket to be closed from the jambonz side, and the associated listen verb to end.

killAudio

1{
2 "type": "killAudio"
3}

This causes any audio that is playing out from the bidirectional socket as well as any buffered audio to be flushed.

mark

1{
2 "type": "mark",
3 "data": {
4 "name": "my-mark-1"
5 }
6}

You can send a mark command if you want to be able to synchronize activities on your end with the playout of the audio stream that you have provided. Because the audio you provide will usually be buffered before it is streamed, if you want to know when a specific piece of audio has started or completed, send a mark command with a name property at the point in the stream you want to sync with. When that point in the audio stream is later reached during playback, you will get a matching json frame back over the websocket:

1{
2 "type": "mark",
3 "data": {
4 "name": "my-mark-1",
5 "event": "playout"
6 }
7}

Note that event will contain either playout or cleared depending on whether the audio stream reached the mark during playout or the mark was never played out due to a killAudio command.

clearMarks

1{
2 "type": "clearMarks"
3}

This command clears (removes) and audio marks that are being tracked. When you remove the marks in this way, you will not receive mark events for the removed marks.