Sha256: e925880fa44f5d4f21f55412206a587d834b45d0bc240732343dd6e32592ef0e

Contents?: true

Size: 736 Bytes

Versions: 10

Compression:

Stored size: 736 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).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

10 entries across 10 versions & 2 rubygems

Version Path
youpy-scissor-0.0.18 lib/scissor/sound_file.rb
youpy-scissor-0.0.19 lib/scissor/sound_file.rb
youpy-scissor-0.0.20 lib/scissor/sound_file.rb
youpy-scissor-0.0.21 lib/scissor/sound_file.rb
scissor-0.1.0 lib/scissor/sound_file.rb
scissor-0.0.26 lib/scissor/sound_file.rb
scissor-0.0.25 lib/scissor/sound_file.rb
scissor-0.0.24 lib/scissor/sound_file.rb
scissor-0.0.23 lib/scissor/sound_file.rb
scissor-0.0.22 lib/scissor/sound_file.rb