How to calculate sound frequency in android?
Asked Answered
L

4

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.

Logger answered 4/10, 2012 at 6:9 Comment(1)
look in android.media.audiofx public int getFft (byte[] fft) method, hope it guides u in right directionMerralee
D
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)

Diley answered 4/10, 2012 at 6:18 Comment(2)
Thanks for Reply I have done this by using sample on linkLogger
The link in this answer is now dead. Please consider editing it.Species
L
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;
}
Logger answered 4/10, 2012 at 9:37 Comment(2)
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.Sprinkling
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).Hitandrun
T
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")

Tool answered 5/10, 2012 at 15:22 Comment(0)
N
-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/

Normalcy answered 5/10, 2012 at 8:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.