我发现在生成频率音调时,使用 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。
我不确定我做错了什么,有人可以帮忙吗?