almoghamdani / audify

Play/Stream/Record PCM audio data & Encode/Decode Opus to PCM audio data
MIT License
121 stars 20 forks source link

RtAudio input stops after ~7 seconds, when output is not opened #28

Open bradisbell opened 2 years ago

bradisbell commented 2 years ago

I'm having trouble with the basic audio input example. The audio input callback stops being called after about 7 seconds. Some notes:

Any thoughts as to the cause of this and how it can be solved?

bradisbell commented 2 years ago

The problem also occurs on Node.js v16.15.1.

bradisbell commented 2 years ago

I've found that this problem doesn't occur as long as I have an output running too. It seems rtAudio.write() must be called regularly for the input to work.

Using null for the output options doesn't work, because calls to .write() cause a crash.

TibixDev commented 2 years ago

I have almost the same exact same issue on Linux with PulseAudio. Based on your workaround I implemented a very dumb way of dealing with it after some experimentation.

// Start the stream
rtAudio.start();
setTimeout(() => {
    try {
        rtAudio.write(null);
    } catch {
        console.log("RTAudio fixed, enjoy your stream.");
    }
})

Calling it once without setTimeout does not work. Putting it in setTimeout and the default 0ms timeout will make it work. I did not have to call it anymore after this.

hamitzor commented 9 months ago

I found out that it's not really related with the write method, you can actually call isStreamOpen or isStreamRunning methods to keep it running. It almost feels like a reference counter or something that goes to zero if you don't reference it. I'm pretty sure it's about the C++ implementation of the binding.

hamitzor commented 9 months ago

Actually, even this worked for me:

//  Not even calling the foo function, just declaring it.
const foo = () => {
    recorder.isStreamOpen() // keeps the recorder stream running.
}
o0101 commented 8 months ago

It's a Schrödinger's stream: if nobody is measuring if it's open or not, it declines to collapse its superposition into a concrete state of existence 🤣