Repository navigation
sound
use sound gives a program notes, melodies, tones and effects; this page shows the part that is computed while it
plays.
A function of the time is a sound: play calls it for every sample, t the sample's time in seconds, a moment ahead
of the speaker, so it plays in real time. Its values go from -1 to 1.
use sound
global phase = 0.0 as float
siren(t) := {
pitch = 440 + 110 * sin(pi * t) // glides between 330 and 550 Hz
phase = phase + 2 * pi * pitch / sample_rate
0.3 * sin(phase)
}
play siren for 4s
The phase adds up each sample's step, so the glide stays smooth; state between samples lives in globals.
A function that fills a whole block is faster: one call per 512 samples instead of one per sample, t the time of the
block's first sample. With two blocks it plays in stereo, left and right; chord(block, t) would be mono.
use sound
chord(left, right, t) := for i in 1 to #left {
time = t + (i - 1) / sample_rate
low = sin(2 * pi * 262 * time)
high = sin(2 * pi * 392 * time)
left#i = 0.4 * low + 0.1 * high
right#i = 0.1 * low + 0.4 * high
}
play chord for 3s
Both run as samples: warp samples/siren_sound.warp and warp samples/stereo_sound.warp.
play [left, right] for 2s plays a pair of functions of the time in stereo. render_sound "song.wav" writes what
played into a WAV file, signals included.