-
Notifications
You must be signed in to change notification settings - Fork 255
Expand file tree
/
Copy pathSyncPoint.test.ts
More file actions
490 lines (404 loc) · 17.3 KB
/
SyncPoint.test.ts
File metadata and controls
490 lines (404 loc) · 17.3 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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
import {
type IEventEmitterOfT,
type IEventEmitter,
EventEmitterOfT,
EventEmitter
} from '@coderline/alphatab/EventEmitter';
import { ScoreLoader } from '@coderline/alphatab/importer/ScoreLoader';
import { AlphaSynthMidiFileHandler } from '@coderline/alphatab/midi/AlphaSynthMidiFileHandler';
import { MidiFile } from '@coderline/alphatab/midi/MidiFile';
import { MidiFileGenerator } from '@coderline/alphatab/midi/MidiFileGenerator';
import type { BackingTrack } from '@coderline/alphatab/model/BackingTrack';
import { Settings } from '@coderline/alphatab/Settings';
import { BackingTrackPlayer, type IBackingTrackSynthOutput } from '@coderline/alphatab/synth/BackingTrackPlayer';
import {
ExternalMediaPlayer,
type IExternalMediaHandler,
type IExternalMediaSynthOutput
} from '@coderline/alphatab/synth/ExternalMediaPlayer';
import type { IAudioSampleSynthesizer } from '@coderline/alphatab/synth/IAudioSampleSynthesizer';
import type { ISynthOutputDevice } from '@coderline/alphatab/synth/ISynthOutput';
import { MidiFileSequencer } from '@coderline/alphatab/synth/MidiFileSequencer';
import type { PositionChangedEventArgs } from '@coderline/alphatab/synth/PositionChangedEventArgs';
import type { Hydra } from '@coderline/alphatab/synth/soundfont/Hydra';
import type { SynthEvent } from '@coderline/alphatab/synth/synthesis/SynthEvent';
import { FlatMidiEventGenerator } from 'test/audio/FlatMidiEventGenerator';
import { TestPlatform } from 'test/TestPlatform';
import { expect } from 'chai';
describe('SyncPointTests', () => {
it('sync-point-update', async () => {
const score = await syncPointTestScore();
const midi = new MidiFile();
const handler = new AlphaSynthMidiFileHandler(midi);
const generator = new MidiFileGenerator(score, new Settings(), handler);
generator.generate();
const sequencer = new MidiFileSequencer(new EmptyAudioSynthesizer());
sequencer.loadMidi(midi);
sequencer.mainUpdateSyncPoints(generator.syncPoints);
expect(
sequencer.currentSyncPoints.map(
p =>
`${p.masterBarIndex},${p.masterBarOccurence},${p.synthBpm},${p.syncBpm},${p.synthTime},${p.syncTime}`
)
).toMatchSnapshot();
});
/**
* See #2158
*/
it('no-syncpoints-modified-tempo-', async () => {
const score = ScoreLoader.loadAlphaTex(`
.
\\tempo 90
C4 * 4 |
\\tempo 120
C4 * 4
`);
const midi = new MidiFile();
const handler = new AlphaSynthMidiFileHandler(midi);
const generator = new MidiFileGenerator(score, new Settings(), handler);
generator.generate();
const sequencer = new MidiFileSequencer(new EmptyAudioSynthesizer());
sequencer.loadMidi(midi);
sequencer.currentUpdateCurrentTempo(0);
expect(sequencer.currentTempo).to.equal(90);
expect(sequencer.modifiedTempo).to.equal(90);
sequencer.currentUpdateCurrentTempo(1000);
expect(sequencer.currentTempo).to.equal(90);
expect(sequencer.modifiedTempo).to.equal(90);
sequencer.currentUpdateCurrentTempo(2000);
expect(sequencer.currentTempo).to.equal(90);
expect(sequencer.modifiedTempo).to.equal(90);
sequencer.currentUpdateCurrentTempo(3000);
expect(sequencer.currentTempo).to.equal(120);
expect(sequencer.modifiedTempo).to.equal(120);
sequencer.currentUpdateCurrentTempo(4000);
expect(sequencer.currentTempo).to.equal(120);
expect(sequencer.modifiedTempo).to.equal(120);
});
async function syncPointTestScore() {
// the testfile is built like this:
// we have the "expected" music sheet in syncpoints-testfile.gp
// this file is synchronized with an "actual" audio having completely different tempos.
// the file with wrong tempos used as backing track is in syncpoints-testfile-backingtrack.gp
// the backing track takes 42secs while the original sound would take 48secs
const data = await TestPlatform.loadFile('test-data/audio/syncpoints-testfile.gp');
const score = ScoreLoader.loadScoreFromBytes(data, new Settings());
return score;
}
it('sync-point-generation', async () => {
const score = await syncPointTestScore();
const handler = new FlatMidiEventGenerator();
const generator = new MidiFileGenerator(score, new Settings(), handler);
generator.generate();
expect(
generator.syncPoints.map(
p =>
`${p.masterBarIndex},${p.masterBarOccurence},${p.synthBpm},${p.syncBpm},${p.synthTime},${p.syncTime}`
)
).toMatchSnapshot();
const update = MidiFileGenerator.generateSyncPoints(score);
expect(generator.syncPoints.length).to.equal(update.length);
for (let i = 0; i < generator.syncPoints.length; i++) {
expect(update[i].masterBarIndex).to.equal(generator.syncPoints[i].masterBarIndex);
expect(update[i].masterBarOccurence).to.equal(generator.syncPoints[i].masterBarOccurence);
expect(update[i].syncBpm).to.equal(generator.syncPoints[i].syncBpm);
expect(update[i].syncTime).to.equal(generator.syncPoints[i].syncTime);
expect(update[i].synthBpm).to.equal(generator.syncPoints[i].synthBpm);
expect(update[i].synthTick).to.equal(generator.syncPoints[i].synthTick);
expect(update[i].synthTime).to.equal(generator.syncPoints[i].synthTime);
}
});
it('sync-point-generation-new', async () => {
const score = await syncPointTestScore();
const update = MidiFileGenerator.generateSyncPoints(score, true);
expect(
update.map(
p =>
`${p.masterBarIndex},${p.masterBarOccurence},${p.synthBpm},${p.syncBpm},${p.synthTime},${p.syncTime}`
)
).toMatchSnapshot();
});
it('modified-tempo-lookup', async () => {
const score = await syncPointTestScore();
const entries = Array.from(MidiFileGenerator.buildModifiedTempoLookup(score)).map(
e => `${e[0].syncPointValue?.millisecondOffset} => ${e[1].syncBpm}`
);
expect(entries).toMatchSnapshot();
});
async function prepareBackingTrackPlayer() {
const score = await syncPointTestScore();
const midi = new MidiFile();
const handler = new AlphaSynthMidiFileHandler(midi);
const generator = new MidiFileGenerator(score, new Settings(), handler);
generator.generate();
const output = new TestBackingTrackOutput();
output.backingTrackDuration = 42000;
const player = new BackingTrackPlayer(output, 500);
player.loadMidiFile(midi);
player.loadBackingTrack(score);
player.updateSyncPoints(generator.syncPoints);
return player;
}
async function prepareExternalMediaPlayer() {
const score = await syncPointTestScore();
const midi = new MidiFile();
const handler = new AlphaSynthMidiFileHandler(midi);
const generator = new MidiFileGenerator(score, new Settings(), handler);
generator.generate();
const player = new ExternalMediaPlayer(500);
const mediaHandler = new TestExternalMediaHandler(player.output as IExternalMediaSynthOutput);
mediaHandler.backingTrackDuration = 42000;
(player.output as IExternalMediaSynthOutput).handler = mediaHandler;
player.loadMidiFile(midi);
player.loadBackingTrack(score);
player.updateSyncPoints(generator.syncPoints);
return player;
}
it('playback-normal-backing-track', async () => {
const player = await prepareBackingTrackPlayer();
const events: PositionChangedEventArgs[] = [];
player.positionChanged.on(e => {
events.push(e);
});
events.shift();
player.play();
(player.output as TestBackingTrackOutput).playThroughSong(0, 42000, 500);
expect(events.map(e => `${e.currentTime},${e.originalTempo},${e.modifiedTempo}`)).toMatchSnapshot();
});
it('playback-fast-backing-track', async () => {
const player = await prepareBackingTrackPlayer();
player.playbackSpeed = 2;
const events: PositionChangedEventArgs[] = [];
player.positionChanged.on(e => {
events.push(e);
});
events.shift();
player.play();
(player.output as TestBackingTrackOutput).playThroughSong(0, 42000, 1000);
expect(events.map(e => `${e.currentTime},${e.originalTempo},${e.modifiedTempo}`)).toMatchSnapshot();
});
it('seek-normal-backing-track', async () => {
const player = await prepareBackingTrackPlayer();
const events: PositionChangedEventArgs[] = [];
player.positionChanged.on(e => {
events.push(e);
});
events.shift();
// seek on player
player.timePosition = 2000;
player.timePosition = 5000;
player.timePosition = 13000;
player.timePosition = 21000;
// seek on backing track
const testOutput = player.output as TestBackingTrackOutput;
testOutput.simulateSeek(8000);
testOutput.simulateSeek(16000);
testOutput.simulateSeek(32000);
expect(events.map(e => `${e.currentTime},${e.originalTempo},${e.modifiedTempo}`)).toMatchSnapshot();
expect(testOutput.seekTimes).toMatchSnapshot();
});
it('seek-fast-backing-track', async () => {
const player = await prepareBackingTrackPlayer();
player.playbackSpeed = 2;
const events: PositionChangedEventArgs[] = [];
player.positionChanged.on(e => {
events.push(e);
});
events.shift();
// seek on player
player.timePosition = 4000 / 2;
player.timePosition = 10000 / 2;
player.timePosition = 26000 / 2;
player.timePosition = 42000 / 2;
// seek on backing track
const testOutput = player.output as TestBackingTrackOutput;
testOutput.simulateSeek(8000);
testOutput.simulateSeek(16000);
testOutput.simulateSeek(32000);
expect(events.map(e => `${e.currentTime},${e.originalTempo},${e.modifiedTempo}`)).toMatchSnapshot();
expect(testOutput.seekTimes).toMatchSnapshot();
});
it('playback-normal-external-media', async () => {
const player = await prepareExternalMediaPlayer();
const events: PositionChangedEventArgs[] = [];
player.positionChanged.on(e => {
events.push(e);
});
events.shift();
player.play();
((player.output as IExternalMediaSynthOutput).handler as TestExternalMediaHandler).playThroughSong(
0,
42000,
500
);
expect(events.map(e => `${e.currentTime},${e.originalTempo},${e.modifiedTempo}`)).toMatchSnapshot();
});
it('playback-fast-external-media', async () => {
const player = await prepareExternalMediaPlayer();
player.playbackSpeed = 2;
const events: PositionChangedEventArgs[] = [];
player.positionChanged.on(e => {
events.push(e);
});
events.shift();
player.play();
((player.output as IExternalMediaSynthOutput).handler as TestExternalMediaHandler).playThroughSong(
0,
42000,
1000
);
expect(events.map(e => `${e.currentTime},${e.originalTempo},${e.modifiedTempo}`)).toMatchSnapshot();
});
it('seek-normal-external-media', async () => {
const player = await prepareExternalMediaPlayer();
const events: PositionChangedEventArgs[] = [];
player.positionChanged.on(e => {
events.push(e);
});
events.shift();
// seek on player
player.timePosition = 2000;
player.timePosition = 5000;
player.timePosition = 13000;
player.timePosition = 21000;
// seek on backing track
const testOutput = (player.output as IExternalMediaSynthOutput).handler as TestExternalMediaHandler;
testOutput.simulateSeek(8000);
testOutput.simulateSeek(16000);
testOutput.simulateSeek(32000);
expect(events.map(e => `${e.currentTime},${e.originalTempo},${e.modifiedTempo}`)).toMatchSnapshot();
expect(testOutput.seekTimes).toMatchSnapshot();
});
it('seek-fast-external-media', async () => {
const player = await prepareExternalMediaPlayer();
player.playbackSpeed = 2;
const events: PositionChangedEventArgs[] = [];
player.positionChanged.on(e => {
events.push(e);
});
events.shift();
// seek on player
player.timePosition = 4000 / 2;
player.timePosition = 10000 / 2;
player.timePosition = 26000 / 2;
player.timePosition = 42000 / 2;
// seek on backing track
const testOutput = (player.output as IExternalMediaSynthOutput).handler as TestExternalMediaHandler;
testOutput.simulateSeek(8000);
testOutput.simulateSeek(16000);
testOutput.simulateSeek(32000);
expect(events.map(e => `${e.currentTime},${e.originalTempo},${e.modifiedTempo}`)).toMatchSnapshot();
expect(testOutput.seekTimes).toMatchSnapshot();
});
});
/**
* @internal
*/
class TestBackingTrackOutput implements IBackingTrackSynthOutput {
public seekTimes: number[] = [];
public simulateSeek(time: number) {
(this.timeUpdate as EventEmitterOfT<number>).trigger(time);
}
public playThroughSong(startTime: number, endTime: number, step: number) {
let time = startTime;
while (time <= endTime) {
this.simulateSeek(time);
time += step;
}
}
public timeUpdate: IEventEmitterOfT<number> = new EventEmitterOfT<number>();
public backingTrackDuration: number = 0;
public playbackRate: number = 1;
public masterVolume: number = 1;
public seekTo(time: number): void {
this.seekTimes.push(time);
}
public loadBackingTrack(_backingTrack: BackingTrack): void {}
public sampleRate: number = 44100;
public open(_bufferTimeInMilliseconds: number): void {
(this.ready as EventEmitter).trigger();
}
public play(): void {}
public destroy(): void {}
public pause(): void {}
public addSamples(_samples: Float32Array): void {}
public resetSamples(): void {}
public activate(): void {}
public ready: IEventEmitter = new EventEmitter();
public samplesPlayed: IEventEmitterOfT<number> = new EventEmitterOfT<number>();
public sampleRequest: IEventEmitter = new EventEmitter();
public async enumerateOutputDevices(): Promise<ISynthOutputDevice[]> {
return [] as ISynthOutputDevice[];
}
public async setOutputDevice(_device: ISynthOutputDevice | null): Promise<void> {}
public async getOutputDevice(): Promise<ISynthOutputDevice | null> {
return null;
}
}
/**
* @internal
*/
class TestExternalMediaHandler implements IExternalMediaHandler {
private _output: IExternalMediaSynthOutput;
public seekTimes: number[] = [];
public simulateSeek(time: number) {
this._output.updatePosition(time);
}
public playThroughSong(startTime: number, endTime: number, step: number) {
let time = startTime;
while (time <= endTime) {
this.simulateSeek(time);
time += step;
}
}
public constructor(output: IExternalMediaSynthOutput) {
this._output = output;
}
public backingTrackDuration: number = 0;
public playbackRate: number = 1;
public masterVolume: number = 1;
seekTo(time: number): void {
this.seekTimes.push(time);
}
play(): void {}
pause(): void {}
}
/**
* @internal
*/
class EmptyAudioSynthesizer implements IAudioSampleSynthesizer {
public masterVolume: number = 0;
public metronomeVolume: number = 0;
public outSampleRate: number = 44100;
public currentTempo: number = 120;
public timeSignatureNumerator: number = 4;
public timeSignatureDenominator: number = 4;
public activeVoiceCount: number = 0;
public noteOffAll(_immediate: boolean): void {}
public resetSoft(): void {}
public resetPresets(): void {}
public loadPresets(
_hydra: Hydra,
_instrumentPrograms: Set<number>,
_percussionKeys: Set<number>,
_append: boolean
): void {}
public setupMetronomeChannel(_metronomeChannel: number, _metronomeVolume: number): void {}
public synthesizeSilent(_sampleCount: number): void {}
public dispatchEvent(_synthEvent: SynthEvent): void {}
public synthesize(_buffer: Float32Array, _bufferPos: number, _ampleCount: number): SynthEvent[] {
return [];
}
public applyTranspositionPitches(_transpositionPitches: Map<number, number>): void {}
public setChannelTranspositionPitch(_channel: number, _semitones: number): void {}
public channelSetMute(_channel: number, _mute: boolean): void {}
public channelSetSolo(_channel: number, _solo: boolean): void {}
public resetChannelStates(): void {}
public channelSetMixVolume(_channel: number, _volume: number): void {}
public hasSamplesForProgram(_program: number): boolean {
return true;
}
public hasSamplesForPercussion(_key: number): boolean {
return true;
}
}