diff --git a/js/midi/gm.js b/js/midi/gm.js index 2e1278ca..42961510 100644 --- a/js/midi/gm.js +++ b/js/midi/gm.js @@ -123,6 +123,24 @@ channel.solo = truthy; } }; + + /* get/setChannelVolume + --------------------------------------------------- */ + root.getChannelVolume = function(channelId) { + var channel = root.channels[channelId]; + return channel && channel.volume; + }; + + root.setChannelVolume = function(channelId, truthy) { + var channel = root.channels[channelId]; + if (delay) { + return setTimeout(function() { + channel.volume = truthy; + }, delay); + } else { + channel.volume = truthy; + } + }; /* channels --------------------------------------------------- */ @@ -132,6 +150,7 @@ channels[i] = { // default values instrument: i, pitchBend: 0, + volume: 127, mute: false, mono: false, omni: false, diff --git a/js/midi/plugin.audiotag.js b/js/midi/plugin.audiotag.js index ec361c68..3fa2e20b 100644 --- a/js/midi/plugin.audiotag.js +++ b/js/midi/plugin.audiotag.js @@ -11,7 +11,6 @@ window.Audio && (function() { var midi = root.AudioTag = { api: 'audiotag' }; var noteToKey = {}; - var volume = 127; // floating point var buffer_nid = -1; // current channel var audioBuffers = []; // the audio channels var notesOn = []; // instrumentId + noteId that is currently playing in each 'channel', for routing noteOff/chordOff calls @@ -37,7 +36,7 @@ return; } audio.src = root.Soundfont[instrumentId][note.id]; - audio.volume = volume / 127; + audio.volume = root.channels[channel].volume / 127; audio.play(); buffer_nid = nid; } @@ -64,9 +63,17 @@ midi.audioBuffers = audioBuffers; midi.send = function(data, delay) { }; - midi.setController = function(channel, type, value, delay) { }; - midi.setVolume = function(channel, n) { - volume = n; //- should be channel specific volume + midi.setController = function(channel, type, value, delay) { + if(type == 7) root.setVolume(channel, value, delay); + }; + midi.setVolume = function(channel, n, delay) { + if (delay) { + return setTimeout(function() { + root.channels[channel].volume = n; + }, delay * 1000); + } else { + root.channels[channel].volume = n; + } }; midi.programChange = function(channel, program) { diff --git a/js/midi/plugin.webaudio.js b/js/midi/plugin.webaudio.js index 15a6f0a7..06d22024 100644 --- a/js/midi/plugin.webaudio.js +++ b/js/midi/plugin.webaudio.js @@ -15,21 +15,24 @@ var ctx; // audio context var sources = {}; var effects = {}; - var masterVolume = 127; var audioBuffers = {}; /// midi.audioBuffers = audioBuffers; midi.send = function(data, delay) { }; - midi.setController = function(channelId, type, value, delay) { }; + midi.setController = function(channelId, type, value, delay) { + if(type == 7) root.setVolume(channelId, value, delay); + }; midi.setVolume = function(channelId, volume, delay) { - if (delay) { - setTimeout(function() { - masterVolume = volume; - }, delay * 1000); - } else { - masterVolume = volume; - } +// if (delay) { +// setTimeout(function() { +// var channel = root.channels[channelId]; +// channel.volume = volume; +// }, delay * 1000); +// } else { + var channel = root.channels[channelId]; + channel.volume = volume; +// } }; midi.programChange = function(channelId, program, delay) { @@ -92,7 +95,8 @@ } /// add gain + pitchShift - var gain = (velocity / 127) * (masterVolume / 127) * 2 - 1; + var channel = root.channels[channelId]; + var gain = ((velocity / 127) * (channel.volume / 127) * 2) - 1; source.connect(ctx.destination); source.playbackRate.value = 1; // pitch shift source.gainNode = ctx.createGain(); // gain