Lesson 139 — Advanced

Audio: Realtime Voice Agents

Voice agent kaam tabhi karta hai jab jawab insaan jaisi deri se aaye — aur deri har millisecond mein banti hai jo tum control kar sakte ho. Is lesson mein hum voice loop ka latency budget, cascaded vs speech-to-speech architecture, VAD endpointing, barge-in, partial transcripts, tool calls ke waqt ka silence aur bad call ka reconstruction dekhenge.

⏱ 30 min• Advanced• Audio: Speech to Text & Text to Speech

Voice loop kya hai

Kya hai?

Realtime voice agent ek live conversation loop hai: suno (STT), socho (LLM), bol do (TTS) — itni der mein ki user ko lage baat ho rahi hai. Isme naya challenge latency aur turn-taking hai, kyunki har silence user sun raha hota hai.

Kab kaam aata hai?

Customer support calls, voice assistants, in-car agents, IVR replacement, coaching bots — jahan user bol kar jawab chahta hai aur screen nahi hai. Text chat wale instincts yahan kaam nahi karte.

Kahan milta hai?

Realtime speech APIs mein, ya khud ke cascaded loop mein — mic → VAD → STT → LLM → TTS → speaker, WebSocket ya WebRTC ke upar chalte hue.

Ek turn hota hai: user bolta hai, tum uska bolna band hona pehchante ho, tabhi transcript model ko jaata hai, jawab banta hai aur playback shuru hota hai. Ye chain chhote-chhote timed parts mein tootti hai — aur jis deri se user ke beech gap sunai deta hai, wahi is domain ka asli product problem hai.

Mental model: User speech → VAD decide karta hai ki bolna ruk gaya → transcript model ko jaata hai → jawab banta hai → speaker chalta hai; user beech mein bole toh playback ruk kar loop wahi se reset. Ye loop hi tumhara design unit hai.

Latency budget — milliseconds kahan jaate hain

# Ek turn ka latency budget (ms) — illustrative targets, apna khud ka measure karo
capture   = 50     # mic buffering + frame collection
network   = 100    # upload + download legs
stt       = 300    # transcript banana (endpoint ke baad)
llm       = 500    # sochna (first token tak)
tts       = 200    # pehla audio chunk
--------------------------------------------
total     = 1150   # 1 sec+ = awkward silence

# Bachaane ke tareeke: overlap (niche), streaming, chhota prompt,
# aur waiting ke waqt holding phrase — sab mila kar budget antar
Rule of thumb: Silence ko kabhi khali mat chhodo — sochne ke naam se chhota holding phrase bolo ("ek second, dekh raha hoon") ya jawab ko chhota karke turant bolna shuru karo. Dead air hi voice mein sabse bada UX bug hai.

Cascaded vs speech-to-speech

# Cascaded (tumhara control): teen steps, teeno inspect ho sakte hain
audio -> stt -> transcript -> llm -> text -> tts -> audio
# har stage ka apna log, apna eval, apna swap

# Speech-to-speech (ek call): audio in, audio out
audio -> realtime_model -> audio
# kam round-trips, par andar ka path chhupa rehta hai

# Compare karte waqt columns:
# latency | cost (per min vs per step) | debuggability | tool use
Interview rule: Cascaded choose tab jab inspectability chahiye (audit trail, tool use, per-step eval); speech-to-speech tab jab latency aur natural turn-taking hi product hai. Dono ka eval alag karo — ek ke numbers se doosre ko judge mat karo.

Endpointing aur barge-in

# Endpointing: kya user ne sachmuch bolna band kiya?
def should_end_turn(audio_stream, silence_ms=700, frame_ms=20):
    silent = 0
    for frame in audio_stream:
        if is_speech(frame):
            silent = 0
        else:
            silent += frame_ms
        if silent >= silence_ms and last_hypothesis_stable():
            return True          # turn end — abhi transcript ko bhejo
    return False

# Barge-in: user ne agent ke jawab ke beech mein bol diya
def on_user_speech_during_playback():
    speaker.stop()               # playback rok do
    discard_unplayed()           # baaki jawab phenk do
    cancel_pending_generation()
    state.commit(heard_only())   # sirf jo suna wahi state mein rakho
Endpointing hi darwaza hai: Jaldi daba diya toh user ka shabd kat gaya; der kar di toh awkward silence. Dono taraf ka tuning apne audio pe karo — apne typical pauses, accents aur background ke saath, default threshold pe nahi.

Silence, tool calls aur bad calls

user bolta hai -> VAD sunta hai -> ruk gaya? (endpointing) -> STT -> LLM
LLM bolta hai -> TTS -> speaker
user bole beech mein -> playback stop + discard + state rollback (barge-in)

tool call chal rahi hai -> "dekhta hoon" (holding phrase) -> result/fallback
session khatam -> audio + transcript + timings + turn boundaries logged
Debug mantra: Ek kharab call ka poora timeline nikalo: user kahan bola, VAD kahan ruka, kya transcribe hua, model ko kya gaya, kya play hua. Aadhi voice bugs timeline dekhte hi dikhte hain — guess se nahi.

Exercise

Question: User ki awaaz mein silence dekh kar decide karna ki uska turn khatam hua — us mechanism ka naam? (ek shabd)

Question: Jab user agent ke jawab ke beech mein bol kar use rok de, us interruption ko kya kehte hain? (ek shabd)

Question: STT, LLM aur TTS ko sequence mein jod kar banaya gaya voice architecture — cascaded ya speech-to-speech?

Question: Capture, network, STT, model, TTS aur playback — poori chain ka total time kis metric se measure karte hain? (end-to-end ___)

Common mistakes

Desi example: Auto-receptionist office ka phone

Accha receptionist caller ko tabhi aage badhta hai jab caller ruk jaata hai. Caller beech mein bole toh receptionist turant chup ho jaaye (barge-in), aur agar deri lage toh "Ramesh ji se baat karwa rahi hoon" bole (holding phrase) — chup khadi phone line sabse bura lagta hai:

text
caller bolta hai  -> chup suno (VAD) -> ruk gaya? (endpointing) -> aage badho
agent bol raha hai -> caller bole -> turant ruko (barge-in)
wait lambi        -> holding phrase -> warna caller phone rakh dega
accha receptionist = accha voice agent loop
Interview angle — yahan se sawaal aate hain
  • Q: Voice agent ka sabse pehla design constraint kya hai? — End-to-end latency; ek second se upar ki khadi silence conversation ko tootti hai.
  • Q: Cascaded aur speech-to-speech mein kaise decide karoge? — Cascaded: logs, per-step control, tool use; speech-to-speech: latency aur natural flow. Requirement decide karti hai.
  • Q: Endpointing itna difficult kyun hai? — Sochne ke pauses, dheere bolne wale aur background chatter sab turn-end jaisa dikhte hain; fixed timeout dono taraf fail karta hai.
  • Q: Barge-in ke baad state kaise consistent rakhte ho? — Playback stop, generation cancel, unplayed discard; history mein sirf actually-played wala hissa.
  • Q: Tool call ke dauraan silence kaise bharte ho? — Acknowledgement phrase + result/failure fallback; dead air hi sabse bada UX bug hai.
Lesson complete?

Voice loop ka core clear — latency budget, endpointing, barge-in aur dead-air handling ab tumhare paas hai. Agla lesson — Cross-modal Search & Retrieval — mein dekhenge image aur text ek hi vector space mein kaise dhoondhte hain.

Popular courses: Data Science Course in Lucknow · AI Course in Lucknow · Python Course in Lucknow · Power BI Course · Data Analytics Course