Sha256: 1e42d7f226908ffc84aa73b630f79ca500912cf11f894ac4eb50e63061cc4ec1

Contents?: true

Size: 734 Bytes

Versions: 3

Compression:

Stored size: 734 Bytes

Contents

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

module Scissor
  class SoundFile
    SUPPORTED_FORMAT = %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_FORMAT.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

3 entries across 3 versions & 1 rubygems

Version Path
youpy-scissor-0.0.15 lib/scissor/sound_file.rb
youpy-scissor-0.0.16 lib/scissor/sound_file.rb
youpy-scissor-0.0.17 lib/scissor/sound_file.rb