-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsound-fx-player.js
111 lines (95 loc) · 3 KB
/
sound-fx-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
'use strict';
// Copyright 2024 Jeff Bush
//
// Licensed under the Apache License, Version 2.0 (the 'License');
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an 'AS IS' BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// This module implements background thread that plays sound effects. The main
// game thread sends messages to this thread to kick off playback.
// Each sound effect consists of a series of frequencies (each being a byte that
// indicates a piano note), amplitudes, as well as an overall speed and
// waveform.
//
function square(time) {
return time > 0.5 ? 1 : 0;
}
function triangle(time) {
return 4 * Math.abs(time - 0.5) - 1;
}
function saw(time) {
return time;
}
class SoundEffectsPlayer extends AudioWorkletProcessor {
constructor(options) {
super();
this.port.onmessage = this.handleMessage.bind(this);
this.time = 0.0;
this.deltaTime = 0;
this.pitches = null;
this.amplitudes = null;
this.effectIndex = 0;
this.samplesPerNote = 0;
this.sampleCount = 0;
this.wavefn = square;
}
// @bug: This has popping and crackling because of abrupt transitions.
process(inputs, outputs, parameters) {
const outputBuf = outputs[0][0];
if (this.pitches === null || this.effectIndex == this.pitches.length) {
return true;
}
for (let i = 0; i < outputBuf.length; i++) {
outputBuf[i] = this.wavefn(this.time) * this.amplitude;
this.time = (this.time + this.deltaTime) % 1.0;
if (++this.sampleCount == this.samplesPerNote) {
this.sampleCount = 0;
if (++this.effectIndex == this.pitches.length) {
break;
} else {
this.setNote(this.pitches[this.effectIndex],
this.amplitudes[this.effectIndex]);
}
}
}
return true;
}
setNote(pitch, amplitude) {
const frequency = 27.5 * 2 ** (Math.floor(pitch) / 12);
this.deltaTime = frequency / sampleRate;
this.amplitude = amplitude / 255;
}
handleMessage(event) {
if (event.data.noteDuration == 0) {
return;
}
this.samplesPerNote = Math.floor(event.data.noteDuration / 255 *
sampleRate);
this.pitches = event.data.pitches;
this.amplitudes = event.data.amplitudes;
switch (event.data.waveform) {
case 0:
this.wavefn = square;
break;
case 1:
this.wavefn = triangle;
break;
case 2:
this.wavefn = saw;
break;
}
this.setNote(this.pitches[0], this.amplitudes[0]);
this.effectIndex = 0;
this.sampleCount = 0;
this.time = 0; // Avoid a pop at the beginning.
}
}
registerProcessor('sound-fx-player', SoundEffectsPlayer);