我试图用扫频产生正弦音。我正在使用下面从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/

10-12 05:12