]> git.0d.be Git - nanofun.git/blobdiff - nanofun.js
ramp to values to avoid audio glitches
[nanofun.git] / nanofun.js
index 9cf77735bcc10948e1f28040a878b22f32baa604..b8dcaf96721bd3ad094f32bd6cd8a26b54cce0f5 100644 (file)
@@ -2,7 +2,7 @@ var NANOPAD_TOUCHS = Array(37, 39, 41, 43, 45, 47, 49, 51,
                            36, 38, 40, 42, 44, 46, 48, 50);
 
 /* on French/Belgian keyboards, emulate pad touches with keypresses */
-var KEYBOARD_CODES = Array('a', 'z', 'e', 'r', 't', 'u', 'i', 'o',
+var KEYBOARD_CODES = Array('a', 'z', 'e', 'r', 't', 'y', 'u', 'i',
                            'q', 's', 'd', 'f', 'g', 'h', 'j', 'k');
 
 var midi = {
@@ -54,10 +54,6 @@ onMIDIAccessChange: function(e) {
     console.log('on midi access change', e);
     //console.log(this);
     var port = e.port;
-    var portContainer = $("#midi" + port.type + "s");
-    if (portContainer.html().startsWith("<p>No connected")) {
-        portContainer.empty();
-    }
 
     if (port.state == "disconnected") {
       if (port.type == "input") {
@@ -65,12 +61,17 @@ onMIDIAccessChange: function(e) {
       } else {
         this.outputs[port.name] = undefined;
       }
+      if (port.name == 'nanoPAD2 MIDI 1' || port.name == 'nanoPAD2 2 PAD') { $('#devices .nanopad').removeClass('on'); }
+      if (port.name == 'nanoKONTROL2 MIDI 1' || port.name == 'nanoKONTROL2 2 SLIDER/KNOB') { $('#devices .nanokontrol').removeClass('on'); }
     } else {
       if (port.type == "input") {
         if (this.inputs[port.name] === undefined) { this.registerPort(port); }
       } else {
         if (this.outputs[port.name] === undefined) { this.registerPort(port); }
       }
+            console.log('hello:', port.name);
+      if (port.name == 'nanoPAD2 MIDI 1' || port.name == 'nanoPAD2 2 PAD') { $('#devices .nanopad').addClass('on'); }
+      if (port.name == 'nanoKONTROL2 MIDI 1' || port.name == 'nanoKONTROL2 2 SLIDER/KNOB') { $('#devices .nanokontrol').addClass('on'); }
       this.renderPort(port);
     }
 },
@@ -89,17 +90,18 @@ registerPort: function(port) {
     } else {
         this.outputs[port.name] = port;
 
-        if (port.name == 'nanoKONTROL2 MIDI 1') {
+        if (port.name == 'nanoKONTROL2 MIDI 1' || port.name == 'nanoKONTROL2 2 SLIDER/KNOB') {
+          var port_name = port.name;
           /* turn "external leds" mode on.
            *
            * The sysex dump has been recorded from the official Korg kontrol
            * editor by the Overtone project, original code at:
            * https://github.com/overtone/overtone/blob/master/src/overtone/device/midi/nanoKONTROL2.clj */
-          device("nanoKONTROL2 MIDI 1").raw([240, 126, 127, 6, 1, 247]);
-          device("nanoKONTROL2 MIDI 1").raw([240, 66, 64, 0, 1, 19, 0, 31, 18, 0, 247]);
-          device("nanoKONTROL2 MIDI 1").raw([240, 126, 127, 6, 1, 247]);
+          device(port_name).raw([240, 126, 127, 6, 1, 247]);
+          device(port_name).raw([240, 66, 64, 0, 1, 19, 0, 31, 18, 0, 247]);
+          device(port_name).raw([240, 126, 127, 6, 1, 247]);
 
-          device("nanoKONTROL2 MIDI 1").raw([ 240, 66, 64, 0, 1, 19, 0, 127, 127,
+          device(port_name).raw([ 240, 66, 64, 0, 1, 19, 0, 127, 127,
                 2, 3, 5, 64, 0, 0, 0, 1, 16, 1, 0, 0, 0, 0, 127, 0, 1, 0, 16,
                 0, 0, 127, 0, 1, 0, 32, 0, 127, 0, 0, 1, 0, 48, 0, 127, 0, 0,
                 1, 0, 64, 0, 127, 0, 16, 0, 1, 0, 1, 0, 127, 0, 1, 0, 0, 17, 0,
@@ -123,11 +125,11 @@ registerPort: function(port) {
                 127, 0, 127, 127, 127, 127, 0, 127, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                 0, 0, 0, 0, 0, 0, 0, 0, 0, 247]);
 
-          device("nanoKONTROL2 MIDI 1").raw([240, 126, 127, 6, 1, 247]);
-          device("nanoKONTROL2 MIDI 1").raw([240, 66, 64, 0, 1, 19, 0, 31, 17, 0, 247]);
+          device(port_name).raw([240, 126, 127, 6, 1, 247]);
+          device(port_name).raw([240, 66, 64, 0, 1, 19, 0, 31, 17, 0, 247]);
 
-          function on(note) { device("nanoKONTROL2 MIDI 1").cc(note, 127); }
-          function off(note) { device("nanoKONTROL2 MIDI 1").cc(note, 0); }
+          function on(note) { device(port_name).cc(note, 127); }
+          function off(note) { device(port_name).cc(note, 0); }
           var leds = Array(43, 44, 42, 41, 45);
           for (var i=0; i<8; i++) {
             leds.push(64+i);
@@ -293,16 +295,34 @@ var nanofun = function() {
   var self = this;
 
   self.initAudio = function() {
-    self.sample_buffers = Array(16);
-    self.samples = Array(16);
+    self.sample_buffers = Array(NANOPAD_TOUCHS.length);
+    self.samples = Array(NANOPAD_TOUCHS.length);
+    self.sample_start_times = Array(NANOPAD_TOUCHS.length);
     self.audioCtx = new window.AudioContext();
-    self.touchGainNodes = Array(16);
+    self.touchGainNodes = Array(NANOPAD_TOUCHS.length);
     self.masterGainNode = self.audioCtx.createGain();
-    for (var i=0; i<16; i++) {
+    self.effectsGainNode = self.audioCtx.createGain();
+    for (var i=0; i<NANOPAD_TOUCHS.length; i++) {
       self.touchGainNodes[i] = self.audioCtx.createGain();
       self.touchGainNodes[i].connect(self.masterGainNode);
     }
     self.masterGainNode.connect(self.audioCtx.destination);
+
+    self.delay = self.audioCtx.createDelay(maxDelayTime=5);
+    self.delay.delayTime.value = 0.5;
+
+    self.feedback = self.audioCtx.createGain();
+    self.feedback.gain.value = 0.8;
+
+    self.filter = self.audioCtx.createBiquadFilter();
+    self.filter.frequency.value = 1000;
+
+    self.effectsGainNode.connect(self.delay);
+    self.delay.connect(self.feedback);
+    self.feedback.connect(self.filter);
+    self.filter.connect(self.delay);
+
+    self.filter.connect(self.masterGainNode);
   }
 
   self.initMIDI = function() {
@@ -316,23 +336,37 @@ var nanofun = function() {
 
   self.initUI = function() {
     var $nanopad = $('#nanopad');
-
-    $('.nanotouch input').on('change', function(ev) {
-      var nanotouch = $(this).parent();
-      var sample_idx = $nanopad.children().index(nanotouch);
-      var reader = new FileReader();
-      reader.onload = function(e) {
-        self.audioCtx.decodeAudioData(this.result, function(buffer) {
-          sample_buffers[sample_idx] = buffer;
-          $(nanotouch).find('span.duration').text(parseInt(buffer.duration) + 's');
-          $(nanotouch).removeClass('error').addClass('loaded');
-        }, function(e) {
-          $(nanotouch).find('span').text('');
-          $(nanotouch).removeClass('loaded').addClass('error');
-        });
+    var $nanotouch = $('.nanotouch');
+    for (var i=0; i<NANOPAD_TOUCHS.length; i++) {
+      var $new_touch = $nanotouch.clone();
+      $new_touch.attr('data-touch', i);
+      $new_touch.appendTo($nanopad);
+    }
+    $nanotouch.remove(); /* remove template */
+
+    $('.nanotouch input[type=file]').on('change', function(ev) {
+      var sample_idx = parseInt($(this).parent().data('touch'));
+      for (var i=0; i<this.files.length; i++) {
+        var reader = new FileReader();
+        var nanotouch = $('.nanotouch')[sample_idx + i];
+        reader.onload = function(e) {
+          var $nanotouch = $(this.nanotouch);
+          var sample_idx = this.sample_idx;
+          self.audioCtx.decodeAudioData(this.result, function(buffer) {
+            sample_buffers[sample_idx] = buffer;
+            $nanotouch.find('span.duration').data('duration', buffer.duration);
+            $nanotouch.find('span.duration').text(parseInt(buffer.duration) + 's');
+            $nanotouch.removeClass('error').addClass('loaded');
+          }, function(e) {
+            $nanotouch.find('span').text('');
+            $nanotouch.removeClass('loaded').addClass('error');
+          });
+        }
+        reader.nanotouch = nanotouch;
+        reader.sample_idx = sample_idx + i;
+        reader.readAsArrayBuffer(this.files[i]);
+        $(nanotouch).find('span.name').text(this.files[i].name);
       }
-      reader.readAsArrayBuffer(this.files[0]);
-      $(nanotouch).find('span.name').text(this.files[0].name);
     });
 
     midi.onTouchOn = function(port, data, sample_idx) {
@@ -341,9 +375,26 @@ var nanofun = function() {
 
     midi.onControlChange = function(port, data, control, value) {
       if (control > 7 && control < 16) return; /* range between sliders and pots */
+      if (control >= 32 && control < 40) { /* "S" buttons */
+          var nanotouch = $('.nanotouch')[control-32];
+          if (value == 127) {
+            var checked = $(nanotouch).find('.loop input').prop('checked');
+            if (checked) {
+              $(nanotouch).find('.loop input').prop('checked', false);
+              device("nanoKONTROL2 MIDI 1").cc(control, 0);
+            } else {
+              $(nanotouch).find('.loop input').prop('checked', true);
+              device("nanoKONTROL2 MIDI 1").cc(control, 127);
+            }
+          }
+      }
       if (control > 23) return; /* after pots */
-      if (control >= 16) { control -= 8; }
-      $('.touch-gain[data-touch=' + control + ']').val(value).trigger('change');
+      if (control < 8) {
+        control += 8; /* sliders, control bottom pads (8-15) */
+      } else {
+        control -= 16; /* pots, control top pads (0-7) */
+      }
+      $('[data-touch=' + control + '] .touch-gain').val(value).trigger('change');
     }
 
     $(document).keypress(function(ev) {
@@ -353,16 +404,62 @@ var nanofun = function() {
       }
     });
 
+    $('.effects input').on('change', function() {
+      var effects = $(this).prop('checked');
+      var i = parseInt($(this).parents('.nanotouch').data('touch'));
+      if (effects) {
+        self.touchGainNodes[i].connect(self.effectsGainNode);
+      } else {
+        self.touchGainNodes[i].disconnect(self.effectsGainNode);
+      }
+    });
+
     $('#master-gain').on('change', function() {
       var fraction = parseInt(this.value) / parseInt(127);
-      self.masterGainNode.gain.value = fraction * fraction;
+      var now = self.audioCtx.currentTime;
+      self.masterGainNode.gain.exponentialRampToValueAtTime(fraction * fraction, now + 0.015);
+    });
+
+    $('#delay').on('change', function() {
+      var value = this.value;
+      if (value == 0) {
+        self.delay.disconnect();
+      } else {
+        var now = self.audioCtx.currentTime;
+        self.delay.delayTime.exponentialRampToValueAtTime(value, now + 0.015);
+        self.delay.connect(self.audioCtx.destination);
+      }
+    });
+
+    $('#feedback').on('change', function() {
+      var now = self.audioCtx.currentTime;
+      self.feedback.gain.exponentialRampToValueAtTime(this.value, now + 0.015);
+    });
+
+    $('#filter').on('change', function() {
+      self.filter.frequency.value = this.value;
     });
 
     $('.touch-gain').on('change', function() {
       var fraction = parseInt(this.value) / parseInt(127);
-      var touchIdx = parseInt($(this).data('touch'));
-      self.touchGainNodes[touchIdx].gain.value = fraction * fraction;
+      var touchIdx = parseInt($(this).parent().data('touch'));
+      var now = self.audioCtx.currentTime;
+      self.touchGainNodes[touchIdx].gain.exponentialRampToValueAtTime(fraction * fraction, now + 0.015);
     });
+
+    self.time_interval_id = setInterval(function() {
+      for (var i=0; i<NANOPAD_TOUCHS.length; i++) {
+        var sample = self.samples[i];
+        if (sample !== undefined) {
+          var start_time = self.sample_start_times[i];
+          var current_position = sample.context.currentTime - start_time;
+          var nanotouch = $('.nanotouch')[i];
+          var duration = $(nanotouch).find('span.duration');
+          var total_duration = parseFloat($(duration).data('duration'))
+          $(duration).text(parseInt(total_duration - current_position) + 's');
+        }
+     }
+    }, 250);
   }
 
   self.startSample = function(sample_idx) {
@@ -376,15 +473,17 @@ var nanofun = function() {
         var sample = self.audioCtx.createBufferSource();
         var gainNode = self.touchGainNodes[sample_idx];
         self.samples[sample_idx] = sample;
-        sample.loop = false;
+        sample.loop = ($(nanotouch).find('.loop input:checked').length == 1);
         sample.connect(gainNode);
         sample.buffer = sample_buffer;
         sample.onended = function() {
-          console.log('ended');
           $(nanotouch).removeClass('playing');
+          var duration = $(nanotouch).find('span.duration');
+          $(duration).text(parseInt($(duration).data('duration')) + 's');
           self.samples[sample_idx] = undefined;
         }
         $(nanotouch).addClass('playing');
+        self.sample_start_times[sample_idx] = sample.context.currentTime;
         sample.start(0);
       }
     }
@@ -397,3 +496,15 @@ var nanofun = function() {
 }
 
 $(function() { nanofun(); });
+
+if ('serviceWorker' in navigator) {
+  window.addEventListener('load', function() {
+    navigator.serviceWorker.register('service-worker.js').then(function(registration) {
+      // Registration was successful
+      console.log('ServiceWorker registration successful with scope: ', registration.scope);
+    }).catch(function(err) {
+      // registration failed :(
+      console.log('ServiceWorker registration failed: ', err);
+    });
+  });
+}