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); + * } + * + *
+ */ + p5.prototype.getAudioContext = function() { + return audiocontext; + }; + + // 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 + } + + return audiocontext; +}); diff --git a/src/gain.js b/src/gain.js index cbeb87d3..79ea33b8 100644 --- a/src/gain.js +++ b/src/gain.js @@ -2,7 +2,6 @@ define(function (require) { var p5sound = require('master'); - require('sndcore'); /** * A gain node is usefull to set the relative volume of sound. diff --git a/src/reverb.js b/src/reverb.js index 8a7fd06c..945b17d5 100644 --- a/src/reverb.js +++ b/src/reverb.js @@ -3,7 +3,6 @@ define(function (require) { var CustomError = require('errorHandler'); var Effect = require('effect'); - require('sndcore'); /** * Reverb adds depth to a sound through a large number of decaying @@ -14,9 +13,9 @@ define(function (require) { * extends p5.Reverb allowing you to recreate the sound of actual physical * spaces through convolution. * - * This class extends p5.Effect. - * Methods amp(), chain(), - * drywet(), connect(), and + * This class extends p5.Effect. + * Methods amp(), chain(), + * drywet(), connect(), and * disconnect() are available. * * @class p5.Reverb diff --git a/src/sndcore.js b/src/shims.js similarity index 83% rename from src/sndcore.js rename to src/shims.js index b99590c6..3de3f62f 100644 --- a/src/sndcore.js +++ b/src/shims.js @@ -1,5 +1,9 @@ 'use strict'; +/** + * This module has shims + */ + define(function () { /* AudioContext Monkeypatch @@ -146,22 +150,6 @@ define(function () { })(window); // <-- end MonkeyPatch. - // 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 - * .

- * - * @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;