I’m a hardware audio design engineer who designs PCM bus and audio serial interfaces for a living. I was scoffing at this at the beginning, but ended up stealing all the code
@@b3agzIt's been 4 years since your videos first got me into really programming my own projects (my boxes engine) and I'm once again led back to you lmao
I know 99% of people who see this can easily figure it out, but by adding a Slider control to the BeatController script, you can control this in game by setting the pitch of the audio source to the value of the Slider. Great tutorial!!!
I was trying to accomplish the same effect with Coroutines and WaitForSeconds(), but that wasn't perfect (it desynced every now and then). Your solution is much more elegant and useful! Thanks for the tips!!! PS: The steps thing is called note length. *2 would be a half note (2 notes per beat), *4 - a quarter note etc. And I guess 0.5 would be "half-time". (I'm not a very good musician, though. So take it with a grain of salt)
Thank you so much for this video! Trying to get audio to sink to a beat has been the bane of my existence across so many engines, and yet this works well and was surprisingly simple to follow!
For as long as I can remember I've been curious about how I could synchronize enemy attacks with the music, and this is the most complete I've seen. Thank you very much :)
This is genuinely brilliant. I've been working on my university project for some time and was getting frustrated with the game being out of sync with my music, especially given the song is fast paved so it's very obvious very quick. This couldn't have been shown to me at a better time!
The music used in the video is piece by Johnnie Holiday kindly released as a CC0 track ( freesound.org/people/Johnnie_Holiday/sounds/563651/ ) and the asset pack used is Unity's own free 3D Game Kit ( assetstore.unity.com/packages/templates/tutorials/3d-game-kit-115747?aid=1101l9Tam& )
I'm just starting my internship and I'm very new at Unity but I was told to learn it and research on topics I'd like to know. I'd like to work with music and audio, I do some music myself as well, so this was a good start. I did some changes, like setting an offset to the beats so they would align to the down beat for example, added a start delay for songs that are not made to loop. This was a very good starting point for someone that knows some programming and knows a little bit abut music.
@@saing_rbn5368 Well, tbh the solution is not very elegant because it's proportionally inverse to how we understand beats, but I'll try to explain: what we want to modify is when the rhythm trigger/interval happens right? So in that class declare a float, I called mine " private float _phaseOffset = 0f;", you can add the [serializefield] to tweak it in the menus. Then since it's a fixed value, it will move the beat at a fixed rate when it's checked by doing "int currentPulse = Mathf.FloorToInt(pulse + _phaseOffset);" after that you do the check "if (currentPulse != _lastPulse) " and the operation to update the pulse and trigger it in the method "checkforNewInterval". The values of the offset have to be set between 0~1, more will just cycle over.
i was so stuck using fixedUpdate, altering timesteps, time.time multiplied by blah blah, using different ways but the game kept getting out of sync after a few minutes. thank you so much
Great video! I made a small rhythm game for a game jam where you have to interact on the beat, but it wasn't synced properly. This tutorial helped me get it working better.
I don't know if anyone mentioned it but a good name for the _steps variable would be _subdivisions I think. As musicians, we subdivide a beat in our head to help us visualize rhythms which is essentially what you're doing with transforming something like a whole note into half notes, quarter notes, eighth notes, etc.
I’m working on an all-around sound and music management system for Unity native audio, and was not sure how to accurately track beats to assist with more precise music transitions. I think this is a great way to get me started. Thanks!
Fantastic tutorial. Glad I decided to consult RUclips. It was a little hard to follow at the pace of the video but after going back and forth a few times I got it. Thanks so much!
Mind sharing the code/script? I'm making a rhythm game myself and would really like something that can switch tempo on the fly without manually inserting it
Thats so great. What a cool idea to write a script for that issue to figure out how to do things on beats. :) pls more of that kind of small helpful scripts that solving one issue.
Thanks! If you have things you'd like me to tackle let me know. Can't promise I'm smart enough to solve it but I love trying to figure things like this out.
I saw this video some time ago that would actually fix some problem I had with my game. But unfortunatly I'm using FMOD and the game is porting to android. I tried a lot but honestly I think i'm going back to built in and using this I'm fed up with FMOD.
It should. As long as your BPM variable correct for track at any given moment, it will always be in sync with the music, even if the BPM changes. You just need to make sure your BPM variable is always right.
@@b3agz hmm, I see. I have an incremental game with a background beat that increases linearly from 80bpm to 160bpm in 92 seconds (numbers are not exact). I can't figure out a way to make it completely sync to the whole thing, since the gameplay itself consists of a ball going back and forth, at a certain speed that increases every x seconds. I kind of came back to hardcoding certain speeds, since I couldn't find a way to change the speed according to the increasing BPM down to floating point precision, but your tutorial was still really informative. the problem is that it cannot be synced at really fast speeds since it desyncs so easily and I can't find the exact 15+ decimal points number for acceleration to keep it consistent with the beat. thanks for the reply!
If I'm understanding you right, you should be able to use the UnityEvents system to trigger a "beat" and then just check if the beat is currently triggered.
FOR PEOPLE WHO PUT IN THIS CODE BEFORE REALISING IT'S FOR BEATS, NOT PRE RECORDED TRACKS First of all, you need ```using System.Linq;``` This might take a while, but you can add ```[SerializeField] private float[] _skips;``` to the start of Intervals and then do ```if(!(_skips.Contains(_lastInterval))) { // _trigger.invoke, etc}```. Then you can put every note to skip if there is no beat there.
I have a pre-made music with its beat being a core part. I now wanted to use it for a rhythm puzzle game. Can you explain this solution? Somehow my object is still out of sync with TestBeat() and the BPM variable doesn't seem to be affecting it for some reason. I'm still looking through it.
I’m a hardware audio design engineer who designs PCM bus and audio serial interfaces for a living. I was scoffing at this at the beginning, but ended up stealing all the code
I'm pinning this 😂
@@b3agzIt's been 4 years since your videos first got me into really programming my own projects (my boxes engine) and I'm once again led back to you lmao
This is **the definitive way** to handle beat matching in Unity. You can't even imagine how much you saved me with this video. Cheers!!
Glad it helped!
I know 99% of people who see this can easily figure it out, but by adding a Slider control to the BeatController script, you can control this in game by setting the pitch of the audio source to the value of the Slider. Great tutorial!!!
I was trying to accomplish the same effect with Coroutines and WaitForSeconds(), but that wasn't perfect (it desynced every now and then). Your solution is much more elegant and useful! Thanks for the tips!!!
PS: The steps thing is called note length. *2 would be a half note (2 notes per beat), *4 - a quarter note etc. And I guess 0.5 would be "half-time". (I'm not a very good musician, though. So take it with a grain of salt)
Thank you so much for this video! Trying to get audio to sink to a beat has been the bane of my existence across so many engines, and yet this works well and was surprisingly simple to follow!
For as long as I can remember I've been curious about how I could synchronize enemy attacks with the music, and this is the most complete I've seen.
Thank you very much :)
This is genuinely brilliant. I've been working on my university project for some time and was getting frustrated with the game being out of sync with my music, especially given the song is fast paved so it's very obvious very quick. This couldn't have been shown to me at a better time!
The music used in the video is piece by Johnnie Holiday kindly released as a CC0 track ( freesound.org/people/Johnnie_Holiday/sounds/563651/ ) and the asset pack used is Unity's own free 3D Game Kit ( assetstore.unity.com/packages/templates/tutorials/3d-game-kit-115747?aid=1101l9Tam& )
Hifi-rush got me really curius on how music syncing works and this tutorial helped me a lot 😄
I'm just starting my internship and I'm very new at Unity but I was told to learn it and research on topics I'd like to know. I'd like to work with music and audio, I do some music myself as well, so this was a good start. I did some changes, like setting an offset to the beats so they would align to the down beat for example, added a start delay for songs that are not made to loop. This was a very good starting point for someone that knows some programming and knows a little bit abut music.
How did you manage the offset?
@@saing_rbn5368 Well, tbh the solution is not very elegant because it's proportionally inverse to how we understand beats, but I'll try to explain:
what we want to modify is when the rhythm trigger/interval happens right? So in that class declare a float, I called mine " private float _phaseOffset = 0f;", you can add the [serializefield] to tweak it in the menus. Then since it's a fixed value, it will move the beat at a fixed rate when it's checked by doing "int currentPulse = Mathf.FloorToInt(pulse + _phaseOffset);" after that you do the check "if (currentPulse != _lastPulse) " and the operation to update the pulse and trigger it in the method "checkforNewInterval".
The values of the offset have to be set between 0~1, more will just cycle over.
Bro explained the math so well I went straight in and built it first try
i was so stuck using fixedUpdate, altering timesteps, time.time multiplied by blah blah, using different ways but the game kept getting out of sync after a few minutes. thank you so much
Great video! I made a small rhythm game for a game jam where you have to interact on the beat, but it wasn't synced properly. This tutorial helped me get it working better.
Awesome! Glad it helped.
Hi! can you share or explain how you coded the part of interacting on the beat? I'm trying to do the same for a game and I'm having some problems
Finally! An advertisement that's something I'm actually interested in, and it doesnt even ask me to go to a website for answers!
Best video so far!!!
I don't know if anyone mentioned it but a good name for the _steps variable would be _subdivisions I think. As musicians, we subdivide a beat in our head to help us visualize rhythms which is essentially what you're doing with transforming something like a whole note into half notes, quarter notes, eighth notes, etc.
I’m working on an all-around sound and music management system for Unity native audio, and was not sure how to accurately track beats to assist with more precise music transitions. I think this is a great way to get me started. Thanks!
absolutely brilliant video, very underrated. Saved my ass more than once
Fantastic tutorial. Glad I decided to consult RUclips. It was a little hard to follow at the pace of the video but after going back and forth a few times I got it. Thanks so much!
super cool! I added a scriptableobject that contains the audioclip and the BPM to be able to switch songs and have the BPM for it.
Mind sharing the code/script? I'm making a rhythm game myself and would really like something that can switch tempo on the fly without manually inserting it
@@leomassafm160 well you still have to manually insert the bpm as a field of the SO
@@wolfeygamedev1688 does the scriptableobject also take into account mid-song tempo changes? Rock songs have a lotta fluctuations in tempo...
this was perfect! You are an angel!
Thats so great. What a cool idea to write a script for that issue to figure out how to do things on beats. :) pls more of that kind of small helpful scripts that solving one issue.
Thanks! If you have things you'd like me to tackle let me know. Can't promise I'm smart enough to solve it but I love trying to figure things like this out.
Very helpful. Thank you bro!
Nice! Thanks for support!
Thank you for making a great tune :D
one thing i've found doesnt work with this well is having a song with a dynamic bpm because it makes the interval variable just randomly move around
I saw this video some time ago that would actually fix some problem I had with my game. But unfortunatly I'm using FMOD and the game is porting to android. I tried a lot but honestly I think i'm going back to built in and using this I'm fed up with FMOD.
love it
art
For some reason I still have an issue with syncing. Also the bpm doesn't seem to do anything for some reason?
This reminds me of Pineapple on Pizza game.
bro, why you dont use the fft blackman harris formula from unity?
if my BPM increases linearly, will changing the corresponding BPM variable inside my game at the same time work with this code?
It should. As long as your BPM variable correct for track at any given moment, it will always be in sync with the music, even if the BPM changes. You just need to make sure your BPM variable is always right.
@@b3agz hmm, I see. I have an incremental game with a background beat that increases linearly from 80bpm to 160bpm in 92 seconds (numbers are not exact). I can't figure out a way to make it completely sync to the whole thing, since the gameplay itself consists of a ball going back and forth, at a certain speed that increases every x seconds.
I kind of came back to hardcoding certain speeds, since I couldn't find a way to change the speed according to the increasing BPM down to floating point precision, but your tutorial was still really informative. the problem is that it cannot be synced at really fast speeds since it desyncs so easily and I can't find the exact 15+ decimal points number for acceleration to keep it consistent with the beat.
thanks for the reply!
I had a question if you don't mind. How would I make it so that if you press a certain button on one of the beats per minute, something happens?
If I'm understanding you right, you should be able to use the UnityEvents system to trigger a "beat" and then just check if the beat is currently triggered.
When comes the Showreveal of Jam3
Should be up towards the end of this coming week.
Simples!
Can it be used in 2D objects??
Yes, you can use it with any game object. There's no fundamental difference between a 2d and a 3d object to unity.
First
Thank you, @b3agz! 🔊🔊🔊🔊🔊
FOR PEOPLE WHO PUT IN THIS CODE BEFORE REALISING IT'S FOR BEATS, NOT PRE RECORDED TRACKS
First of all, you need ```using System.Linq;```
This might take a while, but you can add ```[SerializeField] private float[] _skips;``` to the start of Intervals and then do ```if(!(_skips.Contains(_lastInterval))) { // _trigger.invoke, etc}```. Then you can put every note to skip if there is no beat there.
I have a pre-made music with its beat being a core part. I now wanted to use it for a rhythm puzzle game. Can you explain this solution? Somehow my object is still out of sync with TestBeat() and the BPM variable doesn't seem to be affecting it for some reason. I'm still looking through it.
@@Kiru176 I'll make a google doc. It might help.
@@Kiru176 docs.google.com/document/d/1REb2SxXYiFDRq_n2ceY-ue_wvWAV-umKf64Xzk9BgYc/edit?usp=sharing
@@Sacroooooooooooooooooooooooooo Thank you.
@@Sacroooooooooooooooooooooooooodid you make it?