我在生成频率音时发现Playing an arbitrary tone with Android有用。现在我希望在播放音调时改变频率。
我将genTone修改为与此类似:
private void genTone(double startFreq, double endFreq, int dur) {
int numSamples = dur * sampleRate;
sample = new double[numSamples];
double currentFreq = 0, numerator;
for (int i = 0; i < numSamples; ++i) {
numerator = (double) i / (double) numSamples;
currentFreq = startFreq + (numerator * (endFreq - startFreq));
if ((i % 1000) == 0) {
Log.e("Current Freq:", String.format("Freq is: %f at loop %d of %d", currentFreq, i, numSamples));
}
sample[i] = Math.sin(2 * Math.PI * i / (sampleRate / currentFreq));
}
convertToPCM(numSamples);
}
private void convertToPCM(int numSamples) {
// convert to 16 bit pcm sound array
// assumes the sample buffer is normalised.
int idx = 0;
generatedSnd = new byte[2 * numSamples];
for (final double dVal : sample) {
// scale to maximum amplitude
final short val = (short) ((dVal * 32767));
// in 16 bit wav PCM, first byte is the low order byte
generatedSnd[idx++] = (byte) (val & 0x00ff);
generatedSnd[idx++] = (byte) ((val & 0xff00) >>> 8);
}
}
日志显示currentFreq的正确值,但是,在收听音调时,扫描过高,过快。例如,如果我从400hz开始并达到800hz,示波器显示它实际上是在同一时间从400hz到1200z。
我不确定我做错了什么,有人可以帮忙吗?
答案 0 :(得分:2)
更改采样率对示波器测量的频率有何影响?如果可能的话,我会尝试将采样率增加到更高的值,因为采样率越高,生成的信号就越准确。
无论如何,如果没有帮助,请调整以下公式:
currentFreq = startFreq +(numerator *(endFreq - startFreq));
为:
currentFreq = startFreq +(分子*(endFreq - startFreq)) / 2 ;
告诉我们您现在测量信号的新测量间隔。
祝你好运。