ashishbajaj99 / mic

A simple stream wrapper for arecord (Linux (including Raspbian)) and sox (Mac, Windows). Returns a Passthrough stream object so that stream control like pause(), resume(), pipe(), etc. are all available.
MIT License
103 stars 61 forks source link

Write .wav file format with header. #14

Closed VolodymyrTymets closed 7 years ago

VolodymyrTymets commented 7 years ago

I need make mathematical calculation with sound stream from microphone. For now it can be fft transforming. I made this before with .wav file by decoding via wav-decoder.

But when i try decode buffer from microphone as below, a have exeption Invalid WAV file.

micInputStream.on('data', function(data) {
   WavDecoder.decode(buffer);
});

Even, when i tried write wav file format then decode it. I also had exeption.

var micInputStream = micInstance.getAudioStream(); 
var outputFileStream = fs.WriteStream('output.wav'); 
micInputStream.pipe(outputFileStream);
micInstance.start();

I suppose, this because raw format have no header.

So can i some way decode micInputStream data directly or write wav file format via mic?

nfriedly commented 7 years ago

Try using https://www.npmjs.com/package/wav:

var FileWriter = require('wav').FileWriter;
var mic = require('mic'); // requires arecord or sox, see https://www.npmjs.com/package/mic

var micInstance = mic({
  rate: '16000',
  channels: '1',
  debug: true
});

var micInputStream = micInstance.getAudioStream();

var outputFileStream = new FileWriter('./test.wav', {
  sampleRate: 16000,
  channels: 1
});

micInputStream.pipe(outputFileStream);

micInstance.start();

setTimeout(function() {
  micInstance.stop();
}, 5000);

I just put that together for https://github.com/TooTallNate/node-wav/pull/25, but it looks like it would also do what you want.

ashishbajaj99 commented 7 years ago

Yup the above is how I would do it as well. Thanks @nfriedly