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
audio gem ruby sound wav wave wavefile

A Ruby gem for reading and writing sound files in Wave format (*.wav).

You can use this gem to create Ruby programs that work with audio, such as a command-line drum machine. Since it is written in pure Ruby (as opposed to wrapping an existing C library), you can use it without having to compile a separate extension.

For more info, check out the website: https://wavefilegem.com/

Example Usage

This example shows how to append three separate Wave files into a single file:

require "wavefile"
include WaveFile

FILES_TO_APPEND = ["file1.wav", "file2.wav", "file3.wav"]

Writer.new("append.wav", Format.new(:stereo, :pcm_16, 44100)) do |writer|
  FILES_TO_APPEND.each do |file_name|
    Reader.new(file_name).each_buffer do |buffer|
      writer.write(buffer)
    end
  end
end

More examples can be found at https://wavefilegem.com/examples.

Installation

First, install the WaveFile gem from rubygems.org:

gem install wavefile

...and include it in your Ruby program:

require "wavefile"

Note that if you're installing the gem into the default Ruby that comes pre-installed on MacOS (as opposed to a Ruby installed via RVM or rbenv), you should used sudo gem install wavefile. Otherwise you might run into a file permission error.

Compatibility

WaveFile has been tested with these Ruby versions, and appears to be compatible with them:

2.0 is the minimum supported Ruby version.

If you find any compatibility issues, please let me know by opening a GitHub issue.

Dependencies

WaveFile has no external dependencies when used as a gem.

However, it does have dependencies for local development, in order to run the tests. See below in section "Local Development".

Features

This gem lets you read and write audio data! You can use it to create Ruby programs that work with sound.

Current Release: v1.1.2

Released on December 30, 2022, this version fixes several edge case bugs related to reading a *.wav file's "fmt " chunk. In particular, reading a "fmt " chunk that has extra trailing bytes; reading a "fmt " chunk in WAVE_FORMAT_EXTENSIBLE format whose chunk extension is missing, incomplete, or has extra trailing bytes; and reading a "fmt " chunk whose chunk extension is too large to fit in the chunk. In short, some valid files that were previously rejected can now be read, and some invalid files are handled more properly.

The full details:

Previous Release: v1.1.1

Released on December 29, 2019, this version contains this change:

For changes in previous versions, visit https://github.com/jstrait/wavefile/releases.

Local Development

Running the Tests

First, install the required development/test dependencies:

bundle install

Then, to run the tests:

bundle exec rake test

Generating test fixtures

The *.wav fixtures in test/fixtures/wave are generated from *.yml files defined in /test/fixtures/yaml. To change one of the *.wav fixtures, edit the corresponding *.yml file, and then run:

rake test:create_fixtures

Similarly, if you want to add a new *.wav fixture, add a new *.yml file that describes it in /test/fixtures/yaml, and then run the rake command above.

Behind the scenes, rake test:create_fixtures runs tools/fixture_writer.rb, which is what actually generates each *.wav file.

Generating RDoc Documentation

rake rdoc

Contributing

  1. Fork my repo
  2. Create a branch for your changes
  3. Add your changes, and please include tests
  4. Make sure the tests pass by running rake test
  5. Create a pull request