sndfile provides a fast and easy way to read, process, and write audio file data. It wraps the libsndfile C library via FFI, reading & writing the sample data as a GSLng matrix.
The author has (so far) fleshed out only the parts needed for his own projects. Please do fork this project and contribute to it.
"Libsndfile is a C library for reading and writing files containing sampled sound (such as MS Windows WAV and the Apple/SGI AIFF format) through one standard library interface. It is released in source code format under the Gnu Lesser General Public License."
To use this Ruby library, you must have libsndfile and GSL on your machine. You can install them via apt-get (linux) or homebrew (OS X) or download them directly from their websites.
That being said:
gem install sndfile
or, in a Gemfile
gem "sndfile-ruby", :require => "sndfile"
Here's a simple example, that reads an arbitrary source file and produces a WAV file at half the volume:
fin = Sndfile::File.new(inpath)
Sndfile::File.open(outpath, :mode => :WRITE, :format => :WAV, :encoding => :PCM_16, :channels => fin.info.channels, :samplerate => fin.info.samplerate) do |fout|
while data = fin.read(10000)
fout.write(data * 0.5)
end
end
The audio file info, available as Sndfile::File#info
as per the above example, is an instance of Sndfile::Info
. You can also query it directly for a file:
info = Sndfile::File.info(filepath) #=> an instance of Sndfile::Info
# => info.format
# => info.encoding
# => info.endian
# => info.frames
# => info.samplerate
# => info.channels
See the RDOC for complete details
Known to work with MRI ruby 1.9.3, and 2.0.0
Release notes:
- 1.1.0 - Add detail fields to Sndfile::Error
- 1.0.0 - Add Sndfile::Info. Deprecate individual accessors. No longer support ruby 1.8.7
- 0.2.0 - Support ruby 1.8.7. Thanks to youpy
- 0.1.3 - Back to ruby-gsl-ng: memory leaks fixed
- 0.1.2 - Use ruby-gsl-ngx to avoid memory leaks
- 0.1.1 - Clean up vestigial includes in integration test
- 0.1.0 - Initial public release