]> git.0d.be Git - nanofun.git/commitdiff
move sample processing out of midi code
authorFrédéric Péters <fpeters@0d.be>
Sun, 19 Feb 2017 18:20:08 +0000 (19:20 +0100)
committerFrédéric Péters <fpeters@0d.be>
Sun, 19 Feb 2017 18:20:08 +0000 (19:20 +0100)
nanofun.js

index 66e6147cb4655d5b84ee151371e5508221e7f295..23c8fe70f59c04a020ebf3d885e909bdafe60886 100644 (file)
@@ -3,12 +3,6 @@ var NANOPAD_TOUCHS = {
   36:  8, 38:  9, 40: 10, 42: 11, 44: 12, 46: 13, 48: 14, 50: 15
 };
 
   36:  8, 38:  9, 40: 10, 42: 11, 44: 12, 46: 13, 48: 14, 50: 15
 };
 
-var sample_buffers = Array(16);
-var samples = Array(16);
-var audioCtx = new window.AudioContext();
-var gainNode = audioCtx.createGain();
-gainNode.connect(audioCtx.destination);
-
 var midi = {
 
 onMIDISuccess: function(midiAccess) {
 var midi = {
 
 onMIDISuccess: function(midiAccess) {
@@ -107,31 +101,12 @@ onMIDIMessage: function(message) {
     var data = message.data;
     if (data[0] == 144) { /* touch on */
       var sample_idx = NANOPAD_TOUCHS[data[1]];
     var data = message.data;
     if (data[0] == 144) { /* touch on */
       var sample_idx = NANOPAD_TOUCHS[data[1]];
-      var sample_buffer = sample_buffers[sample_idx];
-      var nanotouch = $('.nanotouch')[sample_idx];
-      if (typeof(sample_buffer) != 'undefined') {
-        console.log(samples[sample_idx]);
-        if (typeof(samples[sample_idx]) != 'undefined' && samples[sample_idx].context.state == 'running') {
-          samples[sample_idx].stop(0);
-          samples[sample_idx] = undefined;
-        } else {
-          var sample = audioCtx.createBufferSource();
-          samples[sample_idx] = sample;
-          sample.loop = false;
-          sample.connect(gainNode);
-          sample.buffer = sample_buffer;
-          sample.onended = function() {
-            console.log('ended');
-            $(nanotouch).removeClass('playing');
-            samples[sample_idx] = undefined;
-          }
-          $(nanotouch).addClass('playing');
-          sample.start(0);
-        }
-      }
+      this.onTouchOn(port, data, sample_idx);
     }
 },
 
     }
 },
 
+onTouchOn: function(port, data, sample_idx) {}
+
 };
 
 // status bytes on channel 1
 };
 
 // status bytes on channel 1
@@ -238,6 +213,11 @@ var device = function(outputName) {
 
 $(function() {
   var $nanopad = $('#nanopad');
 
 $(function() {
   var $nanopad = $('#nanopad');
+  var sample_buffers = Array(16);
+  var samples = Array(16);
+  var audioCtx = new window.AudioContext();
+  var gainNode = audioCtx.createGain();
+  gainNode.connect(audioCtx.destination);
 
   $('.nanotouch input').on('change', function(ev) {
     var nanotouch = $(this).parent();
 
   $('.nanotouch input').on('change', function(ev) {
     var nanotouch = $(this).parent();
@@ -257,6 +237,31 @@ $(function() {
     $(nanotouch).find('span.name').text(this.files[0].name);
   });
 
     $(nanotouch).find('span.name').text(this.files[0].name);
   });
 
+  midi.onTouchOn = function(port, data, sample_idx) {
+    var sample_buffer = sample_buffers[sample_idx];
+    var nanotouch = $('.nanotouch')[sample_idx];
+    if (typeof(sample_buffer) != 'undefined') {
+      console.log(samples[sample_idx]);
+      if (typeof(samples[sample_idx]) != 'undefined' && samples[sample_idx].context.state == 'running') {
+        samples[sample_idx].stop(0);
+        samples[sample_idx] = undefined;
+      } else {
+        var sample = audioCtx.createBufferSource();
+        samples[sample_idx] = sample;
+        sample.loop = false;
+        sample.connect(gainNode);
+        sample.buffer = sample_buffer;
+        sample.onended = function() {
+          console.log('ended');
+          $(nanotouch).removeClass('playing');
+          samples[sample_idx] = undefined;
+        }
+        $(nanotouch).addClass('playing');
+        sample.start(0);
+      }
+    }
+  }
+
   if (navigator.requestMIDIAccess) {
     navigator.requestMIDIAccess({
         sysex: true
   if (navigator.requestMIDIAccess) {
     navigator.requestMIDIAccess({
         sysex: true