From 386c9d489c5a94e748f6ea6c11000d908a3f5a1c Mon Sep 17 00:00:00 2001 From: danigb Date: Tue, 3 Sep 2024 20:33:15 +0200 Subject: [PATCH] fix: remove white fast implementation --- packages/noise/src/dsp.ts | 25 ++++------------------- packages/noise/src/index.ts | 6 ++---- packages/noise/src/processor.ts | 2 +- site/content/docs/(sources)/noise.mdx | 4 +--- site/examples/NoiseExample.tsx | 29 +++++++++++++++++++-------- 5 files changed, 29 insertions(+), 37 deletions(-) diff --git a/packages/noise/src/dsp.ts b/packages/noise/src/dsp.ts index b01f075..8bd7a67 100644 --- a/packages/noise/src/dsp.ts +++ b/packages/noise/src/dsp.ts @@ -2,9 +2,8 @@ export type NoiseAlgorithm = (output: Float32Array) => void; export enum NoiseType { WHITE_RND = 0, - WHITE_FAST = 1, - PINK_COOPER = 10, - PINK_LARRY_TRAMMEL = 11, + PINK_TRAMMEL = 10, + PINK_COOPER = 11, } export function getNoiseAlgorithm( @@ -14,12 +13,10 @@ export function getNoiseAlgorithm( switch (type) { case NoiseType.WHITE_RND: return whiteRnd; - case NoiseType.WHITE_FAST: - return createWhiteFast(); + case NoiseType.PINK_TRAMMEL: + return createPinkLarryTrammel(); case NoiseType.PINK_COOPER: return createPinkCooper(sampleRate); - case NoiseType.PINK_LARRY_TRAMMEL: - return createPinkLarryTrammel(); default: console.warn("Unknown noise type: " + type); return whiteRnd; @@ -32,20 +29,6 @@ function whiteRnd(output: Float32Array) { } } -// Fast white noise from https://www.musicdsp.org/en/latest/Synthesis/216-fast-whitenoise-generator.html -function createWhiteFast(): NoiseAlgorithm { - const SCALE = 2.0 / 0xffffffff; - let g_x1 = 0x67452301; - let g_x2 = 0xefcdab89; - return (output) => { - for (let i = 0; i < output.length; i++) { - g_x1 ^= g_x2; - output[i] = g_x2 * SCALE; - g_x2 += g_x1; - } - }; -} - // Pink noise from http://www.cooperbaker.com/home/code/pink%20noise/ function createPinkCooper(sampleRate: number): NoiseAlgorithm { // Coefficients diff --git a/packages/noise/src/index.ts b/packages/noise/src/index.ts index 88dfad1..3f0536a 100644 --- a/packages/noise/src/index.ts +++ b/packages/noise/src/index.ts @@ -18,10 +18,8 @@ export type NoiseInputParams = { export function getNoiseTypes(): { name: string; value: number }[] { return [ - { name: "White Random", value: NoiseType.WHITE_RND }, - { name: "White Fast", value: NoiseType.WHITE_FAST }, - { name: "Pink Cooper", value: NoiseType.PINK_COOPER }, - { name: "Pink Larry Trammel", value: NoiseType.PINK_LARRY_TRAMMEL }, + { name: "White", value: NoiseType.WHITE_RND }, + { name: "Pink Trammel", value: NoiseType.PINK_TRAMMEL }, ]; } diff --git a/packages/noise/src/processor.ts b/packages/noise/src/processor.ts index 73c664b..31dbdf2 100644 --- a/packages/noise/src/processor.ts +++ b/packages/noise/src/processor.ts @@ -1 +1 @@ -export const PROCESSOR = `"use strict";(()=>{function A(r,t){switch(t){case 0:return N;case 1:return R();case 10:return M(r);case 11:return P();default:return console.warn("Unknown noise type: "+t),N}}function N(r){for(let t=0;t{for(let n=0;nMath.pow(10,c/20),u=c=>20*Math.log10(c),l=0,i=0,p=0,m=r/(2*Math.PI)-1;for(;m>1;)t[l]=2*Math.PI*m/r,m=m/4,l++;for(h=l;l-- >0;)o[l]=a(i),p+=a(i),i-=6;return n=a(-u(p)),c=>{for(let f=0;f{for(let a=0;a{switch(t.data.type){case"DISCONNECT":this.r=!1;break}}}process(t,o,e){return this.t!==e.type[0]&&(this.t=e.type[0],this.d=A(sampleRate,e.type[0])),this.d(o[0][0]),this.r}static get parameterDescriptors(){return[["type",0,0,100]].map(([t,o,e,n])=>({name:t,defaultValue:o,minValue:e,maxValue:n,automationRate:"k-rate"}))}};registerProcessor("NoiseWorkletProcessor",g);})();`; +export const PROCESSOR = `"use strict";(()=>{function g(o,t){switch(t){case 0:return N;case 10:return P();case 11:return M(o);default:return console.warn("Unknown noise type: "+t),N}}function N(o){for(let t=0;tMath.pow(10,h/20),u=h=>20*Math.log10(h),l=0,r=0,d=0,m=o/(2*Math.PI)-1;for(;m>1;)t[l]=2*Math.PI*m/o,m=m/4,l++;for(c=l;l-- >0;)n[l]=a(r),d+=a(r),r-=6;return i=a(-u(d)),h=>{for(let p=0;p{for(let a=0;a{switch(t.data.type){case"DISCONNECT":this.r=!1;break}}}process(t,n,e){return this.t!==e.type[0]&&(this.t=e.type[0],this.d=g(sampleRate,e.type[0])),this.d(n[0][0]),this.r}static get parameterDescriptors(){return[["type",0,0,100]].map(([t,n,e,i])=>({name:t,defaultValue:n,minValue:e,maxValue:i,automationRate:"k-rate"}))}};registerProcessor("NoiseWorkletProcessor",A);})();`; diff --git a/site/content/docs/(sources)/noise.mdx b/site/content/docs/(sources)/noise.mdx index 0509ed6..3c9e2e3 100644 --- a/site/content/docs/(sources)/noise.mdx +++ b/site/content/docs/(sources)/noise.mdx @@ -30,9 +30,7 @@ noise.type.value = NoiseType.PINK_COOPER; - `type`: The type of noise to generate. Can be one of: - 0 `NoiseType.WHITE_RND`: White noise that uses `Math.rand` for random numbers. - - 1 `NoiseType.WHITE_FAST`: White noise that uses this algorithm https://www.musicdsp.org/en/latest/Synthesis/216-fast-whitenoise-generator.html - - 10 `NoiseType.PINK_RND`: Pink noise based on this [Cooper Baker algorithm](http://www.cooperbaker.com/home/code/pink%20noise/) - - 11: `NoiseType.PINK_LARRY_TRAMMEL`: Pink noise based on this [Larry Trammel algorithm](https://www.ridgerat-tech.us/pink/newpink.htm) + - 10: `NoiseType.PINK_TRAMMEL`: 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. diff --git a/site/examples/NoiseExample.tsx b/site/examples/NoiseExample.tsx index 28e1147..cdf85c4 100644 --- a/site/examples/NoiseExample.tsx +++ b/site/examples/NoiseExample.tsx @@ -5,21 +5,28 @@ import { createNoiseNode, getNoiseTypes, NoiseWorkletNode } from "synthlet"; import { useSynth } from "./useSynth"; export function NoiseExample() { - return ( - - - + const [open, setOpen] = useState(false); + + return open ? ( + setOpen(false)} /> + ) : ( + ); } -function NoiseSynthUI() { +function NoiseSynthUI({ onClose }: { onClose: () => void }) { const [selectedNoiseType, setSelectedNoiseType] = useState(0); const [volume, setVolume] = useState(-60); const synth = useSynth((context) => new NoiseSynth(context)); if (!synth) return null; return ( -
+
Noise generator example
Type:
@@ -27,7 +34,6 @@ function NoiseSynthUI() { className="p-1 rounded bg-slate-700 text-slate-200" value={selectedNoiseType} onChange={(e) => { - console.log(e.target.value); const noiseType = parseInt(e.target.value); setSelectedNoiseType(noiseType); synth.noise.type.setValueAtTime(noiseType, 0); @@ -40,7 +46,7 @@ function NoiseSynthUI() { ))}
-
+
Volume:
{volume <= -60 ? "Muted" : volume + "dB"}
+ +
); }