Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

hark

Package Overview
Dependencies
Maintainers
2
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hark - npm Package Compare versions

Comparing version 0.2.1 to 1.0.0

example/6465.js

149

example/demo.bundle.js
;(function(e,t,n){function i(n,s){if(!t[n]){if(!e[n]){var o=typeof require=="function"&&require;if(!s&&o)return o(n,!0);if(r)return r(n,!0);throw new Error("Cannot find module '"+n+"'")}var u=t[n]={exports:{}};e[n][0].call(u.exports,function(t){var r=e[n][1][t];return i(r?r:t)},u,u.exports)}return t[n].exports}var r=typeof require=="function"&&require;for(var s=0;s<n.length;s++)i(n[s]);return i})({1:[function(require,module,exports){
localStorage.debug = true;
var hark = require('../hark.js');
var bows = require('bows');
var tagVolumes = [];
var streamVolumes = [];
var referenceVolumes = [];
for (var i = 0; i < 100; i++) {
tagVolumes.push(-100);
streamVolumes.push(-100);
referenceVolumes.push(-50);
}
(function() {

@@ -21,2 +28,4 @@ //Audio Tag Demo

//log('volume change', volume, threshold);
tagVolumes.push(volume);
tagVolumes.shift();
});

@@ -51,2 +60,4 @@

log(volume, threshold)
streamVolumes.push(volume);
streamVolumes.shift();
});

@@ -61,3 +72,75 @@

},{"../hark.js":2,"attachmediastream":5,"bows":3,"getusermedia":4}],4:[function(require,module,exports){
(function () {
function drawLine(canvas, data, color) {
var drawContext = canvas.getContext('2d');
drawContext.moveTo(0,canvas.height);
drawContext.beginPath();
drawContext.strokeStyle = color;
for (var i = 0; i < data.length; i++) {
var value = -data[i];
var percent = value / 100;
var height = canvas.height * percent;
var vOffset = height; //canvas.height - height - 5;
var hOffset = i * canvas.width / 100.0;
drawContext.lineTo(hOffset, vOffset);
}
drawContext.stroke();
}
function draw() {
var canvas = document.querySelector('canvas');
if (!canvas) return;
var drawContext = canvas.getContext('2d');
drawContext.clearRect (0, 0, canvas.width, canvas.height);
drawLine(canvas, tagVolumes, 'green');
drawLine(canvas, streamVolumes, 'blue');
drawLine(canvas, referenceVolumes, 'black');
window.requestAnimationFrame(draw);
}
window.requestAnimationFrame(draw);
})();
},{"../hark.js":2,"attachmediastream":3,"bows":5,"getusermedia":4}],3:[function(require,module,exports){
module.exports = function (stream, el, options) {
var URL = window.URL;
var opts = {
autoplay: true,
mirror: false,
muted: false
};
var element = el || document.createElement('video');
var item;
if (options) {
for (item in options) {
opts[item] = options[item];
}
}
if (opts.autoplay) element.autoplay = 'autoplay';
if (opts.muted) element.muted = true;
if (opts.mirror) {
['', 'moz', 'webkit', 'o', 'ms'].forEach(function (prefix) {
var styleName = prefix ? prefix + 'Transform' : 'transform';
element.style[styleName] = 'scaleX(-1)';
});
}
// this first one should work most everywhere now
// but we have a few fallbacks just in case.
if (URL && URL.createObjectURL) {
element.src = URL.createObjectURL(stream);
} else if (element.srcObject) {
element.srcObject = stream;
} else if (element.mozSrcObject) {
element.mozSrcObject = stream;
} else {
return false;
}
return element;
};
},{}],4:[function(require,module,exports){
// getUserMedia helper by @HenrikJoreteg

@@ -97,27 +180,2 @@ var func = (navigator.getUserMedia ||

},{}],5:[function(require,module,exports){
module.exports = function (element, stream, play) {
var autoPlay = (play === false) ? false : true;
if (autoPlay) element.autoplay = true;
// handle mozilla case
if (window.mozGetUserMedia) {
element.mozSrcObject = stream;
if (autoPlay) element.play();
} else {
if (typeof element.srcObject !== 'undefined') {
element.srcObject = stream;
} else if (typeof element.mozSrcObject !== 'undefined') {
element.mozSrcObject = stream;
} else if (typeof element.src !== 'undefined') {
element.src = URL.createObjectURL(stream);
} else {
return false;
}
}
return true;
};
},{}],2:[function(require,module,exports){

@@ -130,3 +188,3 @@ var WildEmitter = require('wildemitter');

for(var i=0, ii=fftBins.length; i < ii; i++) {
for(var i=4, ii=fftBins.length; i < ii; i++) {
if (fftBins[i] > maxVolume && fftBins[i] < 0) {

@@ -153,6 +211,7 @@ maxVolume = fftBins[i];

var options = options || {},
smoothing = (options.smoothing || 0.5),
interval = (options.interval || 100),
smoothing = (options.smoothing || 0.1),
interval = (options.interval || 50),
threshold = options.threshold,
play = options.play,
history = options.history || 10,
running = true;

@@ -176,7 +235,7 @@

if (typeof play === 'undefined') play = true;
threshold = threshold || -65;
threshold = threshold || -50;
} else {
//WebRTC Stream
sourceNode = audioContext.createMediaStreamSource(stream);
threshold = threshold || -45;
threshold = threshold || -50;
}

@@ -205,2 +264,6 @@

};
harker.speakingHistory = [];
for (var i = 0; i < history; i++) {
harker.speakingHistory.push(0);
}

@@ -221,9 +284,17 @@ // Poll the analyser node to determine if speaking

if (currentVolume > threshold) {
if (!harker.speaking) {
var history = 0;
if (currentVolume > threshold && !harker.speaking) {
// trigger quickly, short history
for (var i = harker.speakingHistory.length - 3; i < harker.speakingHistory.length; i++) {
history += harker.speakingHistory[i];
}
if (history >= 2) {
harker.speaking = true;
harker.emit('speaking');
}
} else {
if (harker.speaking) {
} else if (currentVolume < threshold && harker.speaking) {
for (var i = 0; i < harker.speakingHistory.length; i++) {
history += harker.speakingHistory[i];
}
if (history == 0) {
harker.speaking = false;

@@ -233,2 +304,4 @@ harker.emit('stopped_speaking');

}
harker.speakingHistory.shift();
harker.speakingHistory.push(0 + (currentVolume > threshold));

@@ -385,3 +458,3 @@ looper();

},{}],3:[function(require,module,exports){
},{}],5:[function(require,module,exports){
(function(window) {

@@ -388,0 +461,0 @@ var logger = require('andlog'),

@@ -1,6 +0,13 @@

localStorage.debug = true;
var hark = require('../hark.js');
var bows = require('bows');
var tagVolumes = [];
var streamVolumes = [];
var referenceVolumes = [];
for (var i = 0; i < 100; i++) {
tagVolumes.push(-100);
streamVolumes.push(-100);
referenceVolumes.push(-50);
}
(function() {

@@ -20,2 +27,4 @@ //Audio Tag Demo

//log('volume change', volume, threshold);
tagVolumes.push(volume);
tagVolumes.shift();
});

@@ -50,2 +59,4 @@

log(volume, threshold)
streamVolumes.push(volume);
streamVolumes.shift();
});

@@ -59,1 +70,32 @@

})();
(function () {
function drawLine(canvas, data, color) {
var drawContext = canvas.getContext('2d');
drawContext.moveTo(0,canvas.height);
drawContext.beginPath();
drawContext.strokeStyle = color;
for (var i = 0; i < data.length; i++) {
var value = -data[i];
var percent = value / 100;
var height = canvas.height * percent;
var vOffset = height; //canvas.height - height - 5;
var hOffset = i * canvas.width / 100.0;
drawContext.lineTo(hOffset, vOffset);
}
drawContext.stroke();
}
function draw() {
var canvas = document.querySelector('canvas');
if (!canvas) return;
var drawContext = canvas.getContext('2d');
drawContext.clearRect (0, 0, canvas.width, canvas.height);
drawLine(canvas, tagVolumes, 'green');
drawLine(canvas, streamVolumes, 'blue');
drawLine(canvas, referenceVolumes, 'black');
window.requestAnimationFrame(draw);
}
window.requestAnimationFrame(draw);
})();

@@ -9,3 +9,3 @@ (function(e){if("function"==typeof bootstrap)bootstrap("hark",e);else if("object"==typeof exports)module.exports=e();else if("function"==typeof define&&define.amd)define(e);else if("undefined"!=typeof ses){if(!ses.ok())return;ses.makeHark=e}else"undefined"!=typeof window?window.hark=e():global.hark=e()})(function(){var define,ses,bootstrap,module,exports;

for(var i=0, ii=fftBins.length; i < ii; i++) {
for(var i=4, ii=fftBins.length; i < ii; i++) {
if (fftBins[i] > maxVolume && fftBins[i] < 0) {

@@ -32,6 +32,7 @@ maxVolume = fftBins[i];

var options = options || {},
smoothing = (options.smoothing || 0.5),
interval = (options.interval || 100),
smoothing = (options.smoothing || 0.1),
interval = (options.interval || 50),
threshold = options.threshold,
play = options.play,
history = options.history || 10,
running = true;

@@ -55,7 +56,7 @@

if (typeof play === 'undefined') play = true;
threshold = threshold || -65;
threshold = threshold || -50;
} else {
//WebRTC Stream
sourceNode = audioContext.createMediaStreamSource(stream);
threshold = threshold || -45;
threshold = threshold || -50;
}

@@ -84,2 +85,6 @@

};
harker.speakingHistory = [];
for (var i = 0; i < history; i++) {
harker.speakingHistory.push(0);
}

@@ -100,9 +105,17 @@ // Poll the analyser node to determine if speaking

if (currentVolume > threshold) {
if (!harker.speaking) {
var history = 0;
if (currentVolume > threshold && !harker.speaking) {
// trigger quickly, short history
for (var i = harker.speakingHistory.length - 3; i < harker.speakingHistory.length; i++) {
history += harker.speakingHistory[i];
}
if (history >= 2) {
harker.speaking = true;
harker.emit('speaking');
}
} else {
if (harker.speaking) {
} else if (currentVolume < threshold && harker.speaking) {
for (var i = 0; i < harker.speakingHistory.length; i++) {
history += harker.speakingHistory[i];
}
if (history == 0) {
harker.speaking = false;

@@ -112,2 +125,4 @@ harker.emit('stopped_speaking');

}
harker.speakingHistory.shift();
harker.speakingHistory.push(0 + (currentVolume > threshold));

@@ -114,0 +129,0 @@ looper();

@@ -7,3 +7,3 @@ var WildEmitter = require('wildemitter');

for(var i=0, ii=fftBins.length; i < ii; i++) {
for(var i=4, ii=fftBins.length; i < ii; i++) {
if (fftBins[i] > maxVolume && fftBins[i] < 0) {

@@ -30,6 +30,7 @@ maxVolume = fftBins[i];

var options = options || {},
smoothing = (options.smoothing || 0.5),
interval = (options.interval || 100),
smoothing = (options.smoothing || 0.1),
interval = (options.interval || 50),
threshold = options.threshold,
play = options.play,
history = options.history || 10,
running = true;

@@ -53,7 +54,7 @@

if (typeof play === 'undefined') play = true;
threshold = threshold || -65;
threshold = threshold || -50;
} else {
//WebRTC Stream
sourceNode = audioContext.createMediaStreamSource(stream);
threshold = threshold || -45;
threshold = threshold || -50;
}

@@ -82,2 +83,6 @@

};
harker.speakingHistory = [];
for (var i = 0; i < history; i++) {
harker.speakingHistory.push(0);
}

@@ -98,9 +103,17 @@ // Poll the analyser node to determine if speaking

if (currentVolume > threshold) {
if (!harker.speaking) {
var history = 0;
if (currentVolume > threshold && !harker.speaking) {
// trigger quickly, short history
for (var i = harker.speakingHistory.length - 3; i < harker.speakingHistory.length; i++) {
history += harker.speakingHistory[i];
}
if (history >= 2) {
harker.speaking = true;
harker.emit('speaking');
}
} else {
if (harker.speaking) {
} else if (currentVolume < threshold && harker.speaking) {
for (var i = 0; i < harker.speakingHistory.length; i++) {
history += harker.speakingHistory[i];
}
if (history == 0) {
harker.speaking = false;

@@ -110,2 +123,4 @@ harker.emit('stopped_speaking');

}
harker.speakingHistory.shift();
harker.speakingHistory.push(0 + (currentVolume > threshold));

@@ -112,0 +127,0 @@ looper();

{
"name": "hark",
"version": "0.2.1",
"version": "1.0.0",
"description": "Converts an audio stream to speech events in the browser",

@@ -14,3 +14,3 @@ "main": "hark.js",

"bows": "~0.1.3",
"attachmediastream": "0.0.1"
"attachmediastream": "1.0.x"
},

@@ -17,0 +17,0 @@ "dependencies": {

@@ -69,3 +69,3 @@ # Hark

* `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 -65db for audio tags, -45db for rtc streams) the volume at which `speaking`/`stopped\_speaking` events will be fired
* `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.

@@ -75,5 +75,5 @@

Fine tuning the volume threshold is the main configuration setting for how this module will behave. The levels of -65db and -45db for audio tags and rtc streams respectively have been chosen based on some basic experimentation on mysetup, but you may wish to change them (and should if it improves your app).
Fine tuning the volume threshold is the main configuration setting for how this module will behave. The level of -50db have been chosen based on some basic experimentation on mysetup, but you may wish to change them (and should if it improves your app).
**What is dB?** Decibels are how sound is measured. The loudest sounds on your system will be at 0dB, and silence in webaudio is -100dB. Speech seems to be between roughly -65dB and -45dB depending on the volume and type of source. If speaking events are being fired too frequently, you would make this number higher (i.e. towards 0). If they are not firing frequently enough (you are speaking loudly but no events are firing), make the number closer to -100dB).
**What is dB?** Decibels are how sound is measured. The loudest sounds on your system will be at 0dB, and silence in webaudio is -100dB. Speech seems to be above -50dB depending on the volume and type of source. If speaking events are being fired too frequently, you would make this number higher (i.e. towards 0). If they are not firing frequently enough (you are speaking loudly but no events are firing), make the number closer to -100dB).

@@ -88,3 +88,4 @@

Chrome 27+ currently
Chrome 27+
Firefox

@@ -91,0 +92,0 @@ ## License

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc