-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathaudio.js
102 lines (90 loc) · 3.33 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
var ctx = null;
var buffer_size = 0;
register_plugin = function (importObject) {
importObject.env.audio_init = function (audio_buffer_size) {
if (ctx != null) {
return;
}
window.startPause = 0;
window.endPause = 0;
document.addEventListener("visibilitychange", (e) => {
if (document.hidden) {
window.startPause = performance.now() / 1000.0;
} else {
window.endPause = performance.now() / 1000.0;
}
}, false);
// https://gist.github.com/kus/3f01d60569eeadefe3a1
{
audioContext = window.AudioContext || window.webkitAudioContext;
ctx = new audioContext();
var fixAudioContext = function (e) {
console.log("fix");
// 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 events
document.removeEventListener('touchstart', fixAudioContext);
document.removeEventListener('touchend', fixAudioContext);
document.removeEventListener('mousedown', fixAudioContext);
};
// iOS 6-8
document.addEventListener('touchstart', fixAudioContext);
// iOS 9
document.addEventListener('touchend', fixAudioContext);
document.addEventListener('mousedown', fixAudioContext);
}
buffer_size = audio_buffer_size;
if (ctx) {
return true;
} else {
return false;
}
}
importObject.env.audio_current_time = function () {
return ctx.currentTime;
}
importObject.env.audio_samples = function (buffer_ptr, start_audio) {
var buffer = ctx.createBuffer(2, buffer_size, ctx.sampleRate);
var channel0 = buffer.getChannelData(0);
var channel1 = buffer.getChannelData(1);
var obuf = new Float32Array(wasm_memory.buffer, buffer_ptr, buffer_size * 2);
for (var i = 0, j = 0; i < buffer_size * 2; i++) {
channel0[i] = obuf[j++];
channel1[i] = obuf[j++];
}
var bufferSource = ctx.createBufferSource();
bufferSource.buffer = buffer;
bufferSource.connect(ctx.destination);
bufferSource.start(start_audio);
var bufferSec = buffer_size / ctx.sampleRate;
return bufferSec;
}
importObject.env.audio_sample_rate = function () {
return ctx.sampleRate;
}
importObject.env.audio_pause_state = function () {
var duration = window.endPause - window.startPause;
if (duration > 0) {
window.endPause = 0;
window.startPause = 0;
return duration;
} else if (window.startPause > 0) {
return -1;
} else {
return 0.0;
}
}
}
miniquad_add_plugin({ register_plugin });