0

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?

froggy_
  • 43
  • 1
  • 10

1 Answers1

0

There is nothing in the current code, indicating an error. You should check the values, you set on option. You most probably, set it to 1 on your 3rd loop and it exits.

Since, we do not know how you calculate the option, you are the only one, who can fix it.

  • 1
    I have edited my question to show how I select the option. It is similar to the way I wait for ENTER pressing. – froggy_ Oct 17 '18 at 07:27