-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.ts
42 lines (33 loc) · 1.61 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import {realtimeBpmProcessorName} from './consts';
import realtimeBpmProcessorContent from './generated-processor';
import {type RealTimeBpmAnalyzerParameters} from './types';
export * from './realtime-bpm-analyzer';
export {analyzeFullBuffer, getBiquadFilter} from './analyzer';
export * from './types';
/**
* Create the RealTimeBpmProcessor needed to run the realtime strategy
* @param audioContext AudioContext instance
* @param processorOptions RealTimeBpmAnalyzerParameters
* @returns An AudioWorkletNode instance
*/
export async function createRealTimeBpmProcessor(audioContext: AudioContext, processorOptions?: RealTimeBpmAnalyzerParameters): Promise<AudioWorkletNode> {
const processorNode = await setupAudioWorkletNode(audioContext, realtimeBpmProcessorName, processorOptions);
await audioContext.resume();
return processorNode;
}
/**
* Creates AudioWorkletNode for the Processor
* @param audioContext AudioContext instance
* @param processorName Name of the audio processor, without the extension
* @param processorOptions RealTimeBpmAnalyzerParameters
* @returns Recording node related components for the app.
*/
async function setupAudioWorkletNode(audioContext: AudioContext, processorName: string, processorOptions?: RealTimeBpmAnalyzerParameters): Promise<AudioWorkletNode> {
const blob = new Blob([realtimeBpmProcessorContent], {type: 'application/javascript'});
const objectUrl = URL.createObjectURL(blob);
await audioContext.audioWorklet.addModule(objectUrl);
const audioWorkletNode = new AudioWorkletNode(audioContext, processorName, {
processorOptions,
});
return audioWorkletNode;
}