var self = this;
self.initAudio = function() {
+ self.focused_pad = undefined;
self.sample_buffers = Array(NANOPAD_TOUCHS.length);
self.samples = Array(NANOPAD_TOUCHS.length);
self.sample_start_times = Array(NANOPAD_TOUCHS.length);
});
midi.onTouchOn = function(port, data, sample_idx) {
- self.startSample(sample_idx);
+ self.toggleSample(sample_idx);
}
midi.onControlChange = function(port, data, control, value) {
}
}
+ if ((control == 58 || control == 59) && value == 127) {
+ /* track < and > buttons: move focus between pads for special functions */
+ if (self.focused_pad === undefined) {
+ self.focused_pad = 0;
+ } else if (control == 58) {
+ self.focused_pad = self.focused_pad - 1;
+ if (self.focused_pad < 0) self.focused_pad = 15;
+ } else if (control == 59) {
+ self.focused_pad = self.focused_pad + 1;
+ if (self.focused_pad > 15) self.focused_pad = 0;
+ }
+ $('[data-touch]').removeClass('focus');
+ $('[data-touch=' + self.focused_pad + ']').addClass('focus');
+ }
+ if (control == 41 && value == 127 && self.focused_pad !== undefined) { /* play */
+ var nanotouch = $('.nanotouch')[self.focused_pad];
+ if ($(nanotouch).is('.playing')) {
+ self.samples[self.focused_pad].onended = function() {}; // disable callback
+ self.stopSample(self.focused_pad);
+ }
+ self.startSample(self.focused_pad);
+ }
+ if (control == 42 && value == 127 && self.focused_pad !== undefined) { /* stop */
+ self.stopSample(self.focused_pad);
+ }
if (control > 23) return; /* after pots */
if (control < 8) {
control += 8; /* sliders, control bottom pads (8-15) */
}, 250);
}
+ self.toggleSample = function(sample_idx) {
+ var nanotouch = $('.nanotouch')[sample_idx];
+ if ($(nanotouch).is('.playing')) {
+ self.stopSample(sample_idx);
+ } else {
+ 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') {
+ var sample = self.audioCtx.createBufferSource();
+ var gainNode = self.touchGainNodes[sample_idx];
+ self.samples[sample_idx] = sample;
+ sample.loop = ($(nanotouch).find('.loop input:checked').length == 1);
+ sample.connect(gainNode);
+ sample.buffer = sample_buffer;
+ sample.onended = function() {
+ $(nanotouch).removeClass('playing');
+ var duration = $(nanotouch).find('span.duration');
+ $(duration).text(parseInt($(duration).data('duration')) + 's');
+ self.samples[sample_idx] = undefined;
+ }
+ $(nanotouch).addClass('playing');
+ self.sample_start_times[sample_idx] = sample.context.currentTime;
+ sample.start(0);
+ }
+ }
+ self.stopSample = function(sample_idx) {
+ var sample_buffer = self.sample_buffers[sample_idx];
+ if (typeof(sample_buffer) != 'undefined') {
+ if (typeof(samples[sample_idx]) != 'undefined') {
self.samples[sample_idx].stop(0);
self.samples[sample_idx] = undefined;
- } else {
- var sample = self.audioCtx.createBufferSource();
- var gainNode = self.touchGainNodes[sample_idx];
- self.samples[sample_idx] = sample;
- sample.loop = ($(nanotouch).find('.loop input:checked').length == 1);
- sample.connect(gainNode);
- sample.buffer = sample_buffer;
- sample.onended = function() {
- $(nanotouch).removeClass('playing');
- var duration = $(nanotouch).find('span.duration');
- $(duration).text(parseInt($(duration).data('duration')) + 's');
- self.samples[sample_idx] = undefined;
- }
- $(nanotouch).addClass('playing');
- self.sample_start_times[sample_idx] = sample.context.currentTime;
- sample.start(0);
}
}
}