From 3e09ac0c10c95bb24a08af62393b4f761e2743d0 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sat, 24 Aug 2013 09:54:45 +0000 Subject: initial commit --- lib/dtas/replaygain.rb | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 lib/dtas/replaygain.rb (limited to 'lib/dtas/replaygain.rb') diff --git a/lib/dtas/replaygain.rb b/lib/dtas/replaygain.rb new file mode 100644 index 0000000..e049b8d --- /dev/null +++ b/lib/dtas/replaygain.rb @@ -0,0 +1,42 @@ +# -*- encoding: binary -*- +# :stopdoc: +# Copyright (C) 2013, Eric Wong +# License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt) +# +# Represents ReplayGain metadata for a DTAS::Source +# cleanup/validate values to prevent malicious files from making us +# run arbitrary commands +# *_peak values are 0..inf (1.0 being full scale, but >1 is possible +# *_gain values are specified in dB + +class DTAS::ReplayGain + ATTRS = %w(reference_loudness track_gain album_gain track_peak album_peak) + ATTRS.each { |a| attr_reader a } + + def check_gain(val) + /([+-]?\d+(?:\.\d+)?)/ =~ val ? $1 : nil + end + + def check_float(val) + /(\d+(?:\.\d+)?)/ =~ val ? $1 : nil + end + + def initialize(comments) + comments or return + + # the replaygain standard specifies 89.0 dB, but maybe some apps are + # different... + @reference_loudness = + check_gain(comments["REPLAYGAIN_REFERENCE_LOUDNESS"]) || "89.0" + + @track_gain = check_gain(comments["REPLAYGAIN_TRACK_GAIN"]) + @album_gain = check_gain(comments["REPLAYGAIN_ALBUM_GAIN"]) + @track_peak = check_float(comments["REPLAYGAIN_TRACK_PEAK"]) + @album_peak = check_float(comments["REPLAYGAIN_ALBUM_PEAK"]) + end + + def self.new(comments) + tmp = super + tmp.track_gain ? tmp : nil + end +end -- cgit v1.2.3-24-ge0c7