 Hello everybody and welcome back to another C++ and SDL programming tutorial. In the last tutorial we were looking at the SDL underscore image library version 2.0 and that allowed us to actually use more image types and file types anyway with our images that we load in SDL. We were able to use .png, .jpeg, .gif or .gif, however and more common file types than the typical bitmap because only by default SDL has I think load a bitmap but that is not going to work out for us because typically we see .png files that's really where we see most of the sprites come from in the real world. So we wanted that img underscore load. Now that library was kind of nice. It didn't have to be really initialized or anything it just had to be included and it would work with services or SDL services just like normal. Now continuing that idea of adding resources and new elements of multimedia into our code and into our software in this video we're going to get into sounds and music. So I'm going to go ahead and save this code. I'm going to create another copy of it called 10a and I'm going to call it I think adding sounds is what I'll call it 10a adding sounds .cpp. Okay cool now let's hop on over to the internet. I've got my internet browser open and I'm at Google and you'll notice that in the last tutorial we were looking at SDL underscore image and it seems like with SDL libraries and modules it always has this theme or this kind of naming convention where we use underscore and then the name of what it is that we want. Now the new library or the module that I want to introduce to you is called SDL underscore mixer and you'll see the first result here gives us a link to another projects page at libsdl.org and that's exactly what we want. Sorry libsdl underscore mixer 2.0 is available and all the things available here do give us the windows binaries the linux and mac stuff that we need. Again probably on linux I'm just going to install this by the command line and I'll show you how to do that very very soon. This description anyway is that SDL mixer is a sample multi-channel audio mixer library. It supports any number of simultaneously playing channels of 16-bit stereo audio plus a single channel of music mixed by the popular flak, mic mod, mod, timidity, midi, augbore, and other stuff that I don't know anything about and that's really all that we need to know. Documentation is shown right up at the top here and let's see I think we can just go to one page html and that'll tell us how to use all this stuff. Alright we don't have to install it this way because it'll kind of go ahead and install that when we install it on our own and actually let's let's get to that. Let's go ahead and install this. I'm on linux so I'm just going to go ahead and use the regular command sudo apt get install sudo to run a super user apt get as their package manager and install and the library is called lib sdl2 just like we've seen before. Now the hyphen is going to denote the name of the module of the library in this case it's mixer and of course we want the development libraries so we use again hyphen and dev. I'm going to get enter I want to enter my password and uh for me this is already installed but it'll just ask you yes do you want to do this and you'll type in y for yes you do want to do this. Okay now in our software in our actual code we'll go ahead and include this just like we included sdl image we're going to use include sdl mixer and you'll see in the documentation this is exactly what it advises us to do. Compiling the way that we can do this uh is anyway pretty similar to the way that we included it with sdl image we use dash l um sdl2 in this case and then underscore mixer. Okay now here it it shows some of the functions that we can use um I believe initialize will initialize it I don't know if that's actually necessary from what I know initialize this mix quit open audio is what I've seen used most often so I'll show you how to use that and uh and here it displays the information for all the arguments that we'll be using and that sort of thing. Anyway let's get down to our code um we're going to end up using all of these probably after our sprite classes and uh and that sort of thing um I'm going to create a little code block for us probably let's see where do I want this we'll do it after we update the window and I'll do the nice little comment initialize the mixer okay now this first function that we're going to run is mix underscore open audio mix is the kind of precursor like or at least the beginning like we've been seeing with img for image and sdl for this for actual the sdl library now we're just going to use a mix with uh only a capital m rather than capitalize everything and open audio is what we want to use here and that'll kind of initialize things for us the frequency is by default 22050 you'll notice there is a constant for that mix default frequency I'm just going to copy and paste that because I'm the lazy and um format I believe is output sample format and I have been using the same argument for that actually now it asks for the number of channels set to for stereo one for mono this doesn't really have anything to do with mixing channels uh for what I've seen we just use two for stereo and uh chunk size bytes per output sample now I don't know anything about this this is just kind of a magic number for me um I I know to type in 4096 and uh hopefully you'll trust me on that it seems to work pretty okay all right now that we have that initialized and we're kind of double checking our uh our documentation notice that it does actually ask us to close audio and that's kind of like the cleanup function like we've been using with sdl quit and sdl destroy window and that sort of thing so before we forget let's go ahead and add that down at the bottom here mix close audio and don't forget your semicolon okay now we'll get into actually loading these sounds um the sounds I I'm hoping that yeah okay good now I'm sure somebody uses this function um mix underscore load wave is what will allow us to load a wave file and all of these sound bytes or sounds are stored in objects called mixed chunks now after our initialization function I'm gonna go ahead and create a new line and just say mix chunk for the type and this is going to be a pointer this will return obviously a pointer and uh I'll just call mine sound by default it can equal null because we're just going to override this but it's generally a good idea to keep uh all pointers to be a null pointer before you start to use them and then we'll go ahead and reset it sound can equal mix underscore the function we're looking at is remember load wave you'll see this in the documentation it loads a file for use as a sample sound it actually uses something in the back end and it can load uh wave files uh aug files and anything else you have to call uh mix open audio before this I know it says stl here but it did say over at the top that there may be some conflicts and confusions you should use a mix open audio rather than stl open audio okay now let's get back down to where the heck I just was hopefully I can find it okay here we go null is returned on errors and you can of course use the mix underscore get error if you're having some trouble with your program and your code okay so that loads the sound for us uh the file name that I'm going to use I do just have a small sound over here uh scratch dot wav wave and uh I think I just had those from lazy foods tutorials or whatever the case may be but yeah try and find a good wave sound online and you can use that as often as you'd like now um the way that we actually play this sound is with a whole new function and that's called mix play channel these channels or the functions allow us to use channels will really play anything that we would that we need to so here this playing or this this this kind of header here it allows us to look at the functions that are associated with this and play channel is exactly what we need we need it on one channel or the channel to play it on negative one will be the first free unreserved channel so that's what the argument that we'll use the chunk is the actual sound or simple to play and loops is whether or not we want to loop negative one is infinite loop one will play the sample twice for one loop so we'll just use it zero so it'll play just once okay now in that code mix underscore play channel negative one for the free channel sound for the mix chunk that we want to use in zero for no repeats awesome now because we have created this chunk because we have created this sound file remember with uh some of the surfaces that we use in uh in sdl we have to free them and that sort of thing uh we have to do the same with um the the chunks and the sound effects that we actually use so down at the bottom here where we're closing the audio for the um open and and initialized mix mixer in that library we actually have also free chunk and uh that is going to free the pointer or free the memory that we used to actually keep track of that sound it's obviously a bad idea to free a chunk that's still being played so and we can reset the pointer just to just go back to be no let's do that over here in our code and mix underscore free chunk sound and sound can equal no okay awesome i'm going to go ahead and compile this just to see how we're doing um g plus plus 10 a adding sounds we're going to use package config in our back ticks and you guys know the drill here remember we're going to be including image and we'll be including uh the mixer and remember to use sdl 2 right here okay compile this no errors thank you thank you thank you run it and i didn't hear it i wasn't able to hear the sound but maybe you were on your machine let's try it again i'm not getting it i'm not getting the sound okay uh after some reviewing now i see the problem i had set up the uh initial argument to the mix open audio function to be mix default frequency and that's exactly what it should be and i told you the wrong information that the second argument should be mixed default frequency but no it should actually be uh something else an entirely different constant it should be mix default uh format and uh this is the suggested default sign 16 bit samples in the host byte order and when we actually use open audio that's kind of what we should be passing in for the format the output sample format so rather than mix default frequency being the second argument as well as the first mix default frequency should be the first and mix default format should be the second sorry about that guys uh i hope you can forgive me on that one but anyway moving on now we should be able to play our sound um actually i'm just going to change this back to uh the sprites too because i don't want i don't want to see a whole lot of that happening around here all right let's go let's run this code compile it no errors now we can run this and oh i hear that sound i hear that scratching sound and i know that all right cool i'm i'm actually playing sounds and that's the function and that's how you can load and and play sound obviously you can look more at what this documentation can do for you but let's go ahead and move on the next thing that we're going to want to do or look at anyway is how we can mix music because i remember the description of the sdl underscore mixer library was able to play music as well and that's stored net and not mixed chunks because those are mixed chunks are just sound samples and sound bytes right no it's instead uh stored in mix music uh objects so let's look for a few more of these the way that we load one of those is by the mix underscore load mus or mus function and uh this just uses another string or a constant character array for the the file name and this can load wave midi aug mp3 any any file that you really kind of use a command to play and um this uses again a pointer so it's very similar to the mixed chunk and um we can kind of keep checking out more of these let's check it out mix music asterisk music by default that's a pointer so it can be null and we'll reset it to that new function we were looking at mix underscore load mus or mus the file name in my case is a beat dot wav or beat dot wave and again that's just something that i found online probably from like lazy food tutorials and um just a sample sound file to test out what we're doing now uh now we actually want to play this music and if we look at our c also we've got a few options here mix underscore play music that will probably do exactly what we need and uh exactly that does what we need um we pass in the mix music objects for the music file to play and whether or not we actually want to loop number of times to play through the music zero plays the music zero times so okay and uh negative one will play the music forever or as close as it can get to forever um this will return zero on success or negative one for errors let's go ahead and run this mix play music music and negative one cool now if i compile this if i run it oh i don't know if you can hear that but man there's plenty of noise going on on my computer okay we'll shut that up and i'll show you a few more functions that you can that you can use here um i notice you can see there are some also's or see also some documentation you can use fade in music to um fade in for a certain amount of time and that's kind of nifty uh i would recommend you just check these out and play with them in your code there are a few others like you can fade into a specific position in the music with this mix fade in music that's kind of nice um you can set the music position really just at any position uh it'll set the position of the currently playing music you should just really be checking out some of these functions see what they can do and um i know there are of course others halt music this will halt the playback of music this interrupts the music fader options and uh it'll always return zero it should just stop the music like you press the final stop button there is of course fade out music uh hook music finished i would check out some more of these playing music mix underscore playing music uh will kind of return uh an integer which is essentially a boolean value because it tells you whether or not music is actively playing it's kind of a yes or no question it will return zero if the music is not playing or one if it is playing like false for not and true for for yes so that's a good kind of thing to know there is of course pause music as to whether or not the music is paused um that's a little weird but there are functions to pause music that's right here and uh resume music and notice it doesn't take any arguments it's what's going to it's going to do these things to the currently playing music because only one piece of music can play at a time there are 16 channels for sounds but only one channel for music and uh rewind music will go back to the starts there are plenty of things to look at here and i won't demonstrate all of these for you but i want you to know that these functions exist and uh it's good to be able to use them so okay thank you for watching everybody hope you enjoyed this tutorial i know it took quite a while but we are looking at two different things here music and sound effects and a whole new freaking library that has its own documentation and nifty things to do and work with c++ and sdl with okay uh i think i'm going to end off here again i want to thank you for watching it'd be nice if you could like the video if you did like the video maybe leave me a comment let me know what you think maybe subscribe if you like what i do and you like the stuff that you see i'll see you in the next tutorial