<?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: Harsh Mittal</title>
    <description>The latest articles on DEV Community by Harsh Mittal (@harshmittal1750).</description>
    <link>https://dev.to/harshmittal1750</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%2F528331%2F886ddcb8-8417-4dcd-a476-235c3f79f943.png</url>
      <title>DEV Community: Harsh Mittal</title>
      <link>https://dev.to/harshmittal1750</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/harshmittal1750"/>
    <language>en</language>
    <item>
      <title>What I learned building real-time 8D audio in the browser with the Web Audio API</title>
      <dc:creator>Harsh Mittal</dc:creator>
      <pubDate>Thu, 26 Nov 2026 17:03:02 +0000</pubDate>
      <link>https://dev.to/harshmittal1750/what-i-learned-building-real-time-8d-audio-in-the-browser-with-the-web-audio-api-3e27</link>
      <guid>https://dev.to/harshmittal1750/what-i-learned-building-real-time-8d-audio-in-the-browser-with-the-web-audio-api-3e27</guid>
      <description>&lt;p&gt;"8D audio" is the effect where a song seems to circle around your head when you wear headphones. Most of the 8D tracks on YouTube are pre-rendered: someone ran the song through a DAW and uploaded the result.&lt;/p&gt;

&lt;p&gt;I wanted it live, on any song you're already playing, with nothing uploaded anywhere. That became &lt;a href="https://8dfy.xyz" rel="noopener noreferrer"&gt;8Dfy&lt;/a&gt;, a browser extension for YouTube, YouTube Music, Apple Music and Spotify Web. Everything runs in the Web Audio API, inside the tab.&lt;/p&gt;

&lt;p&gt;The basic version takes about 20 lines. Making it sound good took months, and nearly every improvement started as a bug I didn't understand. These are the ones worth writing down.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Getting at the page's audio
&lt;/h2&gt;

&lt;p&gt;The entry point is &lt;code&gt;createMediaElementSource&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;AudioContext&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="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;source&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createMediaElementSource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;videoElement&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;source&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;destination&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// nothing is audible until you do this&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two things surprised me:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;It's a one-way door.&lt;/strong&gt; Once a media element is connected, its sound only comes out through your graph. If your graph breaks, the video goes silent, so I added a watchdog that checks the processed signal is still alive.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;One source per element, ever.&lt;/strong&gt; Calling it a second time on the same &lt;code&gt;&amp;lt;video&amp;gt;&lt;/code&gt; throws &lt;code&gt;InvalidStateError&lt;/code&gt;. YouTube reuses its video element across navigations, so the source must be created once and kept.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  2. The classic 8D: a panner and a slow oscillator
&lt;/h2&gt;

&lt;p&gt;The core effect is a &lt;code&gt;StereoPannerNode&lt;/code&gt; whose &lt;code&gt;pan&lt;/code&gt; swings between -1 and 1 every few seconds:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;panner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createStereoPanner&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="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;lfo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createOscillator&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="c1"&gt;// the "low-frequency oscillator"&lt;/span&gt;
&lt;span class="n"&gt;lfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;frequency&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.15&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;           &lt;span class="c1"&gt;// one full swing about every 7 s&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;depth&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createGain&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;depth&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;gain&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                 &lt;span class="c1"&gt;// pan range: -1 .. 1&lt;/span&gt;

&lt;span class="n"&gt;lfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;depth&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;panner&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pan&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;lfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;start&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;&lt;strong&gt;The bug:&lt;/strong&gt; my first version moved &lt;code&gt;pan&lt;/code&gt; from &lt;code&gt;requestAnimationFrame&lt;/code&gt;. It worked until you switched tabs. Browsers pause rAF in background tabs, so the music froze wherever it happened to be, often stuck in one ear.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The fix:&lt;/strong&gt; never animate audio from the main thread. An &lt;code&gt;OscillatorNode&lt;/code&gt; connected to an &lt;code&gt;AudioParam&lt;/code&gt; runs on the audio thread, keeps going in background tabs, and doesn't stutter when the page is busy. Every moving part of 8Dfy now works this way, including the 3D positions below.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Real 3D with HRTF, and the stereo trap
&lt;/h2&gt;

