From: Frédéric Péters Date: Sun, 19 Feb 2017 18:49:04 +0000 (+0100) Subject: add support for using a standard keyboard to trigger samples X-Git-Url: https://git.0d.be/?p=nanofun.git;a=commitdiff_plain;h=af045b668cffc5f799596e231fff6eab7bc4d2eb add support for using a standard keyboard to trigger samples --- diff --git a/nanofun.js b/nanofun.js index 8e6204d..bf03e3a 100644 --- a/nanofun.js +++ b/nanofun.js @@ -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); } } }