Skip to content

Commit e50981b

Browse files
authored
Merge pull request #294 from processing/separate-audio-context
Separate audio context module
2 parents f14890c + f6aea92 commit e50981b

File tree

8 files changed

+87
-56
lines changed

8 files changed

+87
-56
lines changed

Gruntfile.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ module.exports = function(grunt) {
6565
'Tone' : 'node_modules/tone/Tone',
6666
'automation-timeline': 'node_modules/web-audio-automation-timeline/build/automation-timeline-amd',
6767
'panner' : 'src/panner',
68-
'sndcore': 'src/sndcore',
68+
'shims': 'src/shims',
69+
'audiocontext': 'src/audiocontext',
6970
'master': 'src/master',
7071
'helpers': 'src/helpers',
7172
'errorHandler': 'src/errorHandler',

src/app.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
define(function (require) {
44

5-
var p5SOUND = require('sndcore');
6-
require('master');
5+
require('shims');
6+
require('audiocontext');
7+
var p5SOUND = require('master');
78
require('helpers');
89
require('errorHandler');
910
require('panner');

src/audiocontext.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
'use strict';
2+
3+
define(function () {
4+
// Create the Audio Context
5+
var audiocontext = new window.AudioContext();
6+
7+
/**
8+
* <p>Returns the Audio Context for this sketch. Useful for users
9+
* who would like to dig deeper into the <a target='_blank' href=
10+
* 'http://webaudio.github.io/web-audio-api/'>Web Audio API
11+
* </a>.</p>
12+
*
13+
* <p>Some browsers require users to startAudioContext
14+
* with a user gesture, such as touchStarted in the example below.</p>
15+
*
16+
* @method getAudioContext
17+
* @return {Object} AudioContext for this sketch
18+
* @example
19+
* <div><code>
20+
* function draw() {
21+
* background(255);
22+
* textAlign(CENTER);
23+
*
24+
* if (getAudioContext().state !== 'running') {
25+
* text('click to start audio', width/2, height/2);
26+
* } else {
27+
* text('audio is enabled', width/2, height/2);
28+
* }
29+
* }
30+
*
31+
* function touchStarted() {
32+
* if (getAudioContext().state !== 'running') {
33+
* getAudioContext().resume();
34+
* }
35+
* var synth = new p5.MonoSynth();
36+
* synth.play('A4', 0.5, 0, 0.2);
37+
* }
38+
*
39+
* </div></code>
40+
*/
41+
p5.prototype.getAudioContext = function() {
42+
return audiocontext;
43+
};
44+
45+
// if it is iOS, we have to have a user interaction to start Web Audio
46+
// http://paulbakaus.com/tutorials/html5/web-audio-on-ios/
47+
var iOS = navigator.userAgent.match(/(iPad|iPhone|iPod)/g) ? true : false ;
48+
if (iOS) {
49+
var iosStarted = false;
50+
var startIOS = function() {
51+
if (iosStarted) return;
52+
53+
// create empty buffer
54+
var buffer = audiocontext.createBuffer(1, 1, 22050);
55+
var source = audiocontext.createBufferSource();
56+
source.buffer = buffer;
57+
58+
// connect to output (your speakers)
59+
source.connect(audiocontext.destination);
60+
// play the file
61+
source.start(0);
62+
console.log('start ios!');
63+
64+
if (audiocontext.state === 'running') {
65+
iosStarted = true;
66+
}
67+
};
68+
document.addEventListener('touchend', startIOS, false);
69+
document.addEventListener('touchstart', startIOS, false);
70+
71+
// TO DO: fake touch event so that audio will just start
72+
}
73+
74+
return audiocontext;
75+
});

src/gain.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
define(function (require) {
44
var p5sound = require('master');
5-
require('sndcore');
65

76
/**
87
* A gain node is usefull to set the relative volume of sound.

src/reverb.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
define(function (require) {
44
var CustomError = require('errorHandler');
55
var Effect = require('effect');
6-
require('sndcore');
76

87
/**
98
* Reverb adds depth to a sound through a large number of decaying
@@ -14,9 +13,9 @@ define(function (require) {
1413
* extends p5.Reverb allowing you to recreate the sound of actual physical
1514
* spaces through convolution.
1615
*
17-
* This class extends <a href = "/reference/#/p5.Effect">p5.Effect</a>.
18-
* Methods <a href = "/reference/#/p5.Effect/amp">amp()</a>, <a href = "/reference/#/p5.Effect/chain">chain()</a>,
19-
* <a href = "/reference/#/p5.Effect/drywet">drywet()</a>, <a href = "/reference/#/p5.Effect/connect">connect()</a>, and
16+
* This class extends <a href = "/reference/#/p5.Effect">p5.Effect</a>.
17+
* Methods <a href = "/reference/#/p5.Effect/amp">amp()</a>, <a href = "/reference/#/p5.Effect/chain">chain()</a>,
18+
* <a href = "/reference/#/p5.Effect/drywet">drywet()</a>, <a href = "/reference/#/p5.Effect/connect">connect()</a>, and
2019
* <a href = "/reference/#/p5.Effect/disconnect">disconnect()</a> are available.
2120
*
2221
* @class p5.Reverb

src/sndcore.js renamed to src/shims.js

Lines changed: 4 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
'use strict';
22

3+
/**
4+
* This module has shims
5+
*/
6+
37
define(function () {
48

59
/* AudioContext Monkeypatch
@@ -146,22 +150,6 @@ define(function () {
146150
})(window);
147151
// <-- end MonkeyPatch.
148152

149-
// Create the Audio Context
150-
var audiocontext = new window.AudioContext();
151-
152-
/**
153-
* <p>Returns the Audio Context for this sketch. Useful for users
154-
* who would like to dig deeper into the <a target='_blank' href=
155-
* 'http://webaudio.github.io/web-audio-api/'>Web Audio API
156-
* </a>.</p>
157-
*
158-
* @method getAudioContext
159-
* @return {Object} AudioContext for this sketch
160-
*/
161-
p5.prototype.getAudioContext = function() {
162-
return audiocontext;
163-
};
164-
165153
// Polyfill for AudioIn, also handled by p5.dom createCapture
166154
navigator.getUserMedia = navigator.getUserMedia ||
167155
navigator.webkitGetUserMedia ||
@@ -213,34 +201,4 @@ define(function () {
213201
return false;
214202
}
215203
};
216-
217-
// if it is iOS, we have to have a user interaction to start Web Audio
218-
// http://paulbakaus.com/tutorials/html5/web-audio-on-ios/
219-
var iOS = navigator.userAgent.match(/(iPad|iPhone|iPod)/g) ? true : false ;
220-
if (iOS) {
221-
var iosStarted = false;
222-
var startIOS = function() {
223-
if (iosStarted) return;
224-
225-
// create empty buffer
226-
var buffer = audiocontext.createBuffer(1, 1, 22050);
227-
var source = audiocontext.createBufferSource();
228-
source.buffer = buffer;
229-
230-
// connect to output (your speakers)
231-
source.connect(audiocontext.destination);
232-
// play the file
233-
source.start(0);
234-
console.log('start ios!');
235-
236-
if (audiocontext.state === 'running') {
237-
iosStarted = true;
238-
}
239-
};
240-
document.addEventListener('touchend', startIOS, false);
241-
document.addEventListener('touchstart', startIOS, false);
242-
243-
// TO DO: fake touch event so that audio will just start
244-
}
245-
246204
});

src/soundRecorder.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ define(function (require) {
44

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

7-
require('sndcore');
87
var p5sound = require('master');
98
var ac = p5sound.audiocontext;
109

src/soundfile.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
define(function (require) {
44

5-
require('sndcore');
65
var CustomError = require('errorHandler');
76
var p5sound = require('master');
87
var ac = p5sound.audiocontext;

0 commit comments

Comments
 (0)