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

Writer not always writing when buffer has changed #6

Closed danpaul closed 11 years ago

danpaul commented 11 years ago

Thank you for your work on this. I really like this project. I have however noticed some odd behavior. When I write a file, if I attempt to overwrite, no changes are recorded. Oddly, even if I delete the file and allow the application to recreate it, the data from the previous file is recreated. However, if I change the file name, the new data is written to file. I'm not sure if I'm doing something wrong or if there's a bug.

Here is the application I'm working on:

require 'wavefile'
include WaveFile

sample_rate = 44100
buffer_size = sample_rate * 5
buffer_data = Array.new(buffer_size)

class Frequency_node
    def initialize(sample_rate, frequency)
        @sample_rate = sample_rate
        @frequency = sample_rate/frequency.to_f
        @current_frame = 0
    end
    def next()
        return_tone = Math.sin(((@current_frame % @frequency)/@frequency) * 2 * Math::PI)
        @current_frame += 1
        return return_tone
    end
end

f = Frequency_node.new(sample_rate, 440.0)

(0...buffer_size).each do |frame|
    buffer_data[frame] = f.next
end

format = Format.new(:mono, :pcm_16, 44100)
writer = Writer.new("a_440.wav", format)
buffer = Buffer.new(buffer_data, Format.new(:mono, :float, sample_rate))

writer.write(buffer)
writer.close()

After recording the 440 tone, if I then change it, the old tone is still recorded, even if I delete the file and allow the application to recreate it. However, if I rename the file i.e. "a_440_2.wav", the new tone will be recorded.

I'm using Ruby 1.9.3 and wavefile 0.5.0 on OSX 10.8.2

Thanks again.

danpaul commented 11 years ago

Oh man, I feel silly. The issue was with iTunes, apparently playing a cached copy of the file. Things seem to be working fine when I open the wave file with a different application.

jstrait commented 11 years ago

Thanks, I'm glad you find this gem useful! Since it sounds like you fixed the problem, I'll close this issue.