How to play music in udon (music sync time, playlist)

(upgrade : music sync time)
I’m going to explain here a simple way to play music, play music sequentially, and share your thoughts on how to sync music.

" Hi, nice to meet you :slight_smile:
I don’t speak English very well, I’m sorry if you don’t understand
I’m not an expert, I just wrote this because I want to share my thoughts and help
Please tell me about the information that I did not think was wrong.
Any good ideas, advice please
I don’t know much, but I’ll try to write something to help
Please enjoy this :smile: "

/

  • 0. what is this tutorial?

/

intro: hello~! I love listening to good music while watching the beautiful world on vrchat.
I wanted to play each music clip sequentially without combining multiple music into one clip.
udon was released while I was thinking, and I learned how to play music.
And further, I wanted to see the same people and enjoy the same moments in the same world. In addition to Object Sync and Video Player Sync, I wanted to enjoy the world while listening to the same parts of the background music and the same lyrics.
So I’m not good at various methods, but I did some research and made udon by asking friends around me.
This can be a bit odd. I hope I can give someone help or ideas, and I would like to discuss a better opinion.
Please tell me advice and ideas :slight_smile:

/

  • 1. how to play music in udon?

/

simply, To play music you need ‘audio source’ and ‘audio clip’.

1-1. First, create one object and add component ‘udon behavior’ and create a ‘new program’. and ‘open udon graph’


1.1.1 create any object


1.1.2 add component udon behavior


1.1.3 make new program and open udon graph

1-2. We need an event node to start the code. Bring up ‘start’ using ‘add node’ in the upper right
And we need to ‘set audio clip’ to play on the audio source. and we have to ‘play audio source’.
and Connect nodes in order


1.2.1 bring event node (event node tell when to run the algorithm udon code)


1.2.2 bring audio source set clip node (Tell the audio source which clip to use)


1.2.3 bring audio source play node (Command to play the audio source)


1.2.4 Connect nodes in order of execution (When executed, set the audio clip you want to play in the audio source, and play the audio source)

1-3. We have decided on an audio clip and have prepared the code to play the audio source. so we need make ‘audio source’ and ‘audio clip’.
And we need to decide which audio source to prepare and play (instance). And you have to tell them which audio clip to use. (value)
And in order to connect the ‘audio clip’ and ‘audio source’ in the udon code, you need to put it in a variable, and get use ‘get variable’

ready music file
1.3.1 Prepare an audio clip(audio file) to play


1.3.2 Then create an audio source.


(1.3.2.1,2) Uncheck the ‘play on awake’ checkbox


1.3.3 bring variable audio source node


1.3.4 bring variable audio clip node

8variable public
1.3.5 Name the variable you want. And check the public checkbox

9public add full variable
1.3.6 After checking the public checkbox, you can check the public variables in the udon behavior of the inspector window.
And you can drag and drop audio sources and music into the variables


1.3.7 bring get variable node (After setting the variable, you need to get the value contained in the variable)


1.3.8 Use ‘get variable’ to get the audio source and audio clip contained in the variable and connect it to the udon code
‘instance’: get variable containing the audio source (to tell which audio source to set the clip to and which audio source to play)
‘value’: get variable containing the audio clip (in order to tell which music to set to the audio source)

CODE: play music

play music show udon code

AO2W207bQBCGXyXaa2+03p09OBIXLdAKVWqRKLmpImu9O0ZujY18gEaBJ+tFH6mv0A2BtCIWTVsqkIrii/ig9b/fzP+Pv335uiDntuyRTD4sSN6X5Vt7Gk7Ia+zSqW0Km5VIItIXPlzMueECmKEs8Z6CN4Ja4RlVXmltYpVz5sPDZ3VbdEVdkcmCfCYTGid8zCIyX/5lZsyuIlLVHo8P9trwWkJmEcnL+uLmfLa6O12qaq9l9VXRzd9lH9F105XWBSmqtrOVw4M9Mlku2HZNUZ3c3CZH87bD0/HR9cVodNq6uimLLBpNsWmDsh0Ys+UvGu32Zdc3uFNh3zW2jEaHfVYW7g3O39efsNrJtLbSSRUnApCZ5FJLmxjrJXUQWwpgTGCgGJXcae68xVwCuZpdRb/GmaG2DJ2lKpZAQeuEGpFzCtbHXAPnEs0ATiXULU4DzzRvaSIakyWcUcZ4WAoBaJagookH4CBBKj9IE8y6OQ37L3HeokyPl3vZr06KCl/0vqh3y+JszTfOhAqLANXIQo8mkNNEx4aCFAqABfwsH+ArfnQrDJk/+ul4eNZkudUnVaIKL9Zs1z38Vypf1nWJtnpYme+b/inKemXL9k91bTb+3X4/qvvG4ThND0s7T9PVRqZ14dcm4D4TWoCh0uTLCah4MIHIgyey2CbghHdi0wTXU2/lAa42I3ubsXrJ/ok7tibShsh1oWXTwZAYZIWopXI6psaHvYFjkloWI9VeQZzlOQrQA4HB+b2wton5ACvaarpuUN2uwI+YTL+R3qvarcux1WzYLIcE9pzfA/m9ovuc4I+b4PvnWHXpUWebbt3oBnniMwsUg8cpGBFTqzKkCTPO6fANY40c+lBR7J7cuRMTW2XbXQPMgvzv

