Sound

Unlike image buffers, we do not have a primary sound buffer. In Sound class, we make one, if we need one.

In the sample below, we will try to load an external sound file into our sound buffer.

#include "console"
 
#define SND_STAT_PLAYING 1
 
Var:String sChoice;
Var:Number sndTest, nSndStat;
 
function main() {
  Screen:SetTitle("Sound Class Demo")
  writeLine("NOTE: Make sure your speakers are turned on.")
 
  writeLine("Press x to play sound...")
  readChr(sChoice)
 
  if (sChoice == "x") {
 
    Sound:Load("foss.wav",sndTest)
 
    Sound:Play(sndTest)
    Sound:GetStatus(sndTest, nSndStat)
 
    while (nSndStat == SND_STAT_PLAYING) {
 
      Sound:GetStatus(sndTest, nSndStat)
    }
 
    Sound:Free(sndTest)
 
  } else {
    write("...")
 
    Konsol:Delay(1000)
  }
 
  end()
}

Then, if the program allows us, we will try to play the sound buffer. Be sure to download the needed resources.

#include "console"

If you study the whole sample code, the first thing that you might have noticed is the #include “console”.

We need to include console, so we would be able to use the functions, writeLine, write, readChr and end.

console is a console-emulating header file that is bundled with KonsolScript Official distribution since 0.1.15.

#define SND_STAT_PLAYING 1

Next, we made a define for a value 1. You'll learn more about this later in this tutorial

Var:String sChoice;

This is to let the user make the choice if he (or she) wants to hear the sound play or not. (A matter of freedom, eh?)

Var:Number sndTest, nSndStat;

Then, we need a Number variable to be our sound buffer for the sound file.

And another Number variable to check for the current status of our sound buffer.

Screen:SetTitle("Sound Class Demo")

Let's try to make some branding and call this program, “Sound Class Demo”.

writeLine("NOTE: Make sure your speakers are turned on.")

Next, we simply just want to make a notice that the speakers should be turned on.

  writeLine("Press x to play sound...")
  readChr(sChoice)

Let's prompt the user what to do and then, let's get the value of whatever he (or she) pressed.

if (sChoice == "x") { /* scope... */ }

After taking the users response, we need to know if he (or she) wanted to play the sound by checking if x was pressed.

If in case the user wanted to play the sound, this is where we start trying out the Sound class – sweet!

Sound:Load("foss.wav",sndTest)

The code above is what creates the sound buffer. If “foss.wav” does exists, this will be loaded into sndTest.

Sound:Play(sndTest)

What are we waiting for? Let's play the loaded sound now!

Sound:GetStatus(sndTest, nSndStat)

We need to know if the sound did play. Let's get the current status of sndTest – the value will be handled by nSndStat.

while (nSndStat == SND_STAT_PLAYING) { /* scope... */}

The idea is to simply wait for the sound to stop playing. If in case, the value of nSndStat is 1, then it means that it is still playing. That is the reason why we defined 1 to SND_STAT_PLAYING – with this code it would be much more comprehensible.

Sound:GetStatus(sndTest, nSndStat)

Inside the scope of while, we want to repeatedly check if it is still playing.

Sound:Free(sndTest)

When the sound is done playing, let's not forget to free-up the used buffer handled by sndTest.

Others part of the code does not really matter – just some et cetera thing.

Go apply this tutorial to your mute game. :-D

Last Words

The Sound class can play a WAV and a MIDI** file, which are the common sound format used by other games.

Load all the sounds you want your game to play, just not forget to free if not used anymore, okay?

Sources

www.konsolscript.org
© 2005-2011 KonsolScript Labs | All Rights Reversed | Licensed under GNU GPL | Designed by Mj Mendoza IV
http://www.sourceforge.net