How to calculate sound frequency in android?

2019-01-30 16:54发布

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.

4条回答
我欲成王,谁敢阻挡
2楼-- · 2019-01-30 17:08

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)

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-01-30 17:11

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")

查看更多
We Are One
4楼-- · 2019-01-30 17:24

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;
}
查看更多
爷的心禁止访问
5楼-- · 2019-01-30 17:26

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/

查看更多
登录 后发表回答