Audio & Music in the Godot Game Engine -- A Belated Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello ladies and gentlemen it's mike here at game from scratch and today we're going to be going into a godot tutorial i actually covered this topic a little while back but enough has changed that is definitely worth revisiting what we're going to be looking at is playing sound and music in godot i'm going to be using the most current stable version of godot which is good 3.2.3 as of the time of recording and another reason i'm doing this is uh there is a humble bundle on literally right now there's one day left and i said i would show you how to use these assets in a good old game so here i am i'm giving you plenty of time to consider this bundle because you have exactly one day remaining so all the sound effects we are going to be using in this example come from the music and sound effects two bundles so if you already picked it up you can follow along if you haven't don't worry just grab uh you're gonna need like an mp3 or an obvorbus and a couple of wave files to follow along or you can use some of the assets like we are going to use here so in the bundle obviously i've purchased it already we're gonna go ahead and grab uh something we'll start with um orchestral music we're gonna start with a music pack i'll go ahead and let that download and we'll head on over and create our new project i'm going to use a really straightforward user interface fair minimum that i can do here so i got my 2d scene created a node as my route and i'm going to go ahead and create a center container like so uh and we will size that out uh to i don't know what's the default 10 1080 and 600 nope not quite so the thousand all right thereabouts here i'll just i just want to use up the whole viewport of the default all right there we go so here is our uh container and we're just gonna have a bunch of buttons inside of it so we'll go ahead and create uh button a oops so button standard button like so uh and i will again call you button a and over here i will use a bit of a naming convention button a now we're going to have a bunch of these so just ctrl d ctrl d ctrl d ctrl d and ctrl d we'll switch them as we need them so the first one here is okay so we got button button a and we'll call this one play music and oops i don't want a center container this is confusing because in my original one i did a center container and then i changed it actually what i want to do is a grid container just to make my life really easy grid container there we go so there are all of our buttons on board which probably should change out the buttons on that um nah i'll keep this as easy as possible i don't really want to play around with the ui so here we go we'll just go with the complete defaults there so we've got our first button play music all right and now let's give this guy a name that actually makes sense in code button play music so obviously what we're going to do is play some music when we press that button so in order to do that we're going to need obviously some music so i'm going to go ahead and create a new folder for storing our music file in and our download should be done so i'm going to head on over to the downloads folder there we go so here is the pack uh let me just do an extract out all right so there is the downloaded pack uh we're gonna grab just a song here we'll do a victory song i feel like very victorious so this one that's a wave file are these all wave files um i wanted to show ah darn okay so here's the song all right sounding good so what we're going to do we'll go to our music folder and we will open it up in the file manager and we will dump our song over by the way this could be uh og vorbis format it could be mp3 format in this case it is wob format so there is our sound in place so now we need something to play it so i'm going to go back to the root note here and we're going to go ahead and create a new player so this is a stream and so you've got audio stream player or you can do 2d or 3d we'll get back to 2d in just a second but i'll start with just a normal stream player so create an audio stream player we'll keep the default name right there and i'm just going to create a script uh at our root node here we'll call this i don't know sound demo all right so here is our script in place here is our button we'll go over here to node and we will set a button up event so we're going to connect that there button up good to go all right so we now have our audio stream player like so now i need to actually go back over here and select that and we're going to set a stream for it so you see here under stream you can set a number of different stream sources you can do it uh mp3 you can actually record audio from a microphone if you so wish and echo it back out ag vorbis random sounds uh you can generate your own that's something that's new in three point something or other now you have the ability to do procedural audio but what we want to do is just a new stream sample and then once you've got that just come on over here and bring your sample file over and dump it in by the way when you bring audio into godot you have a number of options under importing you can change it down to 8-bit that will use up less space you can make it mono which again will have it so it's not stereo it'll also take up half the space you can have it so that it automatically loops you can change how it is compressed that's a trade-off of obviously cpu versus memory space uh and yeah so we've got our audio in uh so we've got it attached as our sample and now we're gonna go ahead and show you how to hook that up so go to our button it's already connected like so so let's go back over to our script there is our button handler and we're going to show you how to play audio it's about as simple as it gets audio stream player dot play and that that's it uh we go ahead and run this so this is going to give us our first time blah blah blah blah blah blah i haven't seen my scene yet scene save the scene sure go ahead and run it select our scene we're good to go all right so now you're gonna hear it i do not know why with the dual monitor you always get pushed off screen or down to the bottom corner when you run things okay play music all right we're good to go so we have music playing now one of those things you're going to probably have wanted to do there though again let's go back over to our script and you're going to see you probably want to do if and then we'll do this as a negative not audio stream player dot playing then run this otherwise every time you press the button it will basically start the audio again not a huge deal but probably a quick check that you want to put in place now if you want to stop the audio well let's go ahead and go to our second button so let's go here and we'll say stop playing all right so there we've changed our button up there over here we'll call this uh button stop music alright here we go and of course we'll connect that one up and so created by the default all right here we go so if you want to stop music playing it's about as easy it gets audio so you use your audio stream player like so stop that's it so here's how you can start playing audio here's how you can stop playing audio uh pretty straightforward on the whole and that's probably all that most people need to do for the most part now i did mention earlier on that you can have a positional audio so i'm gonna go ahead and show you that already so come back here and we're going to create a new node uh yeah it's right there so we'll call this an audio stream player 2d so we'll create one of these guys right here i'm going to show you something else you can do with audio stream players if you don't want to get into code at all you can actually go ahead and we'll use the same sample so we'll use our um hero wave like so we can actually set it to autoplay when it's created and you can have it play in the editor as well so if you want to hear it in the edit which by the way gets really annoying really fast so i generally turn that one off but this will automatically play now one thing you'll notice i think this will show here this is in stereo what's different with this guy is it's positional okay doesn't show it that way so what i've done is i've moved it to the right channel all right so this one is set up uh in our viewport oh i'm dead center alright so let's move this to the far right and now we'll go ahead and play this and it should be playing it only on the right i can't actually tell in my recording but that is the big difference between the um positional ones there's also one for 3d so you can position them in the 3d space and they will uh hook up accordingly and take into account their positioning which is actually kind of cool all right so that is the basics of playing and stopping music sound effects is really no different they're literally the exact same thing that used to be you had stream player for doing music and you had sample player for doing waveform samples now it all basically just comes down to the type of sound file that you bring in so you'll find playing a sound effect is about as simple as it gets let's go back over here uh to our pack and let's grab some sound effects this time there's an ambient city one that's a nice one to have multiple different sound effects going on so uh background ambient city okay we'll go ahead and download that one this is pretty small download so it should be pretty much instant one of the things i do like with humble is you can just kind of use them as a library and download them as many times as you want just kind of as you need it so here is our download uh there we go 7-zip extract to the directory oh wait i just did the all right let's do the right one this time 7-zip extract ambient city there we go so we got a couple of sound effects here from the pack so these are wave files again of various different locations so we're gonna do thunder we'll do uh rain and we will do a jackhammer yeah that doesn't anything like a jackhammer but works for me okay so jackhammer thunder and rain all right so grab each one of those things and we will dump it over back into our project this time we're going to make a new folder even though music and sound effects are the exact same thing just to keep it logically organized we will move them here and i'm just going to rename these this will make sense later on when i'm doing code to a b and c oops and c all right so there you go so we got three different sound effects we can go ahead and use in our game you'll notice they all imported incorrectly now every once in a while the encoding file might be the coding style might be wrong and you may have to use something like audacity to make it work it can't be a compressed waveform audio sometimes that causes it to the half a little bit so we're gonna go ahead and basically just create a new stream player uh so here's and we'll call this i don't know effect player like so and again you just basically drop in the audio you want to work with uh so by the way you didn't need to create the stream like i did all those times you just drop it on there and it will figure it out itself and uh yeah that's kind of it so let's go here again playing audio also uh button play effect uh and we'll go play effect like so and now let's wire that one up so right here and we will connect and okay good so playing the effect it is the exact same thing as playing music so audio stream player dot play now you'll notice we've already we wired it up so the stream that it's going to use is already defined that was defined in the properties uh right here so effect dot stream oops i just played the wrong stream all right let's try that different effect player dot play all right so go ahead and run that and there we go now one thing to notice if i press it again it starts it over again so each audio effect player can have one thing going on at a time now one thing that you might find interesting okay how do i go about setting up the volume or if i want to do special effects that kind of stuff but one thing to notice with all of these audio players you notice the audio stream player and effect player there is a setting here called bus and when i have an audio item selected such as the effect player or the audio stream player you'll also notice we have a tab down here called audio and here is the default mixing bus so this is master that is there's always the master it is the ultimate output you can define and create your own buses we can add buses here so i could create a completely new bus if i wish i can add multiple buses here so i could have like a localized sound effects or regions of my game where the sound was muffled or whatever and you'll see it sort of this passes through so they they go from right to left and you can choose where they go so if i add another bus here i could actually have this bus go the output to that bus and then that bus go out to this bus so you can basically daisy chain effects together and to actually use these buses well let me go ahead and get rid of these two that do nothing so let's get rid of those you can think of this as a master mixing board so now let's say you want to control something such as the volume well we could do that with another one of our button so let's do this one volume down like so alrighty so button vol down all right so here you can how you would set volume down and over here you might do obviously vol up and i'm only going to showcase one because it's basically the exact same code so here we go with volume down and let's go ahead and control that so we go here we're going to press the button here hook up a new setup there and now we use our audio button so how do we get a hold of the master bus this guy right here well that is done using something called the audio server like so so you do audio server and then you can do uh you can control different values for it we could set new effects for it and so on so we could go set uh decibel bust volume db like so so we can increase the decibels or increase the decibels so you've got an index towards the bus to use this is bus zero so we're going to go zero comma and then what we'll do is again audio server dot set bus or sorry get bus volume db like so and let's take one away from that db it's a logarithmic scale it's a very weird unit of measurement to work with to be honest so uh oh and we got to tell it which bus to work with so master bus so we're saying basically here get the current volume and decrease it by one so this is when we press the volume down button so let's go here and we will play our music and there you see decrease the volume obviously you can do the exact same opposite by copy and paste and making the plus sign i'm picking a minus sign neutral plus side and that is how you can control uh the volume now there's a lot of cool things you can actually do with the audiobus that i'm not going to touch on right here so i can add a special effects bus like so and then in your um so i'll call this my sfx all right so what you can do is in your player like so you can choose which bus you want to pass through so now this will affect it and then it'll pass it into master so if we control the volume at the master level all of the buses to the right are going to have the same thing impacted to them so what i can do now is i can start adding special effects here like i add a high pass filter low pass filter pitch shifting reverb and so on so let's say i add some reverb to this bus like so and you can chain multiple effects by the way so we could actually add distortion as well um and then the the things that are passed through this particular bus will have those special effects applied to them so you'll notice if i have the uh effect selected down here in the audio tab you've got control over so you can set the size of the room for the reverb the dampening and so on and ditto for distortion you can check how the distortion is going to be done and we could also go ahead we could add an equalizer in here and you see the equalizer you've got control over it here as well so you've got a lot of abilities here and you can turn these things off and on using code simple enough so that's where you start getting into the really advanced features of um working with audio in godot again it goes beyond what i want to cover in this video i'm going to try and keep this one under 20 minutes now the last thing i'm going to show you is remember when we played some audio here we were actually only able to play one i'm going to show you a really hacky way to basically spin off as many audio effects as you want by creating one dynamically so let's do uh back to our example here we'll use our final button here and we will set this one as play uh random all right so here we go we're going to play our random song when that is pressed uh and it will change you so you actually make sense button play random all right so when we press this button we are going to play a random sound and this is why i changed my sound effects to a b and so on uh so first thing we're going to do is head on back over to actually no i'm going to wire up the code all right so there we go create a new function all right we're good to go now i'm going to go back up here so when uh we are loading our code so i should use onready i'm going to be lazy here so um audio files i love that pun audio files is a new empty array and we're going to load them in so audio files dot append and we're just going to pre-load a bunch of audio on the way in so oops i should just use auto completion here all right let's try that again so if i get my auto completion back oh i spelled a pen wrong anyways audio files dot append uh preload all right there we go uh so we wanna do a like so b and c so really all we're doing is creating an array of loaded audio files uh that we can use later on and then down here what we're going to do is when we press play a random sample i'm going to show you how to dynamically create uh your own um audio player which is very straightforward i'm also going to show you how to select a random number between 0 and three so var rng equals random number generator dot new so we're gonna create a new random number generator all right i screwed up some no no we're good okay so create a new random number generator and then what we're going to do is randomize our values like so all right so there we got a selection of random numbers available to us and now what we're going to do is go ahead and create a player so var player equals audio stream player just like what we did earlier just without a typo and we'll create a new one like so audio stream player new alright so we created a new audio stream player now this is a node in our world so obviously we need to add it into our world like so and we'll add the player to the world all right so we created a new dynamic player every time someone presses this button which by the way if you think wow that's right for abuse yeah yeah you're right uh you could probably exhaust memory pretty easily here so i would not highly recommend doing it this way without a couple more safeguards in place or have a pool of pre-created audio stream players is a good idea as well and now i'm going to go ahead now that it's created we're going to define the stream so remember earlier on when we created these guys you just basically created the stream this way while creating a stream this way it's just as simple as stream equals and then we're going to use our selection of loaded audio files like so and then finally we just want a random number so rng random integer in range 0 to 2. so 0 1 or 2 which will give us our index of a b or c so this will load in the auto file that we wish to have now that it is loaded we're gonna go ahead and play it there you go that is a way of creating a audio stream dynamically adding it to the scene and playing it now there is definitely a problem with this in that you just kind of keep re building these things in so you're going to probably want to get rid of them at some point so what i'm going to do is do a yeah you yelled a yield on this player uh and when the audio is finished signal uh so that is a signal attached to these guys so you can see here if you go here there is audio stream player has a finish this is called when is when a sound effect finishes playing it's very handy to use for something like uh if you've got a sound manager so if your song ended you want to play a new song after you could handle this this event right here and then after that fires we're going to go ahead and remove the player from the world like so and [Music] i guess we'll we'll print that we did this removed player from we're all now i typed a fair amount of code there so the odds that i didn't screw something up are really not good so let's make sure sfx a wave b c okay so we have three different randomly picked between them uh we're creating a new player each time we press the button so it's going to show you how to dynamically create audio let's go ahead and see if i screwed something up all right so here we go there's one of our sound effects playing so that's the thunder all right we have a number of different sound effects playing there and by the way we can also play our music at the same time and then you're seeing as the players end as the song ends we get rid of it now if you're working in your own environment and you want to create this dynamic kind of object tool because again you can only have one sound playing per stream player so if you want to have 10 simultaneous sounds you need to have 10 audio stream players wait probably the easiest way to do that is basically create an object pool of players and play the next available if you've got more than the number of streams you want to have playing play you basically just do an await for the next one to finish and then chew it in what the downside to this is is you can essentially i could come down here and i don't know what this is going to do but we could start off a whole bunch of these things so we have a lot of different audiobots playing right now and they're going to slowly start chewing out and didn't crash or anything so far but eventually it may so uh this is a very uh primitive way of uh creating dynamic audio on the fly but it's also one of the easiest ones to illustrate so hopefully that all made sense again audio server the bus the bus is very important that is like this kind of master eq system again you can create your own buses buses travel from right to left so if i created another bus here i could chain this one into here so you can create your own series of buses so you could have various different special effects in place if you want to have audio compressor basically all the things that you normally work with in a typical audio editing environment like a daw are generally available here equalizers um distortion effects that kind of stuff and you can sort of create it so that your audio you could have your audio muffled in a certain environment by having the audio player that's there so we'll call this one my muffled audio and have it say the compressor kicked in and does whatever to that audio and then here in the player i basically just pass it to muffled audio and muffled audio can then pass either into my effects or we could have separate effect buses and this one could actually just go straight to master so this one will fire from here over to here uh so it gives you the ability to have multiple different kind of sets of controls you can dynamically set them and you can do a lot of neat special effects using this by the way you can also create your own it's a t-rest so it's a typical resource file but otherwise playing audio is about as simple as it gets basically bring your audio in and you can start playing it you can stop it and again you can handle events such as the finished event and then fire off and do the whatever code so again you can have this queue up another set of music files or whatever and the only other really final thing to be aware of is when you bring an audio file in you do have some control over how it is encoded you also have it um we could also bring in as you saw here when i went to a stream we could have brought in mp3 and og vorbis files as well and now in the newest version of blender 3.x you've actually got the ability to create audio from scratch so if you really wanted to you could write your own music player in godot and generate autumn um generate your your music algorithmically if that was your thing now i'm way over the 20 minutes i'm pushing up on 25 so i'm going to stop there again uh one more reminder if you are interested in some sound effects to check out uh this bundle is ending in less than a day now that video took me it took me a while i'm gonna upload it so you probably got about 23 23 and a half hours left to check that bundle out if you are interested the link will be down below and again since it's humble if you pick it up i do make a small commission and thank you for that alright that's it hopefully you found that tutorial useful and i will talk to you all later goodbye
Info
Channel: Gamefromscratch
Views: 6,603
Rating: undefined out of 5
Keywords: Audio, Audio Bundle, AudioStreamPlayer, Bus, Dynamic Audio, GDScript, Game Development, Game Engine, GameDev, Godot, Godot 3, Godot 4, Humble, Master Bus, Music, SFX, SamplePlayer, Tutorial
Id: nIGJ6ImzSuI
Channel Id: undefined
Length: 25min 5sec (1505 seconds)
Published: Wed Aug 11 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.