Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by EliasReyes · Dec 05, 2015 at 06:11 PM · androidsoundaudioclipdistortionsamplerate

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image EliasReyes · Dec 05, 2015 at 11:54 PM 0
Share

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.

avatar image
0

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).

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Sound not playing on Galaxy S4 device 0 Answers

Only able to play a single sound through AudioSource. 1 Answer

Draw in a Texture a Waveform From Audioclip Before Play 1 Answer

Android Splash Screen Stretches 1 Answer

Where to store files in Android that can be accessed later via path? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges