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

examples: how create reverse file? #30

Closed letarg0 closed 6 years ago

letarg0 commented 6 years ago

i have wav and need reverse file how read file and add noise or add other file wav?

jstrait commented 6 years ago

To reverse a *.wav file, you can read the entire file into a buffer, reverse the buffer's sample array, and then write it back out. For example:

require 'wavefile'
include WaveFile

reader = Reader.new("to_be_reversed.wav")
buffer = reader.read(reader.total_sample_frames)
reader.close

buffer.samples.reverse!

writer = Writer.new("reversed.wav", reader.format) do |writer|
  writer.write(buffer)
end

(There's currently not a good way to reverse a file without reading the entire file into memory).

This article describes how to create some simple sounds and write them to a *.wav file, including white noise: https://www.joelstrait.com/nanosynth_create_sound_with_ruby/

Hope this helps!

jstrait commented 6 years ago

Closing this issue, hope this helped!