github.com/leite/wav
main / published Mar 11, 2026 / repository
a tiny library for reading, writing and synthesizing WAV audio files.
wav
a tiny library for reading, writing and synthesizing WAV audio files.
installation
add this to your shard.yml
dependencies:
wav:
github: leite/wav
version: ~> 0.1.0
usage
synthesis
create audio from scratch using Wav.build block.
require "wav"
wav = Wav.build(44_100.0, 2) do |w|
w.sine 440.0, 1.0 # a4 sine, 1 sec
w.sawtooth 110.0, 0.5 # a2 saw, .5 sec
w.noise 0.1, 0.5 # white noise
w.generate(1.0, 0.8) do |t| # fm
mod = Math.sin(2 * Math::PI * 6.0 * t) * 10.0
Math.sin 2 * Math::PI * (440.0 + mod) * t
end
end
stereo & cursor
position the cursor and target channels independently.
stereo = Wav.build(44_100.0, 2) do |w|
w.left
w.sine 440.0, 1.0 # left channel only
w.right
w.rewind
w.sine 660.0, 1.0 # right channel only
w.all
w.forward
w.noise 0.5, 0.3 # both channels
end
processing & dsp
chainable effects.
lead = Wav.read "lead.wav"
beat = Wav.read "drums.wav"
lead.mix(beat)
.trim(0.0, 5.0)
.low_pass(1_200.0)
.delay(0.3, 0.4)
.chorus(depth: 0.003, lfo_rate: 1.2)
.fade(2.0, head: true, tail: true)
.normalize(0.98)
.to_mono
io
read/write to files, memory or any IO.
wav.write "output.wav"
loaded = Wav.read "output.wav"
mem = IO::Memory.new
wav.write mem
mem.rewind
loaded = Wav.read mem
reference
constructor
Wav.new(rate: 44_100.0, channels: 1, bits: 16, samples: [] of Float64, cursor: 0, target: nil)creates a newWavinstance.rateis samples per second,channelsis the number of channels (1 = mono, 2 = stereo),bitsis bit depth (8 or 16). The optionalsamplesarray can pre‑fill audio data (interleavedFloat64in-1.0..1.0).cursorandtargetare used internally for sequential generation. Raises if parameters are invalid.
I/O
-
Wav.read(path : String) : Wavreads a WAV file from the given file path. Raises if the file does not exist or is invalid. -
Wav.read(io : IO) : Wavreads a WAV file from anyIOobject. The IO must be positioned at the start of a valid WAV stream. Raises on error. -
write(path : String) : selfwrites the current audio data to a file at the given path as a PCM WAV file. -
write(io : IO) : selfwrites the WAV data to anyIOobject.
generators
all generator methods append samples to the audio, respecting the current cursor position and
channel target. They return self for chaining.
-
sine(frequency : Float64, duration : Float64, amplitude: 1.0) : selfappends a sine wave of the given frequency (Hz) fordurationseconds, scaled byamplitude. -
square(frequency : Float64, duration : Float64, amplitude: 1.0) : selfappends a square wave. -
sawtooth(frequency : Float64, duration : Float64, amplitude: 1.0) : selfappends a sawtooth wave (rising ramp). -
saw(frequency : Float64, duration : Float64, amplitude: 1.0) : selfalias forsawtooth. -
triangle(frequency : Float64, duration : Float64, amplitude: 1.0) : selfappends a triangle wave. -
noise(duration : Float64, amplitude: 1.0) : selfappends white noise (uniform distribution-1..1). -
silence(duration : Float64) : selfappends silent samples (all zero) for the given duration. -
generate(duration : Float64, amplitude: 1.0, &block : Float64 -> Float64) : selfappends custom samples generated by the block. The block receives the current time in seconds (starting from the beginning of the generated segment) and should return a sample value between-1.0and1.0. The block is called for each sample frame.
effects
all effects modify the existing samples in place and return self.
-
mix(other : Wav) : selfmixes the samples of anotherWavinstance into the current one (additive). The two must have identical sample rates and channel counts. Mixing stops at the shorter length. -
delay(time : Float64, feedback: 0.4) : selfadds an echo/delay effect.timeis delay in seconds,feedbackcontrols the decay of repeats (0..1). -
low_pass(cutoff : Float64) : selfapplies a simple first‑order low‑pass filter with the given cutoff frequency (Hz). -
chorus(depth: 0.002, lfo_rate: 1.5, mix: 0.5) : selfapplies a chorus effect.depthis modulation depth in seconds,lfo_rateis modulation rate in Hz,mixis the wet/dry balance (0 = dry only, 1 = wet only). -
normalize(target: 0.95) : selfscales the entire audio so that the peak absolute amplitude becomestarget(must be between 0 and 1). Does nothing if all samples are zero. -
fade(duration : Float64, head: false, tail: false) : selfapplies a linear fade. If bothheadandtailarefalse, fades in from the start. Ifheadistrue, fades in from the start. Iftailistrue, fades out at the end. Both can be combined. -
fade_in(duration : Float64) : selfshorthand forfade(duration, head: true). -
fade_out(duration : Float64) : selfshorthand forfade(duration, tail: true).
transforms
-
resample(new_rate : Float64) : Wavresamples the audio to a new sample rate using linear interpolation. Returns a newWavinstance with the new rate. -
trim(start : Float64, finish : Float64) : selfkeeps only the portion betweenstartandfinishseconds (inclusive). Cuts samples outside that range. -
to_mono : Wavconverts multi‑channel audio to mono by averaging channels. Returns a newWavinstance with one channel.
cursor & channels
these methods control where generated samples are written and which channels are affected.
They return self.
-
at(time : Float64) : selfmoves the internal write cursor to the sample attimeseconds from the start. Subsequent generators will insert/overwrite samples starting at that position. -
rewind : selfmoves the cursor to the beginning (time 0). -
forward(time : Float64 = 0.0) : selfmoves the cursor forward bytimeseconds. Iftimeis 0 (default), moves to the end of the current audio. -
left : selfsets the channel target to the left channel (index 0). Generators will write only to this channel. -
right : selfsets the channel target to the right channel (index 1). -
all : selfresets the channel target so that generators write to all channels (default). -
channel(target : Int32?) : selfsets the channel target to a specific index (0‑based). Passnilto write to all channels. Raises if the channel index is out of range.
information
to_s(io) : Nilreturns a human‑readable summary, e.g.#<Wav r=44100 ch=2 b=16 t=1.5s>.
attributes
rate : Float64– sample rate in Hz.channels : Int32– number of channels.bits : Int32– bit depth (8 or 16).samples : Array(Float64)– raw sample array (interleaved, values in-1.0..1.0).
Notes
- all samples are internally stored as
Float64in the range-1.0to1.0. - methods that modify the audio (effects, generation) generally operate on the existing samples and may extend or overwrite them depending on cursor position.
- the library only supports 8‑bit (unsigned) and 16‑bit (signed) PCM WAV files. Reading other formats will raise an error.
- when mixing or combining Wav instances, ensure they have compatible sample rates and channel counts.
API
- Wav
a simple WAV file reader, writer and basic audio processor.