9

I am developing an app that get a source audio from mic in realtime, with no file storage. Basically, I use:

mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mRecorder.setOutputFile("/dev/null");

How I can do a spectrum graphic from this realtime audio, with no files?

All post that I read are analyzing a buffered file.

genpfault
  • 51,148
  • 11
  • 85
  • 139
Víctor Martín
  • 3,352
  • 7
  • 48
  • 94
  • 1
    hilarious typo in the title - Now corrected ;) – marko Aug 18 '13 at 20:04
  • 1
    No need to apologise! Lots of respect for asking questions here in a language whcih is not your first. – marko Aug 18 '13 at 22:08
  • 1
    To my knowledge there's no API for this. You're most likely looking at building up a reasonably-sized buffer of PCM data in RAM and transforming it to the frequency domain using an FFT library. See e.g. http://stackoverflow.com/questions/4675457/how-to-generate-the-audio-spectrum-using-fft-in-c for a bit more information. – Michael Aug 19 '13 at 11:12

3 Answers3

9

yes it can be done.

All you need is a fast FFT algorithm !

First decide the frequency resolution that you want, for example you can set the sample rate from your mic at 8000hz, now choose one chunk size like 1024 or 2048 to capture from your mic.

If you choose 2048 points and sample rate 8000, do you will have a frequency resolution = 3.9063 (8000 /2048).

Apply one window function over your 2048 points, then apply FFT and get magnitude !

Remember of Nyquist theorem sample rate = 8000 / 2 = 4000, now do you know your FFT can get frequencies between 3.9063 Hz at 4000 Hz.

FFT Bin of corresponding frequencies:

1 -> 3,90625  hz    
2 -> 7,8125  hz    
3 -> 11,71875 hz    
...    
1024 -> 4000 hz    
...    
2048 - > 8000 hz

For it you need just the first half values of FFT, for this case 1024.

Now if you plot this data from your FFT, you will have a spectrum !

EDIT

Pseudo code:

#construct one hanning window Function
Chunk = 2048;
windowed = [Chunk];
hanning = [Chunk];
for i 1:Chunk:
      hanning[i] = ((1 - cos(i*2*pi/Chunk-1))/2)

#start capture from Mic
while true:

    #into values capture 2048 points from your mic
    values=dataFromMic(Chunk);
    #Apply Window hanning = multiply window function(hanning) over your 2048 points
    for i 1:Chunk:
            windowed[i] = values[i] * hanning[i]
    #Apply FFT 
    fftData=fft(windowed);
    #Get Magnitude (linear scale) of first half values
    Mag=abs(fftData(1:Chunk/2))
    # update/show results
    plot(Mag)

end
ederwander
  • 3,410
  • 1
  • 18
  • 23
  • This question contains Java code for the FFT that may be useful: http://stackoverflow.com/questions/9272232/fft-library-in-android-sdk – japreiss Aug 19 '13 at 16:45
  • Thanks to all, I don't know anything about sound analysis, so I will try to read and understand your comments. Thank you, I will comment later, for sure. – Víctor Martín Aug 19 '13 at 17:32
  • ederwander, there is one thing that I don't understand how I need to proceed. "Apply one window function over your 2048 points, then apply FFT and get magnitude !" – Víctor Martín Aug 20 '13 at 08:49
  • Thanks ederwander. Another question if you don't mind. How I can distinguish between a low and a sharp sound, I want to show other graphics with each sound of these types. Sorry if I am being too nuisance. – Víctor Martín Aug 20 '13 at 13:25
  • @Victor_J_Martin for it you can use a pitch extractor to tell you in hertz what is the real frequency of you sound, and you can define how low or high it is. – ederwander Aug 20 '13 at 13:46
  • Ok, let me time to know what I can do ^^ Thanks @ederwander – Víctor Martín Aug 20 '13 at 16:01
  • question: why most of analyzers on google play store don't measure frequencies higher than 20 KHZ? is there an hardware limitation? I would like to make an app that generates/measures frequencies higher than 24K ? is it possible using the best Android smartphones? – alexpfx Jan 07 '19 at 19:52
8

There is an open source Android spectrum analyzer on Github which computers FFT on audio from the microphone and displays a 2D spectrogram.

The Spectrum Analyzer project is found in the v2.x/Showcase app

You can see a video of it in action at https://youtu.be/yU05fsgOYO4

You can see a video with build instructions here: https://youtu.be/tVgn30uss2k?t=1m37s

enter image description here

The charting is provided by the SciChart Android chart library which is a commercial control, but the source code to read the mic, compute the FFT and spectrogram is free & open source under MIT License.

As a disclosure: I am the tech lead on the SciChart project

Dr. Andrew Burnett-Thompson
  • 20,980
  • 8
  • 88
  • 178
  • The first link is not pointing to the spectrum analyzer project. – Mehdi Boukhechba Aug 08 '17 at 17:55
  • I've updated the answer to include more info, including direct link to the Audio Analyser project, and a video to show how to build it. Hope that helps. – Dr. Andrew Burnett-Thompson Aug 09 '17 at 11:05
  • 1
    Hi @Dr.ABT Do you have the sources of the sample that we can see in the Youtube video? I have a similar need on Xamarin Android project: record noise from the microphone, and display the related spcetogram. – Gold.strike Jun 08 '18 at 22:11
  • Yes the link to the sources is in my answer where it says “the spectrum analyser project is found at ....”. – Dr. Andrew Burnett-Thompson Jun 09 '18 at 09:15
  • Hi @Dr.ABT thans for your answer. I also discuss with Julia through your email support, but you could maybe help me to know if it's possible to achieve a similar thing ("spectrum analyser") in a Xamarin Android project? You Xamarin Android samples (all the code is in the Fragment) you don't use the same architecture than your Android samples (.xml layouts, fragments as views, viewmodels, binding classes): so I'm not sure that could be done... – Gold.strike Jun 14 '18 at 09:47
  • How is the example code usable, if it bases on commercial code? Not sure what it means to have MIT license here. – mavavilj Oct 11 '22 at 16:02
3

I have developed an open source FFT based spectrum analyzer. Please have a look at

http://som-itsolutions.blogspot.in/2012/01/fft-based-simple-spectrum-analyzer.html.

You can also get the source code from

https://github.com/sommukhopadhyay/FFTBasedSpectrumAnalyzer

Hope this will help you.

somenath mukhopadhyay
  • 1,548
  • 1
  • 16
  • 18