about summary refs log tree commit homepage
path: root/lib/dtas/source
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2013-08-29 09:29:40 +0000
committerEric Wong <normalperson@yhbt.net>2013-08-29 09:50:36 +0000
commit0f73acc225656517526b317a05ecab7e1311141b (patch)
tree81a8a8e373df302cfa478a8af706d125dcc1cb1d /lib/dtas/source
parent4bd49cda05b43fc68f9353620e3f018f1c07fcbb (diff)
downloaddtas-0f73acc225656517526b317a05ecab7e1311141b.tar.gz
We don't need it since IO#read(bytes, buf) will convert to
ASCII-8BIT anyways.  Everywhere else, we ensure path names are
already binary.  We do this mainly at the client layer before using
Shellwords to escape the paths.

We also must be careful about parsing output from soxi/avprobe
which can show us metadata in whatever encoding is in the file.
We must still handle data from parsing command output as binary,
as the encoding of file metadata tends to vary.

This also should buy us Syck compatibility for Ruby 1.9.3 users
on Debian systems where Ruby 1.9.3 still uses Syck.
Diffstat (limited to 'lib/dtas/source')
-rw-r--r--lib/dtas/source/av.rb1
-rw-r--r--lib/dtas/source/av_ff_common.rb19
-rw-r--r--lib/dtas/source/cmd.rb1
-rw-r--r--lib/dtas/source/common.rb1
-rw-r--r--lib/dtas/source/ff.rb1
-rw-r--r--lib/dtas/source/file.rb1
-rw-r--r--lib/dtas/source/mp3gain.rb3
-rw-r--r--lib/dtas/source/sox.rb11
8 files changed, 17 insertions, 21 deletions
diff --git a/lib/dtas/source/av.rb b/lib/dtas/source/av.rb
index 6005344..d44b1a9 100644
--- a/lib/dtas/source/av.rb
+++ b/lib/dtas/source/av.rb
@@ -1,4 +1,3 @@
-# -*- encoding: binary -*-
 # Copyright (C) 2013, Eric Wong <normalperson@yhbt.net> and all contributors
 # License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt)
 require_relative '../../dtas'
diff --git a/lib/dtas/source/av_ff_common.rb b/lib/dtas/source/av_ff_common.rb
index 666adbd..bcb8a0a 100644
--- a/lib/dtas/source/av_ff_common.rb
+++ b/lib/dtas/source/av_ff_common.rb
@@ -1,9 +1,9 @@
-# -*- encoding: binary -*-
 # Copyright (C) 2013, Eric Wong <normalperson@yhbt.net> and all contributors
 # License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt)
 require_relative '../../dtas'
 require_relative '../source'
 require_relative '../replaygain'
+require_relative '../xs'
 require_relative 'file'
 
 # Common code for libav (avconv/avprobe) and ffmpeg (and ffprobe)
@@ -12,6 +12,7 @@ require_relative 'file'
 # support JSON, so we have an ugly parser...
 module DTAS::Source::AvFfCommon # :nodoc:
   include DTAS::Source::File
+  include DTAS::XS
   AStream = Struct.new(:duration, :channels, :rate)
   AV_FF_TRYORDER = 1
 
@@ -35,16 +36,16 @@ module DTAS::Source::AvFfCommon # :nodoc:
     s = qx(@env, cmd, err_str: err, no_raise: true)
     return false if Process::Status === s
     return false if err =~ /Unable to find a suitable output format for/
-    s.scan(%r{^\[STREAM\]\n(.*?)\n\[/STREAM\]\n}m) do |_|
+    s.scan(%r{^\[STREAM\]\n(.*?)\n\[/STREAM\]\n}mn) do |_|
       stream = $1
       if stream =~ /^codec_type=audio$/
         as = AStream.new
         index = nil
-        stream =~ /^index=(\d+)\s*$/m and index = $1.to_i
-        stream =~ /^duration=([\d\.]+)\s*$/m and as.duration = $1.to_f
-        stream =~ /^channels=(\d)\s*$/m and as.channels = $1.to_i
-        stream =~ /^sample_rate=([\d\.]+)\s*$/m and as.rate = $1.to_i
-        index or raise "BUG: no audio index from #{Shellwords.join(cmd)}"
+        stream =~ /^index=(\d+)\s*$/nm and index = $1.to_i
+        stream =~ /^duration=([\d\.]+)\s*$/nm and as.duration = $1.to_f
+        stream =~ /^channels=(\d)\s*$/nm and as.channels = $1.to_i
+        stream =~ /^sample_rate=([\d\.]+)\s*$/nm and as.rate = $1.to_i
+        index or raise "BUG: no audio index from #{xs(cmd)}"
 
         # some streams have zero channels
         @astreams[index] = as if as.channels > 0 && as.rate > 0
@@ -52,9 +53,9 @@ module DTAS::Source::AvFfCommon # :nodoc:
     end
     s.scan(%r{^\[FORMAT\]\n(.*?)\n\[/FORMAT\]\n}m) do |_|
       f = $1
-      f =~ /^duration=([\d\.]+)\s*$/m and @duration = $1.to_f
+      f =~ /^duration=([\d\.]+)\s*$/nm and @duration = $1.to_f
       # TODO: multi-line/multi-value/repeated tags
