Last active
August 30, 2018 20:14
-
-
Save iamhosseindhv/175c9dfaaffbc6d0237b7422efba8069 to your computer and use it in GitHub Desktop.
Get audio from built-in mic, ready to be processed.
This file contains hidden or 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
| /** | |
| * Get audio from built-in mic, ready to be processed. | |
| * use rec output.wav if you want to just record audio. | |
| * | |
| * Usage: node mic.js | |
| * Prereq: brew install sox | |
| * | |
| * Note: to covert .raw output to .wav file: | |
| * sox -t raw -r 16000 -b 16 -c 1 -L -e signed-integer output.raw abbas.wav | |
| * | |
| * link: https://github.com/ashishbajaj99/mic | |
| */ | |
| var mic = require('mic'); | |
| var fs = require('fs'); | |
| var micInstance = mic({ | |
| rate: '16000', | |
| channels: '1', | |
| debug: false, | |
| exitOnSilence: 6, | |
| fileType: 'wav', | |
| }); | |
| var micInputStream = micInstance.getAudioStream(); | |
| var outputFileStream = fs.WriteStream('output.raw'); | |
| micInputStream.pipe(outputFileStream); | |
| micInputStream.on('data', function (data) { | |
| console.log("Recieved Input Stream: "); | |
| console.log(data.length); | |
| }); | |
| micInputStream.on('error', function (err) { | |
| console.log("Error in Input Stream: " + err); | |
| }); | |
| micInputStream.on('startComplete', function () { | |
| console.log("Got SIGNAL startComplete"); | |
| setTimeout(function () { | |
| micInstance.stop(); | |
| }, 5000); | |
| }); | |
| micInputStream.on('stopComplete', function () { | |
| console.log("Got SIGNAL stopComplete"); | |
| }); | |
| micInputStream.on('pauseComplete', function () { | |
| console.log("Got SIGNAL pauseComplete"); | |
| setTimeout(function () { | |
| micInstance.resume(); | |
| }, 5000); | |
| }); | |
| micInputStream.on('resumeComplete', function () { | |
| console.log("Got SIGNAL resumeComplete"); | |
| setTimeout(function () { | |
| micInstance.stop(); | |
| }, 5000); | |
| }); | |
| micInputStream.on('silence', function () { | |
| console.log("Got SIGNAL silence"); | |
| }); | |
| micInputStream.on('processExitComplete', function () { | |
| console.log("Got SIGNAL processExitComplete"); | |
| }); | |
| micInstance.start(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment