20

I want to develop app to calculate Sound frequency in Android. Android Device will take Sound from microphone (i.e. out side sound) and I have one color background screen in app. on sound frequency changes i have to change background color of screen .

So my question is "How can i get sound frequency"?

is there any android API available?

Please help me out of this problem.

Ashish Wadatkar
  • 407
  • 2
  • 5
  • 19
  • 1
    look in android.media.audiofx public int getFft (byte[] fft) method, hope it guides u in right direction – Zia Oct 04 '12 at 06:22

4 Answers4

6

Your problem was solved here EDIT: archived here. Also you can analyze the frequency by using FFT.

EDIT: FFTBasedSpectrumAnalyzer (example code, the link from the comment)

18446744073709551615
  • 14,600
  • 3
  • 82
  • 116
ymn
  • 2,149
  • 2
  • 17
  • 38
4

Thanks for Reply I have done this by using sample on
http://som-itsolutions.blogspot.in/2012/01/fft-based-simple-spectrum-analyzer.html

Just modify your code for to calculate sound frequency by using below method

 // sampleRate = 44100 

public static int calculate(int sampleRate, short [] audioData){

    int numSamples = audioData.length;
    int numCrossing = 0;
    for (int p = 0; p < numSamples-1; p++)
    {
        if ((audioData[p] > 0 && audioData[p + 1] <= 0) || 
            (audioData[p] < 0 && audioData[p + 1] >= 0))
        {
            numCrossing++;
        }
    }

    float numSecondsRecorded = (float)numSamples/(float)sampleRate;
    float numCycles = numCrossing/2;
    float frequency = numCycles/numSecondsRecorded;

    return (int)frequency;
}
Vivek Bajpai
  • 1,527
  • 1
  • 17
  • 31
Ashish Wadatkar
  • 407
  • 2
  • 5
  • 19
  • 3
    You are analyzing zero crossings; I believe this can be used to identify either the dominant frequency or a harmonic. This is not a fourier transform. – Daniel Papasian Sep 04 '13 at 03:35
  • If the signal is a sum of two signals, first with frequency _F_ and amplitude _A_, and second with frequency _N*F_ and amplitude _a_ < _A_, the number of zero crossings will be somewhere between _F_ and _N*F_ (draw a sum of two sine waves to see why). – 18446744073709551615 Mar 12 '15 at 09:56
0

The other answers show how to display a spectrogram. I think the question is how to detect a change in fundamental frequency. This is asked so often on Stack Exchange I wrote a blog entry (with code!) about it:

http://blog.bjornroche.com/2012/07/frequency-detection-using-fft-aka-pitch.html

Admittedly, the code is in C, but I think you'll find it easy to port.

In short, you must:

  1. low-pass the input signal so that higher frequency overtones are not mistaken for the fundamental frequency (this may not appear to be an issue in your application, since you are just looking for a change in pitch, but I recommend doing it anyway for reasons that are too complex to go into here).

  2. window the signal, using a proper windowing function. To get the most responsive output, you should overlap the windows, which I don't do in my sample code.

  3. Perform an FFT on the data in each window, and calculate the frequency using the index of maximum absolute peak value.

Keep in mind for your application where you probably want to detect the change in pitch accurately and quickly, the FFT method I describe may not be sufficient. You have two options:

  1. There are techniques for increasing the specificity of the pitch tracking using phase information, not just the absolute peak.

  2. Use a time-domain method based on autocorrelation. Yin is an excellent choice. (google for "yin pitch tracking")

Bjorn Roche
  • 10,568
  • 6
  • 28
  • 56
-1

Here is a link to the code mentioned. There's also some other useful code there.

https://github.com/gast-lib/gast-lib/blob/master/library/src/root/gast/audio/processing/ZeroCrossing.java

Here's the deal with ZeroCrossings:

It is inaccurate at determining frequency precisely based on recorded audio on an Android. That said, it is still useful for giving your app a general sense that the sound it is hearing is a constant singing tone, versus just noise.

The code here seems to work quite well for determining frequency, (if you can translate it from C# to java) http://code.google.com/p/yaalp/

gregm
  • 11,369
  • 6
  • 52
  • 73