0

I'd like to create a node app that records and parses audio from the system output device in my Windows 10. It looks like it's a real challenge because I've checked many npm packages but didn't find anything that allows audio recording from the output device. So I found a third-party CLI utility that works for Windows 10 I'm not sure that I should give its name because it could be threatened like marketing etc.

so this CLI utility allows to record a loopback audion for the output device and save it to the WAV file.
I'm wondering if there exists some way to intercept the data stream that comes from this utility to the WAV file from my NODE.js app to handle it? or if the CLI utility doesn't provide some API to get this data it isn't possible?

I also was thinking about the immediate opening of the WAV file and reading this file during the recording to get the recent data in the node.js app and handle it but I'm not sure that it's possible due to the write/read stream for the same file (from CLI - write, and from NODE - read)

Thanks for any help or info!

P.S. Sorry if my question is stupid. I'm not familiar with such low-level things

Velidan
  • 5,526
  • 10
  • 48
  • 86
  • Have you checked this [naudiodon](https://www.npmjs.com/package/naudiodon) package? – Abdurrahim Ahmadov May 15 '22 at 11:20
  • You could create a named pipe, write there with your cli tool, and read from it in node.js – Marc May 15 '22 at 11:21
  • Hi @AbdurrahimAhmadov. Yes, I've checked it but it can't record audio from the Output device. I tried to pass the ID of the output device but it throws an error about missed channels – Velidan May 15 '22 at 12:49
  • Hi @Marc. Do you mean that I should add into the CLI tool source code a custom pipe to provide the data to the NODE.js API? – Velidan May 15 '22 at 12:50

1 Answers1

0

After a night of research, I managed to get the data in my particular situation.
I used a spawn from the node child process package to create a dedicated process where I executed the CLI utility with the appropriate arguments that send the recording data directly into the STDOUT. Please, keep in mind that exec won't work in this particular task due to the buffer issue

The most important thing here: Luckily the CLI utility has the possibility to send the recording data chunks to the STDOUT instead of the file. However, I don't know what would I do if it couldn't. I don't know if there is an existing way to intercept the data that writes directly into the output file so this answer isn't 100% fits the question. What to do if a CLI doesn't write the data into the STDOUT? If someone knows about it: please, let me know, I'll appreciate it a lot

Here is the basic code example:

import { spawn } from 'node:child_process';

 const child = spawn('fmedia', ['--record', '--dev-loopback=0', '-o', '@stdout.wav']);
  child.stdout.on('data', data => {
    console.log('stdout data -> ', data);
  })
  child.stderr.on('data', data => {
    console.log('stderr data -> ', data);
  })
  child.on('close', code => {
    console.log('child process exited with code ' + code);
  });

Kind regards

Velidan
  • 5,526
  • 10
  • 48
  • 86