-
Notifications
You must be signed in to change notification settings - Fork 0
/
microphone.js
60 lines (51 loc) · 1.21 KB
/
microphone.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
var threshold = 11000;
var fs = require('fs');
var process = require('child_process');
var Microphone = require('mic');
// var Player = require('player');
var player = require('play-sound')();
function getMicrophone() {
return Microphone({
'rate': '16000',
'channels': '1',
// 'debug': true,
// 'exitOnSilence': 6
});
}
function runColor(file, cb) {
process.exec('sudo python ./rpi_ws281x/python/shutup/' + file, cb);
}
var mic = getMicrophone();
var stream = mic.getAudioStream();
// var sound = new Player('./example.mp3');
var sound = fs.readFileSync('./example.mp3');
var pause = false;
runColor('white.py', function() {
stream.on('data', function (data) {
if (pause) {
return;
}
var max = 0;
for (var i=0; i<data.length; i += 2) {
var cur = data.readInt16LE(i);
if (cur > max) {
max = cur;
}
}
if (max > threshold && max !== 32000) {
console.log('beeeeeeep', max);
pause = true;
runColor('red.py');
player.play('./example.mp3', function() {
console.log('beep end');
runColor('white.py');
pause = false;
});
}
});
stream.on('error', function(e) {
console.log('error: ' + e);
});
console.log('starting mic');
mic.start();
});