使用AudioTrack类在Android中连续合成静态波形

时间:2012-08-21 03:06:20

标签: android audio audiotrack

下面是我的play()方法的代码,它只生成一组任意频率并将它们混合成一种音调。

问题是它只能暂时播放 - 我需要的是连续播放。我很感激有关如何使用Android中的AudioTrack类不断生成声音的建议。我相信它与MODE_STREAM常量有关,但我无法弄清楚如何。

以下是AudioTrack类文档的链接:

http://developer.android.com/reference/android/media/AudioTrack.html

编辑:我忘了提一个重要的方面,它无法循环。由于有时多达50多个频率的混合,它会听起来不稳定,因为所有频率峰值都没有最小公分母 - 或者它在波形上太远而无法存储为一个声音。

/**
 * play - begins playing the sound
 */
public void play() {
    // Get array of frequencies with their relative strengths
    double[][] soundData = getData();

    // Track samples array
    final double samples[] = new double[1024];

    // Calculate the average sum in the array and write it to sample
    for (int i = 0; i < samples.length; ++i) {
            double valueSum = 0;

            for (int j = 0; j < soundData.length; j++) {
                valueSum += Math.sin(2 * Math.PI * i / (SAMPLE_RATE / soundData[j][0]));
            }

            samples[i] = valueSum / soundData.length;
    }

    // Obtain a minimum buffer size
    int minBuffer = AudioTrack.getMinBufferSize(SAMPLE_RATE, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);

    if (minBuffer > 0) {

        // Create an AudioTrack
        mTrack = new AudioTrack(AudioManager.STREAM_MUSIC, SAMPLE_RATE, AudioFormat.CHANNEL_CONFIGURATION_MONO, 
                AudioFormat.ENCODING_PCM_16BIT, minBuffer, AudioTrack.MODE_STREAM);

        // Begin playing track
        mTrack.play();

        // Fill the buffer
        if (mBuffer.length < samples.length) {
             mBuffer = new short[samples.length];
        }

        for (int k = 0; k < samples.length; k++) {
            mBuffer[k] = (short) (samples[k] * Short.MAX_VALUE);
        }

        // Write audio data to track for real-time audio sythesis
        mTrack.write(mBuffer, 0, samples.length);

    }

    // Once everything has successfully begun, indicate such.
    isPlaying = true;
}

1 个答案:

答案 0 :(得分:0)

看起来代码几乎就在那里。它只需要一个循环来保持生成样本,将它们放入缓冲区,并将它们写入AudioTrack。现在只需要一个缓冲区就可以在退出之前写入,这就是它停止这么快的原因。

void getSamples(double[] samples) {
    // Get array of frequencies with their relative strengths
    double[][] soundData = getData();

    // Calculate the average sum in the array and write it to sample
    for (int i = 0; i < samples.length; ++i) {
            double valueSum = 0;

            for (int j = 0; j < soundData.length; j++) {
                valueSum += Math.sin(2 * Math.PI * i / (SAMPLE_RATE / soundData[j][0]));
            }

            samples[i] = valueSum / soundData.length;
    }
}

public void endPlay() {
    done = true;
}

/**
 * play - begins playing the sound
 */
public void play() {
    // Obtain a minimum buffer size
    int minBuffer = AudioTrack.getMinBufferSize(SAMPLE_RATE, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);

    if (minBuffer > 0) {

        // Create an AudioTrack
        mTrack = new AudioTrack(AudioManager.STREAM_MUSIC, SAMPLE_RATE, AudioFormat.CHANNEL_CONFIGURATION_MONO, 
            AudioFormat.ENCODING_PCM_16BIT, minBuffer, AudioTrack.MODE_STREAM);

        // Begin playing track
        mTrack.play();

        // Track samples array
        final double samples[] = new double[1024];

        while (!done) {
            // Fill the buffer
            if (mBuffer.length < samples.length) {
                 mBuffer = new short[samples.length];
            }

            getSamples(samples);

            for (int k = 0; k < samples.length; k++) {
                mBuffer[k] = (short) (samples[k] * Short.MAX_VALUE);
            }

            // Write audio data to track for real-time audio sythesis
            mTrack.write(mBuffer, 0, samples.length);

            // Once everything has successfully begun, indicate such.
            isPlaying = true;
        }
    }

    // Once everything is done, indicate such.
    isPlaying = false;
}