Android - Mixing multiple static waveforms into a single AudioTrack

前端 未结 2 1042
既然无缘
既然无缘 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 13:06

    If you intend to mix multiple waveforms into one, you might prevent clipping in several ways.

    Assuming sample[i] is a float representing the sum of all sounds.

    HARD CLIPPING:

    if (sample[i]> 1.0f)
    {
        sample[i]= 1.0f;
    }
    if (sample[i]< -1.0f)
    {
        sample[i]= -1.0f;
    }
    

    HEADROOM (y= 1.1x - 0.2x^3 for the curve, min and max cap slighty under 1.0f)

    if (sample[i] <= -1.25f)
    {
        sample[i] = -0.987654f;
    }
    else if (sample[i] >= 1.25f)
    {
        sample[i] = 0.987654f;
    }
    else
    {
        sample[i] = 1.1f * sample[i] - 0.2f * sample[i] * sample[i] * sample[i];
    }
    

    For a 3rd polynomial waveshapper (less smooth), replace the last line above with:

    sample[i]= 1.1f * sample[i]- 0.2f * sample[i] * sample[i] * sample[i];
    

提交回复
热议问题