First construct a Reader object, then call each_buffer() on it. Successive sample buffers will be read from the file and passed to the given block, until all sample data in the file has been read. Finally, the Reader will automatically be closed. (Note that this is essentially the same as how IO.open works if you pass it a block).
Reading Wave File Manually
Alternately, you can manually call read() to control exactly how much of the file to read. When doing this, make sure to close the Reader when you’re done.
Reading a Wave File Into an Arbitrary Format
When reading sample data you can convert it to a different format on the fly. For example, let’s say my_file.wav has 2 channels and 16-bit PCM samples, but you want 1 channel normalized floating point samples. By passing a Format instance to the constructor, the samples will be read out in the desired format regardless of what is actually in the file:
Note: Sample data will not be re-sampled to match the target sample rate. For example, if reading data out of a file with 44,100Hz sample rate using a target Format with a 22,050Hz sample rate, the samples will not be re-sampled to a 22,050Hz sample rate. This means that audio will sound pitch shifted unless the file’s actual sample rate and the target sample rate are the same.
Getting Metadata About a Wave File
Reader.format contains info about the format samples will be converted to when being read out of the file, while Reader.native_format contains info about how the samples are actually stored in the file.
Reader.readable_format? will indicate whether the sample data is in a format understood by the WaveFile gem. (If not, an error will be raised when calling Reader.read).
Reader.duration can be used to determine the playback time of the file.
Writing a Wave File With A Block
The Writer object is used to write data to a wave file. When constructing the Writer, you can pass a block inside which the writing will occur. When the block exits the file will automatically be closed. The example below shows how to write a basic square wave to a file.
This should result in a file named my_file.wav being written to the current directory, and it should sound like this:
For more info on how to create simple sounds with Ruby, check out NanoSynth.
Writing a Wave File (Manual File Close Edition)
Alternately, you can manually control when to close the file, as shown below. Note that a file won’t be valid for playback until it is closed. This is because some data required for playback (such as the total number of samples) isn’t written to the file until it is closed.
This should result in a file identical to the previous example:
Copying a Wave File to Different Format
In this example, the sample data in the file original.wav will be written to copy.wav as stereo/16-bit with a 44,100Hz sample rate, regardless of what format the sample data in original.wav is stored in.
This example also shows that you can also pass a block to Writer.new(), and the Writer will automatically be closed when the block exits.
Appending Wave Files
This example will take 3 Wave files, and write them to a single file containing each input file played one after another. Note that the individual files can be in different formats. They will all be converted to the output file’s format.