forked from sugarlabs/musicblocks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp5-sound-adapter.js
More file actions
58 lines (52 loc) · 2.4 KB
/
p5-sound-adapter.js
File metadata and controls
58 lines (52 loc) · 2.4 KB
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
/* global define, window */
define(["p5.sound.min"], function () {
console.log("p5-sound-adapter: p5.sound loaded");
// Restore AudioContext if it was overwritten
if (window.OriginalAudioContext && window.AudioContext !== window.OriginalAudioContext) {
console.log("p5-sound-adapter: Restoring AudioContext");
window.AudioContext = window.OriginalAudioContext;
}
if (
window.OriginalWebkitAudioContext &&
window.webkitAudioContext !== window.OriginalWebkitAudioContext
) {
console.log("p5-sound-adapter: Restoring webkitAudioContext");
window.webkitAudioContext = window.OriginalWebkitAudioContext;
}
// Restore Tone.js
if (window.OriginalTone) {
if (window.Tone !== window.OriginalTone) {
console.log("p5-sound-adapter: Restoring MusicBlocks Tone.js");
window.Tone = window.OriginalTone;
} else {
console.log("p5-sound-adapter: Tone was not overwritten by p5.sound");
}
} else {
console.warn("p5-sound-adapter: No OriginalTone to restore!");
}
// Fix AudioNode.prototype.connect return value
// We force this patch because p5.sound is known to break chaining,
// and sometimes the reference check fails (e.g. if p5.sound wraps it in a way that preserves identity or if we missed the timing).
// The error "s.connect(...) is undefined" confirms we MUST ensure a return value.
if (window.AudioNode && window.AudioNode.prototype) {
const currentConnect = window.AudioNode.prototype.connect;
// Avoid double-patching if we already did it
if (!currentConnect.isP5AdapterPatched) {
console.log(
"p5-sound-adapter: Forcing patch of AudioNode.prototype.connect to support chaining"
);
window.AudioNode.prototype.connect = function () {
const result = currentConnect.apply(this, arguments);
// If the result is undefined (which breaks Tone.js chaining), return the destination (arguments[0])
if (result === undefined) {
return arguments[0];
}
return result;
};
// Mark as patched
window.AudioNode.prototype.connect.isP5AdapterPatched = true;
} else {
console.log("p5-sound-adapter: AudioNode.prototype.connect already patched");
}
}
});