From f260cd93067d81e7e5bc33d8f1b30f0639a1dc89 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 10 Oct 2013 09:10:51 +0000 Subject: format: common detection code (based on sox) 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 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'lib/dtas/format.rb') 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) -- cgit v1.2.3-24-ge0c7