Skip to content

Commit 5a2ae0e

Browse files
committed
v0.9.3; catched messages that are sent before module is ready and added runtime error msg;
1 parent c61113d commit 5a2ae0e

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

src/processor.js

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ if (!(typeof SepiaFW == "object")){
33
}
44
(function (parentModule){
55
var WebAudio = parentModule.webAudio || {};
6-
WebAudio.version = "0.9.2";
6+
WebAudio.version = "0.9.3";
77

88
//Preparations
99
var AudioContext = window.AudioContext || window.webkitAudioContext;
@@ -86,7 +86,9 @@ if (!(typeof SepiaFW == "object")){
8686
//NOTE: currently (Dec 2020) only Chromium can do this:
8787
contextOptions.sampleRate = options.targetSampleRate;
8888
}
89-
return new AudioContext(contextOptions);
89+
var ac = new AudioContext(contextOptions);
90+
//console.log("AC STATE: " + ac.state); //TODO: this can be suspended if the website is restrict and the user didn't interact with it yet
91+
return ac;
9092
};
9193

9294
//Processor class
@@ -309,11 +311,13 @@ if (!(typeof SepiaFW == "object")){
309311
fullOptions.preLoadResults = preLoads;
310312

311313
var thisProcessNode;
314+
312315
function onMessage(event){
313316
if (!event || event.data == undefined){
314317
//TODO: simply ignore?
315318
}else if (event.data.moduleState == 1){
316319
//STATE
320+
thisProcessNode.isReady = true;
317321
completeInitCondition("module-" + i);
318322
if (event.data.moduleInfo) thisProcessNode.moduleInfo = event.data.moduleInfo;
319323
initInfo[i] = {
@@ -380,10 +384,21 @@ if (!(typeof SepiaFW == "object")){
380384
}
381385
}
382386
thisProcessNode = new AudioWorkletNode(mainAudioContext, moduleName, fullOptions);
387+
thisProcessNode.isReady = false;
383388
thisProcessNode.moduleName = moduleName;
384389
thisProcessNode.port.onmessage = onMessage;
385390
thisProcessNode.onprocessorerror = onError;
386-
thisProcessNode.sendToModule = function(msg){ thisProcessNode.port.postMessage(msg); };
391+
thisProcessNode.sendToModule = function(msg){
392+
if (!thisProcessNode.isReady){
393+
onProcessorError({
394+
name: "AudioModuleProcessorException",
395+
message: "'sendToModule' was called before module was actually ready. Consider 'startSuspended' option maybe.'",
396+
module: thisProcessNode.moduleName
397+
});
398+
}else{
399+
thisProcessNode.port.postMessage(msg);
400+
}
401+
};
387402

388403
//Web Worker
389404
}else if (moduleType == 2){
@@ -395,10 +410,22 @@ if (!(typeof SepiaFW == "object")){
395410
}
396411
}
397412
thisProcessNode = new Worker(moduleFolder + moduleName.replace(/-worker$/, "") + '-worker.js'); //NOTE: a worker has to be named "-worker.js"!
413+
thisProcessNode.isReady = false;
398414
thisProcessNode.moduleName = moduleName;
399415
thisProcessNode.onmessage = onMessage;
400416
thisProcessNode.onerror = onError;
401-
thisProcessNode.sendToModule = function(msg){ thisProcessNode.postMessage(msg); };
417+
thisProcessNode.sendToModule = function(msg){
418+
if (!thisProcessNode.isReady){
419+
if (msg && msg.ctrl && msg.ctrl.action == "construct") thisProcessNode.postMessage(msg);
420+
else onProcessorError({
421+
name: "AudioModuleProcessorException",
422+
message: "'sendToModule' was called before module was actually ready. Consider 'startSuspended' option maybe.",
423+
module: thisProcessNode.moduleName
424+
});
425+
}else{
426+
thisProcessNode.postMessage(msg);
427+
}
428+
};
402429
thisProcessNode.sendToModule({ctrl: {action: "construct", options: fullOptions}});
403430

404431
//Script Processor

0 commit comments

Comments
 (0)