Sha256: 26ba59f988758bbc931937f6f1a0f816360f14d0ae35258ac2123ecc4fb6948c

Contents?: true

Size: 741 Bytes

Versions: 12

Compression:

Stored size: 741 Bytes

Contents

require 'mp3info'
require 'pathname'
require 'riff/reader'

module Scissor
  class SoundFile
    SUPPORTED_FORMATS = %w/mp3 wav/

    class Error < StandardError; end
    class UnknownFormat < Error; end

    def initialize(filename)
      @filename = Pathname.new(filename)
      @ext = @filename.extname.sub(/^\./, '').downcase

      unless SUPPORTED_FORMATS.include?(@ext)
        raise UnknownFormat
      end
    end

    def length
      case @ext
      when 'mp3'
        Mp3Info.new(@filename.to_s).length
      when 'wav'
        riff = Riff::Reader.open(@filename ,"r")
        data = riff.root_chunk['data']
        fmt = riff.root_chunk['fmt ']

        data.length / fmt.body.unpack('s2i2')[3].to_f
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
scissor-0.2.9 lib/scissor/sound_file.rb
scissor-0.2.8 lib/scissor/sound_file.rb
scissor-0.2.7 lib/scissor/sound_file.rb
scissor-0.2.6 lib/scissor/sound_file.rb
scissor-0.2.5 lib/scissor/sound_file.rb
scissor-0.2.4 lib/scissor/sound_file.rb
scissor-0.2.3 lib/scissor/sound_file.rb
scissor-0.2.2 lib/scissor/sound_file.rb
scissor-0.2.1 lib/scissor/sound_file.rb
scissor-0.2.0 lib/scissor/sound_file.rb
scissor-0.1.2 lib/scissor/sound_file.rb
scissor-0.1.1 lib/scissor/sound_file.rb