dtas-all
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] format: common detection code (based on sox)


From: Eric Wong
Subject: [PATCH] format: common detection code (based on sox)
Date: Thu, 10 Oct 2013 09:40:42 +0000

This reduces duplication for sox-based components, which our audio
editing components will rely on.  We only use avconv/ffmpeg for odd
formats which sox does not play natively, and editing audio in
strange/lossy formats is undesirable anyways.
---
 lib/dtas/format.rb     | 26 ++++++++++++++++++++++++++
 lib/dtas/source/sox.rb | 20 +-------------------
 lib/dtas/splitfx.rb    | 17 +----------------
 3 files changed, 28 insertions(+), 35 deletions(-)

diff --git a/lib/dtas/format.rb b/lib/dtas/format.rb
index 223d9c0..b1c724e 100644
--- a/lib/dtas/format.rb
+++ b/lib/dtas/format.rb
@@ -35,6 +35,32 @@ class DTAS::Format # :nodoc:
     fmt
   end
 
+  # some of these are sox-only, but that's what we mainly care about
+  # for audio-editing.  We only use ffmpeg/avconv for odd files during
+  # playback.
+
+  extend DTAS::Process
+
+  def self.precision(env, infile)
+    # sox.git f4562efd0aa3
+    qx(env, %W(soxi -p #{infile}), err: "/dev/null").to_i
+  rescue # fallback to parsing the whole output
+    s = qx(env, %W(soxi #{infile}), err: "/dev/null")
+    s =~ /Precision\s+:\s*(\d+)-bit/n
+    v = $1.to_i
+    return v if v > 0
+    raise TypeError, "could not determine precision for #{infile}"
+  end
+
+  def self.from_file(env, infile)
+    fmt = new
+    fmt.channels = qx(env, %W(soxi -c #{infile})).to_i
+    fmt.type = qx(env, %W(soxi -t #{infile})).strip
+    fmt.rate = qx(env, %W(soxi -r #{infile})).to_i
+    fmt.bits ||= precision(env, infile)
+    fmt
+  end
+
   def initialize
     FORMAT_DEFAULTS.each do |k,v|
       instance_variable_set("@#{k}", v)
diff --git a/lib/dtas/source/sox.rb b/lib/dtas/source/sox.rb
index 484a0ec..2a54bb9 100644
--- a/lib/dtas/source/sox.rb
+++ b/lib/dtas/source/sox.rb
@@ -47,26 +47,8 @@ class DTAS::Source::Sox # :nodoc:
     source_file_dup(infile, offset)
   end
 
-  def precision
-    qx(@env, %W(soxi -p address@hidden), err: "/dev/null").to_i # sox.git 
f4562efd0aa3
-  rescue # fallback to parsing the whole output
-    s = qx(@env, %W(soxi address@hidden), err: "/dev/null")
-    s =~ /Precision\s+:\s*(\d+)-bit/n
-    v = $1.to_i
-    return v if v > 0
-    raise TypeError, "could not determine precision for address@hidden"
-  end
-
   def format
-    @format ||= begin
-      fmt = DTAS::Format.new
-      path = @infile
-      fmt.channels = qx(@env, %W(soxi -c #{path})).to_i
-      fmt.type = qx(@env, %W(soxi -t #{path})).strip
-      fmt.rate = qx(@env, %W(soxi -r #{path})).to_i
-      fmt.bits ||= precision
-      fmt
-    end
+    @format ||= DTAS::Format.from_file(@env, @infile)
   end
 
   # This is the number of samples according to the samples in the source
diff --git a/lib/dtas/splitfx.rb b/lib/dtas/splitfx.rb
index b83f7db..6f1654e 100644
--- a/lib/dtas/splitfx.rb
+++ b/lib/dtas/splitfx.rb
@@ -134,27 +134,12 @@ class DTAS::SplitFX # :nodoc:
     load_tracks!(hash)
   end
 
-  # FIXME: duplicate from dtas/source/sox
-  def precision
-    qx(@env, %W(soxi -p address@hidden), err: "/dev/null").to_i # sox.git 
f4562efd0aa3
-  rescue # fallback to parsing the whole output
-    s = qx(@env, %W(soxi address@hidden), err: "/dev/null")
-    s =~ /Precision\s+:\s*(\d+)-bit/n
-    v = $1.to_i
-    return v if v > 0
-    raise TypeError, "could not determine precision for address@hidden"
-  end
-
   def load_input!(hash)
     @infile = hash["infile"] or raise ArgumentError, "'infile' not specified"
     if infmt = hash["infmt"] # rarely needed
       @infmt = DTAS::Format.load(infmt)
     else # likely
-      @infmt = DTAS::Format.new
-      @infmt.channels = qx(@env, %W(soxi -c address@hidden)).to_i
-      @infmt.rate = qx(@env, %W(soxi -r address@hidden)).to_i
-      @infmt.bits ||= precision
-      # we don't care for type
+      @infmt = DTAS::Format.from_file(@env, @infile)
     end
   end
 
-- 
1.8.4




reply via email to

[Prev in Thread] Current Thread [Next in Thread]