Reputation: 2249
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.
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);
}
}
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?
Upvotes: 2
Views: 1272
Reputation: 36
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 + (numerator * (endFreq - startFreq));
to:
currentFreq = startFreq + (numerator * (endFreq - startFreq))/2;
and tell us the new measured interval of variation of your signal now.
Good luck.
Upvotes: 2