Skip to content

Multichannel and stereo call recordings

The problem. A contact center records the agent and the customer on two channels of one stereo file; a phone platform gives you each leg of the call apart. Mixing them to mono and asking diarization who spoke throws away the one fact you already had, and two people talking at once become one garbled utterance.

What the API does. Gladia’s speech-to-text API transcribes each channel on its own, live or from a file: every utterance carries its channel (from 0), so channel 0 is always the same party, and parties talking over each other each keep their words.

Live APIchannels
Pre-recorded (batch)A multichannel file: each channel is transcribed (Transcribe a file)
Live modelssolaria-3, trantoria-1 (every live model)
LanguagesEvery language of the model (supported languages)
Channels1 or 2 in a live session, samples interleaved (audio formats)
Resultchannel on every utterance; live utterance ids start with it (01_00000004)
Pre-recorded billingEvery channel counted: a 10 min stereo file is 20 min of billing_time
{ "encoding": "wav/pcm", "sample_rate": 16000, "bit_depth": 16, "channels": 2, "model": "solaria-3" }

Every channel has the same encoding, sample rate and bit depth.

Send one sample of channel 0, then one of channel 1, and so on: L R L R … for stereo. A stereo WAV file is already laid out that way; send its samples as they are, without the header.

The programs below turn a mono recording into a two-party call, the first half said by the customer on channel 0 and the second half by the agent on channel 1, stream it and print each utterance with its party. Put your API key in the GLADIA_API_KEY environment variable, then run python stereo_call.py call.wav.

stereo_call.py
# pip install -U "gladiaio-sdk>=2.2"
import os
import sys
import threading
import wave
from array import array
from gladiaio_sdk import GladiaClient
API_URL = os.environ.get("GLADIA_API_URL", "https://eu1.api.gladia.io")
API_KEY = os.environ["GLADIA_API_KEY"]
PARTIES = {0: "Customer", 1: "Agent"} # who is on each channel
with wave.open(sys.argv[1]) as wav: # a 16-bit PCM WAV file, mono
rate, frames = wav.getframerate(), wav.readframes(wav.getnframes())
samples = array("h", frames)
half = len(samples) // 2
# Channel 0 says the first half then is silent, channel 1 is silent then says the second half.
stereo = array("h", bytes(4 * len(samples))) # interleaved: channel 0, channel 1, channel 0…
stereo[0 : 2 * half : 2] = samples[:half]
stereo[2 * half + 1 :: 2] = samples[half:]
stereo = stereo.tobytes()
config = {"encoding": "wav/pcm", "sample_rate": rate, "bit_depth": 16, "channels": 2, "model": "solaria-3"}
client = GladiaClient(api_key=API_KEY, api_url=API_URL)
session = client.live_v2().start_session(config)
ended, errors = threading.Event(), []
@session.on("message")
def on_message(message):
if message.type == "transcript" and message.data.is_final:
utterance = message.data.utterance
print(f"{PARTIES[utterance.channel]}: {utterance.text}")
session.on("error", errors.append)
session.once("ended", lambda _: ended.set())
# Stream once the socket is open: a stop sent while it opens can pass the audio queued until then.
connected = threading.Event()
session.once("connected", lambda _: connected.set())
session.once("ended", lambda _: connected.set()) # a session that never connects ends
session.on("error", lambda _: connected.set())
if session.status == "connected": # it opened before the listener above
connected.set()
if not connected.wait(timeout=30) or errors or ended.is_set():
sys.exit(1) # no open session to stream to
step = rate * 2 * 2 // 10 # 100 ms of 2 channels of 16-bit audio
for start in range(0, len(stereo), step):
session.send_audio(stereo[start : start + step]) # live audio: send each chunk as it is captured
session.stop_recording()
ended.wait(timeout=60)
sys.exit(1 if errors or not ended.is_set() else 0)

Upload the stereo file as it is and start a pre-recorded job (Transcribe a file): each channel is transcribed, and each utterance of result.transcription.utterances carries its channel.

  • Two channels in a live session: a session asking for more is refused when it starts. More parties than that (a meeting with a track per participant): one session per pair of tracks, or mix the extra tracks down.
  • Interleave samples, not chunks. L R L R, sample by sample (byte by byte for 8-bit μ-law and A-law). Sending 100 ms of channel 0 then 100 ms of channel 1 is read as noise on both.
  • Silence is fine. A channel nobody speaks on gives no utterance. Send its silence anyway: the channels must stay in step.
  • One party per channel. A channel with several speakers on it (a conference phone) needs speaker diarization as well.
  • A pre-recorded file is billed per channel: billing_time counts each channel’s audio (how long it took). Mix to mono first when you do not need to know who spoke.