Skip to content

Commit 35b27e2

Browse files
committed
v0.9.11; better pre-load caching; better initError handling
1 parent 99e565c commit 35b27e2

File tree

6 files changed

+69
-43
lines changed

6 files changed

+69
-43
lines changed

dist/sepia-web-audio.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/sepia-web-audio.js

Lines changed: 63 additions & 37 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.10";
6+
WebAudio.version = "0.9.11";
77

88
//Preparations
99
var AudioContext = window.AudioContext || window.webkitAudioContext;
@@ -33,6 +33,13 @@ if (!(typeof SepiaFW == "object")){
3333
//debugLog: console.log
3434
}
3535

36+
//Cache
37+
var preLoadCache = {};
38+
39+
WebAudio.clearPreLoadCache = function(){
40+
preLoadCache = {};
41+
}
42+
3643
//Media constraints
3744
WebAudio.getSupportedAudioConstraints = function(){
3845
var sc = navigator.mediaDevices.getSupportedConstraints(); //TODO: can fail due to non-SSL (secure context)
@@ -273,6 +280,10 @@ if (!(typeof SepiaFW == "object")){
273280
var initInfo = new Array(options.modules.length);
274281
var N = options.modules.length;
275282
options.modules.forEach(async function(module, i){
283+
if (!isInitPending){
284+
//already failed so ignore rest
285+
return;
286+
}
276287
addInitCondition("module-" + i);
277288

278289
var info = getModuleInfo(module);
@@ -283,37 +294,47 @@ if (!(typeof SepiaFW == "object")){
283294
//pre-loads - NOTE: there might be room for optimizations here ... - TODO: can/should we cache preloads globally?
284295
var preLoads = {};
285296
var preLoadKeys = Object.keys(info.modulePreLoads);
286-
await Promise.all(preLoadKeys.map(async function(plKey, j){
287-
var plPath = info.modulePreLoads[plKey]; //NOTE: this can be a string or an object ({type: 2, path: 'url'})
288-
var plType = 1; //1: text, 2: arraybuffer
289-
var convert = undefined;
290-
if (typeof plPath == "object"){
291-
plType = (plPath.type && (plPath.type == 2 || plPath.type.toLowerCase() == "arraybuffer"))? 2 : 1;
292-
plPath = plPath.path || plPath.url;
293-
}else if (plKey.indexOf("wasmFile") == 0){
294-
plType = 2;
295-
}else if (plKey.indexOf("wasmBase64") == 0){
296-
plType = 1;
297-
convert = convertBase64ToUint8Array;
298-
}
299-
try{
300-
var data;
301-
if (!plPath || plType > 2){
302-
throw {name: "PreLoadError", message: "Missing 'path' (url) or unsupported type (use 1=text or 2=arraybuffer)"};
303-
}else if (plType == 1){
304-
data = await textLoaderPromise(plPath);
305-
}else if (plType == 2){
306-
data = await arrayBufferLoaderPromise(plPath);
297+
try{
298+
await Promise.all(preLoadKeys.map(async function(plKey, j){
299+
var plPath = info.modulePreLoads[plKey]; //NOTE: this can be a string or an object ({type: 2, path: 'url'})
300+
var plType = 1; //1: text, 2: arraybuffer
301+
var convert = undefined;
302+
if (typeof plPath == "object"){
303+
plType = (plPath.type && (plPath.type == 2 || plPath.type.toLowerCase() == "arraybuffer"))? 2 : 1;
304+
plPath = plPath.path || plPath.url;
305+
}else if (plKey.indexOf("wasmFile") == 0){
306+
plType = 2;
307+
}else if (plKey.indexOf("wasmBase64") == 0){
308+
plType = 1;
309+
convert = convertBase64ToUint8Array;
307310
}
308-
if (typeof convert == "function"){
309-
data = convert(data);
311+
var cachePath = plKey + "_" + plPath;
312+
if (preLoadCache[cachePath]){
313+
preLoads[plKey] = preLoadCache[cachePath];
314+
}else{
315+
try{
316+
var data;
317+
if (!plPath || plType > 2){
318+
throw {name: "PreLoadError", message: "Missing 'path' (url) or unsupported type (use 1=text or 2=arraybuffer)"};
319+
}else if (plType == 1){
320+
data = await textLoaderPromise(plPath);
321+
}else if (plType == 2){
322+
data = await arrayBufferLoaderPromise(plPath);
323+
}
324+
if (typeof convert == "function"){
325+
data = convert(data);
326+
}
327+
preLoads[plKey] = data;
328+
preLoadCache[cachePath] = data;
329+
}catch (err){
330+
throw {name: "AddModuleError", message: ("Failed to pre-load data: " + plKey + " - name: " + moduleName), info: err};
331+
}
310332
}
311-
preLoads[plKey] = data;
312-
}catch (err){
313-
throw {name: "AddModuleError", message: ("Failed to pre-load data: " + plKey + " - name: " + moduleName), info: err};
314-
}
315-
}));
316-
333+
}));
334+
}catch (err){
335+
initializerError(err);
336+
return;
337+
}
317338
//add some context info
318339
var fullOptions = moduleSetup.options || {};
319340
fullOptions.preLoadResults = preLoads;
@@ -389,7 +410,7 @@ if (!(typeof SepiaFW == "object")){
389410
info: errorMessage
390411
});
391412
if (isInitPending && !isInitialized){
392-
completeInitCondition("module-" + i);
413+
//completeInitCondition("module-" + i);
393414
initializerError({message: "Error during setup of module: " + thisProcessNode.moduleName, name: "ProcessorInitError", info: errorMessage});
394415
}
395416
if (moduleSetup.onerror){
@@ -400,7 +421,8 @@ if (!(typeof SepiaFW == "object")){
400421
//AudioWorkletProcessor
401422
if (moduleType == 1){
402423
if (!sourceHasWorkletSupport){
403-
throw {name: "AddModuleError", message: ("Source does not support 'AudioWorkletProcessor' (use only workers instead) - name: " + moduleName)};
424+
initializerError({name: "AddModuleError", message: ("Source does not support 'AudioWorkletProcessor' (use only workers instead) - name: " + moduleName)});
425+
return;
404426
}
405427
if (!fullOptions.processorOptions) fullOptions.processorOptions = fullOptions.setup || {}; //common field is "setup"
406428
if (!fullOptions.processorOptions.ctxInfo){
@@ -456,14 +478,17 @@ if (!(typeof SepiaFW == "object")){
456478

457479
//Script Processor
458480
}else if (moduleType == 3){
459-
throw {name: "AddModuleError", message: "ScriptProcessor nodes are currently not supported as modules (only source)."};
481+
initializerError({name: "AddModuleError", message: "ScriptProcessor nodes are currently not supported as modules (only source)."});
482+
return;
460483

461484
//Audio Node
462485
}else if (moduleType == 4){
463-
throw {name: "AddModuleError", message: "AudioNodes are currently not supported as modules (you can use them as custom source)."};
464-
486+
initializerError({name: "AddModuleError", message: "AudioNodes are currently not supported as modules (you can use them as custom source)."});
487+
return;
488+
465489
}else{
466-
throw {name: "AddModuleError", message: "Unknown module type."};
490+
initializerError({name: "AddModuleError", message: "Unknown module type."});
491+
return;
467492
}
468493
thisProcessNode.moduleType = moduleType;
469494
thisProcessNode.ignoreSendToModules = false; //this is most useful for workers to prevent serialization if message is not processed anyway
@@ -488,7 +513,8 @@ if (!(typeof SepiaFW == "object")){
488513
if (!sourceHasWorkletSupport && i == 0){
489514
var source = processNodes[0];
490515
if (!source.onmessage){
491-
throw {name: "AddModuleError", message: "If source is not compatible to 'AudioWorklet' it has to have a 'onmessage' event to get the processed data."};
516+
initializerError({name: "AddModuleError", message: "If source is not compatible to 'AudioWorklet' it has to have a 'onmessage' event to get the processed data."});
517+
return;
492518
}
493519
source.onmessage = function(e){
494520
//like 'sendToModules' this can be event or data for processing

test-pages/test-recorder-pipeline.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<script src="../src/visualization/uPlot-lazy.min.js?v=0.9.2"></script>
1010
<link rel="stylesheet" href="../src/visualization/uPlot.min.css?v=1.5.2">
1111

12-
<script type="text/javascript" src="../src/sepia-web-audio.js?v=0.9.7"></script>
12+
<script type="text/javascript" src="../src/sepia-web-audio.js?v=0.9.11"></script>
1313
<script>
1414
//set correct modules folder
1515
SepiaFW.webAudio.defaultProcessorOptions.moduleFolder = "../src/modules";

test-pages/test-source-confusion.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<meta name="viewport" content="width=device-width, initial-scale=1">
77
<title>SEPIA Web Audio Input-Output Test</title>
88

9-
<script type="text/javascript" src="../src/sepia-web-audio.js?v=0.9.7"></script>
9+
<script type="text/javascript" src="../src/sepia-web-audio.js?v=0.9.11"></script>
1010
<script>
1111
//set correct modules folder
1212
SepiaFW.webAudio.defaultProcessorOptions.moduleFolder = "../src/modules";

tutorial-code-page.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<meta name="viewport" content="width=device-width, initial-scale=1">
77
<title>SEPIA Web Audio Tutorial Code</title>
88

9-
<script type="text/javascript" src="src/sepia-web-audio.js?v=0.9.9"></script>
9+
<script type="text/javascript" src="src/sepia-web-audio.js?v=0.9.11"></script>
1010
<script>
1111
//set correct modules folder
1212
if (window.SepiaFW) SepiaFW.webAudio.defaultProcessorOptions.moduleFolder = "src/modules";

voice-recorder-demo.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<script src="src/visualization/uPlot-lazy.min.js?v=0.9.2"></script>
1010
<link rel="stylesheet" href="src/visualization/uPlot.min.css?v=1.5.2">
1111

12-
<script type="text/javascript" src="src/sepia-web-audio.js?v=0.9.9"></script>
12+
<script type="text/javascript" src="src/sepia-web-audio.js?v=0.9.11"></script>
1313
<script type="text/javascript" src="src/sepia-recorder.js?v=0.9.7"></script>
1414
<!--<script type="text/javascript" src="dist/sepia-web-audio.min.js?v=0.9.7"></script>-->
1515
<!--<script type="text/javascript" src="dist/sepia-recorder.min.js?v=0.9.7"></script>-->

0 commit comments

Comments
 (0)