Read & write .wav sound files with Ruby
Read/Write Common Formats
PCM Integer – 8, 16, 24, or 32 bits per sample
PCM Float – 32 or 64 bits per sample
Any number of channels
Seamless ConversionsConvert back and forth between any format the gem supports. Read from an 8-bit mono file as if it were 16-bit stereo. Generate floating point samples, but write them as 16-bit integers.
Query MetadataGet the sample format, sample rate, number of channels, number of sample frames, etc. from a Wave file, even if the samples are in a format this gem can’t read or write.
Easy to InstallWritten in pure Ruby, so there’s no need to compile a separate extension in order to use it.
Let’s say you have these three Wave files:
The program below will concatenate them into one file:
include WaveFile # To avoid prefixing classes with "WaveFile::"
FILES_TO_APPEND = [ "one.wav" , "two.wav" , "three.wav" ]
OUTPUT_FORMAT = Format . new ( :stereo , :pcm_16 , 44100 )
Writer . new ( "one_two_three.wav" , OUTPUT_FORMAT ) do | writer |
FILES_TO_APPEND . each do | file_name |
Reader . new ( file_name ). each_buffer do | buffer |
writer . write ( buffer )
Intrigued? Check out
Or, check out the
tutorial for a getting started guide.
First, install the WaveFile gem from rubygems.org:
gem install wavefile
…and include it in your Ruby program:
Note: It’s recommended that you use a Ruby version manager such as
rvm or rbenv. But if using the system Ruby that comes pre-installed on macOS, you should used
sudo gem install wavefile. Otherwise you might run into a file permission error.