0

I am using flutter_sound package to record audio from mic. It provides data in stream of Uint8List. So how can I calculate amplitude from it. I have found many answers in other language but I was having hard time interpreting it into dart.

for reference,

Reading in a WAV file and calculating RMS

Detect silence when recording

how can i translate byte[] buffer to amplitude level

if anyone can interpret this into dart so that I can calculate amplitude

Pokaboom
  • 502
  • 4
  • 19
  • What byte format and codec is your audio in? It looks like you can control the codec in `startRecorder` for example `codec: Codec.pcm16`. Pick pcm16 or float32, then every chunk of bytes you get, interpret as shorts or floats with `ByteData.asXXX`. Then perform your RMS as desired. I'd suggest using float32 simply because then you'll get numbers between -1 and +1 and don't need to normalise yourself. – Richard Heap Feb 04 '22 at 17:51
  • @RichardHeap I have pcm16 codec. As you said I did the `data!.buffer.asByteData().getFloat32()` but the getFloat32() requires `byteOffset` as parameter so what should I pass? – Pokaboom Feb 04 '22 at 18:10
  • 0 for the first, then 4 for the second, then 8, 12, 16, etc – Richard Heap Feb 04 '22 at 18:47
  • but probably simpler to just turn the buffer into a list of floats with https://api.flutter.dev/flutter/dart-typed_data/ByteBuffer/asFloat32List.html – Richard Heap Feb 04 '22 at 18:48
  • note that you need to switch the codec to float32 first, too – Richard Heap Feb 04 '22 at 18:49
  • @RichardHeap `pcmFloat32` is not supported by `flutter_sound`, I get an exception. can you please guide me for pcm16 – Pokaboom Feb 04 '22 at 19:33
  • `buffer.asInt16List()` instead and expect values between -32768 and +32767 (which you could divide by 32768 to normalise). – Richard Heap Feb 04 '22 at 20:59

0 Answers0