Skip to content

Commit bba9ed7

Browse files
committed
v0.9.10; support 'moduleState: 10' custom error; added 'WebAudio.base64' functions;
1 parent 2c1001a commit bba9ed7

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

src/modules/example-processor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class ExampleProcessor extends AudioWorkletProcessor {
3030
that.port.postMessage({
3131
//Default message type is "processing result", but it can be 'moduleState', 'moduleEvent' and 'moduleResponse' ("on-demand" requests) as well
3232
//NOTE: only default processing (no tag) and 'moduleEvent' will be forwarded automatically
33-
moduleState: 1, //1=ready, 2=changed, 9=read for termination
33+
moduleState: 1, //1=ready, 2=changed, 9=ready for termination, 10=custom error
3434
moduleInfo: {
3535
sourceSamplerate: that.sourceSamplerate,
3636
targetSamplerate: that.targetSamplerate,

src/modules/example-worker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function constructWorker(options) {
4444
postMessage({
4545
//Default message type is "processing result", but it can be 'moduleState', 'moduleEvent' and 'moduleResponse' ("on-demand" requests) as well
4646
//NOTE: only default processing (no tag) and 'moduleEvent' will be forwarded automatically
47-
moduleState: 1, //1=ready, 2=changed, 9=read for termination -- for "on-demand" requests outside of normal processing sequence use 'moduleResponse: true'
47+
moduleState: 1, //1=ready, 2=changed, 9=ready for termination, 10=custom error -- for "on-demand" requests outside of normal processing sequence use 'moduleResponse: true'
4848
moduleInfo: {
4949
hello: "world"
5050
}

src/modules/shared/common.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,6 @@ CommonConverters.int16ToFloat32BitAudio = function(output, input){
7575
return CommonConverters.singleSampleInt16ToFloat32BitAudio(input[i]);
7676
}
7777
}
78+
CommonConverters.uint8ArrayToBase64String = function(uint8Array){
79+
return btoa(uint8Array.reduce(function(data, byte){ return data + String.fromCharCode(byte); }, ''));
80+
}

src/sepia-web-audio.js

Lines changed: 24 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.9";
6+
WebAudio.version = "0.9.10";
77

88
//Preparations
99
var AudioContext = window.AudioContext || window.webkitAudioContext;
@@ -324,7 +324,7 @@ if (!(typeof SepiaFW == "object")){
324324
if (!event || event.data == undefined){
325325
//TODO: simply ignore?
326326
}else if (event.data.moduleState == 1){
327-
//STATE
327+
//STATE: READY
328328
thisProcessNode.isReady = true;
329329
completeInitCondition("module-" + i);
330330
if (event.data.moduleInfo) thisProcessNode.moduleInfo = event.data.moduleInfo;
@@ -336,6 +336,7 @@ if (!(typeof SepiaFW == "object")){
336336
completeCallback(initInfo);
337337
}
338338
}else if (event.data.moduleState == 9 && !thisProcessNode.isTerminated){
339+
//STATE: READY TO BE TERMINATED
339340
if (typeof thisProcessNode.terminate == "function"){
340341
try {
341342
thisProcessNode.isTerminated = true;
@@ -345,6 +346,11 @@ if (!(typeof SepiaFW == "object")){
345346
onError({name: "TerminateError", message: "Failed to terminate module", info: err});
346347
}
347348
}
349+
}else if (event.data.moduleState == 10){
350+
//STATE: CUSTOM INIT. ERROR
351+
event.data.error.target = event.target;
352+
onError(event.data.error);
353+
348354
}else if (event.data.moduleResponse){
349355
//RESPONSE to "on-demand" request
350356
//TODO: ignore?
@@ -365,14 +371,16 @@ if (!(typeof SepiaFW == "object")){
365371
if (moduleSetup.onmessage){
366372
moduleSetup.onmessage(event.data, processNodes);
367373
}
368-
};
374+
}
369375
function onError(err){
370376
//TODO: do something with 'completeInitCondition("module-" + i)' or abort whole processor?
371377
var errorMessage;
372378
if (err.message && err.message.indexOf("Uncaught {") == 0){
373379
err.preventDefault();
374380
errorMessage = JSON.parse(err.message.replace(/^Uncaught /, ""));
375381
err.message = errorMessage;
382+
}else{
383+
errorMessage = err;
376384
}
377385
onProcessorError({
378386
name: "AudioModuleProcessorException",
@@ -1425,9 +1433,21 @@ if (!(typeof SepiaFW == "object")){
14251433
}
14261434
return bytes;
14271435
}catch (error){
1428-
throw new Error("Converting base64 string to bytes failed.");
1436+
throw new Error("Converting base64 string to Uint8Array bytes array failed.");
14291437
}
14301438
}
1439+
function convertUint8ArrayToBase64String(uint8Array){
1440+
try {
1441+
return btoa(uint8Array.reduce(function(data, byte){ return data + String.fromCharCode(byte); }, ''));
1442+
}catch (error){
1443+
throw new Error("Converting Uint8Array to base64 string failed.");
1444+
}
1445+
}
1446+
//export
1447+
WebAudio.base64 = {
1448+
base64StringToUint8Array: convertBase64ToUint8Array,
1449+
uint8ArrayToBase64String: convertUint8ArrayToBase64String
1450+
}
14311451

14321452
//Add audio data as audio element to element on page (or body)
14331453
WebAudio.addAudioElementToPage = function(targetEle, audioData, audioType){

0 commit comments

Comments
 (0)