生成2音音频信号

时间:2017-03-20 15:16:07

标签: android

首先抱歉我的英语不好。我尽我所能。

所以,我有一个短代码(音频信号发生器),如下所示

 private AudioTrack generateTone(double freqHz, int durationMs)
{
    int count = (int)(44100.0 * 2.0 * (durationMs / 1000.0)) & ~1;
    short[] samples = new short[count];
    for(int i = 0; i < count; i += 2){
        short sample = (short)(Math.sin(2 * Math.PI * i / (44100.0 / freqHz)) * 0x7FFF);
        samples[i + 0] = sample;
        samples[i + 1] = sample;
    }
    AudioTrack track = new AudioTrack(AudioManager.STREAM_MUSIC, 44100,
            AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT,
            count * (Short.SIZE / 8), AudioTrack.MODE_STATIC);
    track.write(samples, 0, count);
    return track;
}

我可以通过以下方式给他打电话:

 AudioTrack tone = generateTone(500, 1000);
 tone.play();

但我需要一个接一个地使用2个频率半小时。

400Hz持续1秒,然后800持续1秒,然后再持续400持续1秒,依此类推......

我该怎么办?我创建了2个audioTracks tone1和tone2:

AudioTrack tone = generateTone(500, 1000);
AudioTrack tone2 = generateTone(800,1000);

接下来我该怎么办?感谢您花时间回答我,感谢您的回答! 哦,P.S =&gt;这是关于Android应用....

1 个答案:

答案 0 :(得分:0)

您可以使用getPlayState()检查播放状态,看看是否已完成。

        tone.play();
        do{
            SystemClock.sleep(10);
        }while (audioTrack.getPlayState()==AudioTrack.PLAYSTATE_PLAYING);

        tone2.play();        

请确保不要在主线程上执行此操作