Loading Sounds
Playing a Sound
Looping a Sound
Stopping a Sound
Loading Music
Playing Music
Stopping Music
Music and Timing

Loading Sounds
Sounds can be placed into a TNT Basic program by adding them to the resources as a Sound ('snd ') resource.  They cannot be loaded directly though.  In order to load sounds, a Sound Bank ('SnBk') resource needs to be created.

Once the resource contains one of these then it can be loaded simply be calling the function Load Sounds.

Load Sounds 128

This loads all the sounds in bank 128 into memory ready to be played.

Playing a Sound
Once a sound bank has been loaded, any sound contained in it can be played.

int channel=Play Sound (0)

This plays the first sound that was in the sound bank.  The number after the Play Sound statement is the index into the sound bank of the sound to be played.

Play Sound plays the sound at default volume with equal balance through each speaker.  To create additional effects (e.g. volume control, stereo panning) extra parameters can be passed.

int channel=Play Sound (0,100,50,25)

The first number is the index into the bank of the sound to be played.

The second number is the 'balance' of the sound, this means how much of the sound comes from each speaker.  A value of 100 means the sound comes entirely from the right speaker and a value of 0 means it comes entirely from the left speaker.  Any value in between these means the sound will be balanced accordingly between the speakers.

The third number is the volume the sound should be played at.  This can be any value between 255 (as loud as possible) and 0 (silent).

The fourth number is the 'priority' of the sound.  TNT Basic can only play 4 sounds at any one time, if more than four sounds are currently being played TNT Basic uses a priority system to decide which 4 should be played.  The higher the priority, the more likely it is to be played because it will stop a lower priority sound so it can be heard.  Priority can be any value between 0 (lowest) and 65535 (highest).

Looping a Sound
There are some sound effects that you would like the user to hear continuously (e.g. rain, wind).  These can be done by setting a sound to loop (i.e. as soon as it finishes it starts again) using the command Loop Sound.

Loop Sound 0,100,50

The first number is the sound to be looped continuously.

The second number is the balance of the sound.  This works exactly the same as Play Sound.

The third number is the volume of the sound.  Again, this works exactly the same as Play Sound.

Looping sounds do not have a priority.  This is because they are playing continuously and cannot be interrupted, they are automatically set by TNT Basic to have the highest priority possible.

Stopping a Sound
Once a sound has been set looping it will loop continuously until the program eventually finishes.  Most likely, you will stop the sound from looping yourself at certain parts of the game. This can be done using the command Stop Sound.

Stop Sound 0

This command stops sound 0 immediately.

Loading Music
Music can be placed in a TNT Basic program by adding it into the resources as a Music Module ('MADI').  The music file can then be loaded into the program with the command Load Music.

Load Music 128

This command loads music resource 128 into memory ready to be played.

Playing Music
The currently loaded music can then be played easily with the command Play Music.

Play Music

Stopping Music
The music will loop continuously (once it has finished it starts again) until the program finishes.  In order to stop the music currently being played call the command Stop Music.

Stop Music

Music and Timing
TNT Basic has specialised commands for timing music with the program.

Getting the Music Position
The current position of the music can be obtained by using the command Music Position.

while Music Position<60
wend

Stop Music

This piece of code waits until the music has been playing for over 1 second and then stops it.  The number that Music Position returns is measured in 60ths of a second.  This means that for every second of music that is played the music position will increase by 60.

Setting the Music Position
Music does not necessarily have to be played from beginning to end either. The command Set Music Position changes the position the music is currently being played from.

Set Music Position 120

This sets the music position to be 2 seconds into the song.  When the music is played it will start from 2 seconds into the song.

Getting the Music Length
The full length of the music can be obtained using the command Music Length.

while Music Position<Music Length
wend

Stop Music

This piece of code waits until the music has reached the end and then stops it.


Table of contents