我試圖產生5秒正弦波的聲音的頻率1000 我已經寫以下代碼持續時間的生成的音頻正弦波的
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秒鐘。爲什麼?
我正在使用靜態模式而不是流模式。此外,我嘗試了一個按鈕,我仍然有同樣的問題。如果我把樣品10 * sampleRate,我得到5秒。所以它總是除以2. – yasserbn 2013-02-12 22:31:30