Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Delay timings #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 20 additions & 35 deletions js/plugin.webaudio.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,6 @@ export const noteOn = (channelId, noteId, velocity, delay=0) => {
}


// convert relative delay to absolute delay
if (delay < ctx.currentTime) {
delay += ctx.currentTime;
}

// create audio buffer
let source;
if (useStreamingBuffer) {
Expand Down Expand Up @@ -121,17 +116,16 @@ export const noteOn = (channelId, noteId, velocity, delay=0) => {
source.connect(source.gainNode);

if (useStreamingBuffer) {
if (delay) {
return setTimeout(() => {
buffer.currentTime = 0;
buffer.play();
}, delay * 1000);
} else {
setTimeout(() => {
buffer.currentTime = 0;
buffer.play();
}
}, delay * 1000);
} else {
source.start(delay || 0);
source.start(
delay < ctx.currentTime ?
delay + ctx.currentTime :
delay
);
}

sources[channelId + 'x' + noteId] = source;
Expand All @@ -153,33 +147,27 @@ export const noteOff = (channelId, noteId, delay=0) => {
}

if (buffer) {
if (delay < ctx.currentTime) {
delay += ctx.currentTime;
}

const source = sources[channelId + 'x' + noteId];
if (source) {
const startTime = delay < ctx.currentTime ? delay + ctx.currentTime : delay;

if (source.gainNode) {
// @Miranet: 'the values of 0.2 and 0.3 could of course be used as
// a 'release' parameter for ADSR like time settings.'
// add { 'metadata': { release: 0.3 } } to soundfont files
const gain = source.gainNode.gain;
gain.linearRampToValueAtTime(gain.value, delay);
gain.linearRampToValueAtTime(-1.0, delay + 0.3);
gain.linearRampToValueAtTime(gain.value, startTime);
gain.linearRampToValueAtTime(-1.0, startTime + 0.3);
}
if (useStreamingBuffer) {
if (delay) {
setTimeout(() => {
buffer.pause();
}, delay * 1000);
} else {
setTimeout(() => {
buffer.pause();
}
}, delay * 1000);
} else {
if (source.noteOff) {
source.noteOff(delay + 0.5);
source.noteOff(startTime + 0.5);
} else {
source.stop(delay + 0.5);
source.stop(startTime + 0.5);
}
}

Expand Down Expand Up @@ -217,17 +205,14 @@ export const stopAllNotes = () => {
}

for (const sid of Object.keys(sources)) {
let delay = 0;
if (delay < ctx.currentTime) {
delay += ctx.currentTime;
}
const startTime = ctx.currentTime;
const source = sources[sid];
source.gain.linearRampToValueAtTime(1, delay);
source.gain.linearRampToValueAtTime(0, delay + 0.3);
source.gain.linearRampToValueAtTime(1, startTime);
source.gain.linearRampToValueAtTime(0, startTime + 0.3);
if (source.noteOff) { // old api
source.noteOff(delay + 0.3);
source.noteOff(startTime + 0.3);
} else { // new api
source.stop(delay + 0.3);
source.stop(startTime + 0.3);
}
delete sources[sid];
}
Expand Down