Skip to content
This repository has been archived by the owner on Jan 23, 2021. It is now read-only.

Commit

Permalink
fix firefox audio glitches by seperate period size from sab
Browse files Browse the repository at this point in the history
  • Loading branch information
hlolli committed Sep 21, 2020
1 parent b2ea1b1 commit 557a33e
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"printWidth": 100,
"tabWidth": 2,
"singleQuote": true,
"trailingComma": true,
"trailingComma": 'all',
"jsxBracketSameLine": false
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "csound-wasm",
"version": "6.14.0-6",
"version": "6.15.0-0",
"author": "Hlödver Sigurdsson <[email protected]>",
"license": "LGPL-3.0",
"main": "./dist/libcsound.mjs",
Expand Down
10 changes: 5 additions & 5 deletions src/filesystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,11 @@ function rmrfFsRec(rmrfPath) {
}
if (wasmFs.fs.existsSync(rmrfPathSandboxed)) {
wasmFs.fs.readdirSync(rmrfPathSandboxed).forEach(file => {
var curPath = path.join(rmrfPathSandboxed, file);
if (wasmFs.fs.lstatSync(curPath).isDirectory()) {
rmrfFsRec(curPath);
} else {
wasmFs.fs.unlinkSync(curPath);
var currentPath = path.join(rmrfPathSandboxed, file);
if (wasmFs.fs.lstatSync(currentPath).isDirectory()) {
rmrfFsRec(currentPath);
} else {
wasmFs.fs.unlinkSync(currentPath);
}
});
wasmFs.fs.rmdirSync(rmrfPathSandboxed);
Expand Down
4 changes: 2 additions & 2 deletions src/mains/messages.main.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ const safelyClosePorts = ([p1, p2]) => {
if (typeof p1.close !== 'undefined') {
try {
p1.close();
} catch (e) {}
} catch {}
}
if (typeof p2.close !== 'undefined') {
try {
p2.close();
} catch (e) {}
} catch {}
}
};

Expand Down
1 change: 0 additions & 1 deletion src/mains/vanilla.main.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
csoundWorkerFrameRequestPort,
} from '@root/mains/messages.main';

let isInitialized = false;

