Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use the .Write function for wav module in nodejs

Tags:

node.js

wav

It's probably something simple and stupid, but the module doesn't have enough documentation in neither the github page or the npm page.

Can someone write a code example of using the .Write function for writing a wav file

like image 361
bubakazouba Avatar asked Dec 18 '15 21:12

bubakazouba


People also ask

How do you write an audio file in node JS?

Does anyone know how to write audio to a file in node? var fs = require('fs'); const { Writable } = require('stream'); var writeStream = fs. createWriteStream('./output. wav'); const outStream = new Writable({ write(chunk, encoding, callback) { console.

How do I run a node module?

The usual way to run a Node. js program is to run the globally available node command (once you install Node. js) and pass the name of the file you want to execute. While running the command, make sure you are in the same directory which contains the app.

What is fs createWriteStream?

The function fs. createWriteStream() creates a writable stream in a very simple manner. After a call to fs. createWriteStream() with the filepath, you have a writeable stream to work with. It turns out that the response (as well as the request) objects are streams.

What is fs module in node JS?

The Node.js file system module allows you to work with the file system on your computer. To include the File System module, use the require() method: var fs = require('fs'); Common use for the File System module: Read files.


1 Answers

Here's a very simple example using tonegenerator to generate raw PCM data:

var tone   = require('tonegenerator');
var wav    = require('wav');
var writer = new wav.FileWriter('output.wav');

writer.write(new Buffer(tone(220, 5))); // 220Hz for 5 seconds
writer.end();

wav.FileWriter() is a simple wrapper around wav.Writer() to write to a file directly, similar to this:

var writer = new wav.Writer();

writer.pipe(require('fs').createWriteStream('output.wav'));

writer.write(new Buffer(tone(220, 5)));
writer.end();

Long story short: wav.Writer() creates a writable stream that you can .write() raw PCM data to. Most WAVE properties are hardcoded.

like image 159
robertklep Avatar answered Sep 19 '22 05:09

robertklep