Class: Chaussettes::Info

Inherits:
Object
  • Object
show all
Defined in:
lib/chaussettes/info.rb

Overview

encapsulates info about an audio file

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Info

Returns a new instance of Info.



7
8
9
10
11
# File 'lib/chaussettes/info.rb', line 7

def initialize(filename)
  command = Tool.new('soxi') << filename
  output = `#{command}`
  @data = _parse(output)
end

Instance Method Details

#_parse(output) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/chaussettes/info.rb', line 13

def _parse(output)
  output.lines.each.with_object({}) do |line, hash|
    next if line.strip.empty?
    key, value = line.split(/:/, 2)
    hash[key.strip] = value.strip
  end
end

#bit_rateObject



52
53
54
# File 'lib/chaussettes/info.rb', line 52

def bit_rate
  @_bit_rate ||= @data['Bit Rate']
end

#bitsObject



33
34
35
# File 'lib/chaussettes/info.rb', line 33

def bits
  @_bits ||= @data['Precision'].to_i
end

#channelsObject



25
26
27
# File 'lib/chaussettes/info.rb', line 25

def channels
  @_channels ||= @data['Channels'].to_i
end

#durationObject



37
38
39
40
41
42
43
44
45
46
# File 'lib/chaussettes/info.rb', line 37

def duration
  @_duration ||= begin
    timespec = @data['Duration'].split(/ /).first
    h, m, s = timespec.split(/:/)

    h.to_i * 3600 +
      m.to_i * 60 +
      s.to_f
  end
end

#filenameObject



21
22
23
# File 'lib/chaussettes/info.rb', line 21

def filename
  @_filename ||= @data['Input File']
end

#rateObject



29
30
31
# File 'lib/chaussettes/info.rb', line 29

def rate
  @_rate ||= @data['Sample Rate'].to_i
end

#sizeObject



48
49
50
# File 'lib/chaussettes/info.rb', line 48

def size
  @_size ||= @data['File Size']
end