Class: DTAS::Source::Sox

Inherits:
Object
  • Object
show all
Extended by:
XS
Includes:
File, XS
Defined in:
lib/dtas/source/sox.rb

Overview

this is usually one input file

Direct Known Subclasses

SplitFX

Constant Summary collapse

SOX_DEFAULTS =
COMMAND_DEFAULTS.merge(
  "command" => 'exec sox "$INFILE" $SOXFMT - $TRIMFX $RGFX',
  "tryorder" => 0,
)

Constants included from File

File::FILE_SIVS, File::SRC_SIVS

Constants included from Process

Process::PIDS

Constants included from Command

Command::COMMAND_DEFAULTS

Instance Attribute Summary

Attributes included from File

#infile, #offset, #tryorder

Attributes included from Common

#dst, #dst_zero_byte, #requeued

Attributes included from Command

#command, #env, #pid, #spawn_at, #to_io

Instance Method Summary collapse

Methods included from XS

xs

Methods included from File

#__file_init, #__offset_samples, #comments, #cuebreakpoints, #load!, #offset_samples, #offset_us, #replaygain, #samples!, #source_file_dup, #to_hash, #to_source_cat, #to_state_hash, #trimfx

Methods included from Mp3gain

#__mp3gain_peak, #mp3gain_comments

Methods included from Process

#dtas_spawn, #env_expand, #env_expand_ary, #env_expand_i, #qx, reaper

Methods included from DTAS::SpawnFix

#spawn

Methods included from Common

#dst_assoc

Methods included from Command

#command_init, #command_string, #on_death

Methods included from DTAS::Serialize

#ivars_to_hash

Constructor Details

#initialize(mcache = nil) ⇒ Sox

Returns a new instance of Sox.


33
34
35
36
37
# File 'lib/dtas/source/sox.rb', line 33

def initialize(mcache = nil)
  @mcache = nil
  @last_failed = nil
  command_init(SOX_DEFAULTS)
end

Instance Method Details

#__load_commentsObject


104
105
106
107
108
109
110
111
# File 'lib/dtas/source/sox.rb', line 104

def __load_comments
  tmp = {}
  case @infile
  when String
    ent = mcache_lookup(@infile) and tmp = ent['comments']
  end
  tmp
end

#durationObject


91
92
93
# File 'lib/dtas/source/sox.rb', line 91

def duration
  samples / format.rate.to_f
end

#formatObject


84
85
86
87
88
89
# File 'lib/dtas/source/sox.rb', line 84

def format
  @format ||= begin
    ent = mcache_lookup(@infile)
    ent ? DTAS::Format.load(ent) : nil
  end
end

#mcache_lookup(infile) ⇒ Object


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/dtas/source/sox.rb', line 39

def mcache_lookup(infile)
  (@mcache ||= DTAS::Mcache.new).lookup(infile) do |input, dst|
    err = ''.b
    out = qx(@env.dup, %W(soxi #{input}), err_str: err, no_raise: true)
    return soxi_failed(infile, out) if Process::Status === out
    return soxi_failed(infile, err) if err =~ /soxi FAIL formats:/
    out =~ /^Duration\s*:[^=]*= (\d+) samples /n
    samples = dst['samples'] = $1.to_i
    return soxi_failed(infile, 0) if samples == 0

    out =~ /^Channels\s*:\s*(\d+)/n and dst['channels'] = $1.to_i
    out =~ /^Sample Rate\s*:\s*(\d+)/n and dst['rate'] = $1.to_i
    out =~ /^Precision\s*:\s*(\d+)-bit/n and dst['bits'] = $1.to_i

    enc = Encoding.default_external # typically Encoding::UTF_8
    if out =~ /\nComments\s*:[ \t]*\n?(.*)\z/mn
      comments = dst['comments'] = {}
      key = nil
      $1.split(/\n/n).each do |line|
        if line.sub!(/^([^=]+)=/ni, '')
          key = DTAS.dedupe_str(DTAS.try_enc($1.upcase, enc))
        end
        (comments[key] ||= ''.b) << "#{line}\n" unless line.empty?
      end
      comments.each do |k,v|
        v.chomp!
        DTAS.try_enc(v, enc)
        comments[k] = DTAS.dedupe_str(v)
      end
    end
    dst
  end
end

#samplesObject

This is the number of samples according to the samples in the source file itself, not the decoded output


97
98
99
100
101
102
# File 'lib/dtas/source/sox.rb', line 97

def samples
  (@samples ||= begin
    ent = mcache_lookup(@infile)
    ent ? ent['samples'] : nil
   end) || 0
end

#source_defaultsObject


130
131
132
# File 'lib/dtas/source/sox.rb', line 130

def source_defaults
  SOX_DEFAULTS
end

#soxi_failed(infile, msg) ⇒ Object


23
24
25
26
27
28
29
30
31
# File 'lib/dtas/source/sox.rb', line 23

def soxi_failed(infile, msg)
  return if @last_failed == infile
  @last_failed = infile
  case msg
  when Process::Status then msg = "failed with #{msg.exitstatus}"
  when 0 then msg = 'detected zero samples'
  end
  warn("soxi #{infile}: #{msg}\n")
end

#src_spawn(player_format, rg_state, opts) ⇒ Object


113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/dtas/source/sox.rb', line 113

def src_spawn(player_format, rg_state, opts)
  raise "BUG: #{self.inspect}#src_spawn called twice" if @to_io
  e = @env.merge!(player_format.to_env)
  e["INFILE"] = xs(@infile)

  # make sure these are visible to the "current" command...
  e["TRIMFX"] = trimfx
  e["RGFX"] = rg_state.effect(self) || nil
  e.merge!(@rg.to_env) if @rg

  @pid = dtas_spawn(e, command_string, opts)
end

#to_hshObject


126
127
128
# File 'lib/dtas/source/sox.rb', line 126

def to_hsh
  to_hash.delete_if { |k,v| v == SOX_DEFAULTS[k] }
end

#try(infile, offset = nil, trim = nil) ⇒ Object


73
74
75
76
77
78
79
80
81
82
# File 'lib/dtas/source/sox.rb', line 73

def try(infile, offset = nil, trim = nil)
  ent = mcache_lookup(infile) or return
  ret = source_file_dup(infile, offset, trim)
  ret.instance_eval do
    @samples = ent['samples']
    @format = DTAS::Format.load(ent)
    @comments = ent['comments']
  end
  ret
end