Note (1-4.) If you added audio source using ‘Add Component’ in object, you have to use ‘game object get component’


(1.4.1) If you added audio source component inside a game object, you should use ‘Get Game Object Component’
instance : To get the component added to this game object, we use ‘this’ node to let us know that we are this object.
type : Use the ‘Audio Source Type’ node to bring up the audio source component.

CODE: play music (use get component)

play music (use get component) show udon code

AM1W207cMBT8FeTnGPkeG4mHdqEIVWqRgH2p0MqXY5o2JCgX6Ar4sj70k/oL9bKwFDZabVtQUaIodhx7PHPmHP/8/uMKXdiyB7T16QrFviw/2LPUQHvQTca2KawrAWWoL0LqVDI4ySXBUkqGhbcG62gJVpJYGk0E4UQafF63RVfUFdq6Qt/QFmZEbJIMTdMrZWST3GSoqgMc7++0aVmETjIUy/ryrn0y/zqeoWpvYfVV0U0/ui/gu/Ec6xUqqrazlYf9HbQ1m7DtmqI6vfuMDqdtB2ebh7ed2cZZ6+umLFy2MYamTci2E6DZlW2M+rLrG9iuoO8aW2YbB70rC/8epkf1V6i2XZ5b6aWihgsg2lxbqXwUEnDwnGMhlcbOU455iJFzJrwjFN2c3GSP6LyncnI828tudVpU8KYPRT0qi/MFv9RxlTMvcA4k8WtExCanOq3ClRAEQJM4wC/n6p5fQfQyv9lv9/NzjWZbfVUSVXC54HYRw/+E8m1dl2Cr54V51PSvEdY7W7Z/i2s58J/G+2HdNx42J5OD0k4nk/lGxnUK/3sTsOB4zoXGUseARVAsmYDH5AlHrRGeJ+Mtm4DOAn/uAabEkgdItEwyluZzeXoQ5bGRebKXZCFY6jWl6pq8iDvWZqRNKdenkJ0MJolBrgDylI5yinUgKU14IrElFHAelKAuRuAiH0rI7DnIytaqB0usrifwf8xMKyXbSz3z2ZJiqUiO6rPzuoKqu9fnaHoOjxRcjFiotg6/A6op9lBG9XIZpZx7boLH1viIBeQWG5oD9oGJqKJmoMytatoaCERQrGgUWNDAsIuOp9EGnJBGaf5SXngmPUZ1+mFy9LloH0rnGrsf4FSrVZyupuApqlvlh929gLkO90Mw6aLC6yW//hnK3YtZsB52tnkISA3MBGcFBsYkFppTbJUDbIj2Pk9HEqvl0LlDkRVp5Inr10pVy+BPbn4B

/

  • 2. music play list

/

So far, we have learned how to play music from basic udon usage
In Chapter 2, we will talk about how to play multiple songs in order

We will check whether the music is playing or not to play the music in order, and then play the music if it is not playing.
And by numbering by audio clip, each time you play it will set the next number of audio clips in the audio source.


2.1.1 This is a complete shot of the music playlist code


2.1.2 all variable

bool : Variable with true or false value (defaults to false)

audio clip : It is a variable that can hold an audio clip like the one used above. The difference is that
array () can contain multiple clips, not just one.

audio source : As used above, it is a variable that can contain an audio source. (There was also a getcomponent method above, but I will use the public variable method here.)

