7

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.

Ten Bitcomb
  • 2,316
  • 1
  • 25
  • 39
  • 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 Answers2

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
  • 1
    Not 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
  • @JustinTaddei Indeed it doesn't. I fixed the "bug" – vanntile Jan 12 '21 at 07:54
  • 1
    I 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.