xiqi / react-native-live-audio-stream

Get live audio stream data for React Native (works for iOS and Android)
MIT License
71 stars 29 forks source link

Get data as Float32Array #18

Open meherhowji opened 1 year ago

meherhowji commented 1 year ago

What change would you recommend if I want to add a new data format as Float32Array, currently what's returned is base64. I am trying to feed this data to a trained model and would like to avoid the conversion if natively I can fetch such a format.

hans00 commented 1 year ago

At bit16 sample code

const S16_MAX = 2 ** 15
const buffer = Buffer.from(data, 'base64')
const samples = new Float32Array(Array.from(
    { length: buffer.length / 2 },
    (v, i) => buffer.readInt16LE(i * 2) / S16_MAX,
  ))
meherhowji commented 1 year ago

Thank you @hans00 for the code. I will plug this and test it out. 👍

alena424 commented 6 months ago

Can you please explain, why this approach is not correct? I m also training to use the model but there so few documentation on how to do it in RN.

const float32Array = new Float32Array(audioBuffer.buffer)

hans00 commented 6 months ago

Can you please explain, why this approach is not correct? I m also training to use the model but there so few documentation on how to do it in RN.

const float32Array = new Float32Array(audioBuffer.buffer)

The source data is S16 not F32