forked from hackclub/sprig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tunePlayers.js
59 lines (47 loc) 路 1.21 KB
/
tunePlayers.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
import { playCellsOnBeat } from "./sequencer/sequencer.js";
function wait(time) {
return new Promise((resolve) => setTimeout(resolve, time));
}
async function playTuneHelper(tunes, playingRef) {
for (let i = 0; i < 32 * tunes.length; i++) {
if (!playingRef.playing) break;
const cellsIndex = Math.floor(i / 32);
const { cells, bpm } = tunes[cellsIndex];
playCellsOnBeat(cells, bpm, i % 32);
await wait((1000 * 60) / bpm);
}
}
export function playTune() {
let playingRef = { playing: true };
playTuneHelper(arguments, playingRef);
return {
end() {
playingRef.playing = false;
},
isPlaying() {
return playingRef.playing;
},
};
}
export async function loopTuneHelper(tunes, playingRef) {
let i = 0;
while (playingRef.playing) {
const cellsIndex = Math.floor(i / 32) % tunes.length;
const { cells, bpm } = tunes[cellsIndex];
playCellsOnBeat(cells, bpm, i % 32);
i++;
await wait((1000 * 60) / bpm);
}
}
export function loopTune() {
let playingRef = { playing: true };
loopTuneHelper(arguments, playingRef);
return {
end() {
playingRef.playing = false;
},
isPlaying() {
return playingRef.playing;
},
};
}