MP3Gain とは
MP3Gain は MP3 ファイルの音量を調整できるソフトウェア。
MP3Gain は ID3 タグの RVA2 フレームに Replay Gain データを書き込むことで音量を調整している。そのため、元の音楽データ自体は書き換えていない (ファイル自体は書き換えてしまうので注意)。
ID3タグ(アイディースリータグ、ID3 tag)は、MP3ファイルの中に、アーティスト・作成年・曲名等の情報を書き込むための規格である。
ReplayGain(リプレイゲイン)と、その対応ソフト - 音楽ソフト、色々試してみよう
●事前に、ReplayGainの音量解析機能を持つソフトを使って各曲の音量を調査し、その結果を所定の形式で各曲ファイルのタグに記録します。
●ReplayGain再生対応ソフトや機器は、再生する際にタグのReplayGain値を参照し、それに基づいて音量を指定値に揃えて再生します。
「MP3Gain」複数MP3ファイルの音量を音質劣化なしに一括して一定化 - 窓の杜
本ソフトでの音量調節は、MP3の再エンコードなどは行わず、MP3データに含まれる音量の係数“スケールファクター”を書き換えることで行うので音質の劣化が起こらない。また、変更情報はMP3ファイルの情報フィールドに自動保存されるので、いつでも音量を元に戻すことが可能だ。
また、Replay Gain データが基準としている音圧レベルは 89 デシベルとなっている。
The target loudness of ReplayGain utilities is 89 dB sound pressure level.
mp3gain コマンドをインストールする
今回は macOS に Homebrew で MP3Gain のコマンドラインツールをインストールする。
$ brew install mp3gain
バージョンを確認。
$ mp3gain
mp3gain version 1.6.2
copyright(c) 2001-2009 by Glen Sawyer
uses mpglib, which can be found at http://www.mpg123.de
Usage: mp3gain [options] <infile> [<infile 2> ...]
--use -? or -h for a full list of options
mp3gain コマンドのオプション一覧を表示する
mp3gain -h などでヘルプを表示できる。
$ mp3gain -h
mp3gain version 1.6.2
copyright(c) 2001-2009 by Glen Sawyer
uses mpglib, which can be found at http://www.mpg123.de
Usage: mp3gain [options] <infile> [<infile 2> ...]
options:
-v - show version number
-g <i> - apply gain i without doing any analysis
-l 0 <i> - apply gain i to channel 0 (left channel)
without doing any analysis (ONLY works for STEREO files,
not Joint Stereo)
-l 1 <i> - apply gain i to channel 1 (right channel)
-e - skip Album analysis, even if multiple files listed
-r - apply Track gain automatically (all files set to equal loudness)
-k - automatically lower Track/Album gain to not clip audio
-a - apply Album gain automatically (files are all from the same
album: a single gain change is applied to all files, so
their loudness relative to each other remains unchanged,
but the average album loudness is normalized)
-m <i> - modify suggested MP3 gain by integer i
-d <n> - modify suggested dB gain by floating-point n
-c - ignore clipping warning when applying gain
-o - output is a database-friendly tab-delimited list
-t - mp3gain writes modified mp3 to temp file, then deletes original
instead of modifying bytes in original file (default)
-T - mp3gain directly modifies mp3 file (opposite of -t)
-q - Quiet mode: no status messages
-p - Preserve original file timestamp
-x - Only find max. amplitude of file
-f - Assume input file is an MPEG 2 Layer III file
(i.e. don't check for mis-named Layer I or Layer II files)
-? or -h - show this message
-s c - only check stored tag info (no other processing)
-s d - delete stored tag info (no other processing)
-s s - skip (ignore) stored tag info (do not read or write tags)
-s r - force re-calculation (do not read tag info)
-s i - use ID3v2 tag for MP3 gain info
-s a - use APE tag for MP3 gain info (default)
-u - undo changes made (based on stored tag info)
-w - "wrap" gain change if gain+change > 255 or gain+change < 0
(use "-? wrap" switch for a complete explanation)
If you specify -r and -a, only the second one will work
If you do not specify -c, the program will stop and ask before
applying gain change to a file that might clip
MP3 ファイルを解析して音量を調整
mp3gain のコマンド引数にファイル名を指定することで MP3 ファイルの音量を解析できる。
用意したファイル base.mp3 と this.mp3 を解析してみる。
base.mp3 は基準よりも音が大きいようだ。
音量を下げるよう「dB change: -10.620000」「mp3 gain change: -7」と薦められている。
$ mp3gain base.mp3
base.mp3
Recommended "Track" dB change: -10.620000
Recommended "Track" mp3 gain change: -7
Max PCM sample at current gain: 36519.411306
Max mp3 global gain field: 187
Min mp3 global gain field: 111
Recommended "Album" dB change for all files: -10.620000
Recommended "Album" mp3 gain change for all files: -7
this.mp3 は基準よりも音が小さいようだ。
音量を上げるよう「dB change: 1.690000」「mp3 gain change: 1」と薦められている。
$ mp3gain this.mp3
this.mp3
Recommended "Track" dB change: 1.690000
Recommended "Track" mp3 gain change: 1
Max PCM sample at current gain: 17296.257552
Max mp3 global gain field: 177
Min mp3 global gain field: 145
Recommended "Album" dB change for all files: 1.690000
Recommended "Album" mp3 gain change for all files: 1
今回は this.mp3 を base.mp3 の音量に合わせたいため、「mp3 gain change: -7」「mp3 gain change: 1」から両者の差を8と算出し、オプション -g 8 で音量を上げることにする。
この際に、MP3 ファイルが上書きされてしまうので元のファイルをバックアップしておくなどの注意が必要。
$ mp3gain -g 8 this.mp3
Applying gain change of 8 to this.mp3...
done
音量を上げた this.mp3 を解析すると、 base.mp3 と同じく「Recommended "Track" mp3 gain change: -7」と出力されるようになった。
$ mp3gain this.mp3
this.mp3
Recommended "Track" dB change: -10.350000
Recommended "Track" mp3 gain change: -7
Max PCM sample at current gain: 69185.030208
Max mp3 global gain field: 185
Min mp3 global gain field: 153
Recommended "Album" dB change for all files: -10.350000
Recommended "Album" mp3 gain change for all files: -7
Replay Gain データについて
mp3gain が書き込む Replay Gain データについての詳細な情報が、ソースコード id3tag.c のコメントに書かれている。
- ID3v2.4 タグの RVA2 フレームに Replay Gain を保存している
- アルバムとトラックの Replay Gain はそれぞれ別のフレームとして書き込んでいる
- ID3タグを書き込む際に、元のファイルに ID3v2.2 や ID3v2.3 タグがある場合はID3v2.4 にアップグレードしている
MP3Gain / Code / [1f46cd] /mp3gain/id3tag.c
/**
* Handling of Replay Gain information in ID3v2 tags.
*
* We store Replay Gain data in RVA2 frames inside the ID3v2.4 tag.
* Album and track gain are written as two separate frames.
* We use the RVA2 frame as follows:
*
* Identification string: either "track" or "album";
* Channel number: always 1 (master volume);
* Volume adjustment: recommended gain relative to 89 dB standard;
* Bits representing peak: always 16;
* Peak volume: max absolute sample value relative to full scale
*
* The meaning of the RVA2 peak volume field is not specified in ID3v2.4.
* We follow the interpretation of QuodLibet/mutagen: Peak volume is the
* maximum absolute sample value relative to full scale, before application
* of the volume adjustment, stored as an unsigned fixed point value with
* 1 integer bit and (peakbits-1) fractional bits.
*
* In addition to standard Replay Gain data, we also store mp3gain-specific
* fields in TXXX frames. The description string of such frames starts with
* "MP3GAIN_". These frames are only needed when mp3gain updates the encoded
* audio volume in MP3 stream (-r and -a command line options).
*
* We read tag data in ID3v2.2, ID3v2.3 or ID3v2.4 format, from either
* the beginning or the end of the file. Extended tag headers are ignored
* and removed; compressed frames are ignored but preserved. No workarounds
* are attempted for invalid ID3v2.4 tags.
*
* When writing/updating tag data, we always write a single ID3v2.4 tag
* at the beginning of the file, fully unsynchronized. If the original
* file had an ID3v2.2 or ID3v2.3 tag, it is upgraded to ID3v2.4. If the
* original file had only an appended tag, it is moved to the beginning of
* the file. If the original file contained multiple tags, we update only
* the first tag and ignore the rest (this is bad, but it's probably a
* rare case). If the original file had only an ID3v1 tag, it is copied
* to a new ID3v2 tag and the ID3v1 tag is left as it was.
*
* See: http://www.id3.org/id3v2.4.0-structure
* http://www.id3.org/id3v2.4.0-frames
*/