]> git.0d.be Git - nanofun.git/commitdiff
add support for using a standard keyboard to trigger samples
authorFrédéric Péters <fpeters@0d.be>
Sun, 19 Feb 2017 18:49:04 +0000 (19:49 +0100)
committerFrédéric Péters <fpeters@0d.be>
Sun, 19 Feb 2017 18:49:04 +0000 (19:49 +0100)
nanofun.js

index 8e6204d6b27f470e20d69b6d59c73ebb39dd42b7..bf03e3aeba146fbbea38978c5834e6f23b9cff59 100644 (file)
@@ -3,6 +3,10 @@ var NANOPAD_TOUCHS = {
   36:  8, 38:  9, 40: 10, 42: 11, 44: 12, 46: 13, 48: 14, 50: 15
 };
 
+/* on French/Belgian keyboards, emulate pad touches with keypresses */
+var KEYBOARD_CODES = Array('a', 'z', 'e', 'r', 't', 'u', 'i', 'o',
+                           'q', 's', 'd', 'f', 'g', 'h', 'j', 'k');
+
 var midi = {
 
 onMIDISuccess: function(midiAccess) {
@@ -253,26 +257,37 @@ var nanofun = function() {
     });
 
     midi.onTouchOn = function(port, data, 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') {
-          self.samples[sample_idx].stop(0);
+      self.startSample(sample_idx);
+    }
+
+    $(document).keypress(function(ev) {
+      var sample_idx = KEYBOARD_CODES.indexOf(ev.key);
+      if (sample_idx != -1) {
+        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') {
+        self.samples[sample_idx].stop(0);
+        self.samples[sample_idx] = undefined;
+      } else {
+        var sample = self.audioCtx.createBufferSource();
+        self.samples[sample_idx] = sample;
+        sample.loop = false;
+        sample.connect(gainNode);
+        sample.buffer = sample_buffer;
+        sample.onended = function() {
+          console.log('ended');
+          $(nanotouch).removeClass('playing');
           self.samples[sample_idx] = undefined;
-        } else {
-          var sample = self.audioCtx.createBufferSource();
-          self.samples[sample_idx] = sample;
-          sample.loop = false;
-          sample.connect(gainNode);
-          sample.buffer = sample_buffer;
-          sample.onended = function() {
-            console.log('ended');
-            $(nanotouch).removeClass('playing');
-            self.samples[sample_idx] = undefined;
-          }
-          $(nanotouch).addClass('playing');
-          sample.start(0);
         }
+        $(nanotouch).addClass('playing');
+        sample.start(0);
       }
     }
   }