1

I am recording audio with the package react-native-audio-record. I want to use the temporary data to visualize stuff. As in the example (function AudioRecord.on) here:

const options = {
  sampleRate: 16000,
  channels: 1,
  bitsPerSample: 16,
  wavFile: 'test.wav'
};

AudioRecord.init(options);

AudioRecord.on('data', data => {
  const chunk = Buffer.from(data, 'base64');
  console.log('chunk size', chunk.byteLength);
  // do something with audio chunk
}); 

How can I decode the variable chunk to PCM? As far as stated in another forum, this chunk does not include the headerBytes of the .wav File.

Anathapindika
  • 141
  • 1
  • 10
  • 1
    This can be broken into multiple steps, you’re asking how to convert base64 to a byte array https://stackoverflow.com/questions/21797299/convert-base64-string-to-arraybuffer?answertab=votes#tab-top and then how to convert an array buffer to an audio buffer https://stackoverflow.com/questions/50512436/how-to-convert-arraybuffer-to-audiobuffer#56747545 – fdcpp Oct 18 '21 at 18:39
  • thanks for the reply: i just edited the question. I believe, I already have the array buffer via Buffer.from(data,"base64"). As I am working with react-native, i can not use BaseAudioContext. Is there a workaround? – Anathapindika Oct 18 '21 at 19:57
  • 1
    Your options then are wrap the native Audio APIs (tedious) or perform this in pure JavaScript (also tedious). There are still multiple parts to those questions. The first is changing bytes to PCM, the second is [how to DFT / FFT in JavaScript](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/fourier-transform) – fdcpp Oct 18 '21 at 20:10
  • The fft part should be quite clear - edited the question again. I'm stuck how to get to PCM. – Anathapindika Oct 19 '21 at 06:00
  • 1
    Then there’s your question: _How to reconstruct PCM samples from raw bytes?_ – fdcpp Oct 19 '21 at 06:30

1 Answers1

2

As stated in the comments, reconstructing the PCM in pure JavaScript is tedious. I used therefore the node-package node-wav (only works if header is included) as follows:

Store the header of the specific wav options:

const header = Buffer.from([82, 73, 70, 70, 248, 167, ... ])

and concatenate the data as follows:

import wav from 'node-wav';
    
    AudioRecord.on('data', data => {
          const chunks = [header] 
          const chunk = Buffer.from(data, 'base64');
          chunks.push(chunk)
          const pcm = wav.decode(Buffer.concat(chunks)).channelData
        });

This approach works only if you can reconstruct the header. The package node-wave on the other hand is stated as high performance WAV decoder and encoder .

Anathapindika
  • 141
  • 1
  • 10