我正在尝试生成频率为 1000 的 5 秒正弦波声音。我编写了以下代码
int sampleRate = 44100;
int freqOfTone = 1000;
AudioTrack track;
// 5 seconds
short samples = new short[sampleRate*5];
track = new AudioTrack(AudioManager.STREAM_MUSIC,
sampleRate, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT, samples.length,
AudioTrack.MODE_STATIC);
double angle = 0;
double increment = (2 * Math.PI * freqOfTone / sampleRate); // angular increment
for (int i = 0; i < samples.length-1; i++) {
samples[i] = (short) (Math.sin(angle) * Short.MAX_VALUE);
angle += increment;
}
track.write(samples, 0, samples.length); // write data to audio hardware
track.play();
声波长度只有2.5秒,我觉得应该是5秒。为什么?