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
  • 1,110
  • 9
  • 28
  • 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
  • If you converted it can you post the solution? – Yalzeee Jul 09 '22 at 11:56
  • @Yalzeee I have posted some explanation hope it helps and it' s not the answer. – Pokaboom Jul 11 '22 at 08:11

1 Answers1

1

I was not able to calculate amplitude from Uint8List so what I did was write my own native code to directly get amplitude from native you can check out my package here. (I am busy somewhere else so I'm not right now maintaining it but will surely do in future.)

And I don't know but flutter_sound provides maybe false values or I was calculating wrong but I was always getting nearly the same values even when the volume is high or low.

Now, if you want to calculate amplitude then it should be based on values you're getting,

If you are getting values between -32768 and 32767 then from my understanding what you do is define a timeframe and for that timeframe calculate the RMS of it.

The RMS is your amplitude.

Now, as @richard said in the comment to normalize with 32768 if you have Int16List. But I guess that is doing too much work. We get a stream of Int16List so data may be coming every 200ms or whatever it is set at the plugin level. Calculating RMS for each Int16List is a very intensive task so I don't think it's good to do it on the flutter side. Get data from native directly and just pass it flutter using platform channels.

Now, if you want to try then calculate the RMS of an Int16List then for a defined timeframe take the mean of those values.

I may well be wrong but hope this helps or guides you in the right direction. If anyone finds a better way please post it here.

Pokaboom
  • 1,110
  • 9
  • 28