Android - 将多个静态波形混合到一个AudioTrack中

mas*_*mas 5 audio android audiotrack

我正在制作一个带有一系列频率值(即440Hz,880Hz,1760Hz)的类,并播放它们组合成单个AudioTrack的声音.我不是一个合理的程序员,所以我自己写这篇文章很困难,我认为对于经验丰富的声音程序员来说这是一个相对容易的问题.以下是play方法中的一些代码:

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

    // TODO
    // Perform a calculation to fill an array with the mixed sound - then play it in an infinite loop
    // Need an AudioTrack that will play calculated loop
    // Track sample info
    int numOfSamples = DURATION * SAMPLE_RATE;
    double sample[] = new double[numOfSamples];
    byte sound[] = new byte[2 * numOfSamples];

    // fill out the array
    for (int i = 0; i < numOfSamples; ++i) {
            sample[i] = Math.sin(2 * Math.PI * i / (SAMPLE_RATE / 440));
    }

    int i = 0;
    for (double dVal : sample) {
        // scale to maximum amplitude
        final short val = (short) ((dVal * 32767));
        // in 16 bit wav PCM, first byte is the low order byte
        sound[i++] = (byte) (val & 0x00ff);
        sound[i++] = (byte) ((val & 0xff00) >>> 8);
    }

    // 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
        AudioTrack track = new AudioTrack(AudioManager.STREAM_MUSIC, SAMPLE_RATE, AudioFormat.CHANNEL_CONFIGURATION_MONO, 
                AudioFormat.ENCODING_PCM_16BIT, numOfSamples, AudioTrack.MODE_STATIC);

        // Write audio data to track
        track.write(sound, 0, sound.length);

        // Begin playing track
        track.play();
    }

    // Once everything has successfully begun, indicate such.
    isPlaying = true;
}
Run Code Online (Sandbox Code Playgroud)

现在,这段代码只播放音乐会A(440Hz).这是测试此代码是否有效.现在,我需要采取一堆频率,执行某种计算,并编写样本数据.

mas*_*mas 2

好吧,答案确实是一个简单的求和循环。就是这样,只需将这个 for 循环替换为原来的 for 循环即可:

    // fill out the array
    for (int i = 0; i < numOfSamples; ++i) {
            double valueSum = 0;

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

            sample[i] = valueSum / soundData.length;
    }
Run Code Online (Sandbox Code Playgroud)

现在,它所做的就是简单地获取所有可能的频率,将它们添加到变量 中,valueSum然后除以频率数组的长度 ,soundData这是一个简单的平均值。这会产生任意长频率阵列的良好正弦波混合。

我还没有测试性能,但我确实在线程中运行它,否则可能会导致 UI 崩溃。所以,希望这会有所帮助 - 我将其标记为答案。