Picovoice / porcupine

On-device wake word detection powered by deep learning
https://picovoice.ai/
Apache License 2.0
3.77k stars 499 forks source link

PorcupineInvalidStateError: Porcupine is not initialized #1240

Closed Suzzit6 closed 7 months ago

Suzzit6 commented 8 months ago

Have you checked the docs and existing issues?

SDK

Node.js

Porcupine package version

3.0.2

Framework version

21

Platform

Windows (x86_64)

OS/Browser version

11

Describe the bug

I am using node-record-lpcm to record audio and trying to trigger wake word from built in keywords but getting the error mentioned i the title.

const accessKey = "Acccess key here"

let porcupine = new Porcupine(
  accessKey,
  [BuiltinKeyword.JARVIS],
  [0.5]
);

const recording = recorder.record({
  sampleRate: 16000
})
.stream()
recording.pipe(file)

  recording.on('data', (data) => {
    console.log("processing data....")
    const result = porcupine.process(data);

    if (result) {
      console.log("wake word detected")
      setTimeout(() => {
        recording.close()
        // recognizeSpeech()
      }, 9000)
    }
  })
      porcupine.release();
//When i use    porcupine.release(); in setTimeout i get Size of frame array provided to 'process' (8192) does not match the engine 'frameLength' (512)",  error 

Steps To Reproduce

error

Expected Behavior

I want it to detect the wake word and run recording close function after mentioned time period in set timeout

Suzzit6 commented 8 months ago

recording.on("data", (data) => {

let result; let chunkSize = 512; for (let i = 0; i < data.length; i += chunkSize) { let chunk = data.slice(i, i + chunkSize); result = porcupine.process(chunk); } // const result = porcupine.process(data); if (result !== -1) { console.log("Wake word detected"); setTimeout(() => { recording.stop() // recognizeSpeech(); porcupine.release(); }, 10000); }

});

i chunked the frame length to 512, but still the porcupine is unable to detect the wake word

ErisMik commented 8 months ago

As you've figured out Porcupine must get it's PCM audio in 512 chunks, so that change was good. It's hard to tell because you haven't given me a complete piece of code to look at, but it looks like you are calling porcupine.release(); too early. This must only be done once at cleanup time and will leave Porcupine in an unusable state afterwards (it's resources have been cleaned up).

In your first snippet because of the async nature of Javascript release is being called before any of the calls to process . I suggest you go back and take a second look at your code and make sure you aren't calling release early.

Suzzit6 commented 8 months ago

but it looks like you are calling porcupine.release(); too early. This must only be done once at cleanup time and will leave Porcupine in an unusable state afterwards (it's resources have been cleaned up).

Thanks for the response , i figured out what went ugly with my code. I think there's nothing wrong with .release since i used it in setTimeout 10 secs (as seen in my comment ). The actual problem was node-record-lpcm library when i used pvrecorder library (pico voice's audio recording library) and sliced its buffer and data (took from recording. Read()) to 512 frame length without using for loop this time. The problem was pv recorder doesn't allows us to save the file which was recorded in the mic hence i ended up using both, pv recorder for detecting wake word and after it is detected i used a function to call node-record-lpcm which will then record and save the audio file to recognize speech to text , tho settimeout is still being used at 10secs.