-
Notifications
You must be signed in to change notification settings - Fork 254
Expand file tree
/
Copy pathVoice.ts
More file actions
346 lines (294 loc) · 13.9 KB
/
Voice.ts
File metadata and controls
346 lines (294 loc) · 13.9 KB
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// The SoundFont loading and Audio Synthesis is based on TinySoundFont, licensed under MIT,
// developed by Bernhard Schelling (https://github.com/schellingb/TinySoundFont)
// TypeScript port for alphaTab: (C) 2020 by Daniel Kuschny
// Licensed under: MPL-2.0
import { LoopMode } from '@coderline/alphatab/synth/synthesis/LoopMode';
import { OutputMode } from '@coderline/alphatab/synth/synthesis/OutputMode';
import type { Region } from '@coderline/alphatab/synth/synthesis/Region';
import type { TinySoundFont } from '@coderline/alphatab/synth/synthesis/TinySoundFont';
import { VoiceEnvelope, VoiceEnvelopeSegment } from '@coderline/alphatab/synth/synthesis/VoiceEnvelope';
import { VoiceLfo } from '@coderline/alphatab/synth/synthesis/VoiceLfo';
import { VoiceLowPass } from '@coderline/alphatab/synth/synthesis/VoiceLowPass';
import { SynthHelper } from '@coderline/alphatab/synth/SynthHelper';
import { SynthConstants } from '@coderline/alphatab/synth/SynthConstants';
import type { Channel } from '@coderline/alphatab/synth/synthesis/Channel';
/**
* @internal
*/
export class Voice {
/**
* The lower this block size is the more accurate the effects are.
* Increasing the value significantly lowers the CPU usage of the voice rendering.
* If LFO affects the low-pass filter it can be hearable even as low as 8.
*/
static readonly renderEffectSampleBlock: number = SynthConstants.MicroBufferSize;
public playingPreset: number = 0;
public playingKey: number = 0;
public playingChannel: number = 0;
public region: Region | null = null;
public pitchInputTimecents: number = 0;
public pitchOutputFactor: number = 0;
public sourceSamplePosition: number = 0;
public noteGainDb: number = 0;
public panFactorLeft: number = 0;
public panFactorRight: number = 0;
public playIndex: number = 0;
public loopStart: number = 0;
public loopEnd: number = 0;
public ampEnv: VoiceEnvelope = new VoiceEnvelope();
public modEnv: VoiceEnvelope = new VoiceEnvelope();
public lowPass: VoiceLowPass = new VoiceLowPass();
public modLfo: VoiceLfo = new VoiceLfo();
public vibLfo: VoiceLfo = new VoiceLfo();
public mixVolume: number = 0;
public mute: boolean = false;
public updatePitchRatio(c: Channel, outSampleRate: number) {
let pitchWheel = c.pitchWheel;
// add additional note pitch
if (c.perNotePitchWheel.has(this.playingKey)) {
pitchWheel += c.perNotePitchWheel.get(this.playingKey)! - 8192;
}
const pitchShift: number =
pitchWheel === 8192 ? c.tuning : (pitchWheel / 16383.0) * c.pitchRange * 2 - c.pitchRange + c.tuning;
this.calcPitchRatio(pitchShift, outSampleRate);
}
public calcPitchRatio(pitchShift: number, outSampleRate: number): void {
if (!this.region) {
return;
}
const note: number = this.playingKey + this.region.transpose + this.region.tune / 100.0;
let adjustedPitch: number =
this.region.pitchKeyCenter + (note - this.region.pitchKeyCenter) * (this.region.pitchKeyTrack / 100.0);
if (pitchShift !== 0) {
adjustedPitch += pitchShift;
}
this.pitchInputTimecents = adjustedPitch * 100.0;
this.pitchOutputFactor =
this.region.sampleRate / (SynthHelper.timecents2Secs(this.region.pitchKeyCenter * 100.0) * outSampleRate);
}
public end(outSampleRate: number): void {
if (!this.region) {
return;
}
this.ampEnv.nextSegment(VoiceEnvelopeSegment.Sustain, outSampleRate);
this.modEnv.nextSegment(VoiceEnvelopeSegment.Sustain, outSampleRate);
if (this.region.loopMode === LoopMode.Sustain) {
// Continue playing, but stop looping.
this.loopEnd = this.loopStart;
}
}
public endQuick(outSampleRate: number): void {
this.ampEnv.parameters!.release = 0.0;
this.ampEnv.nextSegment(VoiceEnvelopeSegment.Sustain, outSampleRate);
this.modEnv.parameters!.release = 0.0;
this.modEnv.nextSegment(VoiceEnvelopeSegment.Sustain, outSampleRate);
}
public render(
f: TinySoundFont,
outputBuffer: Float32Array,
offset: number,
numSamples: number,
isMuted: boolean
): void {
if (!this.region) {
return;
}
const region: Region = this.region;
const input: Float32Array = region.samples;
let outL: number = 0;
let outR: number = f.outputMode === OutputMode.StereoUnweaved ? numSamples : -1;
// Cache some values, to give them at least some chance of ending up in registers.
const updateModEnv: boolean = region.modEnvToPitch !== 0 || region.modEnvToFilterFc !== 0;
const updateModLFO: boolean =
this.modLfo.delta > 0 &&
(region.modLfoToPitch !== 0 || region.modLfoToFilterFc !== 0 || region.modLfoToVolume !== 0);
const updateVibLFO: boolean = this.vibLfo.delta > 0 && region.vibLfoToPitch !== 0;
const isLooping: boolean = this.loopStart < this.loopEnd;
const tmpLoopStart: number = this.loopStart;
const tmpLoopEnd: number = this.loopEnd;
const tmpSampleEndDbl: number = region.end;
const tmpLoopEndDbl: number = tmpLoopEnd + 1.0;
let tmpSourceSamplePosition: number = this.sourceSamplePosition;
const tmpLowpass: VoiceLowPass = new VoiceLowPass(this.lowPass);
const dynamicLowpass: boolean = region.modLfoToFilterFc !== 0 || region.modEnvToFilterFc !== 0;
let tmpSampleRate: number = 0;
let tmpInitialFilterFc: number = 0;
let tmpModLfoToFilterFc: number = 0;
let tmpModEnvToFilterFc: number = 0;
const dynamicPitchRatio: boolean =
region.modLfoToPitch !== 0 || region.modEnvToPitch !== 0 || region.vibLfoToPitch !== 0;
let pitchRatio: number = 0;
let tmpModLfoToPitch: number = 0;
let tmpVibLfoToPitch: number = 0;
let tmpModEnvToPitch: number = 0;
const dynamicGain: boolean = region.modLfoToVolume !== 0;
let noteGain: number = 0;
let tmpModLfoToVolume: number = 0;
if (dynamicLowpass) {
tmpSampleRate = f.outSampleRate;
tmpInitialFilterFc = region.initialFilterFc;
tmpModLfoToFilterFc = region.modLfoToFilterFc;
tmpModEnvToFilterFc = region.modEnvToFilterFc;
} else {
tmpSampleRate = 0;
tmpInitialFilterFc = 0;
tmpModLfoToFilterFc = 0;
tmpModEnvToFilterFc = 0;
}
if (dynamicPitchRatio) {
pitchRatio = 0;
tmpModLfoToPitch = region.modLfoToPitch;
tmpVibLfoToPitch = region.vibLfoToPitch;
tmpModEnvToPitch = region.modEnvToPitch;
} else {
pitchRatio = SynthHelper.timecents2Secs(this.pitchInputTimecents) * this.pitchOutputFactor;
tmpModLfoToPitch = 0;
tmpVibLfoToPitch = 0;
tmpModEnvToPitch = 0;
}
if (dynamicGain) {
tmpModLfoToVolume = region.modLfoToVolume * 0.1;
} else {
noteGain = SynthHelper.decibelsToGain(this.noteGainDb);
tmpModLfoToVolume = 0;
}
while (numSamples > 0) {
let gainMono: number;
let gainLeft: number;
let gainRight: number = 0;
let blockSamples: number =
numSamples > Voice.renderEffectSampleBlock ? Voice.renderEffectSampleBlock : numSamples;
numSamples -= blockSamples;
if (dynamicLowpass) {
const fres: number =
tmpInitialFilterFc +
this.modLfo.level * tmpModLfoToFilterFc +
this.modEnv.level * tmpModEnvToFilterFc;
tmpLowpass.active = fres <= 13500.0;
if (tmpLowpass.active) {
tmpLowpass.setup(SynthHelper.cents2Hertz(fres) / tmpSampleRate);
}
}
if (dynamicPitchRatio) {
pitchRatio =
SynthHelper.timecents2Secs(
this.pitchInputTimecents +
(this.modLfo.level * tmpModLfoToPitch +
this.vibLfo.level * tmpVibLfoToPitch +
this.modEnv.level * tmpModEnvToPitch)
) * this.pitchOutputFactor;
}
if (dynamicGain) {
noteGain = SynthHelper.decibelsToGain(this.noteGainDb + this.modLfo.level * tmpModLfoToVolume);
}
// Update EG.
this.ampEnv.process(blockSamples, f.outSampleRate);
if (updateModEnv) {
this.modEnv.process(blockSamples, f.outSampleRate);
}
gainMono = noteGain * this.ampEnv.level;
if (isMuted) {
gainMono = 0;
} else {
gainMono *= this.mixVolume;
}
// Update LFOs.
if (updateModLFO) {
this.modLfo.process(blockSamples);
}
if (updateVibLFO) {
this.vibLfo.process(blockSamples);
}
switch (f.outputMode) {
case OutputMode.StereoInterleaved:
gainLeft = gainMono * this.panFactorLeft;
gainRight = gainMono * this.panFactorRight;
while (blockSamples-- > 0 && tmpSourceSamplePosition < tmpSampleEndDbl) {
const pos: number = tmpSourceSamplePosition | 0;
const nextPos: number = pos >= tmpLoopEnd && isLooping ? tmpLoopStart : pos + 1;
// Simple linear interpolation.
// TODO: check for interpolation mode on voice
const alpha: number = tmpSourceSamplePosition - pos;
let value: number = input[pos] * (1.0 - alpha) + input[nextPos] * alpha;
// Low-pass filter.
if (tmpLowpass.active) {
value = tmpLowpass.process(value);
}
outputBuffer[offset + outL] += value * gainLeft;
outL++;
outputBuffer[offset + outL] += value * gainRight;
outL++;
// Next sample.
tmpSourceSamplePosition += pitchRatio;
if (tmpSourceSamplePosition >= tmpLoopEndDbl && isLooping) {
tmpSourceSamplePosition -= tmpLoopEnd - tmpLoopStart + 1.0;
}
}
break;
case OutputMode.StereoUnweaved:
gainLeft = gainMono * this.panFactorLeft;
gainRight = gainMono * this.panFactorRight;
while (blockSamples-- > 0 && tmpSourceSamplePosition < tmpSampleEndDbl) {
const pos: number = tmpSourceSamplePosition | 0;
const nextPos: number = pos >= tmpLoopEnd && isLooping ? tmpLoopStart : pos + 1;
// Simple linear interpolation.
const alpha: number = tmpSourceSamplePosition - pos;
let value: number = input[pos] * (1.0 - alpha) + input[nextPos] * alpha;
// Low-pass filter.
if (tmpLowpass.active) {
value = tmpLowpass.process(value);
}
outputBuffer[offset + outL] += value * gainLeft;
outL++;
outputBuffer[offset + outR] += value * gainRight;
outR++;
// Next sample.
tmpSourceSamplePosition += pitchRatio;
if (tmpSourceSamplePosition >= tmpLoopEndDbl && isLooping) {
tmpSourceSamplePosition -= tmpLoopEnd - tmpLoopStart + 1.0;
}
}
break;
case OutputMode.Mono:
while (blockSamples-- > 0 && tmpSourceSamplePosition < tmpSampleEndDbl) {
const pos: number = tmpSourceSamplePosition | 0;
const nextPos: number = pos >= tmpLoopEnd && isLooping ? tmpLoopStart : pos + 1;
// Simple linear interpolation.
const alpha: number = tmpSourceSamplePosition - pos;
let value: number = input[pos] * (1.0 - alpha) + input[nextPos] * alpha;
// Low-pass filter.
if (tmpLowpass.active) {
value = tmpLowpass.process(value);
}
outputBuffer[offset + outL] = value * gainMono;
outL++;
// Next sample.
tmpSourceSamplePosition += pitchRatio;
if (tmpSourceSamplePosition >= tmpLoopEndDbl && isLooping) {
tmpSourceSamplePosition -= tmpLoopEnd - tmpLoopStart + 1.0;
}
}
break;
}
const inaudible =
this.ampEnv.segment === VoiceEnvelopeSegment.Release &&
Math.abs(gainMono) < SynthConstants.AudibleLevelThreshold;
if (
tmpSourceSamplePosition >= tmpSampleEndDbl ||
this.ampEnv.segment === VoiceEnvelopeSegment.Done ||
// Check if voice is inaudible during release to terminate early
inaudible
) {
this.kill();
return;
}
}
this.sourceSamplePosition = tmpSourceSamplePosition;
if (tmpLowpass.active || dynamicLowpass) {
this.lowPass = tmpLowpass;
}
}
public kill(): void {
this.playingPreset = -1;
}
}