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
1
Question by Mat Brummitt · Mar 30, 2010 at 09:03 AM · iphonemusicinvokerepeatingtimespan

Calculating BPM (beats per minute) using InvokeRepeating() - Issue

Hi,

I am making a music based game/app and need to have a BPM system that fires off events in time. Obviously Unity works on Update() ticks and I need to find a way to convert these ticks to a system that I can use musically via BPM and 'Beats Per Bar' (the divisions that drum notes can play on between the pulse beats).

What i've tried already is having a method SetTempo( int bpm, int ticksPerBeat ) and using this to work out how often I should fire my own Update method called UpdateRepeating(), via the InvokeRepeating() method which basically just calls my method over and over again every x number of seconds. Using this, i'm able to write some procedural drum loops and they play out as i expect, but I'm getting slowdown by simply moving my mouse around the screen at any kind of speed or by scrolling over my Mac's dock. Is there another way I should be doing this that won't cause slowdown?

Should I be using a Timespan to give an accurate measurement of ticksPerMinute to somehow work out when my UpdateRepeating() should fire?

Thanks

Mat

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
3

Answer by KvanteTore · Mar 30, 2010 at 09:35 AM

If you need something that is very regular, I would probably create a coroutine which yields WaitForFixedUpdate() and keeps track of time to call the your UpdateRepeating(). You could use WaitForSeconds, but in my experience, it is less regular than WaitForFixedUpdate.

//C# IEumerator PeriodicUpdater() { float waitTime = 10; //10 seconds between updates

float curTime = Time.time; while (true) { if (Time.time >= curTime + waitTime) { UpdateRepeating(); curTime += waitTime; } yield return new WaitForFixedUpdate(); } }

Comment
Add comment · Show 2 · 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 gregzo · Dec 13, 2011 at 09:27 AM 1
Share

For anyone else trying to have a stable bpm in a music game, InvokeRepeating is what I would advise. Tested many different methods, and asked for more experienced users opinions, all point to InvokeRepeating. Not sure about how that would fare on iPhone, though. Just posted a question on the subject.

avatar image Bunny83 · Dec 13, 2011 at 03:50 PM 1
Share

Just for the record: Waiting for FixedUpdate makes no sense. All coroutines, Invokerepeating "tasks" and even FixedUpdate they are handled all "per frame". FixedUpdate is a time-fixed update function, not a fix-time update. FixedUpdate isn't called at fix timesteps. It is called just before Update "if necessary". If your framerate is higher than your configured fixtimestep FixedUpdate is not called every frame to keep its CallsPerSec. If your framerate is lower than your fixtimestep FixedUpdate is called several times at once.

No matter what you do, FixedUpdate calls are always based on your framerate. Unity uses a single thread approach for the scripting environment.

I'm not sure how InvokeRepeating will handle framerate fluctuations but your approach will definately work as expected but a yield return null will do since it will ensure your timecheck will happen as fast and as often as possible ;)

+1

avatar image
2

Answer by KillMobil · Apr 21, 2015 at 03:08 PM

I know i am late for the party but for future references ill post my findings.

I dont know if unity released this at a later date but the Audio system features a AudioSettings.dspTime. Documentation description:

This is a value specified in seconds and based on the actual number of samples the audio system processes and is therefore much more precise than the time obtained

The documentation has a nice example how to make a steady beat at 140 Bpm. http://docs.unity3d.com/ScriptReference/AudioSource.PlayScheduled.html

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Audio Clip Syncronisation Help! 0 Answers

Guitar Hero style game? 1 Answer

Audio Source with some extra info. 1 Answer

how to use ipodlibrary? 0 Answers

Music resumes between levels on iPhone ( and not on android) 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