 Okay, welcome to space invaders part 13 again. This is an update or actually I guess additional you know code to Accompany my space invaders tutorial. So this is being written five years after the original tutorial was posted So in this video, I want to talk about class cross-platform sound and background music now if I run the program We've already got sound So I can go got that I got a little laser sound I got an explosion sound well if I get something I have an explosion sound So let's take a look at that code real quick. So if I come down to somewhere So for example, okay, so when the I fire the bullet I'm using the OS module and What this does is it actually, you know sends a message to the command line and Since I'm on a Mac here, so it's my school computer or my work computer If I want to put it I'm using a program called a F play and then I'm using a Audio file called laser.wave and the ampersand is what lets it play Asynchronously if I didn't have that it would stop the entire program Play the sound and then when the sound was done then the program would continue But the ampersand lets the the program run while the sound is playing in the background Okay, so this will only work on Mac now usually I use Linux my computer is broken so on Linux it's a play and of course Windows got has to do its own thing There's a whole module on Windows just for this So I'm going to show you how to Make a code that will play sound on all three operating systems. So here we go so the first thing I need to do is I'm going to be using a module called platform. So I'm going to import platform now It should be available on your system by default to test that just go ahead and run your run your program and If it runs good, there's no error message now your editor Especially using like pie charmer something like that might say, you know, you've imported something but haven't used it Just ignore that we're going to use it in a second So here we go. So the first thing I have to do is Determine the platform. So if on Windows You need to Import win sound And as I like to put or better yet Just use Linux Sorry, I can't resist So here's how we do it. So if platform parentheses equals equals two equal signs windows So if you're on Windows You need to use the import the win sound module Okay, so what I'm going to do is I'm going to use something called try and accept I'll explain in a second. So try this if you're on Windows try this import win sound Okay, and what'll happen is it will try that it will the command import win sound will go through Now if your computer for whatever reason doesn't have win sound on it You're gonna get an error and the program will stop. But if you use try and accept Print Win sound module not available Available, you know Okay, so basically what this is gonna do It's gonna check your platform if it happens to be windows it will import It will try importing the win sound module now if it imports it you won't see anything It'll just keep going If it doesn't work, it'll print out an error message to the terminal win sound module not available So you know that you have to go ahead and install that on your system now again I don't use windows. So unfortunately, I can't really test this for you But this should work. Okay, so that's part one We've imported that module now. We got to actually go ahead and use it So what we do is I'm gonna scroll down and I could I could have put this anywhere But I have all my other methods here. So I'm just gonna add this one Here and as long as I put it before I play some sounds will be fine So what I've done is I've created a new method called play sound Okay, and it takes two arguments sound file Okay, so that's the file that you want to play And time and I'll explain that in a minute and the way that's set up So this tutorial is a little bit more advanced than the original tutorial series. There's some stuff in here You may not have seen before But that's how we learn so I Assume at this point that you're familiar with passing parameters and values and things to functions. I'm however what we have here is a The ability to I'm not sure what's called, but you can set a default value So I have an option of sending time or not sending time And I'll explain what time is used for in a minute so Windows for our windows peeps windows. So same thing if platform dot system equals Windows now again, I would have already imported the The module now if the module doesn't exist this this isn't going to work You have to have the module for it to work on Windows. Okay, so We use the win sound Dot play sound notice the capitalization small w big p big s the sound file and Then you need to put it type win sound dot snd a Async okay, and what that does that has the same effect as using the ampersand on Linux and Mac So That is the command that will play a sound in the background on Windows Now the biggest problem I found from Windows users and again, it's that's something I can personally confirm because I don't use Windows I'll price it a hundred times, but I don't use Windows But welcome Windows users the sound file Usually in this case what I've been doing is like something like laser wave and it's in the same folder But for some reason on a lot of the Windows machines, you actually have to put the whole path Users blah blah blah and then would be like laser wave So it will only work in that particular folder. There's probably a way around that Using some of the OS module, but I'm gonna keep this a little bit simple So next one up is my good friend Linux again, I prefer Linux personally, but again, it's it's not working Oops, don't forget the parentheses there system parentheses equals quote Linux and Some people prefer to do Linux. That's fine So in this one, we're gonna be using the OS module. So last that system This is similar to what we did here on the Mac a play and put dash q And what that does is it tells the module or tells a play to be quiet Would not to like display a bunch of messages because it has a tendency to show a bunch of extra information You don't need parentheses and I talked about these parentheses. I think in the Video 11 and how that works The ampersand because we want to be asynchronous quote dot format And in this case sound file Okay, so We're just using the same thing. We're using the a play Program that's already included should already be included on your Linux system. We're using the q flag to keep it from displaying a bunch of messages And then whatever our sound file is we'll get put here That's what the format command does and then again, we want it to play asynchronously Actually, I should have put this L if It's not a matter, but it'll make it look better and then the last one is Mac So if we don't have those systems, let's go ahead and assume we have Mac I could yeah, that's probably just way to do it and I'm gonna say OS dot system And I'm back as a F play. I don't need the q and same thing ampersand Format sound Okay, so and I'll get to this in a minute. So let's go ahead and test it. So I'm gonna go down to Where I've got OS system AF play explosion and I'm gonna change that to play sound and I don't need the ampersand because I'm already adding it up above and Then there's a second place here. It play play sound Explosion Now don't accidentally leave the AF play in there because you'll get an error like I did also make sure there's no space In your file names like if you had like explosion sound, it's not gonna work So either put an underscore or Just leave no space Yeah, there can't be a space in there. Sorry. That's a little gotcha. I had some trouble with that earlier Play sound. I think there's one more place Where's explosion anybody see it? I'm just missing it. Oh explosion explosion. Sorry. I did explosion explosion and laser Ah, there is up here is the laser. That's what that was the third one and then play sound Laser and again, we don't need the ampersand because in the method we're adding it automatically Okay, so I'm gonna head and run it and make sure that works lovely Module object is not callable. Okay, so I made a mistake up here. It's Platform dot system. Okay, so sorry about that Yeah, again, I yeah, it's stupid me. So sorry about that, but you know, you know what? I'm gonna undo I'm gonna undo that and run it again I constantly get a request for help and I'm happy to help But you know what read the error message It tells you exactly where the problem is pretty much Line 12 says module object is not callable platform equals even if you don't understand what module object is not callable means You can go back to the video rewind a little bit and look and see. Oh my god. I forgot to put system in there, okay Yeah, you gotta you gotta learn to debug long story short. So let's run that. Okay. No error messages now Let's see if the sounds are playing Okay, so Looks good to me All right, I'm pretty happy with that now the last thing I want to show you Because this is something I get a lot of requests for is background sound So some background music now the diff well, I mean I was I say like basically you're using the same method You're still gonna be playing a sound but the challenge is to get it to repeat You know every time that you know the sound has to keep going in the background And that's why in this play sound method. I've had Where's that time? Okay So watch what I do here So basically the default is zero. So if you notice I Put play sound laser wave. I didn't put comma zero. I put nothing. Okay, so because there's nothing there Time defaults to zero So Repeat Okay, so if time is greater than zero So if I've specified a repeating time now, this is time in seconds I'm gonna be using the turtle modules on timer method and Watch what I do here. I Use something called a lambda and this is It's called like an inline function. I'm not a hundred percent sure why you got to do this, but you just do so play sound Sound file time comma T equals int time times 1 0 0 Now Let me explain that So this part Okay, this time is in seconds So I'm sending the number of seconds That this sound file is going to play so I have to I have to figure that out before I run it Before I put this the line in here and then this actually determines When it will be repeated. I think I missed a parenthesis. Yeah So this T Is in milliseconds, so the timer And so let's let's say it's 10 seconds in 10,000 milliseconds. We'll call this method again So call this method again In time times 1,000 milliseconds, so it's 10 seconds This function will call again Okay. Now this time Here we're using seconds. So just to make it easier. I thought for human beings So this will repeat the sound I don't know if that was the greatest explanation, but that's the gist of it So what we have to do is we have to a get some background music now I've already got some background music and I found it here on orangefreesounds.com Cinematic I found I chose a cinematic electronic check and it is by someone called Alexander and it's actually very recent it was this week or last week rather and From Alexander blue, so thank you very much to Alexander and he has made this available for non-commercial use and I hope this qualifies and so attribution non-commercial for oh Not sure the international part is but so here is the attribution for Alexander I will put this into the video description as well. So very thank very thankful to him Now I've downloaded the file in this case. It's an mp3 now your system You might have some trouble playing mp3s in which case you could convert it to a wave file But it seems to be working fine on my computer. So I'm gonna go with it So the important thing here is that it is one minute and 59 seconds So that's a hundred and nineteen seconds total. So remember that. Let's go back to our code So just before the main loop starts, I'm gonna say play background music Okay, and sound I'm sorry play sound And I've already saved this as BGM dot mp3 I changed the name again You can't have any spaces in here the original file name was much longer and had a bunch of spaces So I always rename it to something shorter that I can remember and as I said, it's a hundred and nineteen seconds long Okay, so I'm gonna go ahead and run that and Okay, so you can hear the background music And I'm gonna play this for two minutes Until 753 a.m. I'm a morning person and see if it restarts This is really cool music Now again if you're having trouble like if the background music is a little too loud for your program You might want to open it up in an editor and reduce the volume. You can use audacity for that It's pretty straightforward Again, this is a fairly simple system actually the the Mac version of AF play does have a volume control But the other ones. I'm not sure if they do or not. So We're trying to keep this simple So I know this is gonna get a little boring, but I just do what I just make sure it works before I end the video and I could cut it out. I suppose but I like to just leave my videos pretty raw a it's easier and b You know, I think it's good for you to see the mistakes and to see the process It's it's sometimes not pretty Thanks, hey Okay, about 45 seconds repeat. I should have chosen something shorter. I know I Did actually test it with the explosion sound before I did this video That kind of gives me a couple minutes to do this 52 So it's 53 it should repeat I think or by 53. Oh, you can't see the clock. Sorry, but it's up here somewhere I Cut that off in the video okay, it's stopping and There we go restarting now. I could have now that I realized there's a little bit of quiet part at the end I might have made it 115 seconds So there's a little less, you know of a gap, but you know, you can kind of play around with that Okay, again, this is a very simple system now if you notice I stopped the program But the background sound is still playing This happens on Mac. It happens on Linux far to know I haven't tested on Windows. I don't really know. I'm sorry So I tried closing this doesn't work So in the terminal on Mac what you need to do is I kill all AF play And that kills the sound on Linux. It's kill all a play It's not gonna work here because we're not on Linux and a Windows. I'm sorry. I just don't know But it might just shut off because you're using the wind sound module If somebody have to let me know in the comments. So that is that So, you know, here's the code again First you need to import the wind sound module if you're on Windows Actually what you could do is you don't actually need to do that. Just say try import wind sound and Yeah, I can probably just leave it at that actually would probably do it And then but you still you do need the platform thing down here in This method. Okay, so remember the time is in seconds So how many seconds do you want the sound file to repeat if you don't use the time? It doesn't repeat It just plays at one time, which is usually what you want except for the background music again in Windows you're using the wind sound Module on Linux you're using the a play Program and on Mac you're using AF play. Okay, so just be sure to copy this very very carefully Again, if you're not using You know, if you're if you have no intention of using Linux or or Mac for example, you're just using Windows You know, you can shorten this up a little bit as well But yeah, this this part here is a little bit more complicated again. This timer is controlled by This so again, that's in milliseconds. So 10 seconds becomes 10,000 But this method uses seconds so it could be you know, you know, like I said in our case was 119 And then again the background music Don't put it in the loop because you don't want it to play a million times You just want to play one time and then repeat every X amount of seconds And again, same thing here. You might need the full path If you're on especially if you're on Windows Windows users seem to have a lot of problems with this users blah blah blah blah, you know, whatever the full path is On Windows tends to be necessary. It also depends on your editor. So there's a few there's a few gotchas on that But yeah, that's about it for this one. Yeah, hopefully you will get your sound working and You can share it with your friends who are on Windows and share with your friends who are on Mac Share with your friends who are on Linux and then we can all just get along. Have a good one. Take care