Sox in Node.js
I know that we can't currently pass audio data in to the Node.Js object, but I'm wondering if I can use node.js to call the sox package in a way where I could access my system's internal microphone. I'm currently doing something like:
const record = require('node-record-lpcm16');
record
.start({
sampleRateHertz: sampleRateHertz,
threshold: 0,
// Other options, see https://www.npmjs.com/package/node-record-lpcm16#options
verbose: false,
recordProgram: 'sox', // Try also "arecord" or "sox"
silence: '10.0',
})
But this is throwing me a "spawn sox ENOENT" error. I have a feeling it has something to do with sox not being found in the path that max is using for node, but I'm not entirely sure how to remedy this. Does anyone have any insight? I'm not new to programing but relatively new to node so any help is appreciated.
Thanks in advance!
Hey there,
haven't used Sox with N4M myself yet but the error spawn ENOENT error hints at that the sox command wasn't available in your $PATH. Have you installed SoX on your machine and verified that it's found in your $PATH using the Terminal?
Just to be sure you might also wanna restart Max after installing SoX on your machine and try again.
Let me know if that worked or if u need further help.
Florian
Hi Florian,
Thanks for the advice. I managed to solve the error by including "process.env.PATH = '/file location of sox/'" in the script. I'm not sure if this is in line with best practices for node, but it seems to be working at the moment.
Hey Peter.
Glad you where able to make some progress. However, overwriting process.env.PATH like this might lead to some unexpected behaviour. If you need to pull SoX into the PATH variable at runtime it would be better to extend instead of overwriting as the latter might make other commands unavailable for child processes:
const { delimiter } = require("path");
const paths = process.env.PATH.split(delimiter);
paths.push("<sox_path");
process.env.PATH = paths.join(delimiter);
You can read more about the platform specific path delimiter here: https://nodejs.org/api/path.html#path_path_delimiter
However, it might be even better to ensure that SoX can generally be found in your PATH instead of having to do this at runtime. I neither know which platform you are on nor do I know how you installed SoX but for example on OSX a typical way to install would be Homebrew (which takes care of the correct PATH setup by itself) or by manually adjusting your bash_profile. I'm sure a quick googling for your platform, the PATH environment variable and maybe even SoX will give helpful results.
Florian
Thanks Florian. The code you supplied is working equally well and seems a bit safer. I followed the instructions at this link and am getting the terminal to successfully return the correct path, but it doesn't seem to be working once I run the script in Max. I've tried it with a few npm packages that utilize SoX so I'm not confident that the package has a problem, but I'm wondering if there's something particular about the Max/SoX relationship that I'm missing. Thanks for your help on this. Glad we got it working in a workaround, just hoping I'm not missing something systemic about node.