Wav
a simple WAV file reader, writer and basic audio processor.
usage
require "wav"
# Reading and writing
wav = Wav.read("input.wav")
wav.write("output.wav")
# Building from scratch
sine = Wav.build { |w| w.sine 440, 2.0 }
square = Wav.build { |w| w.square 220, 1.0, 0.8 }
# Mixing tracks
mix = sine.mix square
# Applying effects
mix.delay(0.3, feedback: 0.5)
.chorus(depth: 0.003, lfo_rate: 2.0, mix: 0.3)
.low_pass(2_000)
.normalize(0.95)
.fade_in(0.1)
.fade_out(0.5)
# Generating noise and silence
Wav.build { |w| w.noise(2.0, 0.2).silence(0.5).saw 440, 1.0 }
# Trimming and resampling
wav.trim(1.0, 3.5).resample(22_050.0).to_mono
# Multi‑channel composition with cursor and channel targeting
track = Wav.build do |w|
w.left.sine 440, 1.0 # left channel, first second
w.at(1.0).right.square 880, 1.0 # right channel, second second
w.all.forward # move to end
w.triangle 220, 2.0, 0.5 # both channels
end
track.write "track.wav"
# Custom waveform generation
wav = Wav.build do |w|
w.generate(2.0, 0.8) { |t| Math.cos 2 * Math::PI * 330 * t }
end
# Information
puts wav # => #<Wav r=44100 ch=2 b=16 t=2.0s>
Constants
Constructors
creates a new Wav instance and yields it to the block for building.
accepts rate or samples per second (default: 44_100.0), channels number of channels (default: 1), bits bit depth, 8 or 16 (default: 16)
create a new Wav instance
accepts rate or samples per second (default: 44_100.0), channels number of channels (default: 1), bits bit depth, 8 or 16 (default: 16), other exceptional options are: samples initial sample array (default: empty array of Float64), cursor internal cursor position (default: 0) and target optional channel target for generation (default: nil, meaning all channels)
raises exception if parameters are invalid.
reads a WAV file from the given path, raises exception on invalid WAV or non-existent file
Instance methods
moves the internal cursor to the sample corresponding to time in seconds
applies a chorus effect, depth is modulation depth in seconds (default 0.002), lfo_rate is modulation rate in Hz (default 1.5), mix is the wet/dry balance (0 = dry only, 1 = wet only, default 0.5).
adds an echo / delay effect. time is delay in seconds, feedback controls the decay of repeats (default 0.4)
applies a linear fade of duration in seconds. if head is true, fades in at the start, if tail is true fades out at the end. defaults to fade in
moves the internal cursor forward by time in seconds, if time not set moves to the end
appends custom samples generated by block, given duration in seconds, scaled by amplitude, block receives time in seconds and should return a sample value between -1.0 and 1.0
applies a simple first-order low-pass filter with cutoff frequency in Hz
mixes the samples of another Wav, other into the current one (additive),
the two must have identical sample rates and channel counts otherwise raises exception
appends white noise for duration in seconds, scaled by amplitude
scales the audio to the peak absolute amplitude of target, target should be between 0 and 1 (default: 0.95)
resamples the audio using linear interpolation with new_rate, returns a new Wav instance
appends a sawtooth wave of frequency in Hz for duration in seconds, scaled by amplitude
appends a sine wave of frequency in Hz for duration in seconds, scaled by amplitude
appends a square wave of frequency in Hz for duration in seconds, scaled by amplitude
converts multi‑channel audio to mono by averaging the channels, returns a new Wav instance
appends a triangle wave of frequency in Hz for duration in seconds, scaled by amplitude
keeps only the portion between start and finish, values are in seconds