]> git.0d.be Git - nanofun.git/commitdiff
use track buttons to navigate between pads and stop/play buttons to act
authorFrédéric Péters <fpeters@0d.be>
Wed, 20 Jan 2021 09:33:58 +0000 (10:33 +0100)
committerFrédéric Péters <fpeters@0d.be>
Wed, 20 Jan 2021 09:33:58 +0000 (10:33 +0100)
nanofun.css
nanofun.js

index ffe01e0e24f0602a2dd0baba6b043c2c83f63abd..85f73f05ab6179cb8f897bfd7335c526a8738ac7 100644 (file)
@@ -39,6 +39,10 @@ div.nanotouch.loaded.playing {
   background: #6aff6a;
 }
 
+div.nanotouch.focus {
+  box-shadow: 0 0 5px 5px #386ede;
+}
+
 div.nanotouch input[type=file] {
   position: absolute;
   top: 0;
index 13813a124f7b979bedd07e4dac9d7eb19cd94f5b..bf32ff3f0d6f9f47034ff6d9b04ef19dc4d71bdc 100644 (file)
@@ -295,6 +295,7 @@ var nanofun = function() {
   var self = this;
 
   self.initAudio = function() {
+    self.focused_pad = undefined;
     self.sample_buffers = Array(NANOPAD_TOUCHS.length);
     self.samples = Array(NANOPAD_TOUCHS.length);
     self.sample_start_times = Array(NANOPAD_TOUCHS.length);
@@ -370,7 +371,7 @@ var nanofun = function() {
     });
 
     midi.onTouchOn = function(port, data, sample_idx) {
-      self.startSample(sample_idx);
+      self.toggleSample(sample_idx);
     }
 
     midi.onControlChange = function(port, data, control, value) {
@@ -404,6 +405,31 @@ var nanofun = function() {
           }
 
       }
+      if ((control == 58 || control == 59) && value == 127) {
+        /* track < and > buttons: move focus between pads for special functions */
+        if (self.focused_pad === undefined) {
+          self.focused_pad = 0;
+        } else if (control == 58) {
+          self.focused_pad = self.focused_pad - 1;
+          if (self.focused_pad < 0) self.focused_pad = 15;
+        } else if (control == 59) {
+          self.focused_pad = self.focused_pad + 1;
+          if (self.focused_pad > 15) self.focused_pad = 0;
+        }
+        $('[data-touch]').removeClass('focus');
+        $('[data-touch=' + self.focused_pad + ']').addClass('focus');
+      }
+      if (control == 41 && value == 127 && self.focused_pad !== undefined) { /* play */
+        var nanotouch = $('.nanotouch')[self.focused_pad];
+        if ($(nanotouch).is('.playing')) {
+          self.samples[self.focused_pad].onended = function() {};  // disable callback
+          self.stopSample(self.focused_pad);
+        }
+        self.startSample(self.focused_pad);
+      }
+      if (control == 42 && value == 127 && self.focused_pad !== undefined) { /* stop */
+        self.stopSample(self.focused_pad);
+      }
       if (control > 23) return; /* after pots */
       if (control < 8) {
         control += 8; /* sliders, control bottom pads (8-15) */
@@ -485,29 +511,42 @@ var nanofun = function() {
     }, 250);
   }
 
+  self.toggleSample = function(sample_idx) {
+    var nanotouch = $('.nanotouch')[sample_idx];
+    if ($(nanotouch).is('.playing')) {
+      self.stopSample(sample_idx);
+    } else {
+      self.startSample(sample_idx);
+    }
+  }
+
   self.startSample = function(sample_idx) {
     var sample_buffer = self.sample_buffers[sample_idx];
     var nanotouch = $('.nanotouch')[sample_idx];
     if (typeof(sample_buffer) != 'undefined') {
-      if (typeof(samples[sample_idx]) != 'undefined' && samples[sample_idx].context.state == 'running') {
+      var sample = self.audioCtx.createBufferSource();
+      var gainNode = self.touchGainNodes[sample_idx];
+      self.samples[sample_idx] = sample;
+      sample.loop = ($(nanotouch).find('.loop input:checked').length == 1);
+      sample.connect(gainNode);
+      sample.buffer = sample_buffer;
+      sample.onended = function() {
+        $(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);
+    }
+  }
+  self.stopSample = function(sample_idx) {
+    var sample_buffer = self.sample_buffers[sample_idx];
+    if (typeof(sample_buffer) != 'undefined') {
+      if (typeof(samples[sample_idx]) != 'undefined') {
         self.samples[sample_idx].stop(0);
         self.samples[sample_idx] = undefined;
-      } else {
-        var sample = self.audioCtx.createBufferSource();
-        var gainNode = self.touchGainNodes[sample_idx];
-        self.samples[sample_idx] = sample;
-        sample.loop = ($(nanotouch).find('.loop input:checked').length == 1);
-        sample.connect(gainNode);
-        sample.buffer = sample_buffer;
-        sample.onended = function() {
-          $(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);
       }
     }
   }