Android - Mixing multiple static waveforms into a single AudioTrack

前端 未结 2 998
既然无缘
既然无缘 2021-02-06 12:51

I am making a class that takes an array of frequencies values (i.e. 440Hz, 880Hz, 1760Hz) and plays how they would sound combined into a single AudioTrack. I am not a sound prog

2条回答
  •  暖寄归人
    2021-02-06 12:59

    Ok, so the answer did turn out to be a simple summation loop. Here it is, just replace this for loop with the original one:

        // 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;
        }
    

    Now, what this does is simply take all possible frequencies, add them together into the variable, valueSum, and then divide that by the length of the frequency array, soundData, which is a simple average. This produces a nice sine wave mixture of an arbitrarily long array of frequencies.

    I haven't tested performance, but I do have this running in a thread, otherwise it could crash the UI. So, hope this helps - I am marking this as the answer.

提交回复
热议问题