-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
238 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`AdWorkletNode has parameter descriptors 1`] = ` | ||
[ | ||
{ | ||
"automationRate": "k-rate", | ||
"defaultValue": 0, | ||
"maxValue": 1, | ||
"minValue": 0, | ||
"name": "trigger", | ||
}, | ||
{ | ||
"automationRate": "k-rate", | ||
"defaultValue": 0.01, | ||
"maxValue": 10, | ||
"minValue": 0, | ||
"name": "attack", | ||
}, | ||
{ | ||
"automationRate": "k-rate", | ||
"defaultValue": 0.1, | ||
"maxValue": 10, | ||
"minValue": 0, | ||
"name": "decay", | ||
}, | ||
{ | ||
"automationRate": "k-rate", | ||
"defaultValue": 0, | ||
"maxValue": 20000, | ||
"minValue": 0, | ||
"name": "offset", | ||
}, | ||
{ | ||
"automationRate": "k-rate", | ||
"defaultValue": 1, | ||
"maxValue": 10000, | ||
"minValue": 0, | ||
"name": "gain", | ||
}, | ||
] | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
describe("AdWorkletNode", () => { | ||
let AdWorklet: any; | ||
|
||
beforeAll(async () => { | ||
createWorkletTestContext(); | ||
AdWorklet = (await import("./worklet")).AdProcessor; | ||
}); | ||
|
||
it("registers processor", () => { | ||
expect(global.registerProcessor).toHaveBeenCalledWith( | ||
"AdProcessor", | ||
AdWorklet | ||
); | ||
}); | ||
|
||
it("is generates offset when not triggered", () => { | ||
const node = new AdWorklet(); | ||
const params = { | ||
trigger: [0], | ||
attack: [0.01], | ||
decay: [0.1], | ||
offset: [100], | ||
gain: [0.5], | ||
}; | ||
let output = runProcessMono(node, 10, params); | ||
expect(output).toEqual(new Float32Array(10).fill(100)); | ||
}); | ||
|
||
it("generates an envelope with gain", () => { | ||
const node = new AdWorklet(); | ||
const params = { | ||
trigger: [1], | ||
attack: [0.2], | ||
decay: [0.2], | ||
offset: [100], | ||
gain: [50], | ||
}; | ||
let output = runProcessMono(node, 10, params); | ||
expect(Array.from(output)).toEqual([ | ||
119.67346954345703, 131.6060333251953, 138.84349060058594, | ||
143.2332305908203, 145.895751953125, 147.51065063476562, | ||
148.49012756347656, 149.08421325683594, 149.44454956054688, | ||
149.66310119628906, | ||
]); | ||
}); | ||
|
||
it("has parameter descriptors", () => { | ||
expect(AdWorklet.parameterDescriptors).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
function createWorkletTestContext(sampleRate = 10) { | ||
// @ts-ignore | ||
global.sampleRate = sampleRate; | ||
// @ts-ignore | ||
global.AudioWorkletProcessor = class AudioWorkletNodeStub { | ||
port: { | ||
postMessage: jest.Mock<any, any, any>; | ||
onmessage: jest.Mock<any, any, any>; | ||
}; | ||
|
||
constructor() { | ||
this.port = { | ||
postMessage: jest.fn(), | ||
onmessage: jest.fn(), | ||
}; | ||
} | ||
}; | ||
// @ts-ignore | ||
global.registerProcessor = jest.fn(); // Mock registerProcessor | ||
} | ||
|
||
function createInputsOutputs( | ||
options: { ins?: number; outs?: number; length?: number } = {} | ||
) { | ||
const inCount = options.ins ?? 1; | ||
const outCount = options.outs ?? 1; | ||
const length = options.length ?? 10; | ||
const inputs: Float32Array[][] = []; | ||
const outputs: Float32Array[][] = []; | ||
|
||
for (let i = 0; i < inCount; i++) { | ||
inputs.push([new Float32Array(length)]); | ||
} | ||
for (let i = 0; i < outCount; i++) { | ||
outputs.push([new Float32Array(length)]); | ||
} | ||
return { inputs, outputs }; | ||
} | ||
|
||
type Worklet = { | ||
process: ( | ||
inputs: Float32Array[][], | ||
outputs: Float32Array[][], | ||
parameters: any | ||
) => boolean; | ||
}; | ||
|
||
export function runProcessMono( | ||
worklet: Worklet, | ||
size: number, | ||
params: any = {} | ||
) { | ||
const { inputs, outputs } = createInputsOutputs({ length: size }); | ||
worklet.process(inputs, outputs, params); | ||
return outputs[0][0]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export const PROCESSOR = `"use strict";(()=>{var D=class extends AudioWorkletProcessor{r;d;constructor(){super(),this.r=!0,this.d=b(sampleRate,.01,.1),this.port.onmessage=r=>{switch(r.data.type){case"DISPOSE":this.r=!1;break}}}process(r,s,e){return this.d.update(e.trigger[0],e.attack[0],e.decay[0]),this.d.gen(s[0][0],e.offset[0],e.gain[0]),this.r}static get parameterDescriptors(){return[["trigger",0,0,1],["attack",.01,0,10],["decay",.1,0,10],["offset",0,0,2e4],["gain",1,0,1e4]].map(([r,s,e,O])=>({name:r,defaultValue:s,minValue:e,maxValue:O,automationRate:"k-rate"}))}};registerProcessor("AdProcessor",D);function b(a,r,s){let f=!1,o=r,c=s,h=Math.exp(-1/(o*a)),d=Math.exp(-1/(c*a)),i=0,n=0;return{update(E,u,l){E===1?f||(f=!0,i=1):f=!1,u!==o&&(o=u,h=Math.exp(-1/(Math.max(o,.001)*a))),l!==c&&(c=l,d=Math.exp(-1/(Math.max(c,.001)*a)))},gen(E,u,l){let t=0;for(let M=0;M<E.length;M++){switch(i){case 1:t=h*n+(1-h),t-n<=5e-8&&(i=2),n=t;break;case 2:t=d*n,n=t,t<=5e-8&&(i=0);break;case 0:default:t=0;break}E[M]=u+t*l}}}}})();`; | ||
export const PROCESSOR = `"use strict";(()=>{var M=class extends AudioWorkletProcessor{r;d;constructor(){super(),this.r=!0,this.d=O(sampleRate),this.port.onmessage=a=>{switch(a.data.type){case"DISPOSE":this.r=!1;break}}}process(a,n,t){return this.d.update(t.trigger[0],t.attack[0],t.decay[0]),this.d.gen(n[0][0],t.offset[0],t.gain[0]),this.r}static get parameterDescriptors(){return[["trigger",0,0,1],["attack",.01,0,10],["decay",.1,0,10],["offset",0,0,2e4],["gain",1,0,1e4]].map(([a,n,t,d])=>({name:a,defaultValue:n,minValue:t,maxValue:d,automationRate:"k-rate"}))}};registerProcessor("AdProcessor",M);function O(l){let D=l*.05,g=l*.1,u=!1,E=.1,f=.1,h=Math.exp(-1/(.1*D)),A=Math.exp(-1/(.1*g)),r=0,s=0;return{update(o,c,i){if(o===1?u||(u=!0,r=1):u=!1,c!==E){E=c;let e=Math.max(E*D,.001);h=Math.exp(-1/e)}if(i!==f){f=i;let e=Math.max(f*g,.001);A=Math.exp(-1/e)}},gen(o,c,i){let e=0;for(let p=0;p<o.length;p++)r===1?(e=h*s+(1-h),e-s<=5e-8&&(r=2),s=e):r===2?(e=A*s,s=e,e<=5e-8&&(r=0)):e=0,o[p]=c+e*i}}}})();`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.