<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Vahid</title>
    <description>The latest articles on DEV Community by Vahid (@vahidalizadeh).</description>
    <link>https://dev.to/vahidalizadeh</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F210282%2F21b97570-34d6-46dd-8e38-4f96d792f1ba.png</url>
      <title>DEV Community: Vahid</title>
      <link>https://dev.to/vahidalizadeh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vahidalizadeh"/>
    <language>en</language>
    <item>
      <title>Removing echo from a live call in Rust: keeping two audio streams in step</title>
      <dc:creator>Vahid</dc:creator>
      <pubDate>Tue, 24 Nov 2026 08:15:48 +0000</pubDate>
      <link>https://dev.to/vahidalizadeh/removing-echo-from-a-live-call-in-rust-keeping-two-audio-streams-in-step-1bo8</link>
      <guid>https://dev.to/vahidalizadeh/removing-echo-from-a-live-call-in-rust-keeping-two-audio-streams-in-step-1bo8</guid>
      <description>&lt;p&gt;I maintain &lt;a href="https://github.com/naxhq/NexQ" rel="noopener noreferrer"&gt;NexQ&lt;/a&gt;, an open source app that transcribes calls. It records two streams: your microphone ("You") and whatever the computer is playing ("Them"). With headphones that works well. With speakers, the microphone also hears the other person, and the transcript says every sentence twice, once under each name.&lt;/p&gt;

&lt;p&gt;This is how the echo gets removed before the audio reaches speech to text. The code was written by &lt;a href="https://github.com/PLus123456" rel="noopener noreferrer"&gt;Plus Chen&lt;/a&gt;, a contributor, in two pull requests: &lt;a href="https://github.com/naxhq/NexQ/pull/83" rel="noopener noreferrer"&gt;#83&lt;/a&gt; and &lt;a href="https://github.com/naxhq/NexQ/pull/104" rel="noopener noreferrer"&gt;#104&lt;/a&gt;. The second one is the interesting one.&lt;/p&gt;

&lt;h2&gt;
  
  
  The easy half
&lt;/h2&gt;

&lt;p&gt;WebRTC already has a good echo canceller, AEC3. There is a pure Rust port called &lt;a href="https://crates.io/crates/sonora" rel="noopener noreferrer"&gt;sonora&lt;/a&gt;, so no C++ build is needed. You give it two signals:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the reference, what the speakers are playing&lt;/li&gt;
&lt;li&gt;the capture, what the microphone heard&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It learns the path from speaker to microphone and subtracts it. Both streams here are 16 kHz mono, and AEC3 works on 10 ms frames, so 160 samples at a time:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;apm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;process_render_i16&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;reference&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="kd"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;unused&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;apm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;process_capture_i16&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cleaned&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;is_err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;cleaned&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy_from_slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;frame&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the canceller fails on a frame, the original microphone audio goes through. Losing your voice would be worse than an echo.&lt;/p&gt;

&lt;h2&gt;
  
  
  The hard half: keeping two streams in step
&lt;/h2&gt;

&lt;p&gt;AEC3 needs the reference before the microphone audio that contains its echo, and it can only find the echo within about half a second of it. Two separate devices don't promise you that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;WASAPI loopback sends nothing while the output is silent. Not zeros, nothing.&lt;/li&gt;
&lt;li&gt;One device's clock runs a little faster than the other's.&lt;/li&gt;
&lt;li&gt;Sometimes a chunk gets dropped.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The first version also had a drift of our own making. Every capture chunk was resampled to 16 kHz separately, and the length was rounded up. On a MacBook, a 512-frame chunk at 48 kHz became 171 samples when the right answer is 170.67. A 953-frame chunk at 96 kHz became 159 and not 158.8. Both streams ran fast, by different amounts, so they drifted apart by almost half a millisecond every second. Within a minute or two AEC3 was looking at the wrong moment, and the echo was back.&lt;/p&gt;

&lt;p&gt;The fix for that part is one resampler per stream that lives across chunks, so each stream comes out at exactly 16 kHz whatever the chunk size. For the rest, the canceller keeps a small queue and corrects itself.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hold the microphone back.&lt;/strong&gt; Mic audio waits 80 ms before it is processed. That way the matching reference has time to arrive.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Keep a cushion.&lt;/strong&gt; Four reference frames (40 ms) stay queued ahead of the mic frame being cleaned, to absorb uneven chunk timing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When the reference runs out, use silence until the cushion is full again.&lt;/strong&gt; On Windows this happens at every pause in the other person's speech, so it is normal and not an error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;queued&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;refilling&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;refilling&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;queued&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="no"&gt;CUSHION&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;refilling&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;When too much piles up, drop the extra.&lt;/strong&gt; Every half second it looks at the fewest frames that were queued in that time. If even the minimum was above the cushion plus three, the reference is running ahead and the excess is removed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;refilling&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fewest_queued&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;TOO_MANY&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;excess&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fewest_queued&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="no"&gt;CUSHION&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="no"&gt;FRAME&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;reference&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;drain&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;excess&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looking at the minimum and not the current value is what makes this safe. A burst of chunks raises the current value for a moment. Only real drift raises the minimum.&lt;/p&gt;

&lt;p&gt;Both corrections bring the queue back to the same place, where AEC3 already expects the echo.&lt;/p&gt;

&lt;h2&gt;
  
  
  Testing it without a room
&lt;/h2&gt;

&lt;p&gt;The tests build a fake room. The speaker signal is noise shaped like syllables, and the microphone gets it 30 ms later, quieter, with one reflection. The two streams are fed in chunks of different sizes, with gaps where loopback would send nothing. One test asserts that after four seconds the echo is down by more than 20 dB. Another checks that your own voice is still there.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where it stops working
&lt;/h2&gt;

&lt;p&gt;It holds with the two clocks up to 300 ppm apart. At about 1000 ppm the delay moves faster than AEC3 can follow, and the echo comes back. Fixing that would mean resampling the reference to the microphone's clock.&lt;/p&gt;

&lt;p&gt;The code is in &lt;a href="https://github.com/naxhq/NexQ/blob/main/src-tauri/src/audio/echo_cancel.rs" rel="noopener noreferrer"&gt;echo_cancel.rs&lt;/a&gt; and &lt;a href="https://github.com/naxhq/NexQ/blob/main/src-tauri/src/audio/resampler.rs" rel="noopener noreferrer"&gt;resampler.rs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you have solved clock drift between two audio devices in a simpler way, I'd like to read about it.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This article was written with the help of AI, from the code and tests linked above.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>rust</category>
      <category>opensource</category>
      <category>audio</category>
      <category>tauri</category>
    </item>
  </channel>
</rss>
