-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathaudioworkletnode.spec.js
139 lines (114 loc) · 4.08 KB
/
audioworkletnode.spec.js
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
import { waitForAppReady} from './app.js';
import { songsourceeditor, synthsourceeditor } from './editorcontroller.js';
import { getTargetNoteStates } from './visualizer/defaultvisualizer.js';
describe('audioworkletnode', async function() {
this.timeout(20000);
this.beforeAll(async () => {
document.documentElement.appendChild(document.createElement('app-javascriptmusic'));
await waitForAppReady();
});
this.afterAll(async () => {
window.stopaudio();
window.audioworkletnode = undefined;
document.documentElement.removeChild(document.querySelector('app-javascriptmusic'));
});
it('should start audioworklet and play a note that triggers the visualizer', async () => {
const synthsource = `
import { Kick2 } from "../instruments/drums/kick2.class";
import { SineOscillator } from '../synth/sineoscillator.class';
import { Envelope } from '../synth/envelope.class';
import { StereoSignal } from "../synth/stereosignal.class";
import { notefreq } from '../synth/note';
export const PATTERN_SIZE_SHIFT: usize = 4;
export const BEATS_PER_PATTERN_SHIFT: usize = 2;
class SineLead {
private _note: f32;
readonly osc: SineOscillator = new SineOscillator();
readonly env1: Envelope = new Envelope(0.02, 0.15, 0.5, 0.3);
readonly signal: StereoSignal = new StereoSignal();
set note(note: f32) {
if(note > 1) {
this.osc.frequency = notefreq(note);
this._note = note;
this.env1.attack();
} else {
this.env1.release();
}
}
get note(): f32 {
return this._note;
}
next(): void {
const env1: f32 = this.env1.next();
let osc: f32 = this.osc.next();
osc *= env1;
const pan = this._note / 127;
this.signal.left = osc * pan;
this.signal.right = osc * (1 - pan);
}
}
const kick = new Kick2();
const sinelead = new SineLead();
export function setChannelValue(channel: usize, value: f32): void {
switch(channel) {
case 0:
kick.note = value;
break;
case 1:
sinelead.note = value;
break;
}
}
export function mixernext(leftSampleBufferPtr: usize, rightSampleBufferPtr: usize): void {
let left: f32 = 0;
let right: f32 = 0;
kick.next();
left += kick.signal.left;
right += kick.signal.right;
sinelead.next();
left += sinelead.signal.left;
right += sinelead.signal.right;
const gain: f32 = 0.4;
left *= gain;
right *= gain;
store<f32>(leftSampleBufferPtr, left);
store<f32>(rightSampleBufferPtr, right);
}
`;
const songsource = `
global.bpm = 120;
global.pattern_size_shift = 4;
addInstrument('kick', {type: 'number'});
addInstrument('sinelead', {type: 'note'});
playPatterns({
kick: pp(4, [
64, , , ,
64, , , ,
64, , ,10,
64, , 30, ,]),
sinelead: pp(4, [
d5,,,a4,
,a4,,,
c5,d5,,f5,
,f5,d5
])
});`
songsourceeditor.doc.setValue(songsource);
synthsourceeditor.doc.setValue(synthsource);
const appElement = document.getElementsByTagName('app-javascriptmusic')[0].shadowRoot;
appElement.querySelector('#startaudiobutton').click();
while (!window.audioworkletnode) {
await new Promise(resolve => setTimeout(resolve, 100));
}
assert.isDefined(window.audioworkletnode);
assert.isNotNull(window.audioworkletnode);
toggleVisualizer(true);
let firstNoteIndex = -1;
console.log('waiting for visualizer');
do {
firstNoteIndex = getTargetNoteStates().findIndex(state => state > -1);
await new Promise(resolve => setTimeout(resolve, 0));
} while (firstNoteIndex === -1)
assert.isAbove(firstNoteIndex, 0);
});
});