-
Notifications
You must be signed in to change notification settings - Fork 204
/
Copy pathAlphaSynthAudioWorkletOutput.ts
252 lines (226 loc) · 9.51 KB
/
AlphaSynthAudioWorkletOutput.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import { CircularSampleBuffer } from '@src/synth/ds/CircularSampleBuffer';
import { Environment } from '@src/Environment';
import { Logger } from '@src/Logger';
import { AlphaSynthWorkerSynthOutput } from '@src/platform/javascript/AlphaSynthWorkerSynthOutput';
import { AlphaSynthWebAudioOutputBase } from '@src/platform/javascript/AlphaSynthWebAudioOutputBase';
import { SynthConstants } from '@src/synth/SynthConstants';
import { Settings } from '@src/Settings';
/**
* @target web
*/
interface AudioWorkletProcessor {
readonly port: MessagePort;
process(inputs: Float32Array[][], outputs: Float32Array[][], parameters: Record<string, Float32Array>): boolean;
}
/**
* @target web
*/
declare var AudioWorkletProcessor: {
prototype: AudioWorkletProcessor;
new(options?: AudioWorkletNodeOptions): AudioWorkletProcessor;
};
// Bug 646: Safari 14.1 is buggy regarding audio worklets
// globalThis cannot be used to access registerProcessor or samplerate
// we need to really use them as globals
/**
* @target web
*/
declare var registerProcessor: any;
/**
* @target web
*/
declare var sampleRate: number;
/**
* This class implements a HTML5 Web Audio API based audio output device
* for alphaSynth using the modern Audio Worklets.
* @target web
*/
export class AlphaSynthWebWorklet {
private static _isRegistered = false;
public static init() {
if (AlphaSynthWebWorklet._isRegistered) {
return;
}
AlphaSynthWebWorklet._isRegistered = true;
registerProcessor(
'alphatab',
class AlphaSynthWebWorkletProcessor extends AudioWorkletProcessor {
public static readonly BufferSize: number = 4096;
private _outputBuffer: Float32Array = new Float32Array(0);
private _circularBuffer!: CircularSampleBuffer;
private _bufferCount: number = 0;
private _requestedBufferCount: number = 0;
private _isStopped = false;
constructor(options: AudioWorkletNodeOptions) {
super(options);
Logger.debug('WebAudio', 'creating processor');
this._bufferCount = Math.floor(
(options.processorOptions.bufferTimeInMilliseconds * sampleRate) /
1000 /
AlphaSynthWebWorkletProcessor.BufferSize
);
this._circularBuffer = new CircularSampleBuffer(
AlphaSynthWebWorkletProcessor.BufferSize * this._bufferCount
);
this.port.onmessage = this.handleMessage.bind(this);
}
private handleMessage(e: MessageEvent) {
let data: any = e.data;
let cmd: any = data.cmd;
switch (cmd) {
case AlphaSynthWorkerSynthOutput.CmdOutputAddSamples:
const f: Float32Array = data.samples;
this._circularBuffer.write(f, 0, f.length);
this._requestedBufferCount--;
break;
case AlphaSynthWorkerSynthOutput.CmdOutputResetSamples:
this._circularBuffer.clear();
break;
case AlphaSynthWorkerSynthOutput.CmdOutputStop:
this._isStopped = true;
break;
}
}
public override process(
_inputs: Float32Array[][],
outputs: Float32Array[][],
_parameters: Record<string, Float32Array>
): boolean {
if (outputs.length !== 1 && outputs[0].length !== 2) {
return false;
}
let left: Float32Array = outputs[0][0];
let right: Float32Array = outputs[0][1];
if (!left || !right) {
return true;
}
let samples: number = left.length + right.length;
let buffer = this._outputBuffer;
if (buffer.length !== samples) {
buffer = new Float32Array(samples);
this._outputBuffer = buffer;
}
const samplesFromBuffer = this._circularBuffer.read(
buffer,
0,
Math.min(buffer.length, this._circularBuffer.count)
);
let s: number = 0;
const min = Math.min(left.length, samplesFromBuffer);
for (let i: number = 0; i < min; i++) {
left[i] = buffer[s++];
right[i] = buffer[s++];
}
if(samplesFromBuffer < left.length) {
for(let i = samplesFromBuffer; i < left.length; i++) {
left[i] = 0;
right[i] = 0;
}
}
this.port.postMessage({
cmd: AlphaSynthWorkerSynthOutput.CmdOutputSamplesPlayed,
samples: samplesFromBuffer / SynthConstants.AudioChannels
});
this.requestBuffers();
return this._circularBuffer.count > 0 || !this._isStopped;
}
private requestBuffers(): void {
// if we fall under the half of buffers
// we request one half
const halfBufferCount = (this._bufferCount / 2) | 0;
let halfSamples: number = halfBufferCount * AlphaSynthWebWorkletProcessor.BufferSize;
// Issue #631: it can happen that requestBuffers is called multiple times
// before we already get samples via addSamples, therefore we need to
// remember how many buffers have been requested, and consider them as available.
let bufferedSamples =
this._circularBuffer.count +
this._requestedBufferCount * AlphaSynthWebWorkletProcessor.BufferSize;
if (bufferedSamples < halfSamples) {
for (let i: number = 0; i < halfBufferCount; i++) {
this.port.postMessage({
cmd: AlphaSynthWorkerSynthOutput.CmdOutputSampleRequest
});
}
this._requestedBufferCount += halfBufferCount;
}
}
}
);
}
}
/**
* This class implements a HTML5 Web Audio API based audio output device
* for alphaSynth. It can be controlled via a JS API.
* @target web
*/
export class AlphaSynthAudioWorkletOutput extends AlphaSynthWebAudioOutputBase {
private _worklet: AudioWorkletNode | null = null;
private _bufferTimeInMilliseconds: number = 0;
private readonly _settings: Settings;
public constructor(settings: Settings) {
super();
this._settings = settings;
}
public override open(bufferTimeInMilliseconds: number) {
super.open(bufferTimeInMilliseconds);
this._bufferTimeInMilliseconds = bufferTimeInMilliseconds;
this.onReady();
}
public override play(): void {
super.play();
let ctx = this._context!;
// create a script processor node which will replace the silence with the generated audio
Environment.createAudioWorklet(ctx, this._settings).then(
() => {
this._worklet = new AudioWorkletNode(ctx!, 'alphatab', {
numberOfOutputs: 1,
outputChannelCount: [2],
processorOptions: {
bufferTimeInMilliseconds: this._bufferTimeInMilliseconds
}
});
this._worklet.port.onmessage = this.handleMessage.bind(this);
this._source!.connect(this._worklet);
this._source!.start(0);
this._worklet.connect(ctx!.destination);
},
reason => {
Logger.error('WebAudio', `Audio Worklet creation failed: reason=${reason}`);
}
);
}
private handleMessage(e: MessageEvent) {
let data: any = e.data;
let cmd: any = data.cmd;
switch (cmd) {
case AlphaSynthWorkerSynthOutput.CmdOutputSamplesPlayed:
this.onSamplesPlayed(data.samples);
break;
case AlphaSynthWorkerSynthOutput.CmdOutputSampleRequest:
this.onSampleRequest();
break;
}
}
public override pause(): void {
super.pause();
if (this._worklet) {
this._worklet.port.postMessage({
cmd: AlphaSynthWorkerSynthOutput.CmdOutputStop
});
this._worklet.port.onmessage = null;
this._worklet.disconnect();
}
this._worklet = null;
}
public addSamples(f: Float32Array): void {
this._worklet?.port.postMessage({
cmd: AlphaSynthWorkerSynthOutput.CmdOutputAddSamples,
samples: f
});
}
public resetSamples(): void {
this._worklet?.port.postMessage({
cmd: AlphaSynthWorkerSynthOutput.CmdOutputResetSamples
});
}
}