1
我試圖不斷播放聲音,這應該改變smoothley的頻率。例如,我的AudioTrack目前正在以100赫茲的頻率播放,而新的頻率是1000赫茲。然後我不想讓頻率跳躍,而是讓smoothley適應變化。我目前的代碼如下所示:平滑的頻率變化
int buffSize = AudioTrack.getMinBufferSize(sampleRate,
AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT);
AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate,
AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT, buffSize,
AudioTrack.MODE_STREAM);
short samples[] = new short[buffSize];
int amp = 10000;
double twopi = 8. * Math.atan(1.);
double phase = 0.0;
audioTrack.play();
while (playing)
{
double newFrequency = getFrequency();
for (int i = 0; i < buffSize; i++)
{
samples[i] = (short) (amp * Math.sin(phase));
phase += twopi * newFrequency/sampleRate;
}
audioTrack.write(samples, 0, buffSize);
}
audioTrack.stop();
audioTrack.release();
這當然是在後臺運行。我的sampleRate自動設置爲可能的最低值:
private int getMinSampleRate()
{
for (int i = 1; i < 100000; i++)
if (AudioTrack.getMinBufferSize(i, AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT) > 0)
return i;
return -1;
}