Skip to content

Commit 023e2a7

Browse files
committed
add error callback to audioin
1 parent 56b3b6e commit 023e2a7

File tree

3 files changed

+44
-19
lines changed

3 files changed

+44
-19
lines changed

lib/p5.sound.js

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4764,6 +4764,7 @@ var audioin;
47644764
audioin = function () {
47654765
'use strict';
47664766
var p5sound = master;
4767+
var CustomError = errorHandler;
47674768
/**
47684769
* <p>Get audio from an input, i.e. your computer's microphone.</p>
47694770
*
@@ -4830,24 +4831,35 @@ audioin = function () {
48304831
* anything unless you use the connect() method.<br/>
48314832
*
48324833
* @method start
4833-
*/
4834-
p5.AudioIn.prototype.start = function () {
4834+
* @param {Function} successCallback Name of a function to call on
4835+
* success.
4836+
* @param {Function} errorCallback Name of a function to call if
4837+
* there was an error. For example,
4838+
* some browsers do not support
4839+
* getUserMedia.
4840+
*/
4841+
p5.AudioIn.prototype.start = function (successCallback, errorCallback) {
48354842
var self = this;
48364843
// if _gotSources() i.e. developers determine which source to use
48374844
if (p5sound.inputSources[self.currentSource]) {
48384845
// set the audio source
48394846
var audioSource = p5sound.inputSources[self.currentSource].id;
48404847
var constraints = { audio: { optional: [{ sourceId: audioSource }] } };
4841-
navigator.getUserMedia(constraints, this._onStream = function (stream) {
4848+
window.navigator.getUserMedia(constraints, this._onStream = function (stream) {
48424849
self.stream = stream;
48434850
self.enabled = true;
48444851
// Wrap a MediaStreamSourceNode around the live input
48454852
self.mediaStream = p5sound.audiocontext.createMediaStreamSource(stream);
48464853
self.mediaStream.connect(self.output);
4854+
if (successCallback)
4855+
successCallback();
48474856
// only send to the Amplitude reader, so we can see it but not hear it.
48484857
self.amplitude.setInput(self.output);
4849-
}, this._onStreamError = function (stream) {
4850-
console.error(stream);
4858+
}, this._onStreamError = function (e) {
4859+
if (errorCallback)
4860+
errorCallback(e);
4861+
else
4862+
console.error(e);
48514863
});
48524864
} else {
48534865
// if Firefox where users select their source via browser
@@ -4861,8 +4873,13 @@ audioin = function () {
48614873
self.mediaStream.connect(self.output);
48624874
// only send to the Amplitude reader, so we can see it but not hear it.
48634875
self.amplitude.setInput(self.output);
4864-
}, this._onStreamError = function (stream) {
4865-
console.error(stream);
4876+
if (successCallback)
4877+
successCallback();
4878+
}, this._onStreamError = function (e) {
4879+
if (errorCallback)
4880+
errorCallback(e);
4881+
else
4882+
console.error(e);
48664883
});
48674884
}
48684885
};
@@ -5054,7 +5071,7 @@ audioin = function () {
50545071
this.amplitude = null;
50555072
this.output = null;
50565073
};
5057-
}(master);
5074+
}(master, errorHandler);
50585075
var filter;
50595076
filter = function () {
50605077
'use strict';

0 commit comments

Comments
 (0)