Skip to content

Commit

Permalink
add SoundFile::Wav and SoundFile::Mp3
Browse files Browse the repository at this point in the history
  • Loading branch information
youpy committed Jun 4, 2012
1 parent 928174a commit 94f0c91
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 32 deletions.
74 changes: 48 additions & 26 deletions lib/scissor/sound_file.rb
Expand Up @@ -4,44 +4,66 @@

module Scissor
class SoundFile
SUPPORTED_FORMATS = %w/mp3 wav/
class Mp3 < SoundFile
def length
info.length
end

class Error < StandardError; end
class UnknownFormat < Error; end
def mono?
info.channel_mode == 'Single Channel'
end

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

unless SUPPORTED_FORMATS.include?(@ext)
raise UnknownFormat
def info
@info ||= Mp3Info.new(@filename.to_s)
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 ']

class Wav < SoundFile
def length
data.length / fmt.body.unpack('s2i2')[3].to_f
end

def mono?
fmt.body.unpack('s2')[1] == 1
end

private

def riff
@riff ||= Riff::Reader.open(@filename ,"r")
end

def data
@data ||= riff.root_chunk['data']
end

def fmt
@fmt ||= riff.root_chunk['fmt ']
end
end

def mono?
case @ext
when 'mp3'
Mp3Info.new(@filename.to_s).channel_mode == 'Single Channel'
when 'wav'
riff = Riff::Reader.open(@filename ,"r")
data = riff.root_chunk['data']
fmt = riff.root_chunk['fmt ']
SUPPORTED_FORMATS = {
:mp3 => Mp3,
:wav => Wav
}

fmt.body.unpack('s2')[1] == 1
class Error < StandardError; end
class UnknownFormat < Error; end

def self.new_from_filename(filename)
ext = filename.extname.sub(/^\./, '').downcase

unless klass = SUPPORTED_FORMATS[ext.to_sym]
raise UnknownFormat
end

klass.new(filename)
end

def initialize(filename)
@filename = Pathname.new(filename)
end
end
end
2 changes: 1 addition & 1 deletion lib/scissor/tape.rb
Expand Up @@ -17,7 +17,7 @@ def initialize(filename = nil)
@fragments << Fragment.new(
filename,
0,
SoundFile.new(filename).length)
SoundFile.new_from_filename(filename).length)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/scissor/writer.rb
Expand Up @@ -154,7 +154,7 @@ def run_command(cmd)
private

def mono?(filename)
SoundFile.new(filename).mono?
SoundFile.new_from_filename(filename).mono?
end
end
end
8 changes: 4 additions & 4 deletions spec/sound_file_spec.rb
Expand Up @@ -7,16 +7,16 @@

describe Scissor::SoundFile do
before do
@mp3 = Scissor::SoundFile.new(fixture('sample.mp3'))
@wav = Scissor::SoundFile.new(fixture('sine.wav'))
@mp3 = Scissor::SoundFile.new_from_filename(fixture('sample.mp3'))
@wav = Scissor::SoundFile.new_from_filename(fixture('sine.wav'))
end

after do
end

it "raise error if unknown file format" do
lambda {
Scissor::SoundFile.new(fixture('foo.bar'))
Scissor::SoundFile.new_from_filename(fixture('foo.bar'))
}.should raise_error(Scissor::SoundFile::UnknownFormat)
end

Expand All @@ -30,7 +30,7 @@
@mp3.should be_mono
@wav.should_not be_mono

Scissor::SoundFile.new(fixture('mono.wav')).should be_mono
Scissor::SoundFile.new_from_filename(fixture('mono.wav')).should be_mono
end
end
end

0 comments on commit 94f0c91

Please sign in to comment.