我如何生成Java(Android版)正弦扫频 [英] How do I generate a Sine Sweep in Java (Android)
问题描述
我发现播放与Android 任意色调是有帮助的,当我产生频率音。现在我想的频率,而音播放改变。
I found Playing an arbitrary tone with Android to be helpful when I was generating a frequency tone. Now I want the frequency to change while the tone is playing.
我修改了genTone是类似这样的:
I modified the genTone to be similar to this:
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。
The log shows what appears to be the correct value for currentFreq, however, when listening to the tone the sweep goes too high, and too fast. For example if I start at 400hz and go to 800hz, an oscilloscope shows that it is really going from 400hz to 1200z in the same time.
我不知道我做错了,谁能帮助?
I am not sure what I am doing wrong, can anyone help?
推荐答案
哪个效果没有改变的采样率已经在频率示波器测量的?我会尝试增加采样率到如果可能的话较高的值,因为较高的采样率,越准确所产生的信号就可以了。
Which effect does changing the the samplerate has in the frequency measured by the oscilloscope? I would try to increase the samplerate to higher values if possible, because the higher the samplerate is, the more accurate the generated signal can be.
反正如果开不帮助,请从调整公式:
Anyways if that does't help, please tweak the formula from:
currentFreq = startFreq +(分子*(endFreq - startFreq));
currentFreq = startFreq + (numerator * (endFreq - startFreq));
到
currentFreq = startFreq +(分子*(endFreq - startFreq)) / 2
currentFreq = startFreq + (numerator * (endFreq - startFreq))/2;
和告诉我们你的信号变化的新的测量间隔了。
and tell us the new measured interval of variation of your signal now.
祝你好运。
这篇关于我如何生成Java(Android版)正弦扫频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!