Sound FX and Music -- Godot 3 Tutorial Series

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody Mike here came from scratch today we were continuing ongoing could do a three tutorial series today we are covering all things audio from loading sound effects and music files to playing sound affecting music files to special effects and positional audio you name it if it's audio related we will cover it today now quick reminder for all patrons there is an e-book in development you do get preview chapters in every video I have done has a corresponding chapter available so if you're interested in a book covering everything we were covering here do be sure to check out patron alright without further ado let us jump in now in order to get going with audio obviously we need some audio first things first though let's open up our project so here I've created a new project again as I always do a root node right there scene let's save our scene all right and we'll do a run get that out of the way select our scene and we are good to go now with all things like using sprites etc you need some source raw materials and today we are going to be using two belch dot wave and soundtrack dog now the first one is a WAV file I mean both are imported the exact same way oops let's head on back over here and we just dropped the files into our project of course you could also open it show it and file a manager and drop it there using your native file system support and that's all you need to do to import them the two supported file formats there are wave which is a Microsoft format and OGG Vorbis which is an open source format now the big difference between them is wave as a relatively uncompressed format it's very simple to work with it's very fast to load it's great for one-off effects and quick short effects the other one is OGG Vorbis autumn Orbis is a compressed format like mp3 but open source now on Vorbis is better for long-form sound effects because it takes up a lot less space on disk so if you have dialogues being recorded you have a soundtrack to play etc those you would generally support in OGG format now you'll notice with either of these formats so if I example I select awed and we go over here to import there are very few settings here you can loop it and you can set loop offsets loop offsets allow you to have multiple basically soundtracks or music files in a single file and jump to within them directly whereas if we go back to our WAV file you'll see we have a bunch more options available we can basically cause it to be compressed down here we can make it into an 8-bit file we can turn it into mono as opposed to stereo we can change the max rate so we can lower the Hertz rates of it save a little space a little bit of memory if we don't need the visual or the audio quality or audio fidelity of 44.1 kilohertz and keep in mind forty-four point one kilohertz is what CD audio is recorded at so it's generally considered pretty good here you can also trim it down normalize it and loop it loop it is important if you want this amount like to play over and over automatically you can set looping here you can also handle this encode which we will see in a second and you can also handle if it is compressed or not generally for special effects you probably do not want to compress them so they can load and play almost instantly and that is basically it so now let's take a look at how you actually use audio and the basics are really really simple when it comes to Godot to actually just play a sound effect is a matter of basically just creating one note so come on over here we'll create it so we'll add a new add a new child node and the child type we were adding is an audio stream player right there now notice there's a 2 D and a 3 D we will cover them in a little bit for now we just want this guy right here so audio stream player is the node that will play audio fax you can have multiple audio stream player nodes in your scene and in fact you may want to if you have a lot of looping and overlapping effects and then the next thing you need to do is define a stream for it now stream is the audio source you can see you can get OGG Vorbis you can do a random pitch to basically a braw waveform you can play around with or an audio stream sample which is your WAV file format or much easier you can literally just drag and drop in now next up the the settings you've got here are volume now volume is confusing because it's in decibels but basically you can think of decibel range as going - 80 is no audio plus zero is a full audio so it's not in a scale that you're probably used to now if we want to automatically play it we can sit here and say playing on and if I go back to our audio file our belch and we import it and we set it to loop like so and do a really now when we do playing it'll loop over and over and over and over again now I don't want to do that so let's turn that off you've also got the option of autoplay basically when the when the stream player node is created it can automatically play the stream that is attached to it it's pretty freakin straightforward stuff playing audio is very very simple and our soundtrack would have been exactly the same thing even though behind the scenes it's slightly different so now when I go ahead and set that to playing it will you can hear it playing in the background like so now one thing to keep in mind is if you have it looping and playing it will play constantly while you're in the editor editing so you probably want to turn playing on with code just to keep your sanity intact and we'll also look at that neck I show you let's look at this from code completely from scratch so what we're gonna do is do everything we just did creating this audio sprinkler including playing but we're gonna do it without an audio stream players so let's just go ahead and delete that guy so we've got our node right here and we're gonna attach a script to it and let's call this dynamic audio all right so let's create our script right here and all we're gonna do is when we are ready we're gonna create a player so audio stream player new we're gonna add it to our self self dot add child player so and we're gonna go ahead and set the stream on it it's a player dot stream and we need to load it as a resource load so the root directory of our resources will be here so you can see all the various different options available so let's go with our belch right there so we loaded it from file and finally let's just go ahead and play it very straightforward very simple and save that up go ahead and run it there you go so that is how simple it is to do it all dynamically using code if that's your thing and coincidentally you could have just gotten it if you kept this screen player node you could have gotten a reference to it and just called play and that will play whatever the currently defined stream is you can also change streams in code by basically calling this again and passing in a different stream to use that time so remember earlier on we had that stream player 2d now let's take a look at actually doing positional audio basic positional audio using here we're gonna revisit this a little bit later or the more complex example but for now let us just redo our nodes a bit so here we're gonna detach this script all right so no clear scripts we're gonna had a child to this guy so that is gonna be a sprite like so and to that sprite we're gonna add a child audio stream player 2d like so alright so now this is going to basically while our sprite moves the audio stream player attached to it will move as well now we got through the same settings so let's go in here and we'll turn on belch for it let's put belch back into looping like so and Riaan port it and I'm back okay so now as we move around our belching noise will move with it you've got a couple of options down here for how far to go your distances etc but we're gonna ignore that for now what we're gonna do now is go back to our sprite and attach a script to it and call this bright pose audio like so and now what we're going to do in ready this first call is not necessary it's set by default but if you want to be just to make sure this is a setting that can be toggled off audio listener enable 2d to true to make sure that you have to do do enabled again that is turned on by default so you shouldn't have to do that so next we're gonna call our audio stream player and we're gonna play our looping sound nav gets annoying quick but it does demonstrate this thing nicely so now we're gonna implement our process and here all we're gonna do is basically move our sprite around so you can hear the audio move with it position X it's greater than zero - self oops it's greater then zero - self dot texture dot get wait so basically as long as we're still on screen move local X by - blogging pretty straightforward and finally if we moved off screen self thought position x equals get viewport the get visible rect dot size dot X we're just gonna move over to the other side of the screen and I'm gonna go back and add the colons I always forget to add like so and like so and theoretically there's no errors there we should be able to run this example and now you'll hear it pan if you if you're listening to a model source your nothing here anything Oh what just happened there I had an error but I want to get rid of the sound let me see you get width in base no instance on no reference get width so that textured get width I should be fine alright one second oh that actually means exactly what it means when I created my sprite I forgot to give it any details alright skip the step there so let's get this guy in the world and let's move him so he's somewhat centered in the script ah so here we are positioned somewhat in the scene and I actually have a texture attached now so our code should in theory run this time sorry my bad and here you'll see as it moves off to the left-hand side it should be panning off to the left-hand side and then you will hear the audio now cycle out to the right-hand side of the screen and then come on through so that's how easy positional audio is let me just shut that down before it gets too annoying I'm going to turn my volume down a bit too so you're not getting a double on that one but that is positional audio in the Godot engine the 3d actually works almost identically I'll cover 3d in a bit more detail when I start getting into 3d chapters but again it works the same way you attach it to a spatial node in 3d space and off and off it goes basically so now let's take a look at the heart of the audio system and that is the bus system this is where the advanced part of the audio in Godot comes from and if you want to create special effects or their ilk this is where you ultimately will end up doing it and you can access it via down here audio and this is your bus now bus is a little bit confusing to kind of make sense of immediately by default you'll have a bus credit for your default underscore bus underscore layout T res and what you're basically doing here is nodes are added to this guy or buses are added and each bus feeds into the bus potentially to its left it's basically like chaining special effects together so that you can create more advanced audio output now let's look at creating a very simple example we're gonna create a left and right panning note so I'm gonna go here add bus you'll see a new bus came up let's name our bus right here left right and then what you've got here is you've got three sets of controls this is your volume you've got swallow it basically says use only this bus mute which mutes the bus or bypass which skips it completely and next up we're going to go ahead right here and we're going to add an effect to it and we're gonna add a pan or effect now what you'll notice down here at the bottom this is your output now there's always mass or master ultimately feeds out to the speaker so basically this says when you're done generating your sound or modifying the sound we pass it off to master and this will make sense more so as we add more busses to the chain so you'll see also when I added an effect there's a whole bunch of effects you can add to a bus and you can have multiple effects active at once I'm not gonna go into a lot of detail about what each one of these does but we will show you a couple in effect now let's head on back over to our node hierarchy and do a little bit of work first off I'm going to delete everything here like so yep I'm gonna go ahead and add another audio player back in and we are going to loop our belching wav file over and over and over until you go mad with rage now the key thing to notice here though and this is there for all audio stream players there's this value here for buss and you notice by default it is set to master well if that's the case then this guy will only ever actually channel to this guy but you want to pick where to start in the chain so in this case we actually want to switch this out to the left right panners so now it can be actually controlled and let's go ahead and create a bit of a UI for handling panning we're gonna be adding to this guy over time as we add more complexity but I'm gonna pause out for a second while I go ahead and mock up a UI for all of the stuff we're gonna be doing today it's pretty complicated but if you already went through the UI um script in the past you've got an idea of everything we're about to deal with okay I'm back so here's the interface I have created it's gonna show you basically all the stuff we are going to be using and once again I covered all this in the GUI chapter earlier on so if you want more details on how to actually make inner user interfaces like I just did do check that video out I won't be recapping that but I will point out a couple of things I've done here first off we have this slider right here this is for panning left and right and I've set the minimum as minus 1 which is all the way left in the maximum of +1 which is all to be right here we also have a volume container as I mentioned volume goes from negative 8200 being full volume negative 80 being no volume and the decibel scale so I have set those as my range on this slider right here okay first things first what we're going to do is basically get a list of all of the audio files and populate this checkbox drop-down with them so the option panel here is in the sound container option panel we're just gonna go ahead and add grip to it and all we are doing is basically extending and populating all of the sound files that are available to us so first off we need an array to store the files in like so and here in ready get rid of that we want to go ahead and basically VAR player equals get tree so we're getting a reference to the player at the root of our tree so get root not get node audio stream layer so we're just getting a handy reference to our player we're gonna use that later on to actually play the default audio that we load and first VAR Durer equals direct druid new so we're gonna navigate through the directory of our files so first off let's open that guy up so dot open and the URL that we want to start is the root of our res redirector and let's make sure that was okay and assuming it worked out just fine dirt list der begin VAR file equals dirt get next while file not equal empty we will print out the file that won't bother that will just assume everything works great if file dot fine that's not right file that ends with wav or file dot ends with OGG it's an audio file and add it to our array pretty straightforward stuff basically we just loop through all of the files and if they're audio files let's add it to our filesystem array and now that that is done go to the next file file equals dirt get next alright so now for I in range --is file dot size so let's loop through our array of files self dot add item i dot ice basically we are adding all of the different files that we found to the combo box so we can switch between files available and now that that's done we are going to do something a little bit dangerous because I don't want to code around it but basically we are going to load the first file found so res colon slash slash plus files at 0 the reason why I viously this is a bit dangerous is if there are no audio files this code will now come boom and you probably don't want that alright so that is done now what we're going to do is hook up the event on our option here so we could let's go back to edit mode we'll show you that way so our option button has an event on item selected right here so when that happens we're gonna connect it to this event right here and all we are going to do basically is if files that's size it's greater than zero so there I have some safety this time var plot yeah let's just copy paste well you know what I never actually use that oh yes I did I did it right there so we're making the same reference we're getting a copy of our player node and player dots equals load and then just load in the new file ID that we've selected from our drop down like this so okay in theory everything should just work right there let's go find out if our code works or if I screwed anything up now I've done a lot so chances are I've screwed something up non-existent function size in base file my plural inverses not pro I am girl alright by oles alright let's try that again goddamn it ranges in did I do it range I in range files dot sighs that sounds right one second oh I keep making stuff up here plural vs. not plural all right let's go back to that and hopefully that was the last of the typos okay no god damn it one second all right I just resolved this path wrong so I guess I did it twice I forgot to take into account my root node named node so hopefully this is it let us hope downside of writing code life and finally alright no error so you see here we have our sound effect there and we can now switch it and I have one more bloody error all right what was it this time that's size files dots let's try one more time load it up switch it out okay we now have the ability to switch between audio files we're not currently playing them yet but we can switch so now let's move on to actually playing them so now that we have our audio file going let's attach a script to our play button it's uh where did play button go button controller right here alright play button and we'll attach a script create that here all we are going to do is alright first off so we don't keep doing that player thing it's create a variable for it in ready we will get a reference to said Blair variable player equals get tree get root yeah no now of course like I do this by get parent get parent get parent also if I wished some get node node audio stream player hopefully no errors done there so just getting a reference to the player when we first load up and finally let's hook up our on button press funk on I'll wire it that way it's 2d play sound button pressed connect that and we have a couple things to do if get node root node panel container /v bucks container that loop container shut check box loop so what we're doing here is basically checking to see if this check box right here is checked or not if it is we are going to be looping our audio now looping audio remember earlier on when we imported our audio so here is belch we have all of these options whereas if we go to the augs we have just loop well unfortunately between an OGG file format and a WAV file format the way looping is handled is done slightly different so we're gonna actually handle both mechanisms that pressed equals true so if that checkbox is pressed after we press the thing we're gonna go ahead and call a function we are about to create ourselves called set loop mode and set yeah I'll just throw a pass in there and for now so that it stops getting mad at me and funk set loop mode stream kind of loop so this passes in the audio file and we figure out what kind it is so if stream is audio stream IO stream OGG Vorbis so basically if it is an OGG file we set the looping by going stream dot loop equals so otherwise it's a WAV file in which case we go we got my colons I always forget my colons in the case of WAV file we go stream loop underscore mode equals the loop that so unfortunately again the two different audio files have a different way of handling loop and that's why we handle it that way so now that we've got if the loop is checked we do here is go set loop mode player dot stream and one otherwise set loop mode player dot stream zero and now that we are done all that let's go ahead and play our sound file play or not play like so alright let's see if we have a winner here run this belch is selected boom I still have another error there of course a couple of titles here put the slash in front of Roop and I did pressed instead of press but basically that is the line of code we want there I mean doesn't maximize this so you can see this a bit better ok and now before we do this because we're set on a looping sound effect in the first place let's go first turn that back off so we don't want it to loop automatically and now we're also gonna wire up the stop so we can stop this idiocy almost immediately so let's head on back to 2d stop and on pressed here well we'll reuse our play button it doesn't make a whole lot of sense but then at least I don't have to write much more code literally here we just do player dot stop alright so in theory no errors nice theory but let's run this hopefully it's all working now here we go so now we've got belt selected I can play it I can stop it so it obviously it's not there I can set it to loop and play it and it's not looping oh man I'm full of the hits tonight double equals there let's go ahead and run okay so now in theory I should be able to select our audiophile set it to loop press play why are you not working okay I'm not entirely sure what just happened there I basically just deleted the wav file re-imported it and now things work properly so I'm not sure if I encountered a bug or what's up but now we can actually demonstrate you come in here you can now select an audio file up here so I'll go back to our belch play it we can now loop it and play it and we can now stop it okay so now that we have this user interface in mind hooked up let's look at hooking up our pan or special effects basically on this slider right here we're going to interface with that effect we added right here now in order for this to work though we're gonna have to have this audio stream players bus set up correctly so come on down here and locate bus and make sure buss is set to left to right that means it'll start here and move this way now that we've got that hooked up let us go ahead and wire up the on change event for this guy so first we're gonna need a new script sure-sure that name is great alright so now that we've got that done select the slider and we're going to do value changed so this is as our value changes to our slider and we're gonna implement the bomb var pan so we're going to get our pan err equals and then we use the global audio server get bus effect so I'm gonna use that audio server quite a bit when programming with audio effects and Godot again we're gonna use it again audio server get bus index we bye and so we're basically getting which bus is by name so when you pass in the get Busta the first value is the index of the bus to work with and then the second value we'll see in a second so I'm gonna get the bus by name left-right so that the index name is this guy that we defined right there and then our second parameter is the bus effect to get so we only have one value we can handle e-enough use zero to access it yeah let me just try and get a little bit of space back.this 400 that's as small as I can go alright that's fine so now that we've got our bus effect defined we've got it a reference to our panner and we're just gonna set it span value to value so remember the slider goes from negative 1 to positive 1 so now that we've run that what did I do wrong this time audio oh server all right there's a lot of mistakes in this particular tutorial I'm bumbling my way through code ah there we go so now we can set this guide to looping play our belching noise and then pan left and pan right so that is how easy it is to hook up the special effects on the various different bus channels now let's show you how to use the bus for handling volume now do keep in mind you can actually set the volume here on the audio stream player right there so you could handle it that way or you can handle it via the bus let's wire up our volume control right here remember this guy goes from negative eighty to zero because it's controlling the decibels on this guy and we want to set this guy's value so I need to set a script first so let's attach a script to it default is fine select it again on value change and setting the volume globally is extremely easy all we need to do here let's go down to our volume change guy and set audio server is that guy again set bus volume now you'll notice you can have multiple buses in this case we know we're gonna use 0 so the master bus set that to whatever the value is in our slider save that up have them back over and we can now see play our sound sorry about the belt should probably pick something more friendly but now we can volume down volume up volume down volume up so that is how easy it is to set the master volume programmatically now let's look at a little bit more on the chaining side of the bus here so we'll go back to our bus we're gonna add one more audio effect and this one is going to be reverb and it actually gonna be two effects so we're gonna go in here we're gonna call this guy reverb I'm gonna come down here to the add effects and we are going to set the add effects to chorus which gives you a choir like effect and reverb now you'll notice what these things actually select it if you look in the inspector you've actually got the settings you can use for so reverb simulates being in a room so you see over here you can set up the room size dampening the spread dry/wet blah blah chorus also has its own different set so if you've got a whole lot of voices in your choir you can set this really high and get a real big effect or so on and so forth so you can do individual settings for each of the effects you add over here in the inspector window alright so we now have another one set up now the important thing here is you want to be able to set up a chain of busses so we've got this new bus here right now it's going straight to master so if we use the reverb effect will lose our panning effect so what we need to do is instead of going to master we'll go to left to right and this guy has no ability to go to the right you'll notice so the bus order is important you can only pass yourself into the bus to your left but we want to create a chain so basically reverb goes into left right which goes into master so that's why we changed that guy right down there and now we're gonna do a toggle on that check box reverb toggled so head on back over here this guy right here will grab it and let's attach a script to him and of course you could have put this all in one script I'm just doing it the lazy way and then we'll do toggled alright so when we toggle this guy we are going to turn on and off the effect and that is done guess what you use yes audio server set bus bypass effect like so audio server get bus index of our reverb bus this time and the value that we're passing in is a yes or no based off of if the button is pressed so we'll do the opposite button rest like so so if our button is not pressed or pressed accordingly it will turn it off and on go ahead and run this / button pressed button pressed I did a typo in there somewhere all right let's try that again all right so we here for there is our sound let's loop it and we could turn reverb on and off where did I do this time apparently we do won't want to set the but so we want to set bus by price effect not but and let's try one more time nonexistent set bus bypass effect in audio server set bus bypass effects one of those days sorry about this guys and we'll run that yeah so no errors on that one we will loop we will play our sound and you can see it no reverb and we have an issue and this is a classic example of why the buses are important so you notice my audio screen player right here my bus is still set to left or right so that reverb effect is never being triggered in so we switch over to use the reverb bus so now it will be part of the chain and one more time and now we run this guy we can play our sound no reverb reverb no reverb reverb so you can see how you can toggle you see how the buses work together bypassing up accordingly along how you can bypass a bus to basically turn it off in the chain and that's how reverb is working here basically so we're saying even though we're saying start at the reverb portion of it we can then turn that one off using bypass it and if we had a really complicated audio bus going on here like with five six seven eight more layers we can we can bypass ones in the middle by basically just turning them off and on so if we wanted to turn panning off we could just bypass this particular bus and it would work but do keep in mind that you have to set when the audio screen player you want to set it to the rightmost bus so it gets all of the buses in the chain and you want to make sure that each bus passes through accordingly so once again if I switch this guy to run master the panning would immediately stop working so that is how the chaining works how special effects work it seems really complicated but once you get use of it it's quite simple especially if you're not a complete idiot and bumbling your source code just as bad as I am today alright so one last example we've got going on it's probably just as easy for me to start a new scene let's go over to the 2d scene here and what we shall do here so we're gonna go back to showing that audio positioning so I'm gonna go ahead here create the root node create a sprite like so and we're gonna show you kind of how that those different buses can work together on with area based sound turning on and off this is actually some pretty cool stuff so we're gonna be using the same bus layout here so we got our sprite and I'm gonna add a stream player 2d to our sprite like so we're going to set the stream to belch that we all know and love we're going to make sure that belch is set to loop it is great alright so we will have this guy as playing and looping sorry about that while it's going on here let's uh started playing off until we absolutely need to turn that back on again and what we're now going to do is create so our sprite we got to actually make it visible in the world so we're my icon go grab him accordingly let's move him somewhere in the world like so and now we're gonna do is set up an area where it can't go through so go back to your root node and we go here and we're gonna create an area 2d so this is an area in our world put it right about there and inside of area 2d we're gonna create a collision shape so we're doing basically it's defining an audio area where's collision there we go like so we go into hit create the shape new circle let's edit our circle radius is equal to 40 that's not big enough 180 area so we got a big area that we're going on so now that we have our area come here into the area 2d and you will notice we have a couple of cool options here specifically if we scroll down there is audio bus right here and we can set it to override and we can give it a bus so we're passing in so this guy is going to switch it to the reverb bus when you're within this area we're gonna go back here to our sprite right here and we're gonna make sure so the bus is set to master by default so when this goes through it's gonna go from being just this bus it's gonna turn on reverb when we're really in that area and now I think we need to add a little bit of code so that we're moving moving moving I just basically need my code from before so where did my sprite go actually know what I'll just attach it so that was where is my sprite sprite pause audio okay so we're gonna go back to our sprite attach a script and we want sprite positional audio so remember so load that guy up now you'll remember sprite positional audio this is pretty straightforward all it did is it caused our stream player to start playing which is nice and then we just move across the screen so that's really all the logic from that guy so now we just need to come up here project set our probe I think I haven't saved yet say pause audio demo project set project settings and we'll go to the run kind and go here to the open open that guy up positional audio alright and now we're gonna go ahead and run this here you see and there were in our audio radius and you notice that there's a reverb effect going on and then now we're out of it and the reverb effect is off so that is how powerful those busses can be you can use it to create dynamic effects that based off of where your player go so if you're in a certain room you can set up an area that goes to us one specific bus and then you get an effect like that now one thing to show you that's completely unrelated kind of cool is you can actually let me see where it was you can actually come up here to the debug menu and say turn visible collision shapes on so what this allows you to do when you're debugging your code is to actually see the areas for the physics shapes you're dealing with see so you can tell when it's hitting and the reverb is coming on so you've got the ability to debug and have it show collision shapes when you are debugging very powerful feature we're gonna use that a lot when we get into physics shortly and that's about it for now we cover quite a lot today unfortunately it was a little sloppier than I meant a lot of typos in fact fingering on my behalf but hopefully it was illustrative of what the Godot sound functionalities are we did cover a ton here dynamically loading resources playing audio positional audio special effects bus chaining dynamically calling the buses volume control and then area based audio over rides the bus so we did talk about quite a bit in the last 40 minutes but again I apologize for the UH the bumbling code typing I was pretty off today but hopefully you figured out what you need to know about audio we cover just about everything there is to know except basically 3d positional audio and hopefully I'll get back into that when I cover 3d a bit down the road but you'll find that the functionality is almost identical to what to do do was so I'm sure you can extrapolate and guess how to make 3d work uh hopefully you guys found that useful I will talk to you all later good
Info
Channel: Gamefromscratch
Views: 48,362
Rating: undefined out of 5
Keywords: Godot, Godot 3, Tutorial, GameDev, Audio, SoundFX, Music, Game Development, Game Engine, Open Source, WAV
Id: S-8IcHucSNg
Channel Id: undefined
Length: 41min 43sec (2503 seconds)
Published: Mon Jun 11 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.