I'm writing an oscillator in JavaScript that creates a sweep(i.e. chirp) between sine wave frequencies. For testing, I'd like to write the samples(which are floats) to a wav file. How would I do this in Node.js? I've seen lots of information on the browser end of things but not anything specific to Node or anything that relies on browser APIs.
Asked
Active
Viewed 2,302 times
7
-
there's a routine in audiorecorder.js that can do that. you don't need the whole lib, which is for browsers, but it does have that wav-making routine... – dandavis Apr 08 '16 at 21:32
2 Answers
4
This can be done using the minimal package node-wav
and a snippet similar to the one below:
First, install the dependency:
npm i node-wav
Then, use something like
let fs = require('fs');
let wav = require('node-wav');
// Parameters for the below data
const size = 5000
const amplitude = 128
const sampleRate = 20
// Generate some random data for testing
const data = (new Array(3)).fill((new Array(size)).fill(Math.random() * amplitude))
let buffer = wav.encode(data, { sampleRate: sampleRate, float: true, bitDepth: 32 });
fs.writeFile("test.wav", buffer, (err) => {
if (err) return console.log(err);
console.log("test.wav written");
});
Considering that you already know the application of your data, you know all the "constant" parameters (size of the output, bitrate, the actual data to be written, bitdepth).

vanntile
- 2,727
- 4
- 26
- 48
-
1Not really important to the answer, since it's only used to generate data for testing, but I figured I'd mention that [`Math.random`](https://tc39.es/ecma262/#sec-math.random) doesn't take any arguments. – Justin Taddei Jan 12 '21 at 07:51
-
-
1I wasn't able to get node-wav working. I found an alternative: [wav-encoder](https://www.npmjs.com/package/wav-encoder) which has similar syntax. – ATutorMe Mar 27 '21 at 02:27
-
Although it hasn't been maintained for 4 years (as of writing this), `wav-encoder` is still working. Given that the wav format specs haven't changed much in the last decade (see [Sustainability of Digital Formats: Planning for Library of Congress Collections: WAVE Audio File Format](https://www.loc.gov/preservation/digital/formats/fdd/fdd000001.shtml)) these modules will probably all do the job (unless core node or js changes break them of course). – ATutorMe Mar 27 '21 at 02:35
-3
You can use the built in Node.js fs.writeFile()
api to write to a file.
By the way I see it all you have to do is loop over your audio samples, add them to a string within the iteration, and put that string into a file like so:
const fs = require("fs");
// Code to generate audio
let audio = "";
samples.forEach((sample) => {
audio += sample;
});
fs.writeFile("path/to/file.wav or .mp3", audio, (err) => {
if (err) return console.error(err);
console.log("File successfully saved!");
});
If I'm understanding your question correctly, then this should work.

GoodStuffing123
- 172
- 10
-
1
-
You can add .wav or .mp3 to the end of your file path whichever works for you – GoodStuffing123 Jan 17 '21 at 18:53
-
1Oh really? So if I create a text file and change the extension to `.xls` it's now an excel spreadsheet? – WestCoastProjects Jan 17 '21 at 18:54
-
Hmm... ok... my answer assumed that your data was already in .wav or .mp3 format. What format is it in if not .wav or .mp3? Also I don't know about text to excel spreadsheet but I know you can do that with .jpg to .png and back again with no problems. – GoodStuffing123 Jan 17 '21 at 18:58