forked from Kos/js13k-2021
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaudio.ts
161 lines (143 loc) · 3.74 KB
/
audio.ts
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
import CPlayer from "./player-small";
import song from "./song";
type TSong = typeof song;
const ac = new AudioContext();
const bgm: TSong = {
...song,
songData: song.songData.slice(0, 6),
numChannels: 6,
};
function getFirstIndex({ endPattern }: TSong, instrument: number) {
for (let px = 0; px <= endPattern; ++px) {
if (song.songData[instrument].p[px]) {
return px;
}
}
// throw new Error("Failed to find first pattern");
}
function trimEffect(song: TSong, instrument: number, m: number = 0): TSong {
const fx = getFirstIndex(song, instrument);
const { i, p, c } = song.songData[instrument];
return {
...song,
songData: [
{
i,
p: [p[fx] + m],
c,
},
],
numChannels: 1,
endPattern: 1,
};
}
function trimEffect2(song: TSong, instrument: number): TSong {
const fx = getFirstIndex(song, instrument);
const { i, p, c } = song.songData[instrument];
const { i: i2, p: p2, c: c2 } = song.songData[instrument + 1];
return {
...song,
songData: [
{
i,
p: p.slice(fx, fx + 1),
c,
},
{
i: i2,
p: p2.slice(fx, fx + 1),
c: c2,
},
],
numChannels: 2,
endPattern: 1,
};
}
function toBuffer(song: TSong): AudioBuffer {
const p = CPlayer();
p.init(song);
while (p.generate() < 1);
return p.createAudioBuffer(ac);
}
function trimPrefix(buffer: AudioBuffer, seconds: number) {
const samplesToTrim = 0 | (seconds * buffer.sampleRate);
console.log("woudl trim", { samplesToTrim, seconds, secondsPerBeat });
const result = ac.createBuffer(
2,
buffer.length - samplesToTrim,
buffer.sampleRate
);
[0, 1].forEach((n) => {
const data = buffer.getChannelData(n);
const source = data.subarray(samplesToTrim);
const dest = result.getChannelData(n);
dest.set(source);
});
return result;
}
function toSource(buffer: AudioBuffer, trimMs: number = 0) {
const source = ac.createBufferSource();
source.buffer = buffer;
source.connect(ac.destination);
return source;
}
const bpm = 100;
const secondsPerBeat = 60 / bpm;
function timeIntoNextBeat(): number {
let { currentTime } = ac;
const sbb = secondsPerBeat;
const beatsSoFar = 0 | (currentTime / sbb);
const lastBeatTime = beatsSoFar * sbb;
const timeIntoNextBeat = currentTime - lastBeatTime;
return timeIntoNextBeat;
}
export function currentBeatFraction(): number {
return timeIntoNextBeat() / secondsPerBeat;
}
export function nextBeat(): number {
const frac = currentBeatFraction();
const remainingFrac = frac ? 1 - frac : 0;
const nextBeat = ac.currentTime + remainingFrac * secondsPerBeat;
return nextBeat;
}
function play(
song: TSong,
{ loop, now }: { loop?: boolean; now?: boolean } = {}
) {
let buf = toBuffer(song);
return () => {
let source = toSource(buf);
source.loop = loop;
if (loop) {
source.start(nextBeat());
return;
}
if (now) {
source.start();
return;
}
// beat correction for the rest
let cbf = currentBeatFraction();
if (cbf > 0.2) {
source.start(nextBeat());
} else if (cbf > 0.08) {
const trim = (cbf - 0.08) * 0.6;
source = toSource(trimPrefix(buf, trim));
source.start();
} else {
source.start();
}
};
}
const playBGM = play(bgm, { loop: true });
const playQ = play(trimEffect(song, 6));
const playW = play(trimEffect(song, 7));
const playE = play(trimEffect(song, 9));
const playS = play(trimEffect2(song, 11));
const playBoom1 = play(trimEffect(song, 10), { now: true });
const playBoom2 = play(trimEffect(song, 10, 1), { now: true });
function playBoom() {
(Math.random() < 0.5 ? playBoom1 : playBoom2)();
}
export { playBGM, playQ, playW, playE, playS, playBoom };
playBGM();