I have a simple program to record microphone sound in Windows, made with Node.js. I have some troubles when I execute it.
At the time of recording it works perfectly and creates a .wav file as it has to do. But I make this inside a loop and taking user options (as a program with menu). The problem is that when I press again the key to record another .wav file, my program breaks without giving any error or warning.
I leave here my code for this part.
const fs = require('fs')
const record = require('node-record-lpcm16')
const inquirer = require('inquirer')
var file = fs.createWriteStream('test.wav', { encoding: 'binary' })
main()
async function main(){
var option =0
while(option!=1){
//take option
var option = await getOption()
switch (option){
case 0:
await startRecording() //Wait for user to press ENTER
record.start().pipe(file)
console.log("Recording audio")
await stopRecording() //Wait for user to press ENTER
record.stop()
console.log("Audio finished")
break;
case 1:
process.exit(0)
}
}
}
function startRecording()
{
const questions = [
{
name: 'key',
type: 'input',
message: 'Press enter to start recording'
}]
return inquirer.prompt(questions)
}
function stopRecording()
{
const questions = [
{
name: 'key',
type: 'input',
message: 'Press enter to stop recording'
}]
return inquirer.prompt(questions)
}
function getOption(){
const questions = [
{
name: 'option',
type: 'integer',
message: 'Select your option (0 or 1): ',
validate: function( value ) {
if (value.length) {
return true
} else {
return 'Select your option (0 or 1): '
}
}
}]
return inquirer.prompt(questions)
}
The program breaks after printing the message of stopRecording() function for second time.
Does anyone know why is this happening?