int : It is a variable that can hold an integer value.(like …-2,-1,0,1,2,3…)
Here, two int variables ‘now_music_number’ and ‘clipsize’ are used.
‘now_music_number’ is a value that indicates the array number of the clip to be played, and ‘clipsize’ will contain a limit value so as not to exceed the maximum value that ‘now_music_number’ can represent.


2.1.3 We’re going to make an algorithm that checks whether the music is currently playing or not, and if it isn’t, plays the music.

update event : This event is called every frame

branch : is an if statement. If you connect the bool value to the circle of the left down, it makes the behavior different if true and false.

audio source get is playing : Tells whether the audio source connected to the instance is playing or not(true or false)

audio clip get : Select which of the multiple songs in the audio clip array to import. (The audio clip array starts from 0. So, if 3 songs are entered through the array variable, the song numbers will be 0,1,2 in order.)

Use ‘audio source get is playing’ and ‘branch’ to determine if the audiosource is playing.
If is not playing, set the clip of the number of the audio clip array to play in the audio source.
Play an audio source.


2.1.4

set variable : Set the value of the variable (enter the value to set in ‘value’)

int addition : (+) Adds integers together.

int equality : (==) Determines whether integer values ​​are equal or not equal to each other. (true or false)

const int : has one integer value (constant)

You need to play the first music, 0, and when the 0 music is over, you need to play the 2nd music, 1.
The ‘now_music_number’ variable, which tells you the number of the song to play, will be added 1 to the variable after the music is played.
If you do this, if you add 1 after playing the music in ‘now_music_number’, the played music is 0, ‘now_music_number’ is 1.And when the 0th song is over and the code works again, it plays 1 in ‘now_music_number’ and adds 1 again to become 2.

In this way, 0,1,2 clips are set in order.
However, if you continue in this way, ‘now_music_number’ will have a numeric value that is not in the audio clip array(because keep over added).
Therefore, if we exceed the value of the audio clip array that we set, we need to set the value of ‘now_music_number’ back to 0, which is the initial state.


2.1.5

on player joined event: An event that starts when the player joins.

boolean unary negation (op unary negation) : Invert the value of bool. Change from true to false or false to true.

Change bool’s false to true when the player joins.
As a result, this bool value causes the update to start after the player joins. And even if the second player joins other than the first player, only the bool of the recently joined person becomes true, and the bool of the existing player remains true.
This algorithm is described in “Why Did You Do It? (about problem, bug?)” Below.

pl use
2.1.6 how to use?
audioclip size: Enter the number of audio clips to use
Then drag the audio clip below to the variable.

audiosource: Drag the audio source into the variable.

clipsize: Please input the ‘audioclip size’ value once again. (Because the audio clip array starts from 0, if you enter 4, it becomes 0,1,2,3. Therefore, the limit value returned to the initial value is It should be 4 after adding 3 to 1.
Then ‘now_music_number’ plays 3, and when it becomes 4, it is the same as 4 of ‘clipsize’, so it is made 0 again and repeated in order.)

playlist music

music play list show udon code

AO1ayW4bRxD9FWHObKH3RYAP3hI4AWwDsnUJBKJ6UyahhgoXO4qiL8shn5RfSI0WUtK06IFC2pQdSKAWzpDdr169qlfNf/76+6z6AKN5qvZ+OqvyfDR6Dcf4R3UAkxr8KA33T6ezdPxsPB4laKpBNa8jPu2yZpRSICpbRqTkklivIsk0MquYtJQxvPhkPK1n9bip9s6q36s9xnfpoDqt9oikdpeeD6pmHNP7Vy+m+PYVXr/8PhxUeTT+ePXc4eWVB+1KpxdLnTf17PSN/yWF2cHl+s+qupnOoAnp1Ytqr33x6WxSN0dXT1fV+eAht13uf3f/4p+DneNpGE9GtR/sHKTJFLf2RO7S9muw83w+ms0n6UmT5rMJjAY7b+d+VIcf0+m78a+peeKNARWUZk7IRK370yOo/21VV2FZ77K+g9E0fWXrqs4P2xvvJ/irZib4gt45+KSd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Qyuxs6NBEGNcRnnmvJjs2lyfhjH12KvQw5jZkdunMdY3Ddm9cnv7tEZkHrI1mWTe4p8YtgEW15GsAI7tpeFQ6Kz1gsyiOGLuE9WNyOjmDnLWaKmLVjGZ7JhFR2wDsgDRIkCNRgsjraOeMSiJil7ZRrQteY/wFjxOn57mq0irNYX0XvvRGo/rxDsY13FZkZNjgTpOnPMSKzJDCRWGk6g4+KwiMK0Lg+6lcSuc7TzIcvRj3md2bVNMkXZ+PSweGRQB7WU+CyOCxajFdictPQEdVJEllhWaiIzmkciYsfXGiohZw73PoJVUBeR7cWCrmst7D3AwZm15v11vSlcv4tUHslXxkl29S5xK10afpoAGyPFAnBHYmDEmgSHIxrKLcAEzBgWNERsBKR9pwvemnnAvozZeUha7qrhVgSj2Un32X4CULxrTwoj1cfVSvY4P+4HZhyOrwOQFG/q4wHxYY/ryQ2pmw/cnEWZLMHnkKXkIxMRwMWBFIQXUvJTxhwvCulgYkhDjFh96Mx0074hprxHxXcTLi3/TtGU7TX4Yo3ItK0yWWkgRJW4CX1yCBeIZPujgcxYQfYqlKaGVdMUp3p1d9Dop/NQuVn48q1dUO7tQy0n6F/j44Tf18axm/HF4PJ/WYdjMj32afGWH5du6rq6SPR/jDcUcaoc8zmsgQSQsM84y4qWSJHqbk3TYu0BBCRjXdIVrfmRp08vV9hxJd4ASerWt7ROAx3ewv2HLenj+Lw==

