我试图用扫描产生正弦音。我正在使用下面的代码here。问题是,我可以听到200Hz以上的声音,但我听不到低于200Hz的声音(如20hz或50Hz)。 请帮我生成准确的正弦音。
private final int sampleRate = 44100;
public void generateTone(double startFreq, double endFreq, float duration)
{
double dnumSamples = duration * sampleRate;
dnumSamples = Math.ceil(dnumSamples);
numSamples = (int) dnumSamples;
double 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))/2;
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));
}
generatedSnd = new byte[2 * numSamples];
int idx = 0;
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);
}
}
答案 0 :(得分:1)
如果您跟踪正弦波中的当前位置,并根据当前频率递增它,而不是计算每个样本相对于起始位置的位置,那么您的代码将更加强大。
double currentPos = 0.0;
for (int i = 0; i < numSamples; ++i) {
numerator = (double) i / (double) numSamples;
currentFreq = startFreq + (numerator * (endFreq - startFreq))/2;
if ((i % 1000) == 0) {
Log.e("Current Freq:", String.format("Freq is: %f at loop %d of %d", currentFreq, i, numSamples));
}
currentPos += 2 * Math.PI * (currentFreq / sampleRate);
sample[i] = Math.sin(currentPos);
}
这避免了可能导致当前位置向后移动的频率降低的问题。
如果您想让音调淡入一定数量的样本,您可以添加以下代码:
int fadeInSamples = 5000;
double fadeIn = (i < fadeInSamples) ? (double)i / (double)fadeInSamples : 1.0;
sample[i] = Math.sin(currentPos) * fadeIn;