Skip to content

Commit

Permalink
Merge pull request #294 from processing/separate-audio-context
Browse files Browse the repository at this point in the history
Separate audio context module
  • Loading branch information
therewasaguy authored Jun 15, 2018
2 parents f14890c + f6aea92 commit e50981b
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 56 deletions.
3 changes: 2 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
5 changes: 3 additions & 2 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
75 changes: 75 additions & 0 deletions src/audiocontext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
'use strict';

define(function () {
// Create the Audio Context
var audiocontext = new window.AudioContext();

/**
* <p>Returns the Audio Context for this sketch. Useful for users
* who would like to dig deeper into the <a target='_blank' href=
* 'http://webaudio.github.io/web-audio-api/'>Web Audio API
* </a>.</p>
*
* <p>Some browsers require users to startAudioContext
* with a user gesture, such as touchStarted in the example below.</p>
*
* @method getAudioContext
* @return {Object} AudioContext for this sketch
* @example
* <div><code>
* 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);
* }
*
* </div></code>
*/
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;
});
1 change: 0 additions & 1 deletion src/gain.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 3 additions & 4 deletions src/reverb.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 <a href = "/reference/#/p5.Effect">p5.Effect</a>.
* Methods <a href = "/reference/#/p5.Effect/amp">amp()</a>, <a href = "/reference/#/p5.Effect/chain">chain()</a>,
* <a href = "/reference/#/p5.Effect/drywet">drywet()</a>, <a href = "/reference/#/p5.Effect/connect">connect()</a>, and
* This class extends <a href = "/reference/#/p5.Effect">p5.Effect</a>.
* Methods <a href = "/reference/#/p5.Effect/amp">amp()</a>, <a href = "/reference/#/p5.Effect/chain">chain()</a>,
* <a href = "/reference/#/p5.Effect/drywet">drywet()</a>, <a href = "/reference/#/p5.Effect/connect">connect()</a>, and
* <a href = "/reference/#/p5.Effect/disconnect">disconnect()</a> are available.
*
* @class p5.Reverb
Expand Down
50 changes: 4 additions & 46 deletions src/sndcore.js → src/shims.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
'use strict';

/**
* This module has shims
*/

define(function () {

/* AudioContext Monkeypatch
Expand Down Expand Up @@ -146,22 +150,6 @@ define(function () {
})(window);
// <-- end MonkeyPatch.

// Create the Audio Context
var audiocontext = new window.AudioContext();

/**
* <p>Returns the Audio Context for this sketch. Useful for users
* who would like to dig deeper into the <a target='_blank' href=
* 'http://webaudio.github.io/web-audio-api/'>Web Audio API
* </a>.</p>
*
* @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 ||
Expand Down Expand Up @@ -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
}

});
1 change: 0 additions & 1 deletion src/soundRecorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ define(function (require) {

// inspiration: recorder.js, Tone.js & typedarray.org

require('sndcore');
var p5sound = require('master');
var ac = p5sound.audiocontext;

Expand Down
1 change: 0 additions & 1 deletion src/soundfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

define(function (require) {

require('sndcore');
var CustomError = require('errorHandler');
var p5sound = require('master');
var ac = p5sound.audiocontext;
Expand Down

0 comments on commit e50981b

Please sign in to comment.