-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.js
152 lines (125 loc) · 4.15 KB
/
player.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
140
141
142
143
144
145
146
147
148
149
150
151
152
import { loadMod } from './loader.js';
const AUDIO = Symbol('audio');
const GAIN = Symbol('gain');
const WORKLET = Symbol('worklet');
const ROW_CALLBACKS = Symbol('rowCallbacks');
const SINGLE_CALLBACKS = Symbol('singleCallbacks');
const STOP_CALLBACKS = Symbol('stopCallbacks');
export class ModPlayer {
constructor(audioContext) {
this.mod = null;
this.playing = false;
this[AUDIO] = audioContext || null;
this[GAIN] = null;
this[WORKLET] = null;
this[ROW_CALLBACKS] = [];
this[SINGLE_CALLBACKS] = { };
this[STOP_CALLBACKS] = [];
}
async load(url) {
if (this[WORKLET]) this.unload();
this.mod = await loadMod(url);
if (!this[AUDIO]) this[AUDIO] = new AudioContext();
this[GAIN] = this[AUDIO].createGain();
this[GAIN].gain.value = 0.3;
await this[AUDIO].audioWorklet.addModule('./mod-player-worklet.js');
this[WORKLET] = new AudioWorkletNode(this[AUDIO], 'mod-player-worklet');
this[WORKLET].connect(this[GAIN]).connect(this[AUDIO].destination);
this[WORKLET].port.onmessage = this.onmessage.bind(this);
}
onmessage(event) {
const { data } = event;
switch (data.type) {
case 'row':
// Call all the general row callbacks
for (let callback of this[ROW_CALLBACKS]) {
callback(data.position, data.rowIndex);
}
// Call all the single row callbacks
const key = data.position + ':' + data.rowIndex;
if (key in this[SINGLE_CALLBACKS]) {
for (let callback of this[SINGLE_CALLBACKS][key]) {
callback(data.position, data.rowIndex);
}
}
break;
case 'stop':
for (let callback of this[STOP_CALLBACKS]) {
callback();
}
break;
}
}
watchRows(callback) {
this[WORKLET].port.postMessage({
type: 'enableRowSubscription'
});
this[ROW_CALLBACKS].push(callback);
}
watch(position, row, callback) {
this[WORKLET].port.postMessage({
type: 'enableRowSubscription'
});
// Store the callback in a dictionary
const key = position + ':' + row;
// There can be multiple callbacks for the same position and row
// so we store them in an array
if (!(key in this[SINGLE_CALLBACKS])) {
this[SINGLE_CALLBACKS][key] = [];
}
// Add the callback to the array
this[SINGLE_CALLBACKS][key].push(callback);
}
watchStop(callback) {
this[WORKLET].port.postMessage({
type: 'enableStopSubscription'
});
this[STOP_CALLBACKS].push(callback);
}
unload() {
if (this.playing) this.stop();
if (!this[WORKLET]) return;
this[WORKLET].disconnect();
this[AUDIO].close();
this.mod = null;
this[AUDIO] = null;
this[WORKLET] = null;
this[ROW_CALLBACKS] = [];
this[SINGLE_CALLBACKS] = { };
}
play() {
if (this.playing) return;
if (!this[WORKLET]) return;
this[AUDIO].resume();
this[WORKLET].port.postMessage({
type: 'play',
mod: this.mod,
sampleRate: this[AUDIO].sampleRate
});
this.playing = true;
}
stop() {
if (!this.playing) return;
this[WORKLET].port.postMessage({
type: 'stop'
});
this.playing = false;
}
resume() {
if (this.playing) return;
this[WORKLET].port.postMessage({
type: 'resume'
});
this.playing = true;
}
setRow(position, row) {
this[WORKLET].port.postMessage({
type: 'setRow',
position: position,
row: row
});
}
setVolume(volume) {
this[GAIN].gain.value = volume;
}
}