From: Frédéric Péters Date: Sun, 19 Feb 2017 18:34:54 +0000 (+0100) Subject: refactor code into a nanofun function X-Git-Url: https://git.0d.be/?p=nanofun.git;a=commitdiff_plain;h=0ce53e350d93eef64c84ab6b1130d5a67d66f0ab refactor code into a nanofun function --- diff --git a/nanofun.js b/nanofun.js index 23c8fe7..8e6204d 100644 --- a/nanofun.js +++ b/nanofun.js @@ -211,64 +211,76 @@ var device = function(outputName) { return this; }; -$(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(); - var sample_idx = $nanopad.children().index(nanotouch); - var reader = new FileReader(); - reader.onload = function(e) { - 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 nanofun = function() { + var self = this; + + self.initAudio = function() { + self.sample_buffers = Array(16); + self.samples = Array(16); + self.audioCtx = new window.AudioContext(); + self.gainNode = self.audioCtx.createGain(); + self.gainNode.connect(self.audioCtx.destination); + } + + self.initMIDI = function() { + if (navigator.requestMIDIAccess) { + navigator.requestMIDIAccess({sysex: true}).then( + function(midiAccess) { midi.onMIDISuccess(midiAccess); }, + function(e) { midi.onMIDIFailure(e); } + ); } - reader.readAsArrayBuffer(this.files[0]); - $(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; + } + + 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'); + }); + } + reader.readAsArrayBuffer(this.files[0]); + $(nanotouch).find('span.name').text(this.files[0].name); + }); + + 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.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); } } } - if (navigator.requestMIDIAccess) { - navigator.requestMIDIAccess({ - sysex: true - }).then( - function(midiAccess) { midi.onMIDISuccess(midiAccess); }, - function(e) { midi.onMIDIFailure(e); } - ); - } + self.initAudio(); + self.initMIDI(); + self.initUI(); + +} -}); +$(function() { nanofun(); });