Skip to content
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
46 changes: 17 additions & 29 deletions js/midi/plugin.webaudio.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

midi.programChange = function(channelId, program, delay) {
// if (delay) {
// return setTimeout(function() {
// setTimeout(function() {
// var channel = root.channels[channelId];
// channel.instrument = program;
// }, delay);
Expand All @@ -59,6 +59,9 @@
midi.noteOn = function(channelId, noteId, velocity, delay) {
delay = delay || 0;

/// convert relative delay to absolute delay
var startTime = delay < ctx.currentTime ? delay + ctx.currentTime : delay;

/// check whether the note exists
var channel = root.channels[channelId];
var instrument = channel.instrument;
Expand All @@ -68,11 +71,6 @@
// console.log(MIDI.GM.byId[instrument].id, instrument, channelId);
return;
}

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

/// create audio buffer
if (useStreamingBuffer) {
Expand Down Expand Up @@ -101,17 +99,12 @@
source.connect(source.gainNode);
///
if (useStreamingBuffer) {
if (delay) {
return setTimeout(function() {
buffer.currentTime = 0;
buffer.play()
}, delay * 1000);
} else {
setTimeout(function() {
buffer.currentTime = 0;
buffer.play()
}
}, delay * 1000);
} else {
source.start(delay || 0);
source.start(startTime);
}
///
sources[channelId + '' + noteId] = source;
Expand All @@ -122,40 +115,35 @@
midi.noteOff = function(channelId, noteId, delay) {
delay = delay || 0;

/// convert relative delay to absolute delay
var startTime = delay < ctx.currentTime ? delay + ctx.currentTime : delay;

/// check whether the note exists
var channel = root.channels[channelId];
var instrument = channel.instrument;
var bufferId = instrument + '' + noteId;
var buffer = audioBuffers[bufferId];
if (buffer) {
if (delay < ctx.currentTime) {
delay += ctx.currentTime;
}
///
var source = sources[channelId + '' + noteId];
if (source) {
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
var 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(function() {
buffer.pause();
}, delay * 1000);
} else {
setTimeout(function() {
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 @@ -323,4 +311,4 @@
return new (window.AudioContext || window.webkitAudioContext)();
};
})();
})(MIDI);
})(MIDI);