Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resume audio context [WIP] #283

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
108 changes: 108 additions & 0 deletions src/audiocontext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
'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> 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
* <div><code>
* function draw() {
* background(255);
* textAlign(CENTER);
*
* var audioContext = getAudioContext();
* if (audioContext.state !== 'running') {
* text('click to start audio', width/2, height/2);
* } else {
* text('audio is enabled', width/2, height/2);
* }
* }
*
* function touchStarted() {
* startAudioContext();
* var synth = new p5.MonoSynth();
* synth.play('A4', 0.5, 0, 0.2);
* }
* </div></code>
*/
p5.prototype.getAudioContext = function() {
return audiocontext;
};

/**
* <p>A user gesture is required to start or resume the AudioContext
* in some browsers. For example, see the <a target='_blank' href=
* 'https://developers.google.com/web/updates/2017/09/autoplay-policy-changes#webaudio'>
* Chrome autoplay policy</a> effective in Chrome 66+.</p>
* <p>Place this method in an event handle for user
* interaction, such as touchStarted or mousePressed.</p>
* @method startAudioContext
* @return {Promise}
* @example
* <div><code>
* function draw() {
* background(255);
* textAlign(CENTER);
*
* var audioContext = getAudioContext();
* if (audioContext.state !== 'running') {
* text('click to start audio', width/2, height/2);
* } else {
* text('audio is enabled', width/2, height/2);
* }
* }
*
* function touchStarted() {
* startAudioContext();
* var synth = new p5.MonoSynth();
* synth.play('A4', 0.5, 0, 0.2);
* }
* </div></code>
*/
p5.prototype.startAudioContext = function() {
if (audiocontext.state === 'running') {
return Promise.resolve();
}
// will reject if audiocontet state was 'closed', otherwise success
return audiocontext.resume();
};

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

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
3 changes: 0 additions & 3 deletions src/polysynth.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
'use strict';
define(function (require) {


var p5sound = require('master');
var TimelineSignal = require('Tone/signal/TimelineSignal');
require('sndcore');


/**
* An AudioVoice is used as a single voice for sound synthesis.
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