-
-
Notifications
You must be signed in to change notification settings - Fork 682
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #294 from processing/separate-audio-context
Separate audio context module
- Loading branch information
Showing
8 changed files
with
87 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters