Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add SoundFile::Wav and SoundFile::Mp3

  • Loading branch information...
commit 94f0c9178334110cce1ac5939f49b570be101a9f 1 parent 928174a
youpy authored
74 lib/scissor/sound_file.rb
View
@@ -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  lib/scissor/tape.rb
View
@@ -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
2  lib/scissor/writer.rb
View
@@ -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 spec/sound_file_spec.rb
View
@@ -7,8 +7,8 @@
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
@@ -16,7 +16,7 @@
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
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.