&lt;p&gt;Panning only moves sound left and right. For sound that seems to come from in front of you, behind you or above you, the browser has a built-in HRTF engine (head-related transfer function):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;panner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;PannerNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;panningModel&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'HRTF'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;rolloffFactor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;          &lt;span class="c1"&gt;// don't let distance turn the volume down&lt;/span&gt;
  &lt;span class="n"&gt;positionX&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;positionY&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;positionZ&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&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="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;positionX/Y/Z&lt;/code&gt; are AudioParams too, so oscillators can drive them. An orbit is just &lt;code&gt;x = sin(t)&lt;/code&gt; and &lt;code&gt;z = -cos(t)&lt;/code&gt;, which is two oscillators 90° apart. I build each path as a &lt;code&gt;PeriodicWave&lt;/code&gt;, so the shape (orbit, figure-8, drift) is a few harmonic coefficients.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The bug:&lt;/strong&gt; I put a source hard right and still heard it hard left. A &lt;code&gt;PannerNode&lt;/code&gt; defaults to &lt;code&gt;channelCountMode: 'clamped-max'&lt;/code&gt;, which lets a stereo input through largely untouched, so the song's own stereo image beat the 3D position. Downmixing to mono fixed the direction but cost 6 dB and made out-of-phase content sound hollow.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The fix:&lt;/strong&gt; split the channels and give each one its own panner, with &lt;code&gt;channelCount: 1&lt;/code&gt; and &lt;code&gt;channelCountMode: 'explicit'&lt;/code&gt;. The two sources follow the same path a few degrees apart. The song keeps its width, and the position really controls the direction.&lt;/p&gt;

&lt;p&gt;There was one more surprise. Below about 150 Hz your head casts no acoustic shadow, so both panners output nearly the same bass, and their sum is up to +6 dB. Bass-heavy tracks distorted in 3D mode only, so the 3D path now has its own low-frequency compensation.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. "Surround": splitting the song into bands
&lt;/h2&gt;

&lt;p&gt;Surround mode splits the music into three bands. The lows stay centred, the mids orbit, and the highs follow at half depth.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The bug:&lt;/strong&gt; I made the split with single lowpass and highpass biquads, and the result had a ~19 dB hole at each crossover point (250 Hz and 2 kHz). At the cutoff frequency the two filters are out of phase, so they cancel.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The fix:&lt;/strong&gt; use a Linkwitz-Riley crossover, which is two Butterworth biquads in series. Its bands add back up flat. One detail to know: for &lt;code&gt;lowpass&lt;/code&gt; and &lt;code&gt;highpass&lt;/code&gt;, the Web Audio &lt;code&gt;Q&lt;/code&gt; is in &lt;strong&gt;dB&lt;/strong&gt;, so Butterworth is &lt;code&gt;-3.01&lt;/code&gt;, not &lt;code&gt;0.707&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="no"&gt;BUTTERWORTH_Q_DB&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log10&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;SQRT1_2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// ≈ -3.01&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;lr4&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;freq&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;BiquadFilterNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;frequency&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;freq&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;Q&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;BUTTERWORTH_Q_DB&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="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;BiquadFilterNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;frequency&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;freq&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;Q&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;BUTTERWORTH_Q_DB&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;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;b&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;The bands now sum within 0.2 dB. I also removed a 7 ms "width" delay on the highs that combed against the mids, putting a 7 dB dip right through the vocal presence range.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Slowed + Reverb
&lt;/h2&gt;

&lt;p&gt;"Slowed + reverb" is mostly two things.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Slowed&lt;/strong&gt; is a tape-style speed change: lower speed, lower pitch.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="n"&gt;video&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;preservesPitch&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="n"&gt;video&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;playbackRate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.85&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;The bug:&lt;/strong&gt; I left &lt;code&gt;preservesPitch&lt;/code&gt; off when the user went back to normal speed. Then YouTube's own 1.5x menu turned every video into chipmunks for anyone with the extension. Now pitch only follows speed while the extension is the one changing it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reverb&lt;/strong&gt; is a &lt;code&gt;ConvolverNode&lt;/code&gt;, and you need an impulse response for it. I generate one in code instead of shipping audio files. My first attempt was white noise with a fade, and it sounded like a metallic spray. A convincing hall needs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a handful of sparse early reflections, alternating between the ears;&lt;/li&gt;
&lt;li&gt;a diffuse tail that decays exponentially (&lt;code&gt;exp(-6.91·t / RT60)&lt;/code&gt;);&lt;/li&gt;
&lt;li&gt;a lowpass that sweeps from 12 kHz down to 2 kHz across the tail, because real rooms absorb high frequencies first.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  6. A 300% volume booster that doesn't crackle
&lt;/h2&gt;

