respeaker / get_started_with_respeaker

This is the wiki of ReSpeaker Core V2, ReSpeaker Core and ReSpeaker Mic Array.
257 stars 83 forks source link

Improving latency of audio #231

Open alexdufetel opened 5 years ago

alexdufetel commented 5 years ago

I'm using "ReSpeaker Mic Array - Far-field w/ 7 PDM Microphones" with a Raspberry Pi. I can get it to work properly for audio capture / hotword detection. I also want to display volume from my Node app, but I can't get my VU meter under a ~1s latency.

I'm capturing audio via arecord called from Node. I measure audio levels directly through the data being sent by the card (see code below). Any suggestion on how I can bring the latency down? I'd like my VU meter to react approximately as fast as the leds on the Mic Array. I'm open to any solution.

Thanks to anyone who can help out!

` const arProcess = spawn('arecord', [ '-q', '-r', 16000, '-c', 1, '-t', 'wav', '-f', 'S16_LE',
'-D', 'plughw:AR', '-V', 'mono', ], { encoding: 'binary'});

arProcess.stdout.on('data', _update); function _update(buffer){ var bufLength = buffer.length; var min = 0; var max = 0; var sample;

    for (var i = 0;  i < bufLength; i += 2) {
        //Reads an unsigned 16-bit integer from buf at the specified offset with specified endian format (readUInt16BE() returns big endian, readUInt16LE() returns little endian).
        sample = buffer.readInt16LE(i + 0, true);
        min = (sample < min ? sample : min);
        max = (sample > max ? sample : max);
    }
   console.log(_log10(Math.max(max, -min) / 32768) * 20); 
}

`