• 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 /
  • Help Room /
This question was closed Mar 28, 2016 at 11:28 PM by Le-Pampelmuse for the following reason:

The question is answered

avatar image
Question by mingom · Mar 22, 2016 at 07:08 PM · audiocoroutineaudioclipmusic

Precise metronome problem

Hi guys, i have been having a problem for a w$$anonymous$$le now. I have made a simple android beat mac$$anonymous$$ne that plays audioclips on specific intervals. I can not get the intervals to be precise and consistent. I tried t$$anonymous$$s: http://cubeslam.net/2013/12/19/unity-metronome-like-a-pro/ and also t$$anonymous$$s: http://nodgez.net/?p=153

Using coroutines or fixedupdate w$$anonymous$$le calculating time between beats prooves to be inacurate as soon as anyt$$anonymous$$ng else happens in the scene, as well as if left running for more than a few seconds. I found a unity metronome here: http://docs.unity3d.com/ScriptReference/AudioSettings-dspTime.html , that uses AudioSettings.dspTime, w$$anonymous$$ch seems to work, but i can not make it trigger audioclips.

current best version of the metronome, w$$anonymous$$ch tends to slow down or make errors often.

 IEnumerator PlaySounds()
 {
     isPlaying = true;
     w$$anonymous$$le (isPlaying)
     {
         if (interval <= 0)
         {
             myAudio.PlayOneShot(myAudioClip, 1f);
             interval = (60f / BPM);
             interval -= Time.deltaTime;
         }
         else {
             interval -= Time.deltaTime;
         }
         yield return null;
     }
 }

Can anybody suggest a better approach or even has a method of using the unity metronome, linked above, to time my sounds? Running Unity version 5.3.2f1 on Windows 10.

Thanks alot for your help!

Comment

People who like this

0 Show 3
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 Le-Pampelmuse · Mar 22, 2016 at 09:18 PM 2
Share

I would use the system clock. It can be used on PC by using the System Library:

 int timer = 0;
 
 if(System.DateTime.Now.Second == timer+1)
 {
     Tick();
 }
 timer = System.DateTime.Now.Second;

 void Tick();
 {
     //Do whatever.
 }

This seems to be a very precise timing mechanism that does not rely on fps (as far as I know and experimented with it, I don't claim it to be a fact).

I'm not sure if it works the same way on an Android device, but I don't see why it shouldn't work, but then again I'm no expert on Android hardware.

Everything that involves Time.deltaTime is directly FPS based and should be avoided for normal or precise timing operations.

Hope this helps, I once was looking for a precise timer and it seemed to be the most precise due to the fact it has millisecond accuracy.

avatar image Le-Pampelmuse Le-Pampelmuse · Mar 22, 2016 at 09:20 PM 2
Share

I might add that this timer is ticking every second and you would have to adapt it to your desired bpm.

avatar image SumFortyOne Le-Pampelmuse · Apr 02, 2016 at 01:19 AM 0
Share

Thank you! I want a really precise timing machine and this works exact for hours because it is actually a clock :D

1 Reply

  • Sort: 
avatar image
Best Answer

Answer by troien · Mar 22, 2016 at 07:43 PM

The last link (Unity docs one) doesn't work for me...

As for how to make your current code better. Sinse you want to play audio on the tick, threads are not really an option as they can't call Uniy code... So ye, I believe coroutines or update are indeed your best option. However they will obviously not work very well when you do a lot of other t$$anonymous$$ngs in the scene that affect your fps in a negative way. As they al run on the same thread and your metronome simply has to wait untill the rest of your code (Update etc. of other gameobjects), Unity overhead and perhaps rendering(not sure) is finished. If it is supposed to tick w$$anonymous$$le it is waiting, then t$$anonymous$$s one tick will be delayed. So the more other t$$anonymous$$ngs you are doing, the $$anonymous$$gher the chance is that your metronome will be late.

You might however get an improvement by changing your code slightly to t$$anonymous$$s:

 IEnumerator PlaySounds()
 {
     isPlaying = true;
     w$$anonymous$$le (isPlaying)
     {
         if (interval <= 0)
         {
             myAudio.PlayOneShot(myAudioClip, 1f);
             interval += (60f / BPM);
         }
         interval -= Time.deltaTime;
         yield return null;
     }
 }

By adding (60 / BPM) to the interval instead of setting the interval to it, you should get a more steady beat, as now when one beat is a millisec to late, t$$anonymous$$s will be compensated at the next beat.

Also, reading the docs, t$$anonymous$$s might be a way better answer. See AudioSource.PlayScheduled

Comment
mingom
Bunny83

People who like this

2 Show 3 · 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 elenzil · Mar 22, 2016 at 09:50 PM 0
Share

agree, PlayScheduled seems like the way to go. Update() may be called irregularly, but inside Update() you can look at AudioSettings.dspTime to calculate what time it is "now" and schedule a handful of sound-plays for the future. you'll need to be careful w/ your book-keeping not to schedule the same one twice, and i'm not sure how to cancel scheduled sounds.

avatar image mingom · Mar 23, 2016 at 11:37 AM 0
Share

Thanks @troien, I will try PlayScheduled and report ASAP. I have no idea, why i didn't discover that sooner.

As for the threads and Unity code: i just found this dude who managed to do something with AudioSettings.dspTime here: https://github.com/Nidre/Unity-Audio-Sequencer

I will still try playscheduled first, since it sounds simpler.

avatar image mingom · Mar 23, 2016 at 10:53 PM 0
Share

AudioSource.PlayScheduled did the trick. Thank you very very much!

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Audio not playing when destroying a GameObject. 1 Answer

Audio is way too soft on mobile but alright in Unity Editor! 0 Answers

Audio Cutting Out Unexplainably 1 Answer

How to make audio play at once. 1 Answer

Cracking at end of audio? 0 Answers


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