Skip to content

Commit c61113d

Browse files
committed
improved logs and debugging
1 parent ba50f39 commit c61113d

File tree

7 files changed

+88
-19
lines changed

7 files changed

+88
-19
lines changed

src/modules/buffer-switch.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ class BufferProcessor extends AudioWorkletProcessor {
2020
super();
2121

2222
let that = this;
23+
this.moduleId = "buffer-switch-" + Math.round(Math.random() * 1000000) + "-" + Date.now();
24+
this.isReadyForProcessing = false;
2325
this.EXPECTED_SAMPLE_SIZE = 128; //currently 128, but might change in future ... and even become variable! (I hope not)
26+
2427
this.sourceSamplerate = options.processorOptions.ctxInfo.sampleRate; //INFO: should be same as global scope 'sampleRate'
2528

2629
this.emitterBufferSize = options.processorOptions.bufferSize || 512;
@@ -42,9 +45,11 @@ class BufferProcessor extends AudioWorkletProcessor {
4245
init();
4346

4447
function ready(){
48+
that.isReadyForProcessing = true;
4549
that.port.postMessage({
4650
moduleState: 1,
4751
moduleInfo: {
52+
moduleId: that.moduleId,
4853
sourceSampleRate: that.sourceSamplerate,
4954
emitterBufferSize: that.emitterBufferSize,
5055
channelCount: that.channelCount,
@@ -132,6 +137,11 @@ class BufferProcessor extends AudioWorkletProcessor {
132137
}
133138

134139
process(inputs, outputs, parameters) {
140+
if (!this.isReadyForProcessing){
141+
console.error("Buffer module wasn't ready for processing! Input was ignored!", "-", this.moduleId);
142+
return;
143+
}
144+
135145
//Use 1st input and output only
136146
let input = inputs[0];
137147
let output = outputs[0];

src/modules/porcupine-wake-word-worker.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ onmessage = function(e){
4949
}
5050
};
5151

52+
let workerId = "porcupine-wake-word-worker-" + Math.round(Math.random() * 1000000) + "-" + Date.now();
53+
//let doDebug = false;
54+
//let wasConstructorCalled = false;
55+
//let isReadyForProcessing = false; //TODO: implement this ?
56+
5257
let porcupine;
5358
let porcupineVersion;
5459
let _Porcupine;
@@ -88,6 +93,7 @@ function ready(){
8893
postMessage({
8994
moduleState: 1,
9095
moduleInfo: {
96+
moduleId: workerId,
9197
inputSampleRate: inputSampleRate,
9298
channelCount: channelCount,
9399
inputSampleSize: inputSampleSize,
@@ -130,11 +136,10 @@ function constructWorker(options) {
130136

131137
porcupineVersion = options.setup.version || options.setup.porcupineVersion || 19;
132138
porcupineVersion = porcupineVersion.replace(".", "").trim(); //remove dot
133-
//importScripts('./picovoice/porcupine-wasm-module-' + porcupineVersion + '.js');
134139
if (porcupineVersion <= 16){
135140
importScripts('./picovoice/porcupine-wasm-module-' + "14" + '.js'); //we assume this works for 14-16?
136141
}else{
137-
importScripts('./picovoice/porcupine-wasm-module-' + "19" + '.js'); //this will probably fail for 17 and 18 (not supported)
142+
importScripts('./picovoice/porcupine-wasm-module-' + "19" + '.js');
138143
}
139144

140145
keywords = options.setup.keywords || ["Computer"];

src/modules/sepia-vad-worker.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ onmessage = function(e) {
3737
}
3838
};
3939

40+
let workerId = "speia-vad-worker-" + Math.round(Math.random() * 1000000) + "-" + Date.now();
41+
//let doDebug = false;
42+
//let wasConstructorCalled = false;
43+
//let isReadyForProcessing = false; //TODO: implement this ?
44+
4045
let inputSampleRate;
4146
let channelCount;
4247
let inputSampleSize;
@@ -173,6 +178,7 @@ function ready(){
173178
postMessage({
174179
moduleState: 1,
175180
moduleInfo: {
181+
moduleId: workerId,
176182
inputSampleRate: inputSampleRate,
177183
channelCount: channelCount,
178184
inputSampleSize: inputSampleSize,

src/modules/speex-resample-switch.js

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

src/modules/speex-resample-worker.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var speexModule;
88

99
onmessage = function(e) {
1010
//Audio worker interface
11-
//console.log("SpeexResamplerWorker onmessage", e.data); //DEBUG
11+
//if (doDebug) console.log("SpeexResampleWorker - onmessage", e.data); //DEBUG
1212
if (e.data.ctrl){
1313
switch (e.data.ctrl.action){
1414
case "construct":
@@ -34,12 +34,17 @@ onmessage = function(e) {
3434
release(e.data.ctrl.options);
3535
break;
3636
default:
37-
console.log("Unknown control message:", e.data);
37+
console.log("SpeexResampleWorker - Unknown control message:", e.data);
3838
break;
3939
}
4040
}
4141
};
4242

43+
let workerId = "speex-resample-worker-" + Math.round(Math.random() * 1000000) + "-" + Date.now();
44+
let doDebug = false;
45+
let wasConstructorCalled = false;
46+
let isReadyForProcessing = false;
47+
4348
let sourceSamplerate;
4449
let inputSampleSize;
4550
let targetSampleRate;
@@ -89,9 +94,11 @@ function ready(skipResampler){
8994
channelCount, sourceSamplerate, targetSampleRate, resampleQuality
9095
);
9196
}
97+
isReadyForProcessing = true;
9298
postMessage({
9399
moduleState: 1,
94100
moduleInfo: {
101+
moduleId: workerId,
95102
sourceSamplerate: sourceSamplerate,
96103
inputSampleSize: inputSampleSize,
97104
targetSampleRate: targetSampleRate,
@@ -105,6 +112,13 @@ function ready(skipResampler){
105112
}
106113

107114
function constructWorker(options) {
115+
if (wasConstructorCalled){
116+
console.error("SpeexResampleWorker - Constructor was called twice! 2nd call was ignored but this should be fixed!", "-", workerId); //DEBUG
117+
return;
118+
}else{
119+
wasConstructorCalled = true;
120+
}
121+
doDebug = options.setup.doDebug || false;
108122
sourceSamplerate = options.setup.ctxInfo.sampleRate;
109123
inputSampleSize = options.setup.inputSampleSize || 512;
110124
targetSampleRate = options.setup.targetSampleRate || options.setup.ctxInfo.targetSampleRate || 16000;
@@ -113,14 +127,14 @@ function constructWorker(options) {
113127
channelCount = 1; //options.setup.channelCount || 1; //TODO: only MONO atm
114128

115129
calculateRmsVolume = (options.setup.calculateRmsVolume != undefined)? options.setup.calculateRmsVolume : true;
116-
gain = options.setup.gain || 1.0;
130+
gain = options.setup.gain || 1.0; //TODO: keep?
117131

118132
resamplingMode = (targetSampleRate < sourceSamplerate? -1 : (targetSampleRate > sourceSamplerate? 1 : 0));
119133
resampleRatio = targetSampleRate/sourceSamplerate;
120134
init();
121135

122136
function onSpeexLog(msg){
123-
console.error("SpeexModuleLog -", msg); //DEBUG (use postMessage?)
137+
if (doDebug) console.error("SpeexResampleWorker - SpeexModuleLog -", msg, "-", workerId); //DEBUG (use postMessage?)
124138
}
125139
//function onSpeexError(msg){} //TODO: we could wrap the 'resampler.processChunk' function in try-catch and log the error here
126140

@@ -155,6 +169,10 @@ function getEmitterRms() {
155169
}
156170

157171
function process(data) {
172+
if (!isReadyForProcessing){
173+
console.error("SpeexResampleWorker - Module wasn't ready for processing! Input was ignored!", "-", workerId); //DEBUG
174+
return;
175+
}
158176
//expected: data.samples, data.sampleRate, data.targetSampleRate, data.channels, data.type
159177
if (data && data.samples){
160178
//Use 1st input and output only
@@ -166,7 +184,7 @@ function process(data) {
166184
//check inputSampleSize - TODO: this requires constant size? adapt to first value?
167185
if (thisInputSampleSize != inputSampleSize){
168186
let msg = "Sample size is: " + thisInputSampleSize + ", expected: " + inputSampleSize + ". Need code adjustments!";
169-
console.error("SpeexResampler sample size exception - Msg.: " + msg);
187+
console.error("Audio Worker sample size exception - Msg.: " + msg);
170188
throw new SampleSizeException(msg); //TODO: same as above, this probably needs to be a string to show up in worker.onerror properly :-/
171189
}
172190
}
@@ -249,6 +267,7 @@ function release(options){
249267
_newOutputBuffer = null;
250268
resampler = null;
251269
speexModule = null;
270+
isReadyForProcessing = false;
252271
}
253272

254273
//--- helpers ---

src/modules/wave-encoder-worker.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,17 @@ onmessage = function(e) {
2929
release(e.data.ctrl.options);
3030
break;
3131
default:
32-
console.error("Unknown control message:", e.data);
32+
console.error("WaveEncoderWorker - Unknown control message:", e.data);
3333
break;
3434
}
3535
}
3636
//custom interface
3737
if (e.data.gate != undefined){
38-
console.error("Message", e.data); //DEBUG
38+
if (doDebug) console.error("WaveEncoderWorker - DEBUG - Message", e.data); //DEBUG
3939
gateControl(e.data.gate && e.data.gate == "open", e.data.gateOptions);
4040
}
4141
if (e.data.request){
42-
console.error("Message", e.data); //DEBUG
42+
if (doDebug) console.error("WaveEncoderWorker - DEBUG - Message", e.data); //DEBUG
4343
if (e.data.request.get){
4444
switch (e.data.request.get){
4545
case "buffer":
@@ -49,7 +49,7 @@ onmessage = function(e) {
4949
getWave(e.data.request.start, e.data.request.end);
5050
break;
5151
default:
52-
console.log("Unknown request message:", e.data);
52+
console.log("WaveEncoderWorker - Unknown request message:", e.data);
5353
break;
5454
}
5555
}else if (e.data.request.clear){
@@ -58,17 +58,21 @@ onmessage = function(e) {
5858
//TODO: clear buffer and release lookback lock
5959
break;
6060
default:
61-
console.log("Unknown request message:", e.data);
61+
console.log("WaveEncoderWorker - Unknown request message:", e.data);
6262
break;
6363
}
6464
}else{
65-
console.log("Unknown request message:", e.data);
65+
console.log("WaveEncoderWorker - Unknown request message:", e.data);
6666
}
6767
}else if (e.data.encode && e.data.encode.data){
6868
encodeInterface(e.data.encode);
6969
}
7070
};
7171

72+
let workerId = "wave-encoder-worker-" + Math.round(Math.random() * 1000000) + "-" + Date.now();
73+
let doDebug = false;
74+
let wasConstructorCalled = false;
75+
7276
let inputSampleRate;
7377
let inputSampleSize;
7478
let channelCount;
@@ -186,6 +190,13 @@ function gateControl(open, gateOptions){
186190
//Interface
187191

188192
function constructWorker(options){
193+
if (wasConstructorCalled){
194+
console.error("WaveEncoderWorker - Constructor was called twice! 2nd call was ignored but this should be fixed!", "-", workerId); //DEBUG
195+
return;
196+
}else{
197+
wasConstructorCalled = true;
198+
}
199+
doDebug = options.setup.doDebug || false;
189200
inputSampleRate = options.setup.inputSampleRate || options.setup.ctxInfo.targetSampleRate || options.setup.ctxInfo.sampleRate;
190201
inputSampleSize = options.setup.inputSampleSize || 512;
191202
channelCount = 1; //options.setup.channelCount || 1; //TODO: only MONO atm
@@ -210,6 +221,7 @@ function constructWorker(options){
210221
postMessage({
211222
moduleState: 1,
212223
moduleInfo: {
224+
moduleId: workerId,
213225
inputSampleRate: inputSampleRate,
214226
inputSampleSize: inputSampleSize,
215227
inputIsFloat32: isFloat32Input,
@@ -303,7 +315,7 @@ function buildBuffer(start, end){
303315
var isFloat32;
304316
if (recordedBuffers[0]){
305317
isFloat32 = (recordedBuffers[0] && recordedBuffers[0].constructor.name.indexOf("Float32") >= 0);
306-
console.error("isFloat32", isFloat32, recordedBuffers[0].constructor.name);
318+
if (doDebug) console.error("WaveEncoderWorker - DEBUG - isFloat32", isFloat32, recordedBuffers[0].constructor.name);
307319
}
308320
var lookbackSamples;
309321
if (_lookbackRingBuffer && _lookbackRingBuffer.framesAvailable){
@@ -326,7 +338,7 @@ function buildBuffer(start, end){
326338
n++;
327339
}
328340
}
329-
console.error("buffer mismatch", n, dataLength); //TODO: why does this always match?
341+
if (doDebug) console.error("WaveEncoderWorker - DEBUG - buffer mismatch", n, dataLength); //TODO: why does this always match?
330342
//TODO: we clear lookback buffer here ... so we should clear everything
331343
lookbackBufferNeedsReset = false;
332344

@@ -338,7 +350,7 @@ function buildBuffer(start, end){
338350

339351
function encodeWAV(samples, sampleRate, numChannels, convertFromFloat32){
340352
if (!samples || !sampleRate || !numChannels){
341-
console.error("Wave Encoder Worker - encodeWAV - Missing parameters");
353+
console.error("WaveEncoderWorker - encodeWAV - Missing parameters");
342354
return;
343355
}
344356
//Format description: http://soundfile.sapp.org/doc/WaveFormat/

src/modules/webrtc-vad-worker.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ onmessage = function(e) {
4141
}
4242
};
4343

44+
let workerId = "webrtc-vad-worker-" + Math.round(Math.random() * 1000000) + "-" + Date.now();
45+
//let doDebug = false;
46+
//let wasConstructorCalled = false;
47+
//let isReadyForProcessing = false; //TODO: implement
48+
4449
let inputSampleRate;
4550
let channelCount;
4651
let inputSampleSize;
@@ -117,6 +122,7 @@ function ready(){
117122
postMessage({
118123
moduleState: 1,
119124
moduleInfo: {
125+
moduleId: workerId,
120126
inputSampleRate: inputSampleRate,
121127
channelCount: channelCount,
122128
inputSampleSize: inputSampleSize,

0 commit comments

Comments
 (0)