- 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);