diff --git a/Gruntfile.js b/Gruntfile.js index 4d21db54..82ab664f 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -65,7 +65,8 @@ module.exports = function(grunt) { 'Tone' : 'node_modules/tone/Tone', 'automation-timeline': 'node_modules/web-audio-automation-timeline/build/automation-timeline-amd', 'panner' : 'src/panner', - 'sndcore': 'src/sndcore', + 'shims': 'src/shims', + 'audiocontext': 'src/audiocontext', 'master': 'src/master', 'helpers': 'src/helpers', 'errorHandler': 'src/errorHandler', diff --git a/src/app.js b/src/app.js index cc2b6585..c6870512 100644 --- a/src/app.js +++ b/src/app.js @@ -2,8 +2,9 @@ define(function (require) { - var p5SOUND = require('sndcore'); - require('master'); + require('shims'); + require('audiocontext'); + var p5SOUND = require('master'); require('helpers'); require('errorHandler'); require('panner'); diff --git a/src/audiocontext.js b/src/audiocontext.js new file mode 100644 index 00000000..ec43d34c --- /dev/null +++ b/src/audiocontext.js @@ -0,0 +1,75 @@ +'use strict'; + +define(function () { + // Create the Audio Context + var audiocontext = new window.AudioContext(); + + /** + *
Returns the Audio Context for this sketch. Useful for users + * who would like to dig deeper into the Web Audio API + * .
+ * + *Some browsers require users to startAudioContext + * with a user gesture, such as touchStarted in the example below.
+ * + * @method getAudioContext + * @return {Object} AudioContext for this sketch + * @example + *
+ * function draw() {
+ * background(255);
+ * textAlign(CENTER);
+ *
+ * if (getAudioContext().state !== 'running') {
+ * text('click to start audio', width/2, height/2);
+ * } else {
+ * text('audio is enabled', width/2, height/2);
+ * }
+ * }
+ *
+ * function touchStarted() {
+ * if (getAudioContext().state !== 'running') {
+ * getAudioContext().resume();
+ * }
+ * var synth = new p5.MonoSynth();
+ * synth.play('A4', 0.5, 0, 0.2);
+ * }
+ *
+ *
Returns the Audio Context for this sketch. Useful for users - * who would like to dig deeper into the Web Audio API - * .
- * - * @method getAudioContext - * @return {Object} AudioContext for this sketch - */ - p5.prototype.getAudioContext = function() { - return audiocontext; - }; - // Polyfill for AudioIn, also handled by p5.dom createCapture navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || @@ -213,34 +201,4 @@ define(function () { return false; } }; - - // if it is iOS, we have to have a user interaction to start Web Audio - // http://paulbakaus.com/tutorials/html5/web-audio-on-ios/ - var iOS = navigator.userAgent.match(/(iPad|iPhone|iPod)/g) ? true : false ; - if (iOS) { - var iosStarted = false; - var startIOS = function() { - if (iosStarted) return; - - // create empty buffer - var buffer = audiocontext.createBuffer(1, 1, 22050); - var source = audiocontext.createBufferSource(); - source.buffer = buffer; - - // connect to output (your speakers) - source.connect(audiocontext.destination); - // play the file - source.start(0); - console.log('start ios!'); - - if (audiocontext.state === 'running') { - iosStarted = true; - } - }; - document.addEventListener('touchend', startIOS, false); - document.addEventListener('touchstart', startIOS, false); - - // TO DO: fake touch event so that audio will just start - } - }); diff --git a/src/soundRecorder.js b/src/soundRecorder.js index 584e3595..fcd78133 100644 --- a/src/soundRecorder.js +++ b/src/soundRecorder.js @@ -4,7 +4,6 @@ define(function (require) { // inspiration: recorder.js, Tone.js & typedarray.org - require('sndcore'); var p5sound = require('master'); var ac = p5sound.audiocontext; diff --git a/src/soundfile.js b/src/soundfile.js index 7239890c..1816483d 100644 --- a/src/soundfile.js +++ b/src/soundfile.js @@ -2,7 +2,6 @@ define(function (require) { - require('sndcore'); var CustomError = require('errorHandler'); var p5sound = require('master'); var ac = p5sound.audiocontext;