]> git.0d.be Git - nanofun.git/blobdiff - nanofun.js
add per-touch gain nodes
[nanofun.git] / nanofun.js
index 5794cbe41e64dd28a3e1e16ee6a2083c6f338966..9cf77735bcc10948e1f28040a878b22f32baa604 100644 (file)
@@ -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);