From: Frédéric Péters Date: Tue, 21 Feb 2017 09:28:21 +0000 (+0100) Subject: add per-touch gain nodes X-Git-Url: https://git.0d.be/?p=nanofun.git;a=commitdiff_plain;h=678d5d34c64eefa7b8ee74e767b145660aed2ccc add per-touch gain nodes --- diff --git a/index.html b/index.html index a48ff02..2f6e87b 100644 --- a/index.html +++ b/index.html @@ -28,7 +28,26 @@
-
+
+ + + + + + + + + + + + + + + + + +
+
Source code at https://git.0d.be/?p=nanofun.git
diff --git a/nanofun.js b/nanofun.js index 5794cbe..9cf7773 100644 --- a/nanofun.js +++ b/nanofun.js @@ -296,8 +296,13 @@ var nanofun = function() { self.sample_buffers = Array(16); self.samples = Array(16); self.audioCtx = new window.AudioContext(); - self.gainNode = self.audioCtx.createGain(); - self.gainNode.connect(self.audioCtx.destination); + self.touchGainNodes = Array(16); + self.masterGainNode = self.audioCtx.createGain(); + for (var i=0; i<16; i++) { + self.touchGainNodes[i] = self.audioCtx.createGain(); + self.touchGainNodes[i].connect(self.masterGainNode); + } + self.masterGainNode.connect(self.audioCtx.destination); } self.initMIDI = function() { @@ -335,9 +340,10 @@ var nanofun = function() { } midi.onControlChange = function(port, data, control, value) { - if (control == 7) { - $('#gain').val(value).trigger('change'); - } + if (control > 7 && control < 16) return; /* range between sliders and pots */ + if (control > 23) return; /* after pots */ + if (control >= 16) { control -= 8; } + $('.touch-gain[data-touch=' + control + ']').val(value).trigger('change'); } $(document).keypress(function(ev) { @@ -347,9 +353,15 @@ var nanofun = function() { } }); - $('#gain').on('change', function() { + $('#master-gain').on('change', function() { + var fraction = parseInt(this.value) / parseInt(127); + self.masterGainNode.gain.value = fraction * fraction; + }); + + $('.touch-gain').on('change', function() { var fraction = parseInt(this.value) / parseInt(127); - self.gainNode.gain.value = fraction * fraction; + var touchIdx = parseInt($(this).data('touch')); + self.touchGainNodes[touchIdx].gain.value = fraction * fraction; }); } @@ -362,6 +374,7 @@ var nanofun = function() { self.samples[sample_idx] = undefined; } else { var sample = self.audioCtx.createBufferSource(); + var gainNode = self.touchGainNodes[sample_idx]; self.samples[sample_idx] = sample; sample.loop = false; sample.connect(gainNode);