Class: Sox::Stats

Inherits:
Object
  • Object
show all
Defined in:
lib/sox/stats.rb

Defined Under Namespace

Classes: Cache

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file = nil, options = {}) ⇒ Stats

Returns a new instance of Stats.



7
8
9
10
11
12
13
# File 'lib/sox/stats.rb', line 7

def initialize(file = nil, options = {})
  options.each { |k,v| send "#{k}=", v }
  @command = Sox::Command.new.tap do |command|
    command.effect "stats"
    command.input file if file
  end
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



4
5
6
# File 'lib/sox/stats.rb', line 4

def command
  @command
end

#use_cacheObject

Returns the value of attribute use_cache.



5
6
7
# File 'lib/sox/stats.rb', line 5

def use_cache
  @use_cache
end

Instance Method Details

#attributesObject



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

def attributes
  if use_cache and command.inputs.size == 1
    attributes_with_cache
  else
    attributes_without_cache
  end
end

#attributes_with_cacheObject



77
78
79
80
81
# File 'lib/sox/stats.rb', line 77

def attributes_with_cache
  Cache.new(command.inputs.first.filename).fetch do
    attributes_without_cache
  end
end

#attributes_without_cacheObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/sox/stats.rb', line 58

def attributes_without_cache
  @attributes ||=
    begin
      paires = raw_values.collect do |key, _, value|
      value = case value
              when /^-?[0-9.]+$/
                value.to_f
              when "-inf"
                -Float::INFINITY
              else
                value
              end

        [key, value]
      end
      Hash[paires]
    end
end

#peak_levelObject



99
100
101
# File 'lib/sox/stats.rb', line 99

def peak_level
  attributes['Pk lev dB']
end

#raw_outputObject



17
18
19
# File 'lib/sox/stats.rb', line 17

def raw_output
  command.run!.run_output
end

#raw_valuesObject



21
22
23
24
25
26
27
# File 'lib/sox/stats.rb', line 21

def raw_values
  if raw_output.chomp == "sox WARN stats: no audio"
    raise "No audio found by sox"
  else
    raw_output.scan(/^(\w+( \w+)*) +([\w.-]+)/i)
  end
end

#rms_levelObject



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

def rms_level
  attributes['RMS lev dB']
end

#rms_peakObject



95
96
97
# File 'lib/sox/stats.rb', line 95

def rms_peak
  attributes['RMS Pk dB']
end

#silent?(minimum_rms_peak = -45)) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/sox/stats.rb', line 103

def silent?(minimum_rms_peak = -45)
  rms_peak < minimum_rms_peak
end