about summary refs log tree commit homepage
path: root/lib/dtas/source
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2013-08-24 09:54:45 +0000
committerEric Wong <normalperson@yhbt.net>2013-08-24 09:54:45 +0000
commit3e09ac0c10c95bb24a08af62393b4f761e2743d0 (patch)
tree778dffa2ba8798503fc047db0feef6d65426ea22 /lib/dtas/source
downloaddtas-3e09ac0c10c95bb24a08af62393b4f761e2743d0.tar.gz
Diffstat (limited to 'lib/dtas/source')
-rw-r--r--lib/dtas/source/command.rb41
-rw-r--r--lib/dtas/source/common.rb14
-rw-r--r--lib/dtas/source/mp3.rb38
3 files changed, 93 insertions, 0 deletions
diff --git a/lib/dtas/source/command.rb b/lib/dtas/source/command.rb
new file mode 100644
index 0000000..30441eb
--- /dev/null
+++ b/lib/dtas/source/command.rb
@@ -0,0 +1,41 @@
+# -*- encoding: binary -*-
+# :stopdoc:
+# Copyright (C) 2013, Eric Wong <normalperson@yhbt.net>
+# License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt)
+require_relative '../../dtas'
+require_relative '../source'
+require_relative '../command'
+require_relative '../serialize'
+
+class DTAS::Source::Command
+  require_relative '../source/common'
+
+  include DTAS::Command
+  include DTAS::Process
+  include DTAS::Source::Common
+  include DTAS::Serialize
+
+  SIVS = %w(command env)
+
+  def initialize(command)
+    command_init(command: command)
+  end
+
+  def source_dup
+    rv = self.class.new
+    SIVS.each { |iv| rv.__send__("#{iv}=", self.__send__(iv)) }
+    rv
+  end
+
+  def to_hash
+    ivars_to_hash(SIVS)
+  end
+
+  alias to_hsh to_hash
+
+  def spawn(format, rg_state, opts)
+    raise "BUG: #{self.inspect}#spawn called twice" if @to_io
+    e = format.to_env
+    @pid = dtas_spawn(e.merge!(@env), command_string, opts)
+  end
+end
diff --git a/lib/dtas/source/common.rb b/lib/dtas/source/common.rb
new file mode 100644
index 0000000..333e74a
--- /dev/null
+++ b/lib/dtas/source/common.rb
@@ -0,0 +1,14 @@
+# :stopdoc:
+# Copyright (C) 2013, Eric Wong <normalperson@yhbt.net>
+# License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt)
+module DTAS::Source::Common
+  attr_reader :dst_zero_byte
+  attr_reader :dst
+  attr_accessor :requeued
+
+  def dst_assoc(buf)
+    @dst = buf
+    @dst_zero_byte = buf.bytes_xfer + buf.inflight
+    @requeued = false
+  end
+end
diff --git a/lib/dtas/source/mp3.rb b/lib/dtas/source/mp3.rb
new file mode 100644
index 0000000..b013bee
--- /dev/null
+++ b/lib/dtas/source/mp3.rb
@@ -0,0 +1,38 @@
+# -*- encoding: binary -*-
+# :stopdoc:
+# 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::Mp3
+  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