&lt;p&gt;Turning gain up past 1.0 is easy. Doing it without distortion is the hard part. My chain ends like this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;A &lt;code&gt;DynamicsCompressorNode&lt;/code&gt; set as a safety limiter&lt;/strong&gt; (threshold -1 dB, ratio 20, 1 ms attack). It's set so normal music never reaches it, which avoids audible pumping.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A soft clipper:&lt;/strong&gt; a &lt;code&gt;WaveShaperNode&lt;/code&gt; that leaves the signal unchanged below 0.85, then curves smoothly toward a 0.87 ceiling. A 1 ms attack lets the leading edge of a drum hit through, and the soft clipper catches it.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;The bug:&lt;/strong&gt; the soft clipper uses &lt;code&gt;oversample: '4x'&lt;/code&gt; to avoid aliasing, but I measured peaks of &lt;strong&gt;0.92 on impulses and 1.01 on gated transients&lt;/strong&gt; against that 0.87 ceiling. The filter that brings the 4x signal back down rings, so the oversampled stage overshoots its own curve. Smooth test signals like pink noise never revealed it, but real music went over the ceiling on 12.6% of frames in 3D mode.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The fix:&lt;/strong&gt; add a second &lt;code&gt;WaveShaperNode&lt;/code&gt; with &lt;code&gt;oversample: 'none'&lt;/code&gt; that hard-clips at 0.87. Without oversampling there's no resampling, so nothing rings. It only ever touches the rare overshoot sample, so it's inaudible.&lt;/p&gt;

&lt;p&gt;The lesson: test against real music, not just test signals.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Beat detection without the main thread
&lt;/h2&gt;

&lt;p&gt;The Beat Sync and Auto DJ features need to know where the kicks are.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The bug:&lt;/strong&gt; I polled an &lt;code&gt;AnalyserNode&lt;/code&gt; every 80 ms. An analyser only holds its last &lt;code&gt;fftSize&lt;/code&gt; samples (about 21 ms), so each poll saw about a quarter of the audio and missed most kicks. Its byte output also clips at -30 dB, so a loud bassline looked identical to a kick.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The fix:&lt;/strong&gt; a tiny &lt;code&gt;AudioWorkletProcessor&lt;/code&gt; that sees every sample. It computes the RMS of a low-passed mono signal and posts it about every 10.7 ms, stamped with the audio clock instead of &lt;code&gt;Date.now()&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BeatTap&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;AudioWorkletProcessor&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;input&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="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;ch&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&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="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ch&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&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;i&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="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;ch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;ch&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;ch&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;blocks&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;postMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;currentTime&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rms&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;1&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="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;blocks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;n&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="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the main thread, a sample counts as a kick if it is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;above -40 dBFS,&lt;/li&gt;
&lt;li&gt;38% louder than the running average of the last second,&lt;/li&gt;
&lt;li&gt;at least 260 ms after the previous kick.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For tempo, I take the median gap between kicks, fold it into 70–160 BPM, then average the gaps that agree with it. The median alone read 120 BPM as 125, because the timing lands on a grid of fixed report intervals.&lt;/p&gt;

&lt;h2&gt;
  
  
  8. Spotify Web
&lt;/h2&gt;

&lt;p&gt;Spotify's web player can't be processed from inside the page the way YouTube's can. Instead, the extension captures the tab's audio:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The user clicks the toolbar icon. This is the user action Chrome requires before tab capture.&lt;/li&gt;
&lt;li&gt;The background service worker calls &lt;code&gt;chrome.tabCapture.getMediaStreamId({ targetTabId })&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Service workers can't run an &lt;code&gt;AudioContext&lt;/code&gt;, so an &lt;strong&gt;offscreen document&lt;/strong&gt; opens the stream with &lt;code&gt;getUserMedia({ audio: { mandatory: { chromeMediaSource: 'tab', chromeMediaSourceId } } })&lt;/code&gt;, runs the same effect chain and plays the result.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It works well, but speed-based effects like Slowed aren't possible this way. You can't change the playback speed of a live audio stream.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I'd tell myself at the start
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Drive everything from the audio thread.&lt;/strong&gt; Use oscillators on AudioParams and AudioWorklets, never rAF or &lt;code&gt;setInterval&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Measure, don't guess.&lt;/strong&gt; Most of my "it sounds off" problems turned out to be specific numbers: a 19 dB notch, a 6 dB bass sum, a 0.92 overshoot.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Test with real music.&lt;/strong&gt; Pink noise passed every check that a real song failed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Read the spec defaults.&lt;/strong&gt; &lt;code&gt;channelCountMode&lt;/code&gt;, &lt;code&gt;Q&lt;/code&gt; in dB and &lt;code&gt;preservesPitch&lt;/code&gt; were each behind one of these bugs.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you want to hear the result, &lt;a href="https://chromewebstore.google.com/detail/flamcjcggbcpcdjpjheemmbknajhjbnn" rel="noopener noreferrer"&gt;8Dfy is free on the Chrome Web Store&lt;/a&gt;, and there's a &lt;a href="https://8dfy.xyz/tools/8d-audio-converter" rel="noopener noreferrer"&gt;free 8D converter&lt;/a&gt; on the site for audio files you already have. Put on headphones first. I'm happy to go deeper on any of these in the comments.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>audio</category>
      <category>showdev</category>
    </item>
  </channel>
</rss>
