Building a Full-Duplex Voice App with an AI Coding Assistant: Pipeline Design and Usage Optimization

    A full-duplex voice app is hard to validate because correctness depends on timing—speak, reply, and barge-in—not on a clean compile. This article covers the engineering points of a full-duplex voice pipeline, an explicit state machine for testable timing, and how to shape context and task size so AI-assisted development keeps generation cost under control.

    1. From “Code That Compiles” to “A Package You Can Install”

    Generating an installable mobile app with an AI coding tool is no longer the hard part. The hard part is whether the result works in a real scenario. A note from the original material—“the quota did not blow up; debugging probably took most of the time”—captures the real cost structure of AI-assisted development: generation spends limited quota, while verification spends time. Full-duplex voice chat is among the most expensive apps to verify. Correctness is not “it compiles.” It is timing behavior: you speak, it answers, and if you interrupt mid-reply it stops.

    This article treats that as two general technical problems:

    1. Engineering design points for a full-duplex voice interaction pipeline.
    2. How to organize context and task granularity under AI-assisted development so cost per unit of output stays controllable.

    2. Layered Breakdown of a Full-Duplex Voice Pipeline

    The essential difference between full-duplex and half-duplex is that uplink capture and downlink playback overlap in time. The user can start speaking while the system is talking and successfully interrupt it. That creates four engineering points that must be handled explicitly.

    2.1 Concurrent Capture and Playback

    Capture and playback must run on separate threads or callbacks, and sample rate, channel count, and frame length must match the codec. A common approach is a fixed frame length (for example, 10–20 ms per frame) driving a ring buffer, which avoids jitter from dynamic allocation.

    1. Capture thread: mic.read(frame) -> echo cancellation -> noise reduction -> encode -> send queue
    2. Playback thread: receive queue -> decode -> jitter buffer -> speaker.write(frame)

    2.2 Echo Cancellation Is the Usability Threshold

    Once the speaker is on loudspeaker, the capture path also hears the remote audio. The model can easily treat “its own output” as “user input” and enter a self-excitation loop. The implementation must enable the platform’s echo cancellation and automatic gain, and route the reference signal correctly at initialization. Headphones can relax the requirement, but the module should not be removed.

    2.3 Barge-In Detection

    Barge-in cannot rely on a single volume threshold, or one cough will cut off the reply. A reliable approach is three conditions together: an energy gate, voice activity detection, and confirmation over consecutive frames. When the decision holds, stop playback immediately, clear the jitter buffer, then switch state.

    1. if (vadActive && energy > gate && heldFrames >= 3 && state == SPEAKING) {
    2. stopPlayback(); clearJitterBuffer();
    3. state = LISTENING;
    4. }

    2.4 End-to-End Latency Budget

    A rough line for full-duplex experience: the first audio packet arriving within about 300 ms is acceptable; beyond about 800 ms, users feel it is stuck. Split the budget: capture and encoding are on the order of tens of milliseconds; network round trip and queuing are the largest variables; server-side recognition and generation are the second largest; decode and playback buffering are again tens of milliseconds. Optimize network and buffering first, then model speed. Otherwise effort goes to the wrong place.

    3. A State Machine: Make “Correct Timing” Testable Code

    Full-duplex complexity sits in state transitions. Prefer an explicit finite state machine over scattered Boolean flags:

    1. IDLE --wake or button--> LISTENING
    2. LISTENING --silence timeout--> THINKING
    3. THINKING --first audio frame--> SPEAKING
    4. SPEAKING --barge-in decision--> LISTENING
    5. SPEAKING --playback finished--> LISTENING
    6. any state --error or timeout--> IDLE

    The direct benefit is debugging: print state-transition logs to locate problems such as “why it never listens to me,” instead of guessing from the waveform.

    4. Context and Quota Optimization for AI-Assisted Development

    Back to cost. Consumption in an AI coding tool is driven mainly by input context length times the number of round trips, not by the final line count. The reason the quota “did not blow up” in the source material is that the task was bounded to a clearly scoped deliverable. Four reusable practices:

    1. Small tasks, clear acceptance. Split “build a voice app” into five steps—capture working, echo cancellation, state machine, barge-in, and package install—each with an observable acceptance criterion.
    2. Feed only the context that is needed. Paste key interface signatures and the error stack, not the whole file. Trim long logs to the relevant fragment first.
    3. Ask the tool for code that can test itself. Require logging points and toggle parameters in the same output, so verification cost moves from “another chat round” to “run it once locally.”
    4. Freeze repeated conventions. Write naming, directory layout, and error-handling rules as a fixed note and reuse it, so each round does not re-explain them.

    Compile time and on-device debugging usually dwarf generation time. That is why the quota holds while time is consumed. Cutting the number of round trips is more effective than shortening a single prompt.

    5. Implementation Checklist

    • Capture and playback sample parameters match, and frame length is fixed.
    • Echo cancellation and noise reduction are enabled, and the reference route is correct.
    • Barge-in requires the three-way confirmation and clears the buffer.
    • State transitions are logged, and errors can reset to IDLE.
    • Network jitter has a buffer and a frame-drop compensation strategy.
    • First-packet latency is instrumented and measurable, not a feeling of lag.
    • Permission requests and background audio policy are verified on a real device.

    6. Summary

    The difficulty of a full-duplex voice app is not code volume. It is two physical constraints: timing and echo. The difficulty of AI-assisted development is not generation capability. It is context organization and verification efficiency. Layer the pipeline, make state explicit, and cut work into small tasks that carry their own acceptance criteria. That improves delivery quality and spends compute on the steps that matter.