/

  • 3. music sync play

/
Now let’s finally learn how to sync the music that is playing.
This code ensures that even if the player joins the world in the middle, the player can listen to the music at the same time as the player was listening to.
If you are in the same world, you can share the same music.
I’ve had a lot of trouble creating this, maybe it not be accurate, but I’ll share it.

The basic principle is as follows.
Record the song number and playing time of the music being played based on the oldest person in the world (we call this person the ‘master’ (when the master leaves the world, the next oldest person becomes the ‘master’)).
At this time, the recording is made in the synced variable (none).
And when someone other than the master enters the world, the music is played by reading the song number and playing time recorded by the master.
Therefore, in order to match the sync of music, people other than the standard play music based on the standard one (in this case, ‘master’).

The explain mainly focuses on the parts that are not explained in the above two examples (because it is based on the music playlist).


3.1.1 This is a complete shot of the music sync play code


3.1.2 all variable

float : floating point numbers variable (like : 20.0, -3.12, 0.12513)

synced (none) : Synchronize variable values (Here, we check the sync of 3 variables.
The float variable stores the master’s playing time. And the two int variables suit the value associated with the number of the audio clip.)


3.1.3 Identifies whether the player entering the world is master or not.
By default, the sync variable can only change master, but if a player other than master affects the value, synchronization may not work properly. Therefore, we need to distinguish between the master and the not master.


3.1.4 The algorithm that runs when the master.

audio source get time : Get the play time of the audio source (float).

audio source set time : Set the playback time of the audio source.

In the case of a master, in synced variable (time_float), the time of the audio source being played by the master is input to the variable every frame.
And when the audio source is not playing (when the music is finished playing)
Enter 0 in sync variable (time_float) and sync variable (time_float) to
audio source set time before playing music each time.
Then, enter the value of sync variable (now_music_number) in sync variable (sync_music_number).
If you do this, the sync variable (now_music_number) prepares the next audio clip number, and the sync variable (sync_music_number) has the audio clip number that the master is listening to.


3.1.5 Algorithms executed when not master.
Get and set the clip number and time value of the audio source that the master is listening to.
When the audio source is not playing and the sync variable (time_float) is not 0, the clip number is set with sync variable (sync_music_number), and the playback time is set with the sync variable (time_float).

CODE: music sync

music sync show udon code

