Urgent.News

What's breaking now, across hundreds of outlets.

Tech

Mid/side stereo width in the browser: encode, scale, and export a WAV in plain JavaScript

I wanted a "stereo width" control for an audio tool that runs entirely in the browser. The classic way to build one is mid/side processing: convert left/right into two new signals, scale one of them, convert back. It is a few lines of arithmetic, and the browser already gives you everything else: decodeAudioData to read a file, OfflineAudioContext to render, and a small WAV writer to hand the…

I aimed to create a "stereo width" control for an audio tool that operates entirely within a web browser environment. The conventional method to achieve this involves mid/side processing, which entails converting the left and right audio channels into two distinct signals, adjusting the scale of one, and then recombining them. This technique only requires a few lines of arithmetic and is readily supported by the browser's built-in functionalities.

The process begins with decoding the audio file using decodeAudioData, rendering the processed sound with OfflineAudioContext, and finally encoding the result back into a WAV format using a compact WAV writer. This guide delves into the complete pipeline, allowing readers to verify the calculations using plain JavaScript in their local environment.

Mid and side signals are derived as follows: the mid signal represents the shared component between both channels, calculated as (L + R) / 2. Conversely, the side signal captures the differences between the channels, computed as (L - R) / 2. Sounds that are centered, such as lead vocals or a kick drum, tend to be evenly distributed across both channels, thus residing primarily in the mid signal and contributing nothing to the side signal.

Similarly, a sound that is heavily panned to the left appears predominantly in the left channel, thus splitting its presence roughly equally between the mid and side signals. Reversing the process is equally straightforward: L = mid + side and R = mid - side. By amplifying the side signal with a gain factor prior to decoding, one can effectively control the degree of stereo width.

A gain value of 0 reduces the mix to a mono output, a gain of 1 leaves the stereo image untouched, and any gain value exceeding 1 expands the stereo field.

The implementation involves handling Float32Array data structures, which are precisely what the AudioBuffer.getChannelData() method returns. This approach ensures that the core functionality can be validated in a node.js environment using synthesized audio tones, eliminating the need for any audio hardware. The provided code snippet demonstrates the implementation of mid/side processing and the export of a 16-bit WAV file directly from Float32Array arrays.

In actual browser usage, these arrays would be sourced from the AudioBuffer's getChannelData() method, while here, we are generating them synthetically to run the entire process within a node.js context.

Rewritten by urgent.news; may contain errors — check the original.

Source: dev.to

Read the original at dev.to →

Report a problem with this story

More in Tech

More from Sunday, 29 November 2026 →