class VanillaWorkerMainThread {
constructor(audioWorker, wasmDataURI) {
Expand Down
21 changes: 8 additions & 13 deletions src/mains/worklet.main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@ import log, { logWorklet } from '@root/logger';
import {
audioWorkerAudioInputPort,
audioWorkerFrameRequestPort,
cleanupPorts,
emitInternalCsoundLogEvent,
workerMessagePortAudio,
restartMessagePortAudio,
restartWorkerFrameRequestPort,
restartWorkerAudioInputPort,
} from '@root/mains/messages.main';

const connectedMidiDevices = new Set();
Expand Down Expand Up @@ -48,9 +44,8 @@ class AudioWorkletMainThread {
logWorklet(
'event received: realtimePerformanceEnded' + !this.csoundWorkerMain.hasSharedArrayBuffer
? ` cleaning up Vanilla ports`
: ''
: '',
);
!this.csoundWorkerMain.hasSharedArrayBuffer; // && cleanupPorts(this.csoundWorkerMain);
this.audioCtx.close();
this.audioWorkletNode.disconnect();
delete this.audioWorkletNode;
Expand Down Expand Up @@ -109,7 +104,7 @@ class AudioWorkletMainThread {
return;
}

let createWorkletNode = (audoContext, inputsCount) => {
const createWorkletNode = (audoContext, inputsCount) => {
return new AudioWorkletNode(audoContext, 'csound-worklet-processor', {
numberOfInputs: 1,
numberOfOutputs: 1,
Expand Down Expand Up @@ -142,11 +137,11 @@ class AudioWorkletMainThread {
for (let input = midiInputs.next(); input && !input.done; input = midiInputs.next()) {
emitInternalCsoundLogEvent(
this.csoundWorkerMain,
`Connecting midi-input: ${input.value.name || 'unkown'}`
`Connecting midi-input: ${input.value.name || 'unkown'}`,
);
if (!connectedMidiDevices.has(input.value.name || 'unkown')) {
input.value.onmidimessage = this.csoundWorkerMain.handleMidiInput.bind(
this.csoundWorkerMain
this.csoundWorkerMain,
);
connectedMidiDevices.add(input.value.name || 'unkown');
}
Expand All @@ -157,13 +152,13 @@ class AudioWorkletMainThread {
} catch (error) {
emitInternalCsoundLogEvent(
this.csoundWorkerMain,
'error while connecting web-midi: ' + error
'error while connecting web-midi: ' + error,
);
}
} else {
emitInternalCsoundLogEvent(
this.csoundWorkerMain,
'no web-midi support found, midi-input will not work!'
'no web-midi support found, midi-input will not work!',
);
}
}
Expand All @@ -174,7 +169,7 @@ class AudioWorkletMainThread {
? navigator.mediaDevices.getUserMedia
: navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;

const microphoneCallback = stream => {
const microphoneCallback = (stream) => {
if (stream) {
const liveInput = newAudioContext.createMediaStreamSource(stream);
this.inputsCount = liveInput.channelCount;
Expand Down Expand Up @@ -207,7 +202,7 @@ class AudioWorkletMainThread {
},
},
microphoneCallback,
log.error
log.error,
);
} else {
const newNode = createWorkletNode(newAudioContext, 0);
Expand Down
17 changes: 9 additions & 8 deletions src/workers/worklet.worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { AUDIO_STATE, MAX_HARDWARE_BUFFER_SIZE } from '@root/constants';
import { instantiateAudioPacket } from '@root/workers/common.utils';
import { logWorklet } from '@root/logger';

const PERIODS = 3;
const SAB_PERIODS = 3;
const VANILLA_PERIODS = 4;

const workerMessagePort = {
ready: false,
Expand Down Expand Up @@ -38,7 +39,7 @@ function processSharedArrayBuffer(inputs, outputs) {

this.isPerformingLastTime = isPerforming;

if (this.preProcessCount < PERIODS && this.isPerformingLastTime && isPerforming) {
if (this.preProcessCount < SAB_PERIODS && this.isPerformingLastTime && isPerforming) {
Atomics.store(this.sharedArrayBuffer, AUDIO_STATE.ATOMIC_NOFITY, 1);
Atomics.notify(this.sharedArrayBuffer, AUDIO_STATE.ATOMIC_NOTIFY);
this.preProcessCount += 1;
Expand All @@ -51,7 +52,7 @@ function processSharedArrayBuffer(inputs, outputs) {
const hasWriteableInputChannels = writeableInputChannels.length > 0;
const availableOutputBuffers = Atomics.load(this.sharedArrayBuffer, AUDIO_STATE.AVAIL_OUT_BUFS);

if (availableOutputBuffers < this.softwareBufferSize * PERIODS) {
if (availableOutputBuffers < this.softwareBufferSize * SAB_PERIODS) {
Atomics.store(this.sharedArrayBuffer, AUDIO_STATE.ATOMIC_NOFITY, 1);
Atomics.notify(this.sharedArrayBuffer, AUDIO_STATE.ATOMIC_NOTIFY);
}
Expand Down Expand Up @@ -147,7 +148,7 @@ function processVanillaBuffers(inputs, outputs) {
});

if (this.inputsCount > 0 && hasWriteableInputChannels && writeableInputChannels[0].length > 0) {
const inputBufferLength = this.softwareBufferSize * PERIODS;
const inputBufferLength = this.softwareBufferSize * VANILLA_PERIODS;
writeableInputChannels.forEach((channelBuffer, channelIndex) => {
this.vanillaInputChannels[channelIndex].set(channelBuffer, this.vanillaInputReadIndex);
});
Expand Down Expand Up @@ -186,8 +187,8 @@ function processVanillaBuffers(inputs, outputs) {
}

if (
this.vanillaAvailableFrames < this.softwareBufferSize * PERIODS &&
this.pendingFrames < this.softwareBufferSize * PERIODS * 2
this.vanillaAvailableFrames < this.softwareBufferSize * VANILLA_PERIODS &&
this.pendingFrames < this.softwareBufferSize * VANILLA_PERIODS * 2
) {
const futureOutputReadIndex =
(this.vanillaAvailableFrames + nextOutputReadIndex + this.pendingFrames) %
Expand All @@ -198,9 +199,9 @@ function processVanillaBuffers(inputs, outputs) {
futureOutputReadIndex < this.hardwareBufferSize
? futureOutputReadIndex
: futureOutputReadIndex + 1,
numFrames: this.softwareBufferSize * PERIODS,
numFrames: this.softwareBufferSize * VANILLA_PERIODS
});
this.pendingFrames += this.softwareBufferSize * PERIODS;
this.pendingFrames += this.softwareBufferSize * VANILLA_PERIODS;
}

return true;
Expand Down

0 comments on commit 557a33e

Please sign in to comment.