- Home /
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?
Answer by Yury-Habets · Dec 05, 2015 at 09:35 PM
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.
Hello Yury:
yes, I'm using Unity 5.2, and the device is a $$anonymous$$yocera Event, with stock RO$$anonymous$$.
I Just realized that on the computer sound is distorted as well, although it is a diferent distortion than on the cellphone.
The sine wave generator seems to work fine at low frequencies (400-10,000 Hz) but as soon as you start getting into higher frequencies (over 10,000) then the sound starts getting weird, and it is not the correct sound anymore.
Problem is, I actually intend to use this program with frequencies ranging 15,000 - 20,000 Hz.
I tried an alternative: I created two WAV files with a soundwave of 18,000 Hz. I encoded that sound at 44,100 Hz and 48,000 Hz.
When I play the imported sounds on the Unity Player, the one encoded at 44100 sounds distorted, and the one at 48,000 sounds right.
I compiled the program to play those sounds for windows, The generated sound (procedurally) is distorted. The 44.1 $$anonymous$$hz WAV is distorted too; only the 48$$anonymous$$HZ WAV file sounds fine.
I compiled the same program for Android, and the generated wave still sounds distorted, but diferent. And about the WAV files, they don't play at all.
Any Idea what I could be missing? I wouldn't $$anonymous$$d just saving WAV files and playing them insted of procedurally generating the sound, but it bothers me that it won't play the sounds when imported, specially because it actually does play them on the player and on windows... anything android specific to be configured on the Audio Listener or Source so it will actually play sounds?
I think I have Audio sources and Listeneres set correctly because they play just right on windows, but for some reason it won't play WAV files when running on Android.
Thanks, and sorry for the long post.
Answer by guywald · Jul 07, 2016 at 12:17 AM
@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).