<?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: Jesse Pinkman</title>
    <description>The latest articles on DEV Community by Jesse Pinkman (@jphfa).</description>
    <link>https://dev.to/jphfa</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3909215%2F3c174be7-9d57-4771-a884-3f08ae721835.png</url>
      <title>DEV Community: Jesse Pinkman</title>
      <link>https://dev.to/jphfa</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jphfa"/>
    <language>en</language>
    <item>
      <title>I rewrote mp3gain in Rust — 'compatible' turned out to be three different things</title>
      <dc:creator>Jesse Pinkman</dc:creator>
      <pubDate>Sat, 02 May 2026 21:29:13 +0000</pubDate>
      <link>https://dev.to/jphfa/i-rewrote-mp3gain-in-rust-compatible-turned-out-to-be-three-different-things-4fbc</link>
      <guid>https://dev.to/jphfa/i-rewrote-mp3gain-in-rust-compatible-turned-out-to-be-three-different-things-4fbc</guid>
      <description>&lt;p&gt;If you maintain a podcast, music server, or any audio pipeline that needs consistent volume across files, there's a non-trivial chance you have one of these somewhere:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;RUN &lt;/span&gt;apt-get &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-y&lt;/span&gt; mp3gain
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Or in a beets config:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;replaygain&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;backend&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;command&lt;/span&gt;
  &lt;span class="na"&gt;command&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;mp3gain&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Or buried in a cron job from 2014.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;mp3gain&lt;/code&gt; was written in C by Glen Sawyer in 2003. Upstream development stopped around 2009. Distributors (Debian, Ubuntu, Homebrew) keep it alive with security patches, but no new features have shipped in 15+ years. Its AAC counterpart &lt;code&gt;aacgain&lt;/code&gt; died around the same time and doesn't even build cleanly on modern 64-bit systems.&lt;/p&gt;

&lt;p&gt;People keep using both because the popular alternatives — &lt;code&gt;loudgain&lt;/code&gt;, &lt;code&gt;rsgain&lt;/code&gt;, &lt;code&gt;ffmpeg loudnorm&lt;/code&gt; — solve a &lt;em&gt;related&lt;/em&gt; problem (writing ReplayGain tags) but not the &lt;em&gt;same&lt;/em&gt; problem. A tag-only tool doesn't help when your players ignore tags entirely: DJ hardware, smart speakers, most car audio, podcast publishing pipelines that bake volume into the file. For those, you need the bitstream itself rewritten — losslessly, reversibly, fast.&lt;/p&gt;

&lt;p&gt;Rather than CVE-patch a 22-year-old C codebase one more time, I spent the last year writing &lt;a href="https://github.com/M-Igashi/mp3rgain" rel="noopener noreferrer"&gt;mp3rgain&lt;/a&gt;, a Rust implementation that reads and writes the same files mp3gain does. Halfway through, I realized the word "compatible" was hiding three completely different things.&lt;/p&gt;
&lt;h2&gt;
  
  
  Layer 1 — byte-identical output
&lt;/h2&gt;

&lt;p&gt;The strictest compatibility claim is that the &lt;em&gt;output file&lt;/em&gt; is bit-for-bit identical:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cp &lt;/span&gt;original.mp3 a.mp3 &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;cp &lt;/span&gt;original.mp3 b.mp3
mp3gain  &lt;span class="nt"&gt;-g&lt;/span&gt; 2 a.mp3
mp3rgain &lt;span class="nt"&gt;-g&lt;/span&gt; 2 b.mp3
&lt;span class="nb"&gt;sha256sum &lt;/span&gt;a.mp3 b.mp3
&lt;span class="c"&gt;# → same hash&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;To get there, the Rust implementation has to match every detail of the C version's bitstream rewrite: synchronization word detection, MPEG version dispatch, side-information size calculation (which differs by MPEG version × channel mode), and bit-level reads/writes that span byte boundaries.&lt;/p&gt;