AO1cW5OTuRH9K5Sf3ZTualHFA7ekyCbsVlh4SVEuXYmzxia+7GYC/LI85CflL6S/udjG1ni0gw02UEBh48GWTrdOn9OS/L///Pdd71c/WuTevb+965XFaPTMv6EnvUeT8Ww+eH42m+c3z4fj16Pc6/cWw0QvyYwSixJgs2agCufgdTCQAgrOoxPWe/rht5PZcD6cjHv33vX+1bsHgpu7rN87o4eSHn3o98aTlF88fTyjz+71XvV7ZTT57fL5q4tXX3ZDm52PbTEezs9+DP/Icf7yYsDvekMaox/H/PRx7173hrP5lIZ6+XKv9+HVh/5Hk3qe54OXfjr0YW06OSFiEt10LAcldYKAKYHLPMdUhKW/atMReDUdrtT2fPpNOL1nGxPviRyS0dxDTBJBiWLAFx1BGE7DMi6zyHv7gOcitnefn/9j/86bWZxMR8PQv/MyT2c00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7whHLRyJWclrFHmuDXV5n5jC6fvhLNWYlqCVCm/qnQaVpMmI9ujujsRjgUP3Flknux0tuZwh1yq670l3jV4lmeAKkffT2u9N108bQOzZZXtBPPkz8jejjzXv9fzY2HAeRQhIafgFBLP3kQqR4xBsMUoEQrFrCKehVh9V8VnrUeH6e21Sek2btlEiuNNXfOWEJzesYEDy+RXH/4P

Why Did You Do It? (about problem, bug?)
Here, I will share some of the problems that I encountered while writing code.
This is about my thoughts, so it may not be accurate.

  1. When I entered the value of set variable as get variable, it did not work.
    So I solved it by entering by adding 0 to get variable.

  2. As an update event, when the audio source is not playing, it is played and the clip number is added by one. However, for a short period of time (approximately 60 frames) when joining the world and fading in from the black screen to the world screen, the audio clip number increased and the music was not played.
    So I solved it using bool and on player joined so that the update command starts after the player joins.
    (Maybe anothe way, is we can solve this by making the clip number count from the time the audio source is played.)

  3. We confirmed that the sync was not working properly when the player, not the master, joined the world, fetched the synced variable, set the time and clip number, and played it. Sometimes it worked, but sometimes it didn’t.
    Even if it works well sometimes, i checked if there is a minute time difference (about 0.0 … ~ 0.4 …) when the receives the time after playing music.
    So, I made is not work when the synced variable (time_float) value is 0
    I thought that if the value of synced variable (time_float) was not 0, it would be after synchronization. (because Initial value is 0) and fortunately this worked well.

  4. While testing as the master and non-master players left the room and came back in, the audio clip number increased well, but found that the audio time was fixed at 200 seconds.
    Before trying to play the music, I think two methods of initializing the variable each time or using two audio sources
    fortunately it worked just fine used first methoed
    It was solved by always setting the synced variable (time_float) to 0 and setting the playback time to 0 again before the master played the music.

  5. During the synchronization test, I started the clip number with -1 and set the clip number to play after the code added 1.
    But sometimes -1 was entered as is, the code didn’t work.
    So I fixed it by modifying the code to make it start at 0 and adjust the order of adding 1.

Thanks for reading
This method maybe inefficient or may not work smoothly
Please share on a better way or idea :laughing:
I hope you can enjoy the background music synchronized in many worlds
please enjoy this :slight_smile:

5 Likes

Thank you, works like a charm when testing offline, sadly had some issues when trying to update online.

Setup:

Unity Version: 2018.4.20f1
Scripting Runtime Version: .NET 4.X
SDK Version: Working on SDK3 :ramen:

Any Idea on how to get rectify the error?

Debug code: https://pastebin.com/kRynyV7t


Many thanks in advanced.

Check component “Get Variable” and “Set Variable”, in my case the copy/paste doesn’t paste the right var.
In my test, You can’t use this graph on play mode, you need to “Build and Test”.
I think it’s because of the use of the VRCPlayerAPI ?

Thank you for this tutorial, it is really very useful to understand udon!

1 Like

Thanks for the guide! As an addition, here’s how I made a pause button for the music player. I followed Vowgan’s Contextual Button Tutorial.

NOTE: This setup was made for the local player code, I haven’t tested in the synced player code yet.

This is the graph for the pause/unpause toggle. It should be added in the same GameObject with the music player graph. The “Active” boolean has to be turned off.

You will also need to add a branch using the “Active” variable right after the Update event, and before the first branch node from the original code, like this. This branch has to activate on False.

If you don’t do this, the music player will start playing again after you pause. Adding this branch will turn the music player off until you press the button again.

This is the graph for the button object. In the “target” public variable, link the Object with the music player Udon graph.

HOW IT WORKS:

When you click the button, it will send the Custom Event “Enable” to the Music Player. This will pause the music and turn the variable “Active” on, which will also stop the music player code. Clicking the button again will send the Event “Disable”, which will turn “Active” off and enable the music player, and will start playing the music from where it was paused.

In short: when the button is Enabled, the music player is Disabled, and vice-versa.

1 Like