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

How to get a valid wav file from micInputStream.on('data') ? #44

Open jrichardsz opened 9 months ago

jrichardsz commented 9 months ago

I'm using vosk-api for nodejs and in its sample, they use mic:

https://github.com/alphacep/vosk-api/blob/master/nodejs/demo/test_microphone.js#L27

micInputStream.on('data', data => {
    if (rec.acceptWaveform(data))
        console.log(rec.result());
    else
        console.log(rec.partialResult());
});

I'm trying to understand what kind of buffer is the data object and why is compatible with vosk

According to the vosk lib, buffer is

@param {Buffer} data audio data in PCM 16-bit mono format

But if I try to read the data object as wav file with the following libraries, I got this error: Not a supported format

Also if I try to get the standard wav sections (header, data, etc) of the data object I got this:

micInputStream.on('data', async (data) => {    
  if (rec.acceptWaveform(data)){

      console.log("0  >  4 : "+data.slice(0,4).toString())
      console.log("8  > 12 : "+data.slice(8,12).toString())
      console.log("12 > 14 : "+data.slice(12,14).toString())
      console.log("36 > 40 : "+data.slice(36,40).toString())
      console.log("45 > end:", data.slice(45))

      console.log("data:"+JSON.stringify(rec.result()));

Output

image

The received buffer from microphone (https://www.npmjs.com/package/mic) is not a valid wav file but works for vosk


Question

Related issues

https://github.com/alphacep/vosk-api/issues/1504


If anyone here can point me in the right direction of documentation, I will appreciate.

Thanks!