如何计算android中的声音频率?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12721254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to calculate sound frequency in android?
提问by Ashish Wadatkar
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 .
我想开发应用程序来计算 Android 中的声音频率。Android 设备将从麦克风中获取声音(即外部声音),我在应用程序中有一个彩色背景屏幕。在声音频率变化时,我必须更改屏幕的背景颜色。
So my question is "How can i get sound frequency"?
所以我的问题是“我怎样才能获得声音频率”?
is there any android API available?
有没有可用的android API?
Please help me out of this problem.
请帮我解决这个问题。
回答by ymn
Your problem was solved hereEDIT: archived here. Also you can analyze the frequency by using FFT.
你的问题解决了这里编辑:这里保存。您也可以使用FFT分析频率。
EDIT: FFTBasedSpectrumAnalyzer(example code, the link from the comment)
编辑:FFTBasedSpectrumAnalyzer(示例代码,评论中的链接)
回答by Ashish Wadatkar
Thanks for Reply I have done this by using sample on
http://som-itsolutions.blogspot.in/2012/01/fft-based-simple-spectrum-analyzer.html
感谢您的回复我已经通过使用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;
}
回答by Bjorn Roche
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:
其他答案显示了如何显示频谱图。我认为问题是如何检测基频的变化。这在 Stack Exchange 上经常被问到,我写了一篇关于它的博客条目(带有代码!):
http://blog.bjornroche.com/2012/07/frequency-detection-using-fft-aka-pitch.html
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.
诚然,代码是用 C 编写的,但我认为您会发现它很容易移植。
In short, you must:
简而言之,您必须:
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).
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.
Perform an FFT on the data in each window, and calculate the frequency using the index of maximum absolute peak value.
对输入信号进行低通处理,以免将高频泛音误认为基频(这在您的应用中似乎不是问题,因为您只是在寻找音高的变化,但出于某些原因,我建议无论如何都这样做这太复杂了,无法进入这里)。
使用适当的窗口函数对信号进行窗口化。为了获得最灵敏的输出,您应该重叠窗口,我在示例代码中没有这样做。
对每个窗口中的数据执行FFT,并使用最大绝对峰值的索引计算频率。
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:
请记住,对于您可能想要准确快速地检测音调变化的应用,我描述的 FFT 方法可能不够。您有两个选择:
There are techniques for increasing the specificity of the pitch tracking using phase information, not just the absolute peak.
Use a time-domain method based on autocorrelation. Yin is an excellent choice. (google for "yin pitch tracking")
有一些技术可以使用相位信息来提高音调跟踪的特异性,而不仅仅是绝对峰值。
使用基于自相关的时域方法。殷是个不错的选择。(谷歌搜索“阴音跟踪”)
回答by gregm
Here is a link to the code mentioned. There's also some other useful code there.
这是提到的代码的链接。还有一些其他有用的代码。
Here's the deal with ZeroCrossings:
这是与 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.
根据 Android 上录制的音频精确确定频率是不准确的。也就是说,它仍然有助于让您的应用程序感觉它听到的声音是一种持续的歌声,而不仅仅是噪音。
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/
这里的代码在确定频率方面似乎工作得很好,(如果你能把它从 C# 翻译成 java) http://code.google.com/p/yaalp/