Android sound distorted

Hello everyone.

I’m trying to develop an app that generates sinusoidal sound waves given a frequency.

I went to the AudioClip.Create page and copied the code, and the sound was distorted.

Here’s a copy of the code that I use to generate the wave. I had to take out the “Mathf.sign” portion of the calculation in order for it to run.

When I hit play in Unity, the sound comes out fine on the given frequency. I then export for android, and there the sound is distorted, and not at all what i hear when I run it on the computer.

I already tried building it for windows, and the standalone aplication runs fine and the sound is correct.

Is there anything that has to be different for android to run sound right?

I already found on a forum that Unity somehow needed sound at 48000 Hz for it to work. I’ve already been tweaking with samplerate at 44,100 and 48,000 and it made no diference; sound is still distorted.

void Generatewave()
{
    position = 0;
    samplerate = 48000;
    AudioClip myClip = AudioClip.Create("MySinusoid", samplerate * 2, 1, samplerate, true, OnAudioRead, OnAudioSetPosition);
    AudioSource aud = GetComponent<AudioSource>();
    aud.clip = myClip;
    aud.Play();
}
void OnAudioRead(float[] data)
{
    int count = 0;
    while (count < data.Length)
    {
        //data[count] = Mathf.Sign(Mathf.Sin(2 * Mathf.PI * frequency * position / samplerate));
        data[count] = Mathf.Sin(2 * Mathf.PI * frequency * position / samplerate);
        position++;
        count++;
    }
}
void OnAudioSetPosition(int newPosition)
{
    position = newPosition;
}

Any Ideas on what I could be doing wrong?

I assume you are using Unity 5.2+.

Which device are you using for testing?

There’s a large thread on the forums with this regard. The device you are using is reporting Audio Fast Path as a supported feature, when really it is not. This could happen, for example, when using a custom ROM - not the one provided by the manufacturer.

@EliasReyes this worked for me:

To the GameObject with the AudioSource, add also an Audio Low Pass Filter component.
In my case I set the Cutoff Frequency to 1,000 (Which works in the ranges I use) and this distorted noise disappeared. This component cut’s the treble sound (Low-pass filter on Wikipedia).