diff options
author | Eric Wong <normalperson@yhbt.net> | 2013-08-25 09:25:07 +0000 |
---|---|---|
committer | Eric Wong <normalperson@yhbt.net> | 2013-08-25 09:29:46 +0000 |
commit | 68ffa097e187da663fa3f537b430428ea5e8de2e (patch) | |
tree | be7d441b68ab8c77a7ebc062c129b5701bf2a5b1 /lib/dtas/source/mp3gain.rb | |
parent | 9cd8e2776edc246950d2c7ebdea833489efb1d1f (diff) | |
download | dtas-68ffa097e187da663fa3f537b430428ea5e8de2e.tar.gz |
We should've done this at the start, but we didn't.
Diffstat (limited to 'lib/dtas/source/mp3gain.rb')
-rw-r--r-- | lib/dtas/source/mp3gain.rb | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/lib/dtas/source/mp3gain.rb b/lib/dtas/source/mp3gain.rb new file mode 100644 index 0000000..03bc37a --- /dev/null +++ b/lib/dtas/source/mp3gain.rb @@ -0,0 +1,37 @@ +# -*- encoding: binary -*- +# Copyright (C) 2013, Eric Wong <normalperson@yhbt.net> +# License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt) +require_relative '../process' + +module DTAS::Source::Mp3gain # :nodoc: + include DTAS::Process + # we use dBFS = 1.0 as scale (not 32768) + def __mp3gain_peak(str) + sprintf("%0.8g", str.to_f / 32768.0) + end + + # massage mp3gain(1) output + def mp3gain_comments + tmp = {} + case @infile + when String + @infile =~ /\.mp[g23]\z/i or return + qx(%W(mp3gain -s c #@infile)).split(/\n/).each do |line| + case line + when /^Recommended "(Track|Album)" dB change:\s*(\S+)/ + tmp["REPLAYGAIN_#{$1.upcase}_GAIN"] = $2 + when /^Max PCM sample at current gain: (\S+)/ + tmp["REPLAYGAIN_TRACK_PEAK"] = __mp3gain_peak($1) + when /^Max Album PCM sample at current gain: (\S+)/ + tmp["REPLAYGAIN_ALBUM_PEAK"] = __mp3gain_peak($1) + end + end + tmp + else + raise TypeError, "unsupported type: #{@infile.inspect}" + end + rescue => e + $DEBUG and + warn("mp3gain(#{@infile.inspect}) failed: #{e.message} (#{e.class})") + end +end |