Skip to content

Commit

Permalink
band-limited wavetable Sqr and Tri implementation finished for now in…
Browse files Browse the repository at this point in the history
… W.purs
  • Loading branch information
dktr0 committed Sep 9, 2024
1 parent 5c7bac9 commit 9b9b875
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 32 deletions.
77 changes: 49 additions & 28 deletions punctual.js

Large diffs are not rendered by default.

25 changes: 23 additions & 2 deletions src/AudioWorklet.purs
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,33 @@ function clamp(min,max,x) { return Math.max(Math.min(max,x),min); }
function ain(input,n) { return (n >= input.length ? 0.0 : input[n]); }
function genSaw() {
var r = new Float32Array(4096).fill(0.5);
for(var k=1;k<=84;k++) { -- with 84 harmonics, highest harmonic of middle C is just below 22050 Hz
for(var k=1;k<=84;k++) { // with 84 harmonics, highest harmonic of middle C is just below 22050 Hz
var x = Math.pow(-1,k);
for(var t=0;t<4096;t++) {
r[t] -= Math.sin(Math.PI * 2.0 * k * t / 4096) * x / (k * Math.PI);
}
}
return r;
}
function genSqr() {
var r = new Float32Array(4096).fill(0.5);
for(var k=1;k<=83;k+=2) { // with 84 harmonics, highest harmonic of middle C is just below 22050 Hz
for(var t=0;t<4096;t++) {
r[t] += Math.sin(Math.PI * 2.0 * k * t / 4096) * 4 / (k * Math.PI);
}
}
return r;
}
function genTri() {
var r = new Float32Array(4096).fill(0.5);
for(var k=1;k<=83;k+=2) { // with 84 harmonics, highest harmonic of middle C is just below 22050 Hz
var x = Math.pow(-1,(k-1)*0.5);
for(var t=0;t<4096;t++) {
r[t] += Math.sin(Math.PI * 2.0 * k * t / 4096) * 8 * x / (k * k * Math.PI * Math.PI);
}
}
return r;
}
"""
classHeader = "class " <> name <> " extends AudioWorkletProcessor {\n\n"
Expand All @@ -81,7 +100,7 @@ return [
];}
"""
constructor = "constructor() { super(); this.saw = genSaw(); this.framesOut = 0; this.runTime = currentTime; this.f = new Float32Array(" <> show wState.allocatedFloats <> ").fill(0); this.i = new Int32Array(" <> show wState.allocatedInts <> ").fill(0);}\n\n"
constructor = "constructor() { super(); this.saw=genSaw(); this.sqr=genSqr(); this.tri=genTri(); this.framesOut=0; this.runTime=currentTime; this.f=new Float32Array(" <> show wState.allocatedFloats <> ").fill(0); this.i=new Int32Array(" <> show wState.allocatedInts <> ").fill(0);}\n\n"
innerLoopPrefix = """process(inputs,outputs,parameters) {
const input = inputs[0];
const output = outputs[0];
Expand All @@ -101,6 +120,8 @@ const eTime = t - evalTimeAudio;
const eBeat = eTime * cps;
const fOut = fOutEnd == -1.0 ? 1.0 : clamp(0,1,(fOutEnd-t)/fOutDur);
const saw = this.saw;
const sqr = this.sqr;
const tri = this.tri;
"""
fadeCalculations = "const fIn = clamp(0,1,(t-" <> show fInStart <> ")/" <> show fInDur <> ");\nconst fade = Math.min(fIn,fOut);\n"
outputIndices = range 0 (length frame - 1)
Expand Down
10 changes: 8 additions & 2 deletions src/W.purs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,12 @@ wavetable tableName tableLength phase = do
saw :: Sample -> W Sample
saw f = phasor f >>= wavetable "saw" 4096

sqr :: Sample -> W Sample
sqr f = phasor f >>= wavetable "sqr" 4096

tri :: Sample -> W Sample
tri f = phasor f >>= wavetable "tri" 4096

type Frame = Matrix Sample

signalToFrame :: Signal -> W Frame
Expand Down Expand Up @@ -251,9 +257,9 @@ signalToFrame (Bipolar x) = signalToFrame x >>= traverse bipolar
signalToFrame (Unipolar x) = signalToFrame x >>= traverse unipolar

signalToFrame (Osc f) = signalToFrame f >>= traverse osc
-- Tri Signal
signalToFrame (Tri f) = signalToFrame f >>= traverse tri
signalToFrame (Saw f) = signalToFrame f >>= traverse saw
-- Sqr Signal
signalToFrame (Sqr f) = signalToFrame f >>= traverse sqr

signalToFrame (LFTri f) = do
xs <- signalToFrame f >>= traverse phasor
Expand Down

0 comments on commit 9b9b875

Please sign in to comment.