-      f.gsub!(/^TAG:([^=]+)=(.*)$/i) { |_| @comments[$1.upcase] = $2 }
+      f.gsub!(/^TAG:([^=]+)=(.*)$/ni) { |_| @comments[$1.upcase] = $2 }
     end
     ! @astreams.empty?
   end
diff --git a/lib/dtas/source/cmd.rb b/lib/dtas/source/cmd.rb
index 3ea2049..3c409e9 100644
--- a/lib/dtas/source/cmd.rb
+++ b/lib/dtas/source/cmd.rb
@@ -1,4 +1,3 @@
-# -*- encoding: binary -*-
 # Copyright (C) 2013, Eric Wong <normalperson@yhbt.net> and all contributors
 # License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt)
 require_relative '../../dtas'
diff --git a/lib/dtas/source/common.rb b/lib/dtas/source/common.rb
index 7cc5f7c..03d1562 100644
--- a/lib/dtas/source/common.rb
+++ b/lib/dtas/source/common.rb
@@ -1,4 +1,3 @@
-# -*- encoding: binary -*-
 # Copyright (C) 2013, Eric Wong <normalperson@yhbt.net> and all contributors
 # License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt)
 module DTAS::Source::Common # :nodoc:
diff --git a/lib/dtas/source/ff.rb b/lib/dtas/source/ff.rb
index b8e6f6b..fa4bbf7 100644
--- a/lib/dtas/source/ff.rb
+++ b/lib/dtas/source/ff.rb
@@ -1,4 +1,3 @@
-# -*- encoding: binary -*-
 # Copyright (C) 2013, Eric Wong <normalperson@yhbt.net> and all contributors
 # License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt)
 require_relative '../../dtas'
diff --git a/lib/dtas/source/file.rb b/lib/dtas/source/file.rb
index d79dd88..3dadc67 100644
--- a/lib/dtas/source/file.rb
+++ b/lib/dtas/source/file.rb
@@ -1,4 +1,3 @@
-# -*- encoding: binary -*-
 # Copyright (C) 2013, Eric Wong <normalperson@yhbt.net> and all contributors
 # License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt)
 require_relative '../../dtas'
diff --git a/lib/dtas/source/mp3gain.rb b/lib/dtas/source/mp3gain.rb
index fe0b642..b48c759 100644
--- a/lib/dtas/source/mp3gain.rb
+++ b/lib/dtas/source/mp3gain.rb
@@ -1,4 +1,3 @@
-# -*- encoding: binary -*-
 # Copyright (C) 2013, Eric Wong <normalperson@yhbt.net> and all contributors
 # License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt)
 require_relative '../process'
@@ -15,7 +14,7 @@ module DTAS::Source::Mp3gain # :nodoc:
     tmp = {}
     case @infile
     when String
-      @infile =~ /\.mp[g23]\z/i or return
+      @infile =~ /\.mp[g23]\z/in or return
       qx(%W(mp3gain -s c #@infile)).split(/\n/).each do |line|
         case line
         when /^Recommended "(Track|Album)" dB change:\s*(\S+)/
diff --git a/lib/dtas/source/sox.rb b/lib/dtas/source/sox.rb
index d030628..0001689 100644
--- a/lib/dtas/source/sox.rb
+++ b/lib/dtas/source/sox.rb
@@ -1,15 +1,16 @@
-# -*- encoding: binary -*-
 # Copyright (C) 2013, Eric Wong <normalperson@yhbt.net> and all contributors
 # License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt)
 require_relative '../../dtas'
 require_relative '../source'
 require_relative '../replaygain'
+require_relative '../xs'
 
 # this is usually one input file
 class DTAS::Source::Sox # :nodoc:
   require_relative 'file'
 
   include DTAS::Source::File
+  include DTAS::XS
 
   SOX_DEFAULTS = COMMAND_DEFAULTS.merge(
     "command" => 'exec sox "$INFILE" $SOXFMT - $TRIMFX $RGFX',
@@ -27,7 +28,7 @@ class DTAS::Source::Sox # :nodoc:
     if msg
       return if @last_failed == infile
       @last_failed = infile
-      return warn("`#{Shellwords.join(cmd)}' #{msg}")
+      return warn("`#{xs(cmd)}' #{msg}")
     end
     true
   end
@@ -49,7 +50,7 @@ class DTAS::Source::Sox # :nodoc:
     qx(@env, %W(soxi -p #@infile), err: "/dev/null").to_i # sox.git f4562efd0aa3
   rescue # fallback to parsing the whole output
     s = qx(@env, %W(soxi #@infile), err: "/dev/null")
-    s =~ /Precision\s+:\s*(\d+)-bit/
+    s =~ /Precision\s+:\s*(\d+)-bit/n
     v = $1.to_i
     return v if v > 0
     raise TypeError, "could not determine precision for #@infile"
@@ -81,8 +82,8 @@ class DTAS::Source::Sox # :nodoc:
     tmp = {}
     case @infile
     when String
-      qx(@env, %W(soxi -a #@infile)).split(/\n/).each do |line|
-        key, value = line.split(/=/, 2)
+      qx(@env, %W(soxi -a #@infile)).split(/\n/n).each do |line|
+        key, value = line.split(/=/n, 2)
         key && value or next
         # TODO: multi-line/multi-value/repeated tags
         tmp[key.upcase] = value