Sha256: be2b665f1dd3234ccdd1274b3345f162282ce7355010e6b9ccf334ea5a937109

Contents?: true

Size: 1.05 KB

Versions: 1

Compression:

Stored size: 1.05 KB

Contents

require 'chaussettes/tool'

module Chaussettes

  # encapsulates info about an audio file
  class Info
    def initialize(filename)
      command = Tool.new('soxi') << filename
      output = `#{command}`
      @data = _parse(output)
    end

    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

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

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

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

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

    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

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

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

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
chaussettes-1.0.0 lib/chaussettes/info.rb