speech-recorder is a cross-platform, native node.js addon for getting a stream of audio from a device's microphone. Using speech-recorder, you can also get only the audio that corresponds to someone speaking.
This module is used for speech recognition in Serenade. Serenade enables you to write code through natural speech, rather than typing.
speech-recorder has been tested on Windows 10, macOS 10.14+, and Ubuntu 18.04+ (and may work on other platforms as well).
To install speech-recorder, run:
yarn add speech-recorder
If you're using this library with Electron, you should probably use electron-rebuild.
This library uses two voice activity detection mechanisms: a fast first pass (the WebRTC VAD), and a slightly slower, but much more accurate, second pass (the Silero VAD). See below for the various options you can supply to each.
When you start recording, you can register various callbacks. onAudio
is called when any audio comes in from the microphone. onChunkStart
is called when a chunk of speech begins, and onChunkEnd
is called when speech ends.
const { SpeechRecorder } = require("speech-recorder");
const recorder = new SpeechRecorder({
onChunkStart: ({ audio }) => {
console.log(Date.now(), "Chunk start");
},
onAudio: ({ speaking, probability, volume }) => {
console.log(Date.now(), speaking, probability, volume);
},
onChunkEnd: () => {
console.log(Date.now(), "Chunk end");
},
});
console.log("Recording for 5 seconds...");
recorder.start();
setTimeout(() => {
console.log("Done!");
recorder.stop();
}, 5000);
You can write all audio from the microphone to a file with:
const { SpeechRecorder } = require("speech-recorder");
const writeStream = fs.createWriteStream("audio.raw");
const recorder = new SpeechRecorder({
onAudio: ({ audio }) => {
writeStream.write(audio);
}
});
Or, just the speech with:
const { SpeechRecorder } = require("speech-recorder");
const writeStream = fs.createWriteStream("audio.raw");
const recorder = new SpeechRecorder({
onAudio: ({ audio, speech }) => {
if (speech) {
writeStream.write(audio);
}
}
});
You can get a list of supported devices with:
const { devices } = require("speech-recorder");
console.log(devices());
consecutiveFramesForSilence
: How many frames of audio must be silent beforeonChunkEnd
is fired. Default10
.consecutiveFramesForSpeaking
: How many frames of audio must be speech beforeonChunkStart
is fired. Default1
.device
: ID of the device to use for input (i.e., from the example above). Specify-1
to use the system default. Default-1
.leadingBufferFrames
: How many frames of audio to keep in a buffer that's included inonChunkStart
. Default10
.onChunkStart
: Callback to be executed when speech starts.onAudio
: Callback to be executed when any audio comes in.onChunkEnd
: Callback to be executed when speech ends.samplesPerFrame
: How many audio samples to be included in each frame from the microphone. Default480
.sampleRate
: Audio sample rate. Default16000
.sileroVadBufferSize
: How many audio samples to pass to the VAD. Default2000
.sileroVadRateLimit
: Rate limit, in frames, for how frequently to call the VAD. Default3
.sileroVadSilenceThreshold
: Probability threshold for speech to transition to silence. Default0.1
.sileroVadSpeakingThreshold
: Probability threshold for silence to transition to speech. Default0.3
.webrtcVadLevel
: Aggressiveness for the first-pass VAD filter.0
is least aggressive, and3
is most aggressive. Default3
.webrtcVadBufferSize
: How many audio samples to pass to the first-pass VAD filter. Default480
. Can only be160
,320
, or480
.webrtcVadResultsSize
: How many first-pass VAD filter results to keep in history. Default10
.
If you want to build speech-recorder from source, first install the necessary dependencies by running:
./setup.sh <arch>
Where <arch>
specifies the architecture you'd like to build for and is one of x86
, x64
, or arm64
. If you're not sure, you probably want x64
.
Then, you can build speech-recorder with:
./build.sh <arch>