&lt;p&gt;I wanted to "clean up" something the C code did awkwardly more than once. Every time I had to remind myself: the moment I lose byte-identical output, I lose the right to call this a drop-in replacement. There's a CI script (&lt;code&gt;scripts/compatibility-test.sh&lt;/code&gt;) that diffs SHA-256 hashes between both tools across MPEG1/2/2.5, mono/stereo/joint stereo, CBR/VBR, and a range of gain values. If even one case mismatches, the PR doesn't merge.&lt;/p&gt;
&lt;h2&gt;
  
  
  Layer 2 — tag interoperability
&lt;/h2&gt;

&lt;p&gt;mp3gain stores undo information in APEv2 tags:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight properties"&gt;&lt;code&gt;&lt;span class="py"&gt;mp3gain_undo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;-3,-2,N&lt;/span&gt;
&lt;span class="py"&gt;mp3gain_minmax&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;100,148&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;If I run &lt;code&gt;mp3gain -g 2&lt;/code&gt;, then later &lt;code&gt;mp3rgain -u&lt;/code&gt;, the undo has to work — and vice versa. This is a different layer from byte-identical output: it's about the &lt;em&gt;metadata block&lt;/em&gt;, not the audio frame data.&lt;/p&gt;

&lt;p&gt;mp3rgain reads and writes the same APEv2 fields with the same string format. There's one intentional break: after &lt;code&gt;-u&lt;/code&gt;, mp3gain leaves an empty APEv2 tag block in place (probably because rewriting it would shift downstream frame offsets). mp3rgain removes the tag completely. The audio data is identical either way and the bidirectional undo property still holds, so I judged this as still "compatible enough."&lt;/p&gt;
&lt;h2&gt;
  
  
  Layer 3 — text protocol
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;mp3gain -o&lt;/code&gt; (no argument) prints a tab-separated table:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csvs"&gt;&lt;code&gt;&lt;span class="k"&gt;File&lt;/span&gt;    &lt;span class="k"&gt;MP&lt;/span&gt;&lt;span class="mf"&gt;3&lt;/span&gt; &lt;span class="k"&gt;gain&lt;/span&gt;    &lt;span class="k"&gt;dB&lt;/span&gt; &lt;span class="k"&gt;gain&lt;/span&gt; &lt;span class="k"&gt;Max&lt;/span&gt; &lt;span class="k"&gt;Amplitude&lt;/span&gt;   &lt;span class="k"&gt;Max&lt;/span&gt; &lt;span class="k"&gt;global&lt;/span&gt;&lt;span class="err"&gt;_&lt;/span&gt;&lt;span class="k"&gt;gain&lt;/span&gt; &lt;span class="k"&gt;Min&lt;/span&gt; &lt;span class="k"&gt;global&lt;/span&gt;&lt;span class="err"&gt;_&lt;/span&gt;&lt;span class="k"&gt;gain&lt;/span&gt;
&lt;span class="k"&gt;song&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="k"&gt;mp&lt;/span&gt;&lt;span class="mf"&gt;3&lt;/span&gt;    &lt;span class="mf"&gt;0&lt;/span&gt;   &lt;span class="mf"&gt;0.0&lt;/span&gt; &lt;span class="mf"&gt;17234&lt;/span&gt;   &lt;span class="mf"&gt;148&lt;/span&gt; &lt;span class="mf"&gt;100&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;a href="https://beets.io/" rel="noopener noreferrer"&gt;beets&lt;/a&gt; parses this with regex. So do an unknown number of personal scripts that have run unmodified for a decade. Change the column order, the header text, or the separator, and you break all of them silently.&lt;/p&gt;

&lt;p&gt;mp3rgain emits the exact same header — one &lt;code&gt;println!&lt;/code&gt; line at &lt;a href="https://github.com/M-Igashi/mp3rgain/blob/master/src/main.rs#L1275" rel="noopener noreferrer"&gt;main.rs:1275&lt;/a&gt;:&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="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"File&lt;/span&gt;&lt;span class="se"&gt;\t&lt;/span&gt;&lt;span class="s"&gt;MP3 gain&lt;/span&gt;&lt;span class="se"&gt;\t&lt;/span&gt;&lt;span class="s"&gt;dB gain&lt;/span&gt;&lt;span class="se"&gt;\t&lt;/span&gt;&lt;span class="s"&gt;Max Amplitude&lt;/span&gt;&lt;span class="se"&gt;\t&lt;/span&gt;&lt;span class="s"&gt;Max global_gain&lt;/span&gt;&lt;span class="se"&gt;\t&lt;/span&gt;&lt;span class="s"&gt;Min global_gain"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;New structured output lives behind &lt;code&gt;-o json&lt;/code&gt;, opt-in, never the default.&lt;/p&gt;
&lt;h2&gt;
  
  
  What I deliberately &lt;em&gt;didn't&lt;/em&gt; keep
&lt;/h2&gt;

&lt;p&gt;Compatibility isn't free, and not every quirk is worth preserving:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;AAC support&lt;/strong&gt;: mp3gain has none. mp3rgain rewrites AAC &lt;code&gt;global_gain&lt;/code&gt; in place (the same idea aacgain used) and stores undo info in MP4 freeform metadata atoms because APEv2 doesn't fit MP4 containers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;-o json&lt;/code&gt; and &lt;code&gt;--dry-run&lt;/code&gt;&lt;/strong&gt;: new flags for automated pipelines. Preview safely, then apply — something the original CLI didn't really support.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ID3v2 RVA2 / TXXX ReplayGain tags (&lt;code&gt;-s i&lt;/code&gt;)&lt;/strong&gt;: opt-in. foobar2000, mpd, and other ReplayGain-aware players read these; APEv2 tags are invisible to them.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Migrating: what it actually looks like
&lt;/h2&gt;

&lt;p&gt;For most pipelines, migration is one substitution.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Shell scripts:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; &lt;span class="s1"&gt;'s/\bmp3gain\b/mp3rgain/g'&lt;/span&gt; your_pipeline.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Dockerfile&lt;/strong&gt; — replace the apt-installed binary with a 2 MB static image:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;- RUN apt-get install -y mp3gain &amp;amp;&amp;amp; rm -rf /var/lib/apt/lists/*
- ENTRYPOINT ["mp3gain"]
- FROM ghcr.io/m-igashi/mp3rgain:latest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;That's it. The image is &lt;code&gt;FROM scratch&lt;/code&gt; with a musl-static binary: no shell, no glibc, no apt cache to clean.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;beets&lt;/strong&gt; — change one line in &lt;code&gt;~/.config/beets/config.yaml&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;replaygain&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;backend&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;command&lt;/span&gt;
&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;command&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;mp3gain&lt;/span&gt;
&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;command&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;mp3rgain&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The full migration guide is at &lt;a href="https://github.com/M-Igashi/mp3rgain/blob/master/docs/migrating-from-mp3gain.md" rel="noopener noreferrer"&gt;docs/migrating-from-mp3gain.md&lt;/a&gt;, with sed patterns, CI snippets, and the apt/dnf/pacman/brew/winget/cargo install matrix.&lt;/p&gt;
&lt;h2&gt;
  
  
  Why bother
&lt;/h2&gt;

&lt;p&gt;Three reasons that mattered to me:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Memory safety&lt;/strong&gt;. mp3gain's history includes a stream of CVEs — heap overflows in the side-info parser, mostly. Patching those in 2025 means tracking down a long-quiet maintainer's intent. A Rust rewrite removes the whole class from the picture.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;AAC&lt;/strong&gt;. Most personal libraries on Apple platforms are AAC, and there's been no working tool to volume-normalize them losslessly since aacgain stopped building. DJ hardware, car audio, and smart speakers all ignore ReplayGain tags, so tag-only tools don't help.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Distribution&lt;/strong&gt;. Static binary in a 2 MB image, plus packages on Homebrew / Winget / AUR / PPA / Docker / Cargo. No "build from source on this niche distro" required.&lt;/li&gt;
&lt;/ol&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://assets.dev.to/assets/github-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/M-Igashi" rel="noopener noreferrer"&gt;
        M-Igashi
      &lt;/a&gt; / &lt;a href="https://github.com/M-Igashi/mp3rgain" rel="noopener noreferrer"&gt;
        mp3rgain
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      Lossless MP3 volume adjustment - a modern mp3gain replacement written in Rust
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;div class="markdown-heading"&gt;
&lt;h1 class="heading-element"&gt;mp3rgain&lt;/h1&gt;
&lt;/div&gt;

&lt;p&gt;&lt;a href="https://opensource.org/licenses/MIT" rel="nofollow noopener noreferrer"&gt;&lt;img src="https://camo.githubusercontent.com/fdf2982b9f5d7489dcf44570e714e3a15fce6253e0cc6b5aa61a075aac2ff71b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d79656c6c6f772e737667" alt="License: MIT"&gt;&lt;/a&gt;
&lt;a href="https://www.rust-lang.org" rel="nofollow noopener noreferrer"&gt;&lt;img src="https://camo.githubusercontent.com/ca1fb72f9979a9fdae8374b318ef81c515751ee3dde21f74644e5d46c3f1747e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f727573742d312e37302532422d626c75652e737667" alt="Rust"&gt;&lt;/a&gt;
&lt;a href="https://crates.io/crates/mp3rgain" rel="nofollow noopener noreferrer"&gt;&lt;img src="https://camo.githubusercontent.com/f16970b9fc048f70836d37c0cb580b789bba45a4f9772930075eb239edd27bd3/68747470733a2f2f696d672e736869656c64732e696f2f6372617465732f762f6d7033726761696e2e737667" alt="crates.io"&gt;&lt;/a&gt;
&lt;a href="https://m-igashi.github.io/mp3rgain/" rel="nofollow noopener noreferrer"&gt;&lt;img src="https://camo.githubusercontent.com/4b7c8a4625158be8ddf1562ee096b922b43f056e139064a2c31aa5cdc417348b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f646f776e6c6f6164732f4d2d4967617368692f6d7033726761696e2f746f74616c3f6c6162656c3d646f776e6c6f61647326636f6c6f723d627269676874677265656e" alt="GitHub Downloads"&gt;&lt;/a&gt;
&lt;a href="https://github.com/M-Igashi/mp3rgain/docs/compatibility-report.md" rel="noopener noreferrer"&gt;&lt;img src="https://camo.githubusercontent.com/07e0a0ee53a70ca6498fea85204a336e9677e28cc7c9c3119aa3491c7cc3dbcc/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6d70336761696e2d636f6d70617469626c652d627269676874677265656e2e737667" alt="mp3gain compatible"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Lossless MP3/AAC volume adjustment - a modern mp3gain / aacgain replacement written in Rust&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;mp3rgain adjusts MP3 and AAC volume without re-encoding by modifying the &lt;code&gt;global_gain&lt;/code&gt; field in each frame. This preserves audio quality while achieving permanent volume changes.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The only actively maintained tool that performs lossless AAC/M4A bitstream gain adjustment.&lt;/strong&gt;
aacgain has been unmaintained since ~2009 and rarely builds on modern 64-bit systems. mp3rgain is the only practical option today for re-encode-free AAC volume normalization.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Features&lt;/h2&gt;
&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Only tool with lossless AAC bitstream gain&lt;/strong&gt;: re-encode-free &lt;code&gt;global_gain&lt;/code&gt; rewrite for AAC/M4A — a capability previously only available in the long-abandoned aacgain&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Lossless &amp;amp; Reversible&lt;/strong&gt;: No re-encoding, all changes can be undone (MP3 and AAC)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ReplayGain&lt;/strong&gt;: Track and album gain analysis for MP3 and AAC/M4A&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Zero dependencies&lt;/strong&gt;: Single static binary (no ffmpeg, no mp3gain, no aacgain)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cross-platform&lt;/strong&gt;: macOS, Linux, Windows (x86_64 and ARM64)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;mp3gain / aacgain compatible&lt;/strong&gt;…&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/M-Igashi/mp3rgain" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;h2&gt;
  
  
  What I'd love to hear
&lt;/h2&gt;

&lt;p&gt;If you have mp3gain or aacgain in a pipeline somewhere, I'd be curious which of the three compatibility layers actually matters to you in practice — and whether anyone is relying on &lt;code&gt;-s i&lt;/code&gt; ID3v2 ReplayGain tags I should know about. Issues and migration reports welcome.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Disclosure: prose drafted and generated descriptive cover.png with AI editorial assistance. Code, design decisions, and the SHA-256 verification setup are my own.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>rust</category>
      <category>podcast</category>
      <category>showdev</category>
      <category>opensource</category>
    </item>
  </channel>
</rss>
