jstrait / wavefile

A Ruby gem for reading and writing sound files in Wave format (*.wav)
https://wavefilegem.com
MIT License
209 stars 24 forks source link

Possible bug when no block is given to the writer? #12

Closed emmx closed 10 years ago

emmx commented 10 years ago

If I'm not wrong, both examples below are supposed to work just as well. However, the second test produces a WAV file with the correct size, but somehow it's corrupted.

require "wavefile"

# Test 1, this works

reader = WaveFile::Reader.new "test.wav"
WaveFile::Writer.new("output1.wav", reader.format) do |writer|
  reader.each_buffer(4096) do |buffer|
    writer.write WaveFile::Buffer.new(buffer.samples, reader.format)
  end
end

# Test 2, output corrupted

reader = WaveFile::Reader.new "test.wav"
writer = WaveFile::Writer.new("output2.wav", reader.format)
reader.each_buffer(4096) do |buffer|
  writer.write WaveFile::Buffer.new(buffer.samples, reader.format)
end
jstrait commented 10 years ago

If you add writer.close at the end of the second example, does it work? That is:

reader = WaveFile::Reader.new "test.wav"
writer = WaveFile::Writer.new("output2.wav", reader.format)
reader.each_buffer(4096) do |buffer|
  writer.write WaveFile::Buffer.new(buffer.samples, reader.format)
end
writer.close

The file won't be valid until close is called. When using a block as in the first example, the WaveFile will automatically be closed when the block exits, which is why you don't need to call close there.

emmx commented 10 years ago

It makes sense, now it works!