Hello and welcome to another episode in the Godot Basics tutorial series. In this episode, we will be taking a look at the audio stream player class. Now, when it comes to 2D, there are two different types of audio stream players. There's the original, which is called Audio Stream Player, which plays audio non positionally. And there is audio stream player 2D and the audio stream player 2D plays audio that dampens with the distance from the screen center based on the position of your audio stream player 2D node.
One thing to keep in mind is that the audio stream player class inherits from the node class and the audio stream player 2D class inherits from node 2D. What this means is that your audio stream player 2D Node has transform properties and that would include position. However, one thing to keep in mind is that both the audio stream player class and the audio stream player to declasse have the life cycle scripts available to them. Now the audio stream player and the audio stream player to declasse come with similar properties and methods.
So let's go ahead and take a look at the ones you'll need in order to play your music. Let's start with the properties. First, you have the auto play property. And just like the name implies, when the auto play property is set to true, your music will play. When the audio stream player has been loaded onto the scene tree. On top of that, you also have a bus property which lets you know where you'd like your music or excuse me, sounds to be connected to. The third property is the stream property, which is just basically an audio stream data type, which is just your music file.
The fourth property is Stream paused and just like the name implies, it will pause the playing of your audio. And lastly, you do have the property called Volume DB, which is a float data type. And all that does is it increases or decreases the sound output of your audio. Now, out of all these properties, the most important property to understand will be the stream property, which again is basically your audio file. So let's go ahead and take a look at that. So the audio stream class is the base class for audio streams, and it only comes with one method, no properties.
And that method is called get link. And all that returns back to you is a float value, which will be the total length of your audio file in seconds. The audio stream class does have children five to be exact, and those are the audio streams. Sample class, which handles WHV files the audio stream Ogie Voorhis Class, which handles OGF files, which is similar to MPLX three. So basically this audio file type is compressed. The third child class is audio stream generator, which allows you to manually generate your own sounds.
The fourth class is audio stream random pitch, which changes the pitch of your music randomly on every play through. And lastly is the audio stream microphone class, which is just for recording if your computer or device supports microphones. Now moving back into the audio stream player class, we do have five methods and that is the get playback position, which is where you are in your music file. Currently you have the jet stream playback, which returns back in audio stream playback data type.
You have the play method, which takes in one optional argument, which just means that you're going to play your audio file at the position you pass in and if you do not pass in a position, then you'll just start at the beginning. The fourth method is called Seke and it takes in one argument, which is that it will set its playback position to the argument you pass in. The difference between the play method and the Sikh method is that the play method on top of starting on the position you pass in to the argument, you will go ahead and play that music for your user, whereas the C# method will just move the playback position to whatever the argument value is and it will not play the music to the user.
And the last method is the stop method, which just stops your audio file from playing. And on top of that, the audio stream player comes with one signal called Finished. When your song or audio file finishes playing, it will emit the finished signal. We will not go over signals in this episode. Now, let's go ahead and take a look at some code. Over here in the same tree tub, you'll notice we have a root node called No Tudy and the child node called audio stream player. And notice it's missing something, it's missing the animation player.
And that's because we don't need the animation player to play music.
We can actually skip the animation player class and go directly into the audio stream player class in order to play our music with and without code. So let's go ahead and take a look at how to play music through code first. So over here, I have a script attached to it called Audio Script. That G.T. notice here that it extends from audio stream player. We have the ready virtual method and we declare a variable called audio stream. The data type is audio stream.
And we're going to load our simple melody wav file inside our variable called audio stream. We're going to take this variable and we're going to pass it in to our stream property using the set stream setter method. And notice we use the self key word because we are extending from audio stream players. So we have access to the methods and properties of the audio stream player class that we saw in the slide shows. Now, on top of setting our stream, now that we have a music file to play, we can set the volume.
I'm going to set it to three decibels. So hopefully not too loud, but loud enough to hear the music play. Last but not least, we are going to use the play method we take in no arguments. So that means we're going to start at the beginning and we're going to play our audio file. Let me go ahead and add the self keyword. So as you can see, using the audio stream player is fairly straightforward, but we can do more. We won't go over in this episode. But I just want to show you regardless.
So in this case, we have a variable called Audio Stream and it's a data type audio stream. And we are preloading our simple melody that the wav file well, we know we are preloading a wav file. So instead of using audio stream, we can use the child class called audio stream sample. Now, when we inherit from the audio stream sample, not only do we have that one function called get link from the audio stream class, we also get functions and properties available to us in the audio stream sample that includes things like looping, where to start, where to end the loop mode and so forth.
And so, as you can see here through code, we can do quite a lot of things with our audio file depending on the audio file we are loading. Now, if I go ahead and press play, you're going to hear the music.
And that's basically it when it comes to using code to play music, using the audio stream player note.
Now let me go ahead and detach the script and let's go ahead and see how we can use the audio stream player known to play music without code. To elaborate, the audio stream player comes with a few properties, the most important property being the stream property, which takes in an audio stream data tape. If we go ahead and click the drop down menu and you're going to see the five choices, the two that deals with music will be audio stream, sample and audio stream.
Oggi Morbus, because the music is a wav file, it will be the audio stream sample we need to use. So if I go ahead and click it, you're going to see it pops up, different options, different formats, looping options. If we wanted in stereo, we can go ahead and click it to replace it and we'll see that it automatically updates to fit our music. As you can see here, the eight bit changed to 16 or mix rate change to forty thousand. And this is basically all the options we have when dealing with the audio stream sample class.
Let me go ahead and exit out. The audio stream player comes with a few other properties, the two properties you may find useful if wanting to play music without code will be out to play. If you set this to true and you press the play button for the scene that this audio stream player note is attached to, when the scene loads, you're going to play the music over. The coolest thing is the playing property. By default, it set to folks. You have to use the play method to set this to true. However, without actually playing the game.
If you go ahead and check mark this button, the music will play.
And that's pretty cool, it allows us to debug our music, or rather it gives us one way of quickly debugging our music without actually running our game. Another property you may find useful, especially if you are well versed in audio engineering, is the best property and by default, we are set to master. However, we are able to edit and customize our audio buses, but we won't look into that in this episode. We will cover that in the next episode.
Now, moving back to the stream property, I'm going to go ahead and clear this now. New audio stream Ogie Morbus requires that you have a dog file. The new audio stream generator requires you to basically use code to create your own music. The microphone requires you to have a microphone. The one I do want to go over with you is the random pitch or excuse me, the audio stream, random pitch class. Now, notice here at our stream, data type property is the audio stream random pitch class, and it offers us two different properties, audio stream and random pitch.
And so I want you to imagine you have a game like Doki Doki Literature Club and you have this happy, upbeat music like my simple melody. But at some point you may want to change the pitch well, by using the random pitch audio stream class or excuse me, the audio stream, random pitch class, all you have to do is drag and drop your audio stream into your audio stream property. Now let's discuss about random pitch real quick. The lower the number, the closer it is to something like the original file.
The larger the number, the more extreme it sounds when it plays. I'm just going to leave it to the default value one point one, and it's going to give it an eerie sound, sort of like Doki Doki Literature Club. And so when I go ahead and play this audio stream player note, it's going to sound off.
And there it is, you can hear that it does not sound like the original melody sounds kind of eerie, perhaps you can find a use for the audio stream, random pitch class for your future games.
Well, that's all I have for you in this episode. Thank you so much for joining me. Thank you for clicking the like button. And thank you for clicking the subscribe button. I'm going to go ahead and upload everything we went through onto GitHub, so please feel free to download the GitHub project and play around with the code. Anyway, I look forward to seeing you in the next episode. Have an amazing day!