Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to convert to decibel #22

Open
tawat25 opened this issue Aug 3, 2022 · 8 comments
Open

how to convert to decibel #22

tawat25 opened this issue Aug 3, 2022 · 8 comments

Comments

@tawat25
Copy link

tawat25 commented Aug 3, 2022

Hi I have used FFT(); like this
...
final fftx = FFT(audio.length);
final freqlist = fftx.realFft(audio);
...
that result is real and imagine number
How i convert to Decibel ?

@liamappelbe
Copy link
Owner

Use the magnitudes function to get the amplitudes. Since you're doing real FFT, you probably don't care about the redundant upper half of the frequencies, so you should also use discardConjugates:

final fftx = FFT(audio.length);
final freqlist = fftx.realFft(audio).discardConjugates().magnitudes();

That will give you the amplitudes.

Now, you said you wanted the decibels. Decibels is a logarithmic scale. If you're sure that's what you want, you can use this formula to convert the amplitude to decibels: 20 * log10(amplitude):

final decibels = freqlist.map(amp => 20 * math.log(amp) / math.ln10).toList();

If you want to know which frequency a particular entry in decibels corresponds to, use the frequency function:

for (int i = 0; i < decibels.length; ++i) {
  final freq = fftx.frequency(i, sampleRate);
  print("$freq Hz: ${decibels[i]} dB");
}

Where sampleRate is the sampling rate of your input audio (probably 44100Hz or 48000Hz or something like that).

@tawat25
Copy link
Author

tawat25 commented Aug 3, 2022

I have run code on console mode is ok , I try to run on android but not finished yet .Are you ever implement this library with android ?

@liamappelbe
Copy link
Owner

It is implemented on android. It's probably taking a long time because your phone is slower than your PC.

What are you using it for? How long is your audio file?

You might want to use STFT instead. That will run a bunch of small FFTs, rather than one big one, and is much faster for long audio files.
https://pub.dev/documentation/fftea/latest/stft/STFT-class.html

@zjjt
Copy link

zjjt commented Nov 22, 2022

Hi @liamappelbe , i have a kinda similar issue. i am trying to detect pauses or silence within an audio and act accordingly depending on the result. The audio files are mp3s and are for voice reading. How can i detect those and their position within the track ?

@liamappelbe
Copy link
Owner

i am trying to detect pauses or silence within an audio

@zjjt You don't need FFT to do that. Try just iterating through the audio samples, and if the value stays below some threshold for some amount of time, that's silence.

For more robust results, you can calculate a moving average of the absolute value (or maybe the squared value) of the samples.

@zjjt
Copy link

zjjt commented Nov 22, 2022

@liamappelbe oh ...im struggling to find à way to do this in dart and thought that fftea could help me out. Any ideas how can I access the samples data to start ?

@liamappelbe
Copy link
Owner

@zjjt Whether or not you use fftea, you'll need to load the MP3s and get the audio samples. That's a separate problem.

flutter_lame looks like it can read MP3 files, but it seems to only work on Flutter. If you're able to convert the MP3s to WAV files, then you can use package:wav (another of my packages 😉) to get the samples.

@zjjt
Copy link

zjjt commented Nov 22, 2022

@liamappelbe you are the one Who have written the wav package? Cool ...thanks for the tips ill see how I can convert my files to wav I guess it ll be better...For now i load my files feom the assetbundle and use audioplayers package ti olay them.ok let me revert when I am done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants