serenadeai / speech-recorder

speech-recorder is a node.js module for streaming audio from a device's microphone and filtering for speech.
MIT License
86 stars 19 forks source link

Crash on recorder start after stop #32

Closed dsouza95 closed 1 year ago

dsouza95 commented 1 year ago

Calling the start and stop methods of the recorder instance works great the first time, however the second time the start method is called, a crash occurs on the cpp code. This means I am unable to reuse the same recorder instance, so I tried to create a new recorder instance before every start, but the VAD behaves erratically and there is some overhead from loading the VAD model again.

@tmacwill any insights on what could be the issue here? I would be happy to make a PR to fix this, and any tips may speed up the process :)

For instance, the following example will lead to a crash when calling recorder.start() a second time:

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! Waiting...");
    recorder.stop();
    setTimeout(() => {
        console.log("Recording for 5 seconds...");
        recorder.start();

        setTimeout(() => {
            console.log("Done Again!");
            recorder.stop();
        }, 5000)
    }, 1000)
}, 5000);
tmacwill commented 1 year ago

thanks for the report! I just looked into why we don't run into this in the serenade app, and it's because we also re-create the object instance each time: https://github.com/serenadeai/serenade/blob/master/client/src/main/stream/microphone.ts#L47. (at some point, I think we had a caching layer somewhere that prevented loading the VAD models multiple times?)

I skimmed through the source, and nothing jumps out at me yet. my first step would probably be to get a stack trace via gdb in the C++ codebase—building https://github.com/serenadeai/speech-recorder/tree/master/lib via CMake creates a small test binary from test/main.cpp you can use. my intuition is that either we're not closing out the portaudio stream correctly, not resetting some state in the stop functions, or there's a thread-related issue from stopping/starting again.

dsouza95 commented 1 year ago

Oh, it's great to know that there is some caching, perhaps I can make see if I can make re-creating work for my case then. Otherwise, I will try debugging the parts you suggested. Thanks a lot!

dsouza95 commented 1 year ago

@tmacwill re-creating the object instance is working like a charm now. Thanks!