Class: Scissor::SoundFile

Inherits:
Object
  • Object
show all
Defined in:
lib/scissor/sound_file.rb

Defined Under Namespace

Classes: Error, UnknownFormat

Constant Summary collapse

SUPPORTED_FORMATS =
%w/mp3 wav/

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ SoundFile

Returns a new instance of SoundFile.



12
13
14
15
16
17
18
19
# File 'lib/scissor/sound_file.rb', line 12

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

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

Instance Method Details

#lengthObject



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/scissor/sound_file.rb', line 21

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