Skip to content

Commit

Permalink
fix: docs
Browse files Browse the repository at this point in the history
  • Loading branch information
danigb committed Sep 30, 2024
1 parent 336f87c commit 55c984c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 21 deletions.
7 changes: 4 additions & 3 deletions packages/karplus-strong/src/dsp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ export function createKS(sampleRate: number, minFrequency: number) {
) => {
const outputLength = output.length;

const decayTimeInSamples = decay * sampleRate;
const decayTimeInSamples = 0.1 * decay * sampleRate;
const filterCoefficient = Math.pow(targetAmplitude, 1 / decayTimeInSamples);

if (trigger > 0 && prevTrigger <= 0) {
if (trigger >= 1 && prevTrigger < 0.9) {
// Some hysterisis to avoid double triggering
delayInSamples = sampleRate / frequency;
delayInSamples = Math.min(
Math.max(delayInSamples, 1),
Expand Down Expand Up @@ -62,7 +63,7 @@ export function createKS(sampleRate: number, minFrequency: number) {
writeIndex = (writeIndex + 1) % maxDelayLineLength;

// Stop playing if the signal has decayed below a threshold
if (Math.abs(currentSample) < 1e-6) {
if (Math.abs(currentSample) < 1e-8) {
isPlaying = false;
for (let j = i + 1; j < outputLength; j++) {
output[j] = 0;
Expand Down
2 changes: 1 addition & 1 deletion packages/karplus-strong/src/processor.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const PROCESSOR = `"use strict";(()=>{function b(o,n){let e=Math.ceil(o/n)+2,t=new Float32Array(e),i=e-2,a=0,f=!1,h=0;return(c,y,S,A)=>{let g=c.length,I=A*o,M=Math.pow(.001,1/I);if(y>0&&h<=0){i=o/S,i=Math.min(Math.max(i,1),e-2);for(let r=0;r<e;r++)t[r]=Math.random()*2-1;a=0,f=!0}if(h=y,f)for(let r=0;r<g;r++){let l=a-i;l<0&&(l+=e);let m=Math.floor(l),k=l-m,w=m%e,F=(m+1)%e,x=t[w],L=t[F],u=x+k*(L-x),P=M*u;if(t[a]=P,c[r]=u,a=(a+1)%e,Math.abs(u)<1e-6){f=!1;for(let p=r+1;p<g;p++)c[p]=0;break}}else c.fill(0)}}var d=class extends AudioWorkletProcessor{r;g;constructor(){super(),this.r=!0,this.g=b(sampleRate,100),this.port.onmessage=n=>{switch(n.data.type){case"DISPOSE":this.r=!1;break}}}process(n,s,e){let t=s[0][0];return this.g(t,e.trigger[0],e.frequency[0],e.decay[0]),this.r}static get parameterDescriptors(){return[["trigger",0,0,1],["frequency",440,20,2e4],["decay",.1,.01,5]].map(([n,s,e,t])=>({name:n,defaultValue:s,minValue:e,maxValue:t,automationRate:"k-rate"}))}};registerProcessor("KsProcessor",d);})();`;
export const PROCESSOR = `"use strict";(()=>{function b(o,n){let e=Math.ceil(o/n)+2,t=new Float32Array(e),i=e-2,a=0,f=!1,h=0;return(c,y,S,A)=>{let g=c.length,I=.1*A*o,M=Math.pow(.001,1/I);if(y>=1&&h<.9){i=o/S,i=Math.min(Math.max(i,1),e-2);for(let r=0;r<e;r++)t[r]=Math.random()*2-1;a=0,f=!0}if(h=y,f)for(let r=0;r<g;r++){let l=a-i;l<0&&(l+=e);let m=Math.floor(l),k=l-m,w=m%e,F=(m+1)%e,x=t[w],L=t[F],u=x+k*(L-x),P=M*u;if(t[a]=P,c[r]=u,a=(a+1)%e,Math.abs(u)<1e-8){f=!1;for(let p=r+1;p<g;p++)c[p]=0;break}}else c.fill(0)}}var d=class extends AudioWorkletProcessor{r;g;constructor(){super(),this.r=!0,this.g=b(sampleRate,100),this.port.onmessage=n=>{switch(n.data.type){case"DISPOSE":this.r=!1;break}}}process(n,s,e){let t=s[0][0];return this.g(t,e.trigger[0],e.frequency[0],e.decay[0]),this.r}static get parameterDescriptors(){return[["trigger",0,0,1],["frequency",440,20,2e4],["decay",.1,.01,5]].map(([n,s,e,t])=>({name:n,defaultValue:s,minValue:e,maxValue:t,automationRate:"k-rate"}))}};registerProcessor("KsProcessor",d);})();`;
21 changes: 4 additions & 17 deletions site/content/docs/(sources)/ks.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ description: A Karplus-Strong source node

import KsExample from "../../../examples/KsExample";

A source generator based on the Karplus-Strong algorithm.
A (currently) very simple Karplus-Strong source node:

```ts
import { registerKarplusStrongWorklet, KarplusStrong } from "synthlet";
Expand All @@ -26,19 +26,6 @@ osc.trigger.value = 1;

## Parameters

- `type`: The type of noise to generate. Can be one of:
- 0 `KsType.White`: White noise that uses `Math.rand` for random numbers.
- 10: `KsType.Pink`: Pink noise based on this [Larry Trammel algorithm](https://www.ridgerat-tech.us/pink/newpink.htm)

Since type is an audio param, any arbitrary value can be used, but only the above values are valid. If you suply an invalid value, a warning will be printed and the noise will default to white random.

## References

- https://en.wikipedia.org/wiki/Colors_of_noise
- Voss-McCartney algorithm:
- https://www.firstpr.com.au/dsp/pink-noise/
- https://dsp.stackexchange.com/questions/62342/understanding-voss-mccartney-pink-noise-generation-algorithm
- Larry-Trammel algorithm:
- https://www.ridgerat-tech.us/pink/newpink.htm
- Mathlab pink noise implementation: https://ccrma.stanford.edu/~jos/sasp/Example_Synthesis_1_F_Ks.html
- https://github.com/Stenzel/newshadeofpink
- `trigger`: A trigger to start the sound (1 means start)
- `frequency`: The frequency of the generated sound.
- `decay`: The decay time of the generated sound.

0 comments on commit 55c984c

Please sign in to comment.