Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 sinetwo · Apr 14, 2015 at 10:26 PM · audiorhythm

Does Unity 5 help with rhythm games? Or should I stick to AudioSource.PlayScheduled?

I'm currently trying to make a simple metronome, based on a BPM (say 60). The metronome will play a sample every 1 seconds. The user can press a key when they hear a sound, and the application will say how accurate their key press was against the metronome.

Is there anything in Unity 5 to help with this type of rhythm game scenario? Or should I stick to AudioSource.PlayScheduled? Obviously using coroutines that are dependant on frames is a bad idea, so I've moved away from that...

Any help is greatly appreciated!

Thanks.

Comment
Add comment · Show 2
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 _Gkxd · Apr 14, 2015 at 10:48 PM 0
Share

I have written a rhythm game library for a personal project. You can find the code and documentation here. It was written for Unity 4, so you might have to update a few things, but do look through the code to see how things are done. Feel free to ask if you have any questions.

The library is based off of using AudioSource.timeSamples for ti$$anonymous$$g ins$$anonymous$$d of Time.time.

avatar image sinetwo · Apr 15, 2015 at 08:57 AM 0
Share

Hi _Gkxd - thanks a lot for providing the code and documentation. It will hopefully provide me with a good way of understanding how these games are built from foundation. I did start off with Time.time and using a delta to correct for any lag. However I'll try using AudioSource.timeSamples.

I guess both AudioSource.timeSamples and AudioSource.PlayScheduled could work, depending on the implementation. But it seems like AudioSource.timeSamples would be great for a longer audio file.

Again, thank you for offering to help, I greatly appreciate it.

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by pablo_leban · Apr 15, 2015 at 09:08 AM

Im making a rhythm game too right now. The metronome is easy. First you need to know the BPM of the song, let's say 120 BPM. Then because 120 BPM are 120 beats in 60 seconds you can say that: 120/60 = 2. This means that there are 2 beats every second. If you know this, then you can get the samples of each beat and doing that, you can store that number in a variable and do this:

   public double BPM = 120;
   public double beatSamples;
   public double nextBeatSamples;  
   
   void Start(){   
     //44100 are the sample rates, assuming your audio is in 44100hz
     beatSamples = (44100 / (BPM / 60));
     //beatSamples = 22050
   }
 
   void Update(){
     //audio.TimeSamples will increase when the song is playing
     if (audio.timeSamples >= nextBeatSamples && audio.isPlaying) {
         //plays the metronome sound
         audio.Play();
         //here it increases the nextBeatSamples, so it will not enter the If 
         //again and it will wait until the timeSamples
         //get to the new nextBeatSamples
         nextBeatSamples += beatSamples;
     }
 
   }

Comment
Add comment · 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 sinetwo · Apr 15, 2015 at 01:19 PM 0
Share

void Update() is called every frame, would that be reliable for a long song? The reason i'm asking is that i've been told that coroutines are a bad way of ensuring accuracy. However, I can't seem to find any info on whether or not void Update() is a coroutine or not. If not, then great, I'll look into using audio.timeSamples!

avatar image pablo_leban · Apr 15, 2015 at 02:17 PM 0
Share

Update() it is not a coroutine. It doesn't matter if the song is too long or not. Update just makes a check of two numbers between the audio.timeSamples and the nextBeatSamples, and if it does it only increase a variable. It's not creating objects or something like that, which will not create any kind of lag.

avatar image sinetwo · Apr 15, 2015 at 02:32 PM 1
Share

Thanks pablo_leban, I understand now why you'd want to check BP$$anonymous$$ against audio.timeSamples as it provides you back the time in samples :) Thanks for providing this elegant solution, I'll post any updates as I go along!

avatar image
0

Answer by DHARMAKAYA · Dec 26, 2015 at 11:13 AM

Looks like a wonderful script and also nice info! Likely try this later today!

Hey, any reliable onset beat detection script anywhere?

Thanks in advance! @pablo_leban

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

6 People are following this question.

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

Related Questions

Calculating rhythm of any music? 3 Answers

Does anyone know of a tutorial for a rhythm game? 0 Answers

Lag/jerkiness (as opposed to low framerate) in a Rhythm game 2 Answers

Spawn object, or not, depending on audio properties 0 Answers

Simple rhythm question. 2 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