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