Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reading from microphone every x seconds #30

Open
ragymorkos opened this issue May 7, 2019 · 0 comments
Open

Reading from microphone every x seconds #30

ragymorkos opened this issue May 7, 2019 · 0 comments

Comments

@ragymorkos
Copy link

Thanks for sharing this great package! I am currently trying to read data from the microphone every x seconds using the code below, where I just try to print out the length of the buffer obtained from the microphone:

const setTimeoutPromise = require("util").promisify(setTimeout);
const AudioIO = require("naudiodon").AudioIO;
const interval = 1;
let handleMICPromises = [];
let readPromises = [];
let microphoneOn = false;

const options = {inOptions: {channelCount: 1, sampleFormat: 16, sampleRate: 8000}};
const microphoneStream = new AudioIO(options);

function handleMIC(resolve, reject)
{
    if (microphoneOn)
    {
        const readPromise = setTimeoutPromise(interval * 1000);
        readPromise.then(read);
        readPromises.push(readPromise);
    }
    else
    {
        microphoneStream.quit();
    }
    handleMICPromises.shift();
    resolve();
}

function read()
{
    readPromises.shift();
    const handleMICPromise = new Promise(handleMIC);
    handleMICPromises.push(handleMICPromise);

    if (microphoneStream.readable)
    {
        let buffer = microphoneStream.read();
        if (buffer !== null)
        {
            console.log(buffer.byteLength);
        }
    }
}

process.on("SIGINT", () => {
    microphoneOn = false;
    Promise.all(readPromises.concat(handleMICPromises)).then(()=> {process.exit();});
});

microphoneStream.start();
microphoneOn = true;
const handleMICPromise = new Promise(handleMIC);
handleMICPromises.push(handleMICPromise);

When I try to run the above file, it prints out 16384 for the length of the buffer every 1 second. This seems about right, since audio sampled every second at 8000 samples per second with a sampling format of 16 (2 bytes) will be about 16,000 bytes. So far so good.

However, when I try to set the interval at the beginning of the code to, say, 5, I still get the same buffer length of 16384 printed out. This shouldn't be right. According to the NodeJS documentation on read(), read() should read everything that's in the readable stream. But this is not happening in this case.

Let me know if I'm missing something, or if this is a possible bug. I also looked at the resulting audio file when I parse and save the results of the buffer to a WAV file, and I only get 1 second worth of audio for every call to read() even if I make that call to read() every 5 seconds.

Thanks in advance for your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant