audio.dsp
A Fourier transform, windows, filters and resampling for sound.
import audio.dsp
This is the arithmetic behind sound: a Fourier transform to see what frequencies are in a block, windows to taper the block first, filters to shape it and resampling to move it between rates. The codecs in this family lean on the same code, which is why it lives here rather than inside any one of them.
Everything works on a clip or on plain arrays of f64. The transform is radix-2, so a length has to be a power of two: use NextPow2 to pad up to one.
Things to know
FFTandIFFTwork in place on two arrays, the real parts and the imaginary parts.IFFTalready divides by the length, so it undoesFFTexactly.- A real signal shows the same strength at bin k and at bin n - k, so only the first half of a spectrum tells you anything new.
- Taper a block with
ApplyWindowbefore transforming it, or a tone that does not fit a whole number of cycles will smear across every bin. - The filters follow the well known cookbook formulas. A
Biquadremembers two samples, soBiquadResetit before a different clip, and preferBiquadProcessAll, which gives each channel its own copy. BiquadResponsetells you what a filter does at a frequency without running any sound through it, which makes filters easy to test.ResampleSincis the one to use on music;ToRatein audio is the quick linear version.
Low-pass filtering a signal
A biquad is a small filter with a few coefficients and two remembered samples. LowPass builds one from a cutoff in hertz, a Q and the sample rate. Before touching any sound you can ask BiquadResponse how much the filter passes at a frequency, as a plain multiplier: near 1 at 100 Hz, near 0 at 10 kHz.
import audio
import audio.dsp
import math
func tone(rate: int, freq: f64, frames: int): audio.Clip:
mut c := audio.New(rate, 1, frames)
mut i := 0
until i < frames:
audio.SetSample(c, i, 0, math.Sin(2.0 * math.PI * freq * (i as f64) / (rate as f64)))
i++
end
ret c
end
func main:
lp := dsp.LowPass(1000.0, 0.707, 48000)
println dsp.BiquadResponse(lp, 100.0, 48000) > 0.99
println dsp.BiquadResponse(lp, 10000.0, 48000) < 0.02
mut low := tone(48000, 100.0, 2048)
mut high := tone(48000, 10000.0, 2048)
low_before := audio.RMS(low)
high_before := audio.RMS(high)
dsp.BiquadProcessAll(lp, low)
dsp.BiquadProcessAll(lp, high)
low_db := audio.GainToDb(audio.RMS(low) / low_before)
high_db := audio.GainToDb(audio.RMS(high) / high_before)
println low_db > -1.0
println high_db < -20.0
audio.Free(low)
audio.Free(high)
endBiquadProcessAll runs a fresh copy of the filter over every channel of a clip, in place. The program measures the level before and after with audio.RMS and audio.GainToDb: the 100 Hz tone loses less than a decibel and the 10 kHz tone loses more than twenty.
Finding the strongest frequency
Fill two vectors with the real and imaginary parts of the signal, here a 1000 Hz sine sampled at 8000 Hz. Multiply the real part by a window first with ApplyWindow, so the ends of the block do not leak into neighbouring bins. FFTVec transforms both vectors in place; the length must be a power of two, and it returns false when it is not.
import audio.dsp
import math
func main:
n := 256
rate := 8000
mut re := Vector {} as Vector<f64>
mut im := Vector {} as Vector<f64>
defer re.Free()
defer im.Free()
mut i := 0
until i < n:
re.Push(math.Sin(2.0 * math.PI * 1000.0 * (i as f64) / (rate as f64)))
im.Push(0.0)
i++
end
dsp.ApplyWindow(re.Ref(0), n, dsp.WindowHann, 0.0)
println dsp.FFTVec(re, im)
mags := dsp.Magnitude(re.Ref(0), im.Ref(0), n)
defer mags.Free()
peak := dsp.PeakBin(mags)
println peak, dsp.BinFrequency(peak, n, rate)
println dsp.IsPow2(n), dsp.NextPow2(300)
endMagnitude gives the strength of each bin, PeakBin picks the strongest, and BinFrequency turns that bin number into hertz. With 256 samples at 8000 Hz each bin is 31.25 Hz wide, so 1000 Hz lands exactly in bin 32.
Changing the sample rate
ResampleTo returns a new clip at the rate you ask for. The last argument picks the method: QualityLinear is quick, QualityCubic a good middle, and QualitySinc the one to use on music. Going from 8000 to 16000 Hz doubles the frame count and keeps the duration at 0.1 seconds.
import audio
import audio.dsp
import math
func main:
mut c := audio.New(8000, 1, 800)
mut i := 0
until i < 800:
audio.SetSample(c, i, 0, 0.5 * math.Sin(2.0 * math.PI * 300.0 * (i as f64) / 8000.0))
i++
end
mut up := dsp.ResampleTo(c, 16000, dsp.QualitySinc)
println up.rate, up.frames, audio.Duration(up)
mut back := dsp.ResampleTo(up, 8000, dsp.QualitySinc)
println back.rate, back.frames
println dsp.Correlation(c, back) > 0.99
audio.Free(c)
audio.Free(up)
audio.Free(back)
endTo see whether a round trip kept the sound, resample back and call Correlation, which is 1 for identical clips and near 0 for unrelated ones. Both new clips belong to you and must be freed.
Types
Compressor
struct Compressor| Name | Type | Description |
|---|---|---|
threshold | f64 = -20 | The level in decibels above which the squeezing starts. |
ratio | f64 = 4 | How much of the excess to keep. A ratio of 4 lets a quarter of it through. |
attack | f64 = 0.005 | How many seconds to take hold once the signal goes over. |
release | f64 = 0.1 | How many seconds to let go once it comes back under. |
makeup | f64 = 0 | Decibels of gain added afterwards, to win back what the squeezing cost. |
import audio
import audio.dsp
import audio.synth
import math
func main:
mut src := synth.Tone(440.0, 0.1, 22050, synth.WaveSine)
println src.frames, src.rate
mut e := dsp.Echo(src, 0.02, 2, 0.5)
println e.frames, e.frames == src.frames + 2 * 441
println math.Abs(audio.Sample(e, 451, 0) - (audio.Sample(src, 451, 0) + audio.Sample(src, 10, 0) * 0.5)) < 0.001
mut rv := dsp.Reverb(src, 0.5, 0.3)
println rv.frames, audio.Peak(rv) <= 1.0
mut dry := dsp.Reverb(src, 0.5, 0.0)
println math.Abs(audio.Sample(dry, 50, 0) - audio.Sample(src, 50, 0)) < 0.0001
mut ch := dsp.Chorus(src, 0.5, 2.0, 0.5)
mut fl := dsp.Flanger(src, 0.5, 1.0, 0.5)
println ch.frames, fl.frames
println dsp.Correlation(src, ch) < 0.999, dsp.Correlation(src, fl) < 0.9999
mut tr := audio.Clone(src)
dsp.Tremolo(tr, 10.0, 1.0)
println audio.RMS(tr) < audio.RMS(src), audio.Peak(tr) <= audio.Peak(src) + 0.0001
mut dl := audio.Clone(src)
dsp.Delay(dl, 0.01, 0.5, 0.5)
println dl.frames == src.frames
k := dsp.Compressor { threshold = -20.0, ratio = 8.0, attack = 0.001, release = 0.05 }
mut hard := audio.Clone(src)
dsp.CompressorProcess(k, hard)
println audio.RMSDb(hard) < audio.RMSDb(src) - 6.0
mut hot := audio.Clone(src)
audio.Scale(hot, 4.0)
dsp.LimiterProcess(hot, -1.0)
println audio.PeakDb(hot) <= -1.0 + 0.001
mut gated := audio.Clone(src)
audio.Gain(gated, -60.0)
dsp.GateProcess(gated, -40.0, 0.001)
println audio.Peak(gated) < 0.0001
mut longer := dsp.TimeStretch(src, 2.0)
println longer.frames == src.frames * 2, longer.rate
mut up := dsp.PitchShift(src, 12.0)
println up.frames, up.rate
println dsp.DetectPitch(up, 0, 2048) > 700.0
audio.Free(src)
audio.Free(e)
audio.Free(rv)
audio.Free(dry)
audio.Free(ch)
audio.Free(fl)
audio.Free(tr)
audio.Free(dl)
audio.Free(hard)
audio.Free(hot)
audio.Free(gated)
audio.Free(longer)
audio.Free(up)
endBiquad
struct Biquad| Name | Type | Description |
|---|---|---|
b0 | f64 = 1 | The first feed forward coefficient. |
b1 | f64 = 0 | The second feed forward coefficient. |
b2 | f64 = 0 | The third feed forward coefficient. |
a1 | f64 = 0 | The first feedback coefficient, already divided through by a0. |
a2 | f64 = 0 | The second feedback coefficient, already divided through by a0. |
z1 | f64 = 0 | The first remembered sample. Reset it before filtering a new clip. |
z2 | f64 = 0 | The second remembered sample. |
import audio
import audio.dsp
import math
func tone(rate: int, freq: f64, frames: int): audio.Clip:
mut c := audio.New(rate, 1, frames)
mut i := 0
until i < frames:
audio.SetSample(c, i, 0, math.Sin(2.0 * math.PI * freq * (i as f64) / (rate as f64)))
i++
end
ret c
end
func main:
lp := dsp.LowPass(1000.0, 0.707, 48000)
println dsp.BiquadResponse(lp, 100.0, 48000) > 0.99
println math.Abs(dsp.BiquadResponse(lp, 1000.0, 48000) - 0.7071) < 0.01
println dsp.BiquadResponse(lp, 10000.0, 48000) < 0.02
hp := dsp.HighPass(1000.0, 0.707, 48000)
println dsp.BiquadResponse(hp, 10000.0, 48000) > 0.99, dsp.BiquadResponse(hp, 100.0, 48000) < 0.02
bp := dsp.BandPass(1000.0, 1.0, 48000)
println math.Abs(dsp.BiquadResponse(bp, 1000.0, 48000) - 1.0) < 0.01, dsp.BiquadResponse(bp, 100.0, 48000) < 0.2
nt := dsp.Notch(1000.0, 1.0, 48000)
println dsp.BiquadResponse(nt, 1000.0, 48000) < 0.01, dsp.BiquadResponse(nt, 100.0, 48000) > 0.9
ap := dsp.AllPass(1000.0, 0.707, 48000)
println math.Abs(dsp.BiquadResponse(ap, 500.0, 48000) - 1.0) < 0.000001
pk := dsp.Peaking(1000.0, 1.0, 6.0, 48000)
println dsp.BiquadResponse(pk, 1000.0, 48000) > 1.98, dsp.BiquadResponse(pk, 1000.0, 48000) < 2.01
ls := dsp.LowShelf(1000.0, 0.707, 6.0, 48000)
println dsp.BiquadResponse(ls, 50.0, 48000) > 1.9, math.Abs(dsp.BiquadResponse(ls, 20000.0, 48000) - 1.0) < 0.05
hs := dsp.HighShelf(1000.0, 0.707, 6.0, 48000)
println dsp.BiquadResponse(hs, 20000.0, 48000) > 1.9
mut hi := tone(48000, 10000.0, 2048)
before := audio.RMS(hi)
dsp.BiquadProcessAll(lp, hi)
println audio.GainToDb(audio.RMS(hi) / before) < -20.0
mut lo := tone(48000, 100.0, 2048)
lo_before := audio.RMS(lo)
dsp.BiquadProcessAll(lp, lo)
println audio.GainToDb(audio.RMS(lo) / lo_before) > -1.0
mut one := dsp.LowPass(1000.0, 0.707, 48000)
dsp.BiquadStep(one, 1.0)
println one.z1 != 0.0
dsp.BiquadReset(one)
println one.z1, one.z2
println math.Abs(one.b0 - lp.b0) < 0.000000001
mut ch := tone(8000, 100.0, 400)
audio.Scale(ch, 0.5)
mut k := 0
until k < ch.frames:
audio.AddSample(ch, k, 0, 0.25)
k++
end
println audio.DcOffset(ch) > 0.2
dsp.DCRemove(ch)
println math.Abs(audio.DcOffset(ch)) < 0.000001
mut single := dsp.LowPass(1000.0, 0.707, 8000)
dsp.BiquadProcess(single, ch, 0)
println ch.frames
audio.Free(hi)
audio.Free(lo)
audio.Free(ch)
endSpectrum
struct Spectrum| Name | Type | Description |
|---|---|---|
bins | int = 0 | How many frequency bins each row holds, which is half the window. |
frames | int = 0 | How many rows there are. |
rate | int = 0 | The sample rate the clip had, needed to turn a bin into a frequency. |
hop | int = 0 | How many samples apart the rows are. |
data | void* | The magnitudes, row after row. Release them with FreeSpectrum. |
import audio
import audio.dsp
import audio.synth
import math
func main:
mut low := synth.Tone(110.0, 0.1, 22050, synth.WaveSine)
mut high := synth.Tone(1760.0, 0.1, 22050, synth.WaveSine)
println dsp.ZeroCrossingRate(high) > dsp.ZeroCrossingRate(low) * 4.0
println dsp.ZeroCrossingRate(audio.Clip {})
each f in [220.0, 440.0]:
t := synth.Tone(f, 0.1, 22050, synth.WaveSine)
got := dsp.DetectPitch(t, 0, 2048)
println math.Abs(got - f) / f < 0.05
mut d := t
audio.Free(d)
end
println dsp.DetectPitch(audio.Clip {}, 0, 2048)
mut rich := synth.Additive(220.0, 8, 0.7, 0.12, 22050)
mut s := dsp.STFT(rich, 1024, 256, dsp.WindowHann)
println s.bins, s.rate, s.hop, s.frames > 1
println dsp.SpectrumAt(s, 0, 9999)
cen := dsp.SpectralCentroid(s, 1)
println cen > 100.0, cen < 5000.0
println dsp.SpectralRolloff(s, 1, 0.85) > 0.0
flat := dsp.SpectralFlatness(s, 1)
println flat >= 0.0, flat <= 1.0
println dsp.SpectralFlux(s, 0)
mut noise := synth.NoiseWhite(0.12, 22050)
mut ns := dsp.STFT(noise, 1024, 256, dsp.WindowHann)
println dsp.SpectralFlatness(ns, 1) > flat
mut loud := synth.Tone(440.0, 0.1, 22050, synth.WaveSine)
lu := dsp.LoudnessLUFS(loud)
println lu < 0.0, lu > -40.0
mut soft := audio.Clone(loud)
audio.Gain(soft, -20.0)
println math.Abs((dsp.LoudnessLUFS(soft) - lu) + 20.0) < 1.0
mut norm := audio.Clone(loud)
dsp.NormalizeLUFS(norm, -16.0)
println math.Abs(dsp.LoudnessLUFS(norm) + 16.0) < 1.0
println dsp.TruePeakDb(loud) >= audio.PeakDb(loud) - 0.01
mut beat := audio.New(22050, 1, 22050)
mut b := 0
until b < 8:
at := b * 2756
mut k := 0
until k < 1000:
env := 1.0 - (k as f64) / 1000.0
audio.SetSample(beat, at + k, 0, 0.8 * env * math.Sin(2.0 * math.PI * 600.0 * (k as f64) / 22050.0))
k++
end
b++
end
mut onsets := dsp.DetectOnsets(beat, 1024, 256)
println onsets.Len() >= 4
bpm := dsp.DetectBPM(beat)
println bpm >= 60.0, bpm <= 200.0
println dsp.DetectBPM(audio.Clip {}), dsp.DetectOnsets(audio.Clip {}, 1024, 256).Len()
onsets.Free()
dsp.FreeSpectrum(s)
dsp.FreeSpectrum(ns)
audio.Free(low)
audio.Free(high)
audio.Free(rich)
audio.Free(noise)
audio.Free(loud)
audio.Free(soft)
audio.Free(norm)
audio.Free(beat)
endComplex
struct Complex| Name | Type | Description |
|---|---|---|
re | f64 = 0 | The real part. |
im | f64 = 0 | The imaginary part. |
import audio.dsp
import math
func main:
a := dsp.Complex { re = 3.0, im = 4.0 }
b := dsp.Complex { re = 1.0, im = -2.0 }
println a.re, a.im
println dsp.CAbs(a)
println dsp.CAdd(a, b).re, dsp.CAdd(a, b).im
println dsp.CSub(a, b).re, dsp.CSub(a, b).im
println dsp.CMul(a, b).re, dsp.CMul(a, b).im
println dsp.CScale(a, 2.0).re, dsp.CScale(a, 2.0).im
println dsp.CConj(a).re, dsp.CConj(a).im
up := dsp.Complex { re = 0.0, im = 1.0 }
println math.Abs(dsp.CArg(up) - math.PI / 2.0) < 0.000000001
p := dsp.CFromPolar(2.0, 0.0)
println math.Abs(p.re - 2.0) < 0.000000001, math.Abs(p.im) < 0.000000001
println dsp.CAbs(dsp.CFromPolar(5.0, 1.0))
endConstants and variables
Resampling quality Quality…
| Name | Value |
|---|---|
QualityLinear | 0 |
QualityCubic | 1 |
QualitySinc | 2 |
import audio
import audio.dsp
import math
func tone(rate: int, freq: f64, frames: int): audio.Clip:
mut c := audio.New(rate, 1, frames)
mut i := 0
until i < frames:
audio.SetSample(c, i, 0, math.Sin(2.0 * math.PI * freq * (i as f64) / (rate as f64)))
i++
end
ret c
end
func main:
mut src := tone(44100, 440.0, 4410)
mut up := dsp.ResampleSinc(src, 48000, 16)
println up.rate, up.frames
mut back := dsp.ResampleSinc(up, 44100, 16)
println back.rate, back.frames
mut inner := audio.Slice(src, 64, src.frames - 128)
mut inner2 := audio.Slice(back, 64, back.frames - 128)
println dsp.Correlation(inner, inner2) > 0.999
mut cub := dsp.ResampleCubic(src, 48000)
mut cback := dsp.ResampleCubic(cub, 44100)
mut inner3 := audio.Slice(cback, 64, cback.frames - 128)
println cub.rate, dsp.Correlation(inner, inner3) > 0.999
println dsp.ResampleTo(src, 48000, dsp.QualityLinear).rate
println dsp.ResampleTo(src, 48000, dsp.QualityCubic).rate
println dsp.ResampleTo(src, 48000, dsp.QualitySinc).rate
mut same := dsp.ResampleSinc(src, 44100, 16)
println math.Abs(dsp.Correlation(src, same) - 1.0) < 0.000001
println dsp.Correlation(src, audio.Clip {})
println dsp.KaiserBeta
mut down := dsp.ResampleSinc(src, 22050, 16)
println down.rate, down.frames
audio.Free(src)
audio.Free(up)
audio.Free(back)
audio.Free(inner)
audio.Free(inner2)
audio.Free(cub)
audio.Free(cback)
audio.Free(inner3)
audio.Free(same)
audio.Free(down)
endKaiserBeta
const KaiserBeta: f64 = 8.6The example for this item is shown with the ones above: dsp-Resample.salam
Window shapes Window…
| Name | Value |
|---|---|
WindowRectangular | 0 |
WindowHann | 1 |
WindowHamming | 2 |
WindowBlackman | 3 |
WindowBlackmanHarris | 4 |
WindowSine | 5 |
WindowVorbis | 6 |
WindowKaiser | 7 |
import audio.dsp
import math
func main:
println dsp.BesselI0(0.0)
hann := dsp.MakeWindow(dsp.WindowHann, 9, 0.0)
println hann.Len(), hann.Get(0) < 0.000000001, math.Abs(hann.Get(4) - 1.0) < 0.000000001
println hann.Get(8) < 0.000000001
println dsp.WindowValue(dsp.WindowRectangular, 0, 9, 0.0)
println math.Abs(dsp.WindowValue(dsp.WindowHamming, 0, 9, 0.0) - 0.08) < 0.000000001
println math.Abs(dsp.MakeWindow(dsp.WindowBlackman, 9, 0.0).Get(4) - 1.0) < 0.000000001
println math.Abs(dsp.MakeWindow(dsp.WindowBlackmanHarris, 9, 0.0).Get(4) - 1.0) < 0.000000001
println math.Abs(dsp.MakeWindow(dsp.WindowSine, 9, 0.0).Get(4) - 1.0) < 0.000000001
println dsp.MakeWindow(dsp.WindowVorbis, 9, 0.0).Get(0) < 0.000000001
println math.Abs(dsp.MakeWindow(dsp.WindowKaiser, 9, 8.6).Get(4) - 1.0) < 0.000000001
println dsp.WindowName(dsp.WindowHann), dsp.WindowName(dsp.WindowKaiser), dsp.WindowName(99)
println dsp.WindowSum(dsp.WindowRectangular, 8, 0.0)
mut buf := Vector {} as Vector<f64>
mut i := 0
until i < 8:
buf.Push(1.0)
i++
end
dsp.ApplyWindow(buf.Ref(0), 8, dsp.WindowHann, 0.0)
println buf.Get(0) < 0.000000001, buf.Get(4) > 0.9
endFunctions
ResampleCubic
func ResampleCubic(c: audio.Clip, rate: int): audio.ClipThe example for this item is shown with the ones above: dsp-Resample.salam
ResampleSinc
func ResampleSinc(c: audio.Clip, rate: int, taps: int): audio.ClipThe example for this item is shown with the ones above: dsp-Resample.salam
ResampleTo
func ResampleTo(c: audio.Clip, rate: int, quality: int): audio.ClipThe example for this item is shown with the ones above: dsp-Resample.salam
Correlation
func Correlation(a: audio.Clip, b: audio.Clip): f64The example for this item is shown with the ones above: dsp-Resample.salam
Delay
func Delay(c &: audio.Clip, seconds: f64, mix: f64, feedback: f64)The example for this item is shown with the ones above: dsp-Effect.salam
Echo
func Echo(c: audio.Clip, seconds: f64, repeats: int, decay: f64): audio.ClipThe example for this item is shown with the ones above: dsp-Effect.salam
Reverb
func Reverb(c: audio.Clip, room: f64, mix: f64): audio.ClipThe example for this item is shown with the ones above: dsp-Effect.salam
Chorus
func Chorus(c: audio.Clip, depth: f64, speed: f64, mix: f64): audio.ClipThe example for this item is shown with the ones above: dsp-Effect.salam
Flanger
func Flanger(c: audio.Clip, depth: f64, speed: f64, mix: f64): audio.ClipThe example for this item is shown with the ones above: dsp-Effect.salam
Tremolo
func Tremolo(c &: audio.Clip, speed: f64, depth: f64)The example for this item is shown with the ones above: dsp-Effect.salam
CompressorProcess
func CompressorProcess(k: Compressor, c &: audio.Clip)The example for this item is shown with the ones above: dsp-Effect.salam
LimiterProcess
func LimiterProcess(c &: audio.Clip, ceiling_db: f64)The example for this item is shown with the ones above: dsp-Effect.salam
GateProcess
func GateProcess(c &: audio.Clip, threshold_db: f64, release: f64)The example for this item is shown with the ones above: dsp-Effect.salam
TimeStretch
func TimeStretch(c: audio.Clip, factor: f64): audio.ClipThe example for this item is shown with the ones above: dsp-Effect.salam
PitchShift
func PitchShift(c: audio.Clip, semitones: f64): audio.ClipThe example for this item is shown with the ones above: dsp-Effect.salam
LowPass
func LowPass(freq: f64, q: f64, rate: int): BiquadThe example for this item is shown with the ones above: dsp-Filter.salam
HighPass
func HighPass(freq: f64, q: f64, rate: int): BiquadThe example for this item is shown with the ones above: dsp-Filter.salam
BandPass
func BandPass(freq: f64, q: f64, rate: int): BiquadThe example for this item is shown with the ones above: dsp-Filter.salam
Notch
func Notch(freq: f64, q: f64, rate: int): BiquadThe example for this item is shown with the ones above: dsp-Filter.salam
AllPass
func AllPass(freq: f64, q: f64, rate: int): BiquadThe example for this item is shown with the ones above: dsp-Filter.salam
Peaking
func Peaking(freq: f64, q: f64, gain_db: f64, rate: int): BiquadThe example for this item is shown with the ones above: dsp-Filter.salam
LowShelf
func LowShelf(freq: f64, q: f64, gain_db: f64, rate: int): BiquadThe example for this item is shown with the ones above: dsp-Filter.salam
HighShelf
func HighShelf(freq: f64, q: f64, gain_db: f64, rate: int): BiquadThe example for this item is shown with the ones above: dsp-Filter.salam
BiquadReset
func BiquadReset(f &: Biquad)The example for this item is shown with the ones above: dsp-Filter.salam
BiquadStep
func BiquadStep(f &: Biquad, x: f64): f64The example for this item is shown with the ones above: dsp-Filter.salam
BiquadResponse
func BiquadResponse(f: Biquad, freq: f64, rate: int): f64The example for this item is shown with the ones above: dsp-Filter.salam
BiquadProcess
func BiquadProcess(f &: Biquad, c &: audio.Clip, ch: int)The example for this item is shown with the ones above: dsp-Filter.salam
BiquadProcessAll
func BiquadProcessAll(f: Biquad, c &: audio.Clip)The example for this item is shown with the ones above: dsp-Filter.salam
DCRemove
func DCRemove(c &: audio.Clip)The example for this item is shown with the ones above: dsp-Filter.salam
BesselI0
func BesselI0(x: f64): f64The example for this item is shown with the ones above: dsp-Window.salam
WindowValue
func WindowValue(kind: int, i: int, n: int, param: f64): f64The example for this item is shown with the ones above: dsp-Window.salam
WindowName
func WindowName(kind: int): strThe example for this item is shown with the ones above: dsp-Window.salam
MakeWindow
func MakeWindow(kind: int, n: int, param: f64): Vector<f64>The example for this item is shown with the ones above: dsp-Window.salam
ApplyWindow
func ApplyWindow(p: f64*, n: int, kind: int, param: f64)The example for this item is shown with the ones above: dsp-Window.salam
WindowSum
func WindowSum(kind: int, n: int, param: f64): f64The example for this item is shown with the ones above: dsp-Window.salam
ZeroCrossingRate
func ZeroCrossingRate(c: audio.Clip): f64The example for this item is shown with the ones above: dsp-Analyze.salam
FreeSpectrum
func FreeSpectrum(s &: Spectrum)The example for this item is shown with the ones above: dsp-Analyze.salam
STFT
func STFT(c: audio.Clip, size: int, hop: int, window: int): SpectrumThe example for this item is shown with the ones above: dsp-Analyze.salam
SpectrumAt
func SpectrumAt(s: Spectrum, frame: int, bin: int): f64The example for this item is shown with the ones above: dsp-Analyze.salam
SpectralCentroid
func SpectralCentroid(s: Spectrum, frame: int): f64The example for this item is shown with the ones above: dsp-Analyze.salam
SpectralRolloff
func SpectralRolloff(s: Spectrum, frame: int, share: f64): f64The example for this item is shown with the ones above: dsp-Analyze.salam
SpectralFlatness
func SpectralFlatness(s: Spectrum, frame: int): f64The example for this item is shown with the ones above: dsp-Analyze.salam
SpectralFlux
func SpectralFlux(s: Spectrum, frame: int): f64The example for this item is shown with the ones above: dsp-Analyze.salam
DetectPitch
func DetectPitch(c: audio.Clip, from_frame: int, size: int): f64The example for this item is shown with the ones above: dsp-Analyze.salam
DetectOnsets
func DetectOnsets(c: audio.Clip, size: int, hop: int): Vector<int>The example for this item is shown with the ones above: dsp-Analyze.salam
DetectBPM
func DetectBPM(c: audio.Clip): f64The example for this item is shown with the ones above: dsp-Analyze.salam
LoudnessLUFS
func LoudnessLUFS(c: audio.Clip): f64The example for this item is shown with the ones above: dsp-Analyze.salam
NormalizeLUFS
func NormalizeLUFS(c &: audio.Clip, target: f64)The example for this item is shown with the ones above: dsp-Analyze.salam
TruePeakDb
func TruePeakDb(c: audio.Clip): f64The example for this item is shown with the ones above: dsp-Analyze.salam
IsPow2
func IsPow2(n: int): boolimport audio.dsp
import math
func main:
println dsp.IsPow2(64), dsp.IsPow2(63), dsp.NextPow2(1000), dsp.NextPow2(64)
n := 64
k := 5
mut re := Vector {} as Vector<f64>
mut im := Vector {} as Vector<f64>
mut i := 0
until i < n:
ang := 2.0 * math.PI * (k as f64) * (i as f64) / (n as f64)
re.Push(math.Cos(ang))
im.Push(math.Sin(ang))
i++
end
println dsp.FFTVec(re, im)
mags := dsp.Magnitude(re.Ref(0), im.Ref(0), n)
println dsp.PeakBin(mags), mags.Get(k) > 63.9, mags.Get(k) < 64.1
mut leak := 0.0
mut b := 0
until b < n:
if b != k and mags.Get(b) > leak:
leak = mags.Get(b)
end
b++
end
println leak < 0.000000001
pw := dsp.PowerSpectrum(re.Ref(0), im.Ref(0), n)
println pw.Get(k) > 4095.0
ph := dsp.Phase(re.Ref(0), im.Ref(0), n)
println math.Abs(ph.Get(k)) < 0.000000001
println dsp.BinFrequency(k, n, 48000), dsp.BinFrequency(0, n, 48000)
println dsp.IFFTVec(re, im)
println math.Abs(re.Get(0) - 1.0) < 0.000000001, math.Abs(im.Get(0)) < 0.000000001
println dsp.FFT(re.Ref(0), im.Ref(0), n), dsp.IFFT(re.Ref(0), im.Ref(0), n)
println math.Abs(re.Get(0) - 1.0) < 0.000000001, dsp.IFFT(re.Ref(0), im.Ref(0), 15)
println dsp.FFT(re.Ref(0), im.Ref(0), 15)
println dsp.PeakBin(Vector {} as Vector<f64>)
endNextPow2
func NextPow2(n: int): intThe example for this item is shown with the ones above: dsp-FFT.salam
CAdd
func CAdd(a: Complex, b: Complex): ComplexThe example for this item is shown with the ones above: dsp-Complex.salam
CSub
func CSub(a: Complex, b: Complex): ComplexThe example for this item is shown with the ones above: dsp-Complex.salam
CMul
func CMul(a: Complex, b: Complex): ComplexThe example for this item is shown with the ones above: dsp-Complex.salam
CScale
func CScale(a: Complex, k: f64): ComplexThe example for this item is shown with the ones above: dsp-Complex.salam
CConj
func CConj(a: Complex): ComplexThe example for this item is shown with the ones above: dsp-Complex.salam
CAbs
func CAbs(a: Complex): f64The example for this item is shown with the ones above: dsp-Complex.salam
CArg
func CArg(a: Complex): f64The example for this item is shown with the ones above: dsp-Complex.salam
CFromPolar
func CFromPolar(mag: f64, angle: f64): ComplexThe example for this item is shown with the ones above: dsp-Complex.salam
FFT
func FFT(re: f64*, im: f64*, n: int): boolThe example for this item is shown with the ones above: dsp-FFT.salam
IFFT
func IFFT(re: f64*, im: f64*, n: int): boolThe example for this item is shown with the ones above: dsp-FFT.salam
FFTVec
func FFTVec(re &: Vector<f64>, im &: Vector<f64>): boolThe example for this item is shown with the ones above: dsp-FFT.salam
IFFTVec
func IFFTVec(re &: Vector<f64>, im &: Vector<f64>): boolThe example for this item is shown with the ones above: dsp-FFT.salam
Magnitude
func Magnitude(re: f64*, im: f64*, n: int): Vector<f64>The example for this item is shown with the ones above: dsp-FFT.salam
Phase
func Phase(re: f64*, im: f64*, n: int): Vector<f64>The example for this item is shown with the ones above: dsp-FFT.salam
PowerSpectrum
func PowerSpectrum(re: f64*, im: f64*, n: int): Vector<f64>The example for this item is shown with the ones above: dsp-FFT.salam
BinFrequency
func BinFrequency(bin: int, n: int, rate: int): f64The example for this item is shown with the ones above: dsp-FFT.salam
PeakBin
func PeakBin(mags: Vector<f64>): intThe example for this item is shown with the ones above: dsp-FFT.salam