Skip to content

Commit

Permalink
Merge pull request otalk#39 from ibc/fix-audioContext-issue38
Browse files Browse the repository at this point in the history
Ensure a single AudioContext is internally created
  • Loading branch information
fippo committed Aug 25, 2018
2 parents e5149ef + f3fa6e4 commit e7f6e51
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ speech.on('speaking', function() {
* `interval` (optional, default 100ms) how frequently the analyser polls the audio stream to check if speaking has started or stopped. This will also be the frequency of the `volume_change` events.
* `threshold` (optional, default -50db) the volume at which `speaking`/`stopped\_speaking` events will be fired
* `play` (optional, default true for audio tags, false for webrtc streams) whether the audio stream should also be piped to the speakers, or just swallowed by the analyser. Typically for audio tags you would want to hear them, but for microphone based webrtc streams you may not to avoid feedback.
* `audioContext` (optional, default is to create a new context) If you have already created an `AudioContext`, you can pass it to hark to avoid unnecessarily creating more than one.
* `audioContext` (optional, default is to create a single context) If you have already created an `AudioContext`, you can pass it to hark to use it instead of an internally generated one.

## Understanding dB/volume threshold

Expand Down
26 changes: 16 additions & 10 deletions example/demo.bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ for (var i = 0; i < 100; i++) {
window.requestAnimationFrame(draw);
})();

},{"../hark.js":2,"attachmediastream":5,"bows":3,"getusermedia":4}],4:[function(require,module,exports){
},{"../hark.js":2,"attachmediastream":5,"bows":4,"getusermedia":3}],3:[function(require,module,exports){
// getUserMedia helper by @HenrikJoreteg
var func = (navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
Expand Down Expand Up @@ -228,7 +228,6 @@ var audioContext = null;
module.exports = function(stream, options) {
var harker = new WildEmitter();


// make it not break in non-supported browsers
if (!audioContextType) return harker;

Expand All @@ -239,13 +238,11 @@ module.exports = function(stream, options) {
threshold = options.threshold,
play = options.play,
history = options.history || 10,
audioContext = options.audioContext || null,
running = true;

//Setup Audio Context
if (!audioContext) {
audioContext = new audioContextType();
}
// Ensure that just a single AudioContext is internally created
audioContext = options.audioContext || audioContext || new audioContextType();

var sourceNode, fftBins, analyser;

analyser = audioContext.createAnalyser();
Expand Down Expand Up @@ -347,7 +344,6 @@ module.exports = function(stream, options) {
};
looper();


return harker;
}

Expand Down Expand Up @@ -506,7 +502,7 @@ WildEmitter.mixin = function (constructor) {

WildEmitter.mixin(WildEmitter);

},{}],3:[function(require,module,exports){
},{}],4:[function(require,module,exports){
(function(window) {
var logger = require('andlog'),
goldenRatio = 0.618033988749895,
Expand Down Expand Up @@ -545,8 +541,18 @@ WildEmitter.mixin(WildEmitter);
},{"andlog":7}],7:[function(require,module,exports){
// follow @HenrikJoreteg and @andyet if you like this ;)
(function () {
function getLocalStorageSafely() {
var localStorage;
try {
localStorage = window.localStorage;
} catch (e) {
// failed: access to localStorage is denied
}
return localStorage;
}

var inNode = typeof window === 'undefined',
ls = !inNode && window.localStorage,
ls = !inNode && getLocalStorageSafely(),
out = {};

if (inNode) {
Expand Down
10 changes: 3 additions & 7 deletions hark.bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ var audioContext = null;
module.exports = function(stream, options) {
var harker = new WildEmitter();


// make it not break in non-supported browsers
if (!audioContextType) return harker;

Expand All @@ -36,13 +35,11 @@ module.exports = function(stream, options) {
threshold = options.threshold,
play = options.play,
history = options.history || 10,
audioContext = options.audioContext || null,
running = true;

//Setup Audio Context
if (!audioContext) {
audioContext = new audioContextType();
}
// Ensure that just a single AudioContext is internally created
audioContext = options.audioContext || audioContext || new audioContextType();

var sourceNode, fftBins, analyser;

analyser = audioContext.createAnalyser();
Expand Down Expand Up @@ -144,7 +141,6 @@ module.exports = function(stream, options) {
};
looper();


return harker;
}

Expand Down
10 changes: 3 additions & 7 deletions hark.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ var audioContext = null;
module.exports = function(stream, options) {
var harker = new WildEmitter();


// make it not break in non-supported browsers
if (!audioContextType) return harker;

Expand All @@ -34,13 +33,11 @@ module.exports = function(stream, options) {
threshold = options.threshold,
play = options.play,
history = options.history || 10,
audioContext = options.audioContext || null,
running = true;

//Setup Audio Context
if (!audioContext) {
audioContext = new audioContextType();
}
// Ensure that just a single AudioContext is internally created
audioContext = options.audioContext || audioContext || new audioContextType();

var sourceNode, fftBins, analyser;

analyser = audioContext.createAnalyser();
Expand Down Expand Up @@ -142,6 +139,5 @@ module.exports = function(stream, options) {
};
looper();


return harker;
}

0 comments on commit e7f6e51

Please sign in to comment.