RubyDSP
🚧 Status: This project is currently in early development. It is fully functional, but API changes are expected.
RubyDSP is an audio processing and DSP Ruby gem. Ultimately, it aims to be librosa-wannabe for Ruby. It uses C++ under the hood, utilizing miniaudio and Rice
Features
- Fast: Basically all of the code is written in C++. While not extremely optimized currently, it still absolutely shreds native Ruby.
- Format Agnostic Loading: Automatically decodes standard audio formats (WAV, MP3, FLAC) via
miniaudio. - Zero-Dependency Native Build: No need to install
ffmpegorlibsndfileon your system. - YARD Support: Includes pure-Ruby stubs (in
stubs, duh) for IDE autocomplete and inline documentation.
Installation
Since the gem is currently in pre-release, you can install it directly from GitHub by adding this line to your application's Gemfile:
source "https://rubygems.org"
gem 'ruby_dsp', github: 'cichrrad/rubyDSP'
And then execute:
$ bundle install
(Note: Installing this gem requires a modern C++ compiler, as it builds the native extensions directly on your machine upon installation. It requires Ruby 3.0+).
After this, you can run your scripts with
$ bundle exec ruby script.rb
Quick Start
Here is a quick look at what you can do with a loaded AudioTrack:
require 'ruby_dsp'
# Load an audio file
track = RubyDSP::AudioTrack.new("raw_vocals.wav")
puts track
# => ['raw_vocals.wav', 12.450s duration, 2 channel(s), 48000Hz sample rate]
# Do stuff!
track.to_mono! # Averages channels into mono
track.resample!(44100) # Linearly resamples to target rate
track.trim_silence!(-60.0) # Strips leading/trailing silence below -60dB
# Analysis & Math
puts "Peak Amp: #{track.peak_amp}"
puts "Overall RMS: #{track.rms}"
puts "Overall ZCR: #{track.zcr}"
# You can also get framed analysis for time-series data:
# framed_rms_data = track.framed_rms(2048, 512) also works
framed_rms_data = track.framed_rms(frame_length: 2048, hop_length: 512)
# Save the results
track.save_track("processed_vocals.wav")
Development
If you want to clone the repo and hack on the C++ guts, we have fully automated the compilation and testing steps.
- Clone the repo and run
bundle installto grab the development dependencies. - Run
rake test— this will automatically compile the C++extconf.rband run the Minitest suite. - Run
rake doc:generate ; rake doc:server— this will compile the YARD stubs into HTML and boot a live-reloading local web server athttp://localhost:8808so you can read the docs!
License
The gem is available as open source under the terms of the MIT License.