Osc
#struct OscAn oscillator: a waveform, a pitch, and where in the cycle it is.
| Name | Type | Description |
|---|---|---|
wave | int = 0 | Which waveform it makes. |
freq | f64 = 440 | Its pitch in hertz. |
phase | f64 = 0 | Where in the cycle it is, from 0 to 1. |
duty | f64 = 0.5 | How much of a pulse cycle is high. |
rate | int = 44100 | The sample rate it is stepping at. |
import audio
import audio.synth
import math
func main:
mut o := synth.NewOsc(synth.WaveSine, 440.0, 44100)
println o.wave, o.freq, o.rate, o.phase
first := synth.OscNext(o)
println math.Abs(first) < 0.0001, o.phase > 0.0
println synth.WaveValue(synth.WaveSine, 0.0, 0.5)
println synth.WaveValue(synth.WaveSaw, 0.5, 0.5)
println synth.WaveValue(synth.WaveSquare, 0.25, 0.5),
synth.WaveValue(synth.WaveSquare, 0.75, 0.5)
println synth.WaveValue(synth.WaveTriangle, 0.25, 0.5)
println synth.WaveValue(synth.WavePulse, 0.1, 0.25)
t := synth.Tone(440.0, 0.05, 22050, synth.WaveSine)
println t.frames, t.rate, audio.Peak(t) > 0.9
e := synth.ADSR {
attack = 0.1,
decay = 0.1,
sustain = 0.5,
release = 0.2
}
println e.attack, e.sustain
println synth.ADSRAt(e, 0.0, 1.0), synth.ADSRAt(e, 0.05, 1.0), synth.ADSRAt(e, 0.1, 1.0)
println synth.ADSRAt(e, 0.2, 1.0), synth.ADSRAt(e, 0.5, 1.0)
println synth.ADSRAt(e, 1.2, 1.0) < 0.0001, synth.ADSRAt(e, 5.0, 1.0)
println synth.ADSRTotal(e, 1.0)
shaped := synth.ToneEnvelope(440.0, 0.05, 22050, synth.WaveSine, e, 0.8)
println audio.Sample(shaped, 0, 0), audio.Peak(shaped) <= 0.81
mut d := t
audio.Free(d)
mut d2 := shaped
audio.Free(d2)
end0 440 44100 0 true true 0 0 1 -1 0 1 1103 22050 true 0.1 0.5 0 0.5 1 0.5 0.5 true 0 1.2 0 true