forked from not-fl3/quad-snd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio.js
192 lines (156 loc) · 6 KB
/
audio.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
"use strict";
const AudioContext = window.AudioContext || window.webkitAudioContext;
let audio_context;
let sounds = new Map();
let playbacks = [];
let sound_key_next = 1;
let playback_key_next = 1;
function audio_init() {
if (audio_context == null) {
audio_context = new AudioContext();
let audio_listener = audio_context.listener;
{
let AudioContext = window.AudioContext || window.webkitAudioContext;
let ctx = new AudioContext();
var fixAudioContext = function (e) {
console.log("fix");
// On newer Safari AudioContext starts in a suspended state per
// spec but is only resumable by a call running in an event
// handler triggered by the user. Do it here. Reference:
// https://stackoverflow.com/questions/56768576/safari-audiocontext-suspended-even-with-onclick-creation
audio_context.resume();
// On older Safari, audio context should be explicitly unpaused
// in a mouse/touch input event even if it was created after
// first input event on the page thanks to:
// https://gist.github.com/kus/3f01d60569eeadefe3a1
// Create empty buffer
var buffer = ctx.createBuffer(1, 1, 22050);
var source = ctx.createBufferSource();
source.buffer = buffer;
// Connect to output (speakers)
source.connect(ctx.destination);
// Play sound
if (source.start) {
source.start(0);
} else if (source.play) {
source.play(0);
} else if (source.noteOn) {
source.noteOn(0);
}
// Remove event handlers
document.removeEventListener('touchstart', fixAudioContext);
document.removeEventListener('touchend', fixAudioContext);
document.removeEventListener('mousedown', fixAudioContext);
document.removeEventListener('keydown', fixAudioContext);
};
// iOS 6-8
document.addEventListener('touchstart', fixAudioContext);
// iOS 9
document.addEventListener('touchend', fixAudioContext);
// Mac
document.addEventListener('mousedown', fixAudioContext);
document.addEventListener('keydown', fixAudioContext);
}
}
}
function audio_add_buffer(content, content_len) {
let content_array = wasm_memory.buffer.slice(content, content + content_len);
let sound_key = sound_key_next;
sound_key_next += 1;
audio_context.decodeAudioData(content_array, function(buffer) {
sounds.set(sound_key, buffer);
}, function(e) {
// fail
console.error("Failed to decode audio buffer", e);
});
return sound_key;
}
function audio_source_is_loaded(sound_key) {
return sounds.has(sound_key) && sounds.get(sound_key) != undefined;
}
function recycle_playback() {
let playback = playbacks.find(playback => playback.sound_key === 0);
if (playback != null) {
playback.source = audio_context.createBufferSource();
} else {
playback = {
sound_key: 0,
playback_key: 0,
source: audio_context.createBufferSource(),
gain_node: audio_context.createGain(),
ended: null,
};
playbacks.push(playback);
}
return playback;
}
function stop(playback) {
try {
playback.source.removeEventListener('ended', playback.ended);
playback.source.disconnect();
playback.gain_node.disconnect();
playback.sound_key = 0;
playback.playback_key = 0;
} catch (e) {
console.error("Error stopping sound", e);
}
}
function audio_play_buffer(sound_key, volume, repeat) {
let playback_key = playback_key_next++;
let pb = recycle_playback();
pb.sound_key = sound_key;
pb.playback_key = playback_key;
pb.source.connect(pb.gain_node);
pb.gain_node.connect(audio_context.destination);
pb.gain_node.gain.value = volume;
pb.source.loop = repeat;
pb.ended = function() {
stop(pb);
};
pb.source.addEventListener('ended', pb.ended);
try {
pb.source.buffer = sounds.get(sound_key);
pb.source.start(0);
} catch (e) {
console.error("Error starting sound", e);
}
return playback_key;
}
function audio_source_set_volume(sound_key, volume) {
playbacks.forEach(playback => {
if (playback.sound_key === sound_key) {
playback.gain_node.gain.value = volume;
}
});
}
function audio_source_stop(sound_key) {
playbacks.forEach(playback => {
playback.sound_key === sound_key && stop(playback);
});
}
function audio_source_delete(sound_key) {
audio_source_stop(sound_key);
sounds.delete(sound_key);
}
function audio_playback_stop(playback_key) {
let playback = playbacks.find(playback => playback.playback_key === playback_key);
playback != null && stop(playback);
}
function audio_playback_set_volume(playback_key, volume) {
let playback = playbacks.find(playback => playback.playback_key === playback_key);
if (playback != null) {
playback.gain_node.gain.value = volume;
}
}
function register_plugin(importObject) {
importObject.env.audio_init = audio_init;
importObject.env.audio_add_buffer = audio_add_buffer;
importObject.env.audio_play_buffer = audio_play_buffer;
importObject.env.audio_source_is_loaded = audio_source_is_loaded;
importObject.env.audio_source_set_volume = audio_source_set_volume;
importObject.env.audio_source_stop = audio_source_stop;
importObject.env.audio_source_delete = audio_source_delete;
importObject.env.audio_playback_stop = audio_playback_stop;
importObject.env.audio_playback_set_volume = audio_playback_set_volume;
}
miniquad_add_plugin({ register_plugin, version: 1, name: "macroquad_audio" });