forked from ircam-ismm/node-web-audio-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonkey-patch.js
More file actions
125 lines (104 loc) · 3.19 KB
/
Copy pathmonkey-patch.js
File metadata and controls
125 lines (104 loc) · 3.19 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
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
const fs = require('fs');
let contextId = 0;
const isPlainObject = function(obj) {
return Object.prototype.toString.call(obj) === '[object Object]';
};
function patchAudioContext(NativeAudioContext) {
class AudioContext extends NativeAudioContext {
constructor(...args) {
super(...args);
// prevent garbage collection
const processId = `__AudioContext_${contextId}`;
process[processId] = this;
Object.defineProperty(this, '__processId', {
value: processId,
enumerable: false,
writable: false,
configurable: false,
});
contextId += 1;
// keep process awake
const keepAwakeId = setInterval(() => {}, 10000);
Object.defineProperty(this, '__keepAwakeId', {
value: keepAwakeId,
enumerable: false,
writable: true,
configurable: false,
});
}
// promisify sync APIs
resume() {
clearTimeout(this.__keepAwakeId);
this.__keepAwakeId = setInterval(() => {}, 2000);
return Promise.resolve(super.resume());
}
suspend() {
return Promise.resolve(super.suspend());
}
close() {
delete process[this.__processId];
clearTimeout(this.__keepAwakeId);
return Promise.resolve(super.close());
}
decodeAudioData(audioData) {
if (!isPlainObject(audioData) || !('path' in audioData)) {
throw new Error(`Invalid argument, please consider using the load helper`);
}
try {
const audioBuffer = super.decodeAudioData(audioData);
return Promise.resolve(audioBuffer);
} catch (err) {
return Promise.reject(err);
}
}
}
return AudioContext;
}
function patchOfflineAudioContext(NativeOfflineAudioContext) {
class OfflineAudioContext extends NativeOfflineAudioContext {
constructor(...args) {
super(...args);
console.log(args);
// not sure this is usefull, to be tested
const keepAwakeId = setInterval(() => {}, 10000);
Object.defineProperty(this, '__keepAwakeId', {
value: keepAwakeId,
enumerable: false,
writable: true,
configurable: false,
});
}
// promisify sync APIs
startRendering() {
try {
const audioBuffer = super.startRendering();
clearTimeout(this.__keepAwakeId);
return Promise.resolve(audioBuffer);
} catch (err) {
return Promise.reject(err);
}
}
decodeAudioData(audioData) {
if (!isPlainObject(audioData) || !('path' in audioData)) {
throw new Error(`Invalid argument, please consider using the load helper`);
}
try {
const audioBuffer = super.decodeAudioData(audioData);
return Promise.resolve(audioBuffer);
} catch (err) {
return Promise.reject(err);
}
}
}
return OfflineAudioContext;
}
module.exports.patchAudioContext = patchAudioContext;
module.exports.patchOfflineAudioContext = patchOfflineAudioContext;
// dumb method provided to mock an xhr call and mimick browser's API
// see also `AudioContext.decodeAudioData`
module.exports.load = function(path) {
if (!fs.existsSync(path)) {
throw new Error(`File not found: "${path}"`);
}
return { path };
}