Canardoux / flutter_sound

Flutter plugin for sound. Audio recorder and player.
Mozilla Public License 2.0
866 stars 566 forks source link

[HELP] Get DBs from sound file #389

Open Mohamed-Fakhry opened 4 years ago

Mohamed-Fakhry commented 4 years ago

How to get dbs from sound file to visualize it

Larpoux commented 4 years ago

Is it something related to #379 ? Do you want to get this information statically (not during play back) ?

@fzyzcjy, @Mohamed-Fakhry : I think that this feature would be very interesting to implement in Flutter Sound

Mohamed-Fakhry commented 4 years ago

Yes I can get now will recording only from onRecorderDbPeakChanged I want to get it from file

Larpoux commented 4 years ago

I really want this feature in Flutter Sound 👍 . If you work on this feature , I will be interested to have some Pull Request from you.

If you cannot contribute, I will work on that myself. But later (now I am very busy).

Mohamed-Fakhry commented 4 years ago

I work on it but Do you have an idea about how to make it ?

Larpoux commented 4 years ago

My idea is to convert your sound to a PCM format if your sound file is with a compressed format like MP3 or OPUS. You can use FFmpeg (which is embedded inside Flutter sound) to do the conversion. Probably a WAVE (.wav) file would be convenient. During this process, you must specify a Sample Rate.

Then you can remove the ".wav" header (easy, because this header has a fixed length).

Then you will have an array of Integers 16 bits (if you got a PCM-Linear16 format) or an array of floating point numbers (if you got a PCM-float file format). Because you know the Sample Rate, it is easy to have correspondence with your timing.

You must process those numbers to get Decibels.

This is my actual idea. But I am not a sound expert, so I am not sure that this would work. But I would like to work on that 👍

Mohamed-Fakhry commented 4 years ago

@Larpoux I solve the problem with FFmpeg command

ffmpeg -i audio.wav -af "aresample=60000,asetnsamples=3000,astats=reset=1:metadata=1,ametadata=print:key='lavfi.astats.Overall.Peak_level':file=status.log" -f null -

you can choose what you want lavfi.astats.Overall.RMS_level Or lavfi.astats.Overall.Peak_level https://stackoverflow.com/a/60811629

Or

Decode audio to PCM https://stackoverflow.com/a/41663511

Larpoux commented 4 years ago

🥇 👍

Thank you so much 😄 FFmpeg is really a great library !

We must implement a Flutter Sound API verb which will use this command.

Mohamed-Fakhry commented 4 years ago

I get the db as recording values using lavfi.astats.Overall.Peak_level

lavfi.astats.Overall.RMS_level give me different result

Future<List<int>> getPeakLevels(String _pathAudio) async {
    File outputFile = File(
        '/data/user/0/com.flutter.shoutout/cache/1-flutter_sound_example.txt');

    await _flutterFFmpeg
        .execute(
            "-i '$_pathAudio' -af aresample=16000,asetnsamples=16000,astats=reset=1:metadata=1,ametadata=print:key='lavfi.astats.Overall.Peak_level':file=${outputFile.path} -f null -")
        .then((rc) => print("FFmpeg process exited with rc $rc"));

    List<int> wave = List();

    await outputFile
        .openRead()
        .map(utf8.decode)
        .transform(new LineSplitter())
        .forEach((l) {
      if (l.startsWith("frame")) return;
      double value = double.tryParse(l.split("=").last) ?? 0;
      wave.add(70 + value.toInt());
    });

    return wave;
  }
Mohamed-Fakhry commented 4 years ago

@Larpoux May I make PR ?

Larpoux commented 4 years ago

Sure 👍 If you do now, probably I will be able to put it in next version 5.1.0

Thank you to contribute to Flutter Sound 🥇

github-actions[bot] commented 4 years ago

This issue is stale because it has been open 90 days with no activity. Leave a comment or this will be closed in 7 days.

Larpoux commented 3 years ago

Dear "bot", please keep this issue open

Larpoux commented 3 years ago

Dear "bot", please keep this issue open 😡

Larpoux commented 3 years ago

Registered in the Flutter Sound Project

github-actions[bot] commented 8 months ago

This issue is stale because it has been open 90 days with no activity. Leave a comment or this will be closed in 7 days.

wuxiaohan2023 commented 4 months ago

Finally, how to get dbs through file or path