about summary refs log tree commit homepage
diff options
context:
space:
mode:
-rw-r--r--lib/dtas/replaygain.rb22
-rw-r--r--lib/dtas/source.rb5
-rw-r--r--test/test_rg_integration.rb30
3 files changed, 53 insertions, 4 deletions
diff --git a/lib/dtas/replaygain.rb b/lib/dtas/replaygain.rb
index cf397a3..cbe7b57 100644
--- a/lib/dtas/replaygain.rb
+++ b/lib/dtas/replaygain.rb
@@ -10,7 +10,11 @@
 
 class DTAS::ReplayGain # :nodoc:
   ATTRS = %w(reference_loudness track_gain album_gain track_peak album_peak)
-  ATTRS.each { |a| attr_reader a }
+  ENV_ATTRS = {}
+  ATTRS.each do |a|
+    attr_reader a
+    ENV_ATTRS["REPLAYGAIN_#{a.upcase}"] = a
+  end
 
   def check_gain(val)
     /([+-]?\d+(?:\.\d+)?)/ =~ val ? $1 : nil
@@ -20,13 +24,25 @@ class DTAS::ReplayGain # :nodoc:
     /(\d+(?:\.\d+)?)/ =~ val ? $1 : nil
   end
 
+  # note: this strips the "dB" suffix, but that should be easier for apps
+  # to deal with anyways...
+  def to_env
+    rv = {}
+    # this will cause nil to be set if some envs are missing, this causes
+    # Process.spawn to unset the environment if it was previously set
+    # (leaked from some other process)
+    ENV_ATTRS.each do |env_name, attr_name|
+      rv[env_name] = __send__(attr_name)
+    end
+    rv
+  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"
+    @reference_loudness = check_gain(comments["REPLAYGAIN_REFERENCE_LOUDNESS"])
 
     @track_gain = check_gain(comments["REPLAYGAIN_TRACK_GAIN"])
     @album_gain = check_gain(comments["REPLAYGAIN_ALBUM_GAIN"])
diff --git a/lib/dtas/source.rb b/lib/dtas/source.rb
index 404d2b0..747fa9f 100644
--- a/lib/dtas/source.rb
+++ b/lib/dtas/source.rb
@@ -34,6 +34,7 @@ class DTAS::Source # :nodoc:
     @offset = offset
     @comments = nil
     @samples = nil
+    @rg = nil
   end
 
   # this exists mainly to make the mpris interface easier, but it's not
@@ -120,7 +121,8 @@ class DTAS::Source # :nodoc:
   end
 
   def replaygain
-    DTAS::ReplayGain.new(comments) || DTAS::ReplayGain.new(mp3gain_comments)
+    @rg = DTAS::ReplayGain.new(comments) ||
+          DTAS::ReplayGain.new(mp3gain_comments)
   end
 
   def spawn(format, rg_state, opts)
@@ -131,6 +133,7 @@ class DTAS::Source # :nodoc:
     # make sure these are visible to the "current" command...
     @env["TRIMFX"] = @offset ? "trim #@offset" : nil
     @env["RGFX"] = rg_state.effect(self) || nil
+    e.merge!(@rg.to_env) if @rg
 
     @pid = dtas_spawn(e.merge!(@env), command_string, opts)
   end
diff --git a/test/test_rg_integration.rb b/test/test_rg_integration.rb
index d8a8b85..b9ef2b9 100644
--- a/test/test_rg_integration.rb
+++ b/test/test_rg_integration.rb
@@ -114,4 +114,34 @@ class TestRgIntegration < Minitest::Unit::TestCase
 
     stop_playback(default_pid, s)
   end
+
+  def test_rg_env_in_source
+    s = client_socket
+    s.preq("rg mode=album_gain")
+    assert_equal "OK", s.readpartial(666)
+    pluck, len = tmp_pluck
+    cmd = DTAS::Source::SOURCE_DEFAULTS["command"]
+    fifo = tmpfifo
+    s.preq("source ed command='env > #{fifo}; #{cmd}'")
+    assert_equal "OK", s.readpartial(666)
+    s.preq("sink ed default command='cat >/dev/null' active=true")
+    assert_equal "OK", s.readpartial(666)
+    s.preq(%W(enq #{pluck.path}))
+    assert_equal "OK", s.readpartial(666)
+
+    rg = {}
+    File.readlines(fifo).each do |line|
+      line =~ /\AREPLAYGAIN_/ or next
+      k, v = line.chomp!.split(/=/)
+      rg[k] = v
+    end
+    expect = {
+      "REPLAYGAIN_TRACK_GAIN" => "-2",
+      "REPLAYGAIN_ALBUM_GAIN" => "-3.0",
+      "REPLAYGAIN_TRACK_PEAK" => "0.666",
+      "REPLAYGAIN_ALBUM_PEAK" => "0.999",
+      "REPLAYGAIN_REFERENCE_LOUDNESS" => nil
+    }
+    assert_equal expect, rg
+  end
 end