Permalink
Browse files

copy 1st channel to 2nd channel when source file is mono

  • Loading branch information...
1 parent 1cbe03d commit 64a8aad4759579b55b3e352e811f2c629399e20f @youpy committed Jun 4, 2012
Showing with 32 additions and 1 deletion.
  1. +13 −0 lib/scissor/sound_file.rb
  2. +10 −1 lib/scissor/writer.rb
  3. BIN spec/fixtures/mono.wav
  4. +9 −0 spec/sound_file_spec.rb
@@ -30,5 +30,18 @@ def length
data.length / fmt.body.unpack('s2i2')[3].to_f
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 ']
+
+ fmt.body.unpack('s2')[1] == 1
+ end
+ end
end
end
View
@@ -28,10 +28,13 @@ def add_track(fragments)
def join_fragments(fragments, outfile, tmpdir)
position = 0.0
cmd = %w/ecasound/
+ is_mono = {}
fragments.each_with_index do |fragment, index|
fragment_filename = fragment.filename
+ is_mono[fragment_filename] ||= mono?(fragment_filename)
+
if !index.zero? && (index % 28).zero?
run_command(cmd.join(' '))
cmd = %w/ecasound/
@@ -44,7 +47,7 @@ def join_fragments(fragments, outfile, tmpdir)
movie.transcode(fragment_outfile.to_s, :audio_sample_rate => 44100)
end
- cmd << "-a:#{index} -o:#{outfile} -y:#{position}"
+ cmd << "-a:#{index} -o:#{outfile} -y:#{position}#{is_mono[fragment_filename] ? ' -chcopy:1,2' : ''}"
if fragment.stretched? && fragment.pitch.to_f != 100.0
rubberband_out = tmpdir + (Digest::MD5.hexdigest(fragment_filename.to_s) + "rubberband_#{index}.wav")
@@ -147,5 +150,11 @@ def run_command(cmd)
return result
end
+
+ private
+
+ def mono?(filename)
+ SoundFile.new(filename).mono?
+ end
end
end
View
Binary file not shown.
@@ -24,4 +24,13 @@
@mp3.length.should be_within(0.1).of(178.1)
@wav.length.should eql(1.0)
end
+
+ describe '#mono?' do
+ it "should return true if sound file is mono" do
+ @mp3.should be_mono
+ @wav.should_not be_mono
+
+ Scissor::SoundFile.new(fixture('mono.wav')).should be_mono
+ end
+ end
end

0 comments on commit 64a8aad

Please sign in to comment.