Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by julianhahn56 · Oct 22, 2021 at 09:26 AM · loopmusicsyncdsp

My code generated Metronome gets out of sync with the audio source metronome.

Hello, this problem drives me crazy, so i have to post my first ever question.

Im am prototyping a rythm game, and i am trying to keep my code in sync with the music. In my simple test case, the music is a looping audio clip consisting of 4 clicks, at 125 BPM. So with a beat every 0.48 seconds, the Clip is 1.920 seconds long. alt text Now i want anotcher click to be played in the same pace, at exactly the same times, but by code. So i ceate a AudioSource, and .play() it everytime my code determines a beat is completed. This seems to work fine for the first few loops, but after a while it loses sync. I made a short video to demonstrate the difference (20 seconds in it cuts to out of sync) Youtube Video

I've read through several tutorials for unity rythm games, and made use of dspTime and playSheduled. Can you guys see other problems with my approach? Here is my code:

 // Start is called before the first frame update
     void Start()
     {
         //Calculate the number of seconds in each beat
         secPerBeat = 60f / songBpm; //125 bpm
 
         songStartDelay = secPerBeat * 4;
 
         //Record the time when the music starts
         dspSongTime = AudioSettings.dspTime + songStartDelay;
 
         //Start the music
         audioSource.PlayScheduled(AudioSettings.dspTime + songStartDelay);
     }
 
     void Update()     
     {
         //determine song position, with delta if we dont get a new position
         if(AudioSettings.dspTime != lastDSPTime)
         {
             lastDSPTime = AudioSettings.dspTime;
             songPosition = AudioSettings.dspTime - dspSongTime;
         }
         else
         {
             songPosition += Time.unscaledDeltaTime;
         }
 
         //determine how many beats since the song started
         songPositionInBeats = songPosition / secPerBeat;
 
         //calculate the loop position
         if (songPositionInBeats >= (completedLoops + 1) * beatsPerLoop)
             completedLoops++;
 
         loopPositionInBeats = songPositionInBeats - completedLoops * beatsPerLoop;
         loopPositionInAnalog = loopPositionInBeats / beatsPerLoop;
         //detect new beat
         beatPositionInAnalog = songPositionInBeats - completedBeats;
 
         if (beatPositionInAnalog >= 1)
         {
             MusicDelayText.text = beatPositionInAnalog.ToString();
             debugClap.PlayScheduled(AudioSettings.dspTime);
             completedBeats++;
         }
    }
 

Maybe the Problem is that i try to detect a new beat by checking if "beatPositionInAnalog" is >= 1? as you can see in the video, it never equals 1, so maybe the delay adds up over time? But i dont know how to prevent this, IF this causes the problem.

Any feedback would be highly appriciated.

screenshot-2021-10-21-215841.png (18.1 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

170 People are following this question.

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

Related Questions

FindObjectsOfType not find all objects and code not do anything. 0 Answers

Coroutines Madness (); 1 Answer

loop for perimeter of game board 0 Answers

How to stop this infinite loop 1 Answer

Weird infinite loop-or just long loading time 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