gpt4 book ai didi

java - 使用扫描生成正弦音 - Android

转载 作者:行者123 更新时间:2023-11-30 01:19:45 25 4
gpt4 key购买 nike

我正在尝试通过扫描生成正弦音。我正在使用从 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);

}
}

最佳答案

如果您跟踪正弦波中的当前位置并根据当前频率递增它,而不是计算每个样本相对于起始位置的位置,您的代码将更加健壮。

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;

关于java - 使用扫描生成正弦音 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37291195/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com