|
| 1 | +import { CircularSampleBuffer } from '@src/synth/ds/CircularSampleBuffer'; |
| 2 | +import { Environment } from '@src/Environment'; |
| 3 | +import { Logger } from '@src/Logger'; |
| 4 | +import { AlphaSynthWorkerSynthOutput } from './AlphaSynthWorkerSynthOutput'; |
| 5 | +import { AlphaSynthWebAudioOutputBase } from './AlphaSynthWebAudioOutputBase'; |
| 6 | + |
| 7 | +/** |
| 8 | + * @target web |
| 9 | + */ |
| 10 | +interface AudioWorkletProcessor { |
| 11 | + readonly port: MessagePort; |
| 12 | + process(inputs: Float32Array[][], outputs: Float32Array[][], parameters: Record<string, Float32Array>): boolean; |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * @target web |
| 17 | + */ |
| 18 | +declare var AudioWorkletProcessor: { |
| 19 | + prototype: AudioWorkletProcessor; |
| 20 | + new (options?: AudioWorkletNodeOptions): AudioWorkletProcessor; |
| 21 | +}; |
| 22 | + |
| 23 | +/** |
| 24 | + * This class implements a HTML5 Web Audio API based audio output device |
| 25 | + * for alphaSynth using the modern Audio Worklets. |
| 26 | + * @target web |
| 27 | + */ |
| 28 | +export class AlphaSynthWebWorklet { |
| 29 | + public static init() { |
| 30 | + (Environment.globalThis as any).registerProcessor( |
| 31 | + 'alphatab', |
| 32 | + class AlphaSynthWebWorkletProcessor extends AudioWorkletProcessor { |
| 33 | + public static readonly BufferSize: number = 4096; |
| 34 | + private static readonly TotalBufferTimeInMilliseconds: number = 5000; |
| 35 | + |
| 36 | + private _outputBuffer: Float32Array = new Float32Array(0); |
| 37 | + private _circularBuffer!: CircularSampleBuffer; |
| 38 | + private _bufferCount: number = 0; |
| 39 | + private _requestedBufferCount: number = 0; |
| 40 | + |
| 41 | + constructor(...args: any[]) { |
| 42 | + super(...args); |
| 43 | + |
| 44 | + this._bufferCount = Math.floor( |
| 45 | + (AlphaSynthWebWorkletProcessor.TotalBufferTimeInMilliseconds * |
| 46 | + Environment.globalThis.sampleRate) / |
| 47 | + 1000 / |
| 48 | + AlphaSynthWebWorkletProcessor.BufferSize |
| 49 | + ); |
| 50 | + this._circularBuffer = new CircularSampleBuffer( |
| 51 | + AlphaSynthWebWorkletProcessor.BufferSize * this._bufferCount |
| 52 | + ); |
| 53 | + |
| 54 | + this.port.onmessage = this.handleMessage.bind(this); |
| 55 | + } |
| 56 | + |
| 57 | + private handleMessage(e: MessageEvent) { |
| 58 | + let data: any = e.data; |
| 59 | + let cmd: any = data.cmd; |
| 60 | + switch (cmd) { |
| 61 | + case AlphaSynthWorkerSynthOutput.CmdOutputAddSamples: |
| 62 | + const f: Float32Array = data.samples; |
| 63 | + this._circularBuffer.write(f, 0, f.length); |
| 64 | + this._requestedBufferCount--; |
| 65 | + break; |
| 66 | + case AlphaSynthWorkerSynthOutput.CmdOutputResetSamples: |
| 67 | + this._circularBuffer.clear(); |
| 68 | + break; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + public process( |
| 73 | + _inputs: Float32Array[][], |
| 74 | + outputs: Float32Array[][], |
| 75 | + _parameters: Record<string, Float32Array> |
| 76 | + ): boolean { |
| 77 | + if (outputs.length !== 1 && outputs[0].length !== 2) { |
| 78 | + return false; |
| 79 | + } |
| 80 | + let left: Float32Array = outputs[0][0]; |
| 81 | + let right: Float32Array = outputs[0][1]; |
| 82 | + let samples: number = left.length + right.length; |
| 83 | + let buffer = this._outputBuffer; |
| 84 | + if (buffer.length !== samples) { |
| 85 | + buffer = new Float32Array(samples); |
| 86 | + this._outputBuffer = buffer; |
| 87 | + } |
| 88 | + this._circularBuffer.read(buffer, 0, Math.min(buffer.length, this._circularBuffer.count)); |
| 89 | + let s: number = 0; |
| 90 | + for (let i: number = 0; i < left.length; i++) { |
| 91 | + left[i] = buffer[s++]; |
| 92 | + right[i] = buffer[s++]; |
| 93 | + } |
| 94 | + this.port.postMessage({ |
| 95 | + cmd: AlphaSynthWorkerSynthOutput.CmdOutputSamplesPlayed, |
| 96 | + samples: left.length |
| 97 | + }); |
| 98 | + this.requestBuffers(); |
| 99 | + |
| 100 | + return true; |
| 101 | + } |
| 102 | + |
| 103 | + private requestBuffers(): void { |
| 104 | + // if we fall under the half of buffers |
| 105 | + // we request one half |
| 106 | + const halfBufferCount = (this._bufferCount / 2) | 0; |
| 107 | + let halfSamples: number = halfBufferCount * AlphaSynthWebWorkletProcessor.BufferSize; |
| 108 | + // Issue #631: it can happen that requestBuffers is called multiple times |
| 109 | + // before we already get samples via addSamples, therefore we need to |
| 110 | + // remember how many buffers have been requested, and consider them as available. |
| 111 | + let bufferedSamples = |
| 112 | + this._circularBuffer.count + |
| 113 | + this._requestedBufferCount * AlphaSynthWebWorkletProcessor.BufferSize; |
| 114 | + if (bufferedSamples < halfSamples) { |
| 115 | + for (let i: number = 0; i < halfBufferCount; i++) { |
| 116 | + this.port.postMessage({ |
| 117 | + cmd: AlphaSynthWorkerSynthOutput.CmdOutputSampleRequest |
| 118 | + }); |
| 119 | + } |
| 120 | + this._requestedBufferCount += halfBufferCount; |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + ); |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +/** |
| 129 | + * This class implements a HTML5 Web Audio API based audio output device |
| 130 | + * for alphaSynth. It can be controlled via a JS API. |
| 131 | + * @target web |
| 132 | + */ |
| 133 | +export class AlphaSynthAudioWorkletOutput extends AlphaSynthWebAudioOutputBase { |
| 134 | + private _worklet: AudioWorkletNode | null = null; |
| 135 | + |
| 136 | + public open() { |
| 137 | + super.open(); |
| 138 | + this.onReady(); |
| 139 | + } |
| 140 | + |
| 141 | + public play(): void { |
| 142 | + super.play(); |
| 143 | + let ctx = this._context!; |
| 144 | + // create a script processor node which will replace the silence with the generated audio |
| 145 | + ctx.audioWorklet.addModule(Environment.scriptFile!).then( |
| 146 | + () => { |
| 147 | + this._worklet = new AudioWorkletNode(ctx!, 'alphatab'); |
| 148 | + this._worklet.port.onmessage = this.handleMessage.bind(this); |
| 149 | + |
| 150 | + this._source!.connect(this._worklet, 0, 0); |
| 151 | + this._source!.start(0); |
| 152 | + this._worklet.connect(ctx!.destination); |
| 153 | + }, |
| 154 | + reason => { |
| 155 | + Logger.debug('WebAudio', `Audio Worklet creation failed: reason=${reason}`); |
| 156 | + } |
| 157 | + ); |
| 158 | + } |
| 159 | + |
| 160 | + private handleMessage(e: MessageEvent) { |
| 161 | + let data: any = e.data; |
| 162 | + let cmd: any = data.cmd; |
| 163 | + switch (cmd) { |
| 164 | + case AlphaSynthWorkerSynthOutput.CmdOutputSamplesPlayed: |
| 165 | + this.onSamplesPlayed(data.samples); |
| 166 | + break; |
| 167 | + case AlphaSynthWorkerSynthOutput.CmdOutputSampleRequest: |
| 168 | + this.onSampleRequest(); |
| 169 | + break; |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + public pause(): void { |
| 174 | + super.pause(); |
| 175 | + if (this._worklet) { |
| 176 | + this._worklet.disconnect(0); |
| 177 | + } |
| 178 | + this._worklet = null; |
| 179 | + } |
| 180 | + |
| 181 | + public addSamples(f: Float32Array): void { |
| 182 | + this._worklet?.port.postMessage({ |
| 183 | + cmd: AlphaSynthWorkerSynthOutput.CmdOutputAddSamples, |
| 184 | + samples: f |
| 185 | + }); |
| 186 | + } |
| 187 | + |
| 188 | + public resetSamples(): void { |
| 189 | + this._worklet?.port.postMessage({ |
| 190 | + cmd: AlphaSynthWorkerSynthOutput.CmdOutputResetSamples |
| 191 | + }); |
| 192 | + } |
| 193 | +} |
0 commit comments