Playing Some Tunes CD Audio, MIDI and MP3 Music

Overview

Rock-a-bye baby in the tree top, when the wind blows the cradle will rock…

What image does this familiar song bring to mind? Can you imagine a nursery, with a mother cradling her baby as she gently rocks it to sleep? In that moment of peace and warmth, the cares of the world are washed away. Imagine quiet music playing from a mural over the crib, a soft white light shining from a lamp nearby. What memories or feelings does this scene arouse? Music affects our temperament, and it often affects our mood without our knowledge.

Do you recall the last suspenseful movie you watched? In such movies, right before the antagonist is about to strike, the scene is accompanied by suspenseful music, which affects your impression of what will happen in the scene (or at least gives you the suspicion that something is about to happen). Background music is used in many forms of entertainment to affect an audience's mood. This chapter will familiarize you with the features of DarkBASIC related to music. It is closely related to Chapter 13 because you can use both sound effects and music to affect mood and change the impression that one gets from a scene in video games and in motion pictures. Specifically, this chapter will show you how to load and play music files, including CD-ROM music tracks, MIDI files, and MP3 files.


Introduction to Background Music

Take your mind's eye back to one of your favorite TV shows. What sticks out the most about that show? Most people would tend to say that it was funny or it had a good story line, but rarely do you hear people mention the background music. The background music, however, is a key component to any form of entertainment. It definitely helps you visualize another place and time using a concept called suspension of disbelief, in which, for a short time, you are fully immersed in a game (or a TV show or a movie, as the case may be). If you are able to accomplish this esoteric manipulation of another person's mind using your own imagination, then you have succeeded in making a great game. A video game without background music is like a room without dcor: It is still a room, but it is not quite the same without the furnishings. Figure 14.1 illustrates the difference between a room that has no mood (so to speak) and a room that reflects a mood. Try to think of the background music for a game in terms of a room that is furnished compared to a room that is bare.

click to expand
Figure 14.1: A furnished room is to an empty room as a game with music is to a game without music.

The Benefits of Background Music

Background music can set a mood. Just like a window with light coming through it sets a lighter tone than a room with no windows, background music helps to set the feeling in a game. If an evil alien is chasing a player, the game music should probably be something fast-paced to get the heart pumping. Or, if the character falls in love with another character, the music should reflect a romantic mood.

Setting the Mood for the Game

How do you choose what mood you want to set for a game? This is simple, but hard at the same time. If your game were full of alien bad-guys that you are shooting out of the sky, your mood would be panic. If you were writing an adventure between two star-crossed lovers, your mood would be romantic. The best way to decide a mood is to look at the overall picture you are producing. After you've decided that, picking the music is pretty easy. Trust your tastes in music. If the music makes you feel happy, put it in a happy part of your game. If it makes you sad, put it in a sad part.


Loading Music Files

All this talk about background music has probably made you wonder, "Okay, how do I make this music?" DarkBASIC uses a file format called MIDI (Musical Instrument Direct Interface) to load background music. It is a collection of music notes stored in a file. MIDI is to a computer as sheet music is to a pianist. The normal extension of a MIDI file is .mid.

MIDIs are generally created using a combination of software and hardware. A good piece of professional software is a program like Cakewalk's Sonar. A good piece of professional hardware is a Yamaha synthesizer. Most people, however, cannot afford such high-grade stuff, so there are alternatives. You can find many low-cost software alternatives by searching the Internet for MIDI software. You can also find a good sound card and a cheap synthesizer to handle the hardware side. I use a Casio keyboard with the Sound Blaster Live audio card in my computer. Figure 14.2 shows a typical setup for recording MIDI files using a keyboard.

click to expand
Figure 14.2: A typical setup for creating MIDI files involves a PC and a MIDI keyboard.

You can also find MIDIs on the Internet. You don't have to generate the songs yourself to get good ones; just doing a search on the Internet can be fruitful.

  Tip

DarkBASIC Professional also supports the popular MP3 music format, which is a highly compressed digital audio format capable of reproducing near-CD quality at a fraction of the size of an equivalent WAV file (which is the file format normally generated by copying a CD track to your hard drive—also called ripping). The syntax and music playback commands are all precisely the same under DarkBASIC Pro. Just keep in mind that you can load an MP3 file rather than a MIDI file if you want!

MIDI and MP3

In the music world, there is always a fight between which format is better—MIDI or MP3. DarkBASIC only supports MIDI, but DarkBASIC Pro supports MIDI and MP3.

MIDIs are smaller than MP3s because they store no digital sound. A MIDI song is more like a piece of sheet music than a digital recording. Depending on your sound card, MIDIs can sound really good or really bad. In the old days (in the early-to mid-1990s), sound cards would synthesize (or generate) the instruments used in a MIDI. My Sound Blaster Live card makes MIDIs sound really good because it uses digitized instruments instead of synthesized ones. The sound card contains pre-recorded WAV files that represent each instrument.

MP3s are more popular today then MIDI files because they are a highly compressed digital sound format. MP3s with good compression can run near CD-quality music at about 1 megabyte per minute (44 kHz, 16 bit). You can find MP3s all over the Internet, in most offices, and even in some of your favorite games, such as Age of Empires 2. MP3 format achieves such small file sizes by removing any sound that the human ear cannot hear.

Why use one format instead of the other? If you are concerned about space in your game, a good way to save some room is to use a MIDI instead of an MP3. If you are using a good-quality sound card, a MIDI will be about 30 kilobytes for a three-minute song, whereas the same song in MP3 format will be about 3 MB. However, because a MIDI is more like sheet music, it might not be suitable for all situations. For example, any instrument that is not loaded into the sound card will not play. Therefore, if you need instruments not readily available via MIDI, your best bet is to use the MP3 format.

Loading a Music File

The LOAD MUSIC command loads a MIDI file (or an MP3 file if you are using DarkBASIC Pro) into a specific music slot in the memory. The song number must be an integer. You can load a maximum of 32 songs into memory at one time. The syntax for the LOAD MUSIC command is

LOAD MUSIC Filename, Music Number
  Note

You cannot have more than 32 songs loaded in memory at any one time. Keep this in mind when you are creating music tracks. Normally you will only use one song at a time, but DarkBASIC gives you the ability to load more than one track into memory.

Deleting a Previously Loaded Song

The DELETE MUSIC command removes a MIDI file from a specific music number. This is useful when you are trying to conserve memory because it frees up a music number. The format for this command is

DELETE MUSIC Music Number


Playing Background Music

Now you will attack the most important background music commands—the playback commands. They are pretty simple, yet very powerful. They give you control over the state of the background music.

Playing a Song

The PLAY MUSIC command starts a music number playing. This command always starts at the beginning of the music and plays to the end. The command format is

PLAY MUSIC Music Number

Here is an example of how to use the PLAY MUSIC command:

LOAD MUSIC "TitleMusic.mid", 20
PLAY MUSIC 20

Looping a Song

The LOOP MUSIC command starts a music number playing and looping. This command always starts at the beginning of the music, plays to the end, and loops back to the beginning. The command format is

LOOP MUSIC Music Number

Stopping the Playback

The STOP MUSIC command stops any music number that is playing or looping. After a music number is stopped, it will only play from the beginning again. The syntax for this command is

STOP MUSIC Music number

Pausing the Playback

The PAUSE MUSIC command pauses any music at the point at which it is playing. The command format is

PAUSE MUSIC Music Number

Following is a short snippet of code that demonstrates how to use the PAUSE MUSIC command.

LOAD MUSIC "TitleMusic.mid", 20
PLAY MUSIC 20
SLEEP 1000
PAUSE MUSIC 20

Resuming the Playback

The RESUME MUSIC command resumes playback of a song from the position where it was previously paused. The command format is

RESUME MUSIC Music Number

Here is an example of the RESUME MUSIC command:

LOAD MUSIC "TitleMusic.mid",20
PLAY MUSIC 20
SLEEP 1000
PAUSE MUSIC 20
SLEEP 1000
RESUME MUSIC 20


Music Playback Properties

Some of the most important commands in music playback are the music playback properties. These give you limited control over what music is playing and when. They are useful when you are trying to determine the status of the music.

Checking the Validity of a Song

The MUSIC EXIST command determines whether a song has been loaded into memory in the slot specified by the Music Number parameter. The command returns a 1 if the song exists or a 0 if it does not. The syntax for this command is

Value = MUSIC EXIST(Music Number)

Determining When a Song Is Playing

The MUSIC PLAYING command lets you know whether a music number is currently playing. Only one background music number can play at a time. This command returns 0 if the music number is not playing and 1 if it is. The syntax for this command is

Value = MUSIC PLAYING(Music Number)

Here is a snippet of code that demonstrates how to use the MUSIC PLAYING command:

LOAD MUSIC "TitleMusic.mid",20
PLAY MUSIC (20)
While MUSIC PLAYING(20) = 1
Endwhile STOP MUSIC (20)

Checking the Loop Flag

The MUSIC LOOPING command lets you know whether a music number is looping. This is different than checking whether a music number is playing because looping will play the number continuously. This command returns a 0 if the music number is not playing and a 1 if it is. The format for this command is

Value = MUSIC LOOPING(Music Number)

Checking the Paused Flag

The MUSIC PAUSED command lets you know whether the music number is paused. Pausing music can be a good dramatic effect. The command format is

Value = MUSIC PAUSED(Music Number)

Here is an example of how to use the MUSIC PAUSED command:

LOAD MUSIC "TitleMusic.mid",20
PLAY MUSIC 20
SLEEP 1000
PAUSE MUSIC 20
WHILE MUSIC PAUSED(20) = 1
 IF(MOUSEKEY() = 1) THEN RESUME MUSIC 20
ENDWHILE


Introduction to Digital CD Audio

Sometimes it is preferable to use a physical CD audio track for music in a game in order to play back music at the highest possible quality. Neither MIDI nor MP3 files provide the quality or range of sound possible with CD audio. Well, you are in luck! DarkBASIC supports playing CD tracks as background music. You could even use this feature to write your own CD audio library program. Who knows? The possible uses for features such as CD audio (not to mention MP3 playback) are endless.

Using CD Music Tracks

Using CD audio tracks is just like loading a MIDI file for playback. The music commands apply equally to CD music and MIDI or MP3 files. DarkBASIC is smart enough to remember the format of the music referenced and play it accordingly.

The LOAD CDMUSIC Command

The LOAD CDMUSIC command loads a selected track as your background music. You can only load and play one CD track at a time. If you want to change tracks, you have to delete the old track first, and then load the new track. The syntax for this command is

LOAD CDMUSIC Track Number, Music Number


Project The MusicJukebox Program

Now that you have been exposed to DarkBASIC's music-playing capabilities, it's time to put that knowledge into action! The next section of source code is a simple program called MusicJukebox. This program plays MIDI files, MP3 songs, or CD audio tracks. You can use the up and down arrow keys to scroll through the music selection. You can also use the mouse to select the music. Table 14.1 shows the commands that control the MusicJukebox program, and Figure 14.3 shows the output of the program.

Table 14.1: MusicJukebox Commands

Key

Description

Enter

Plays the highlighted song

Space

Stops the current playback

P

Pauses the music playback

R

Resumes the music playback

Esc

Ends the program

click to expand
Figure 14.3: The MusicJukebox program loads and plays songs that you choose.

The source code for the MusicJukebox program includes three support functions. The first function, LoadAllMusic, reads data into several arrays. The second function, DisplayTitles, refreshes the song list for the highlighted song. The third function, GetNewSongName, reads the song under the mouse so it can be highlighted.

REMSTART
-----------------------------
Beginner's Guide To Game Programming With DarkBASIC
Copyright (C)2002 Jonathan S. Harbour and Joshua R. Smith
Chapter 14 - Playing Some Tunes: CD Audio and MIDI Music
-----------------------------
REMEND
REM The files and Names of the songs.
DATA "songsSong1.mid","Battle Star Galactica"
DATA "songsSong2.mid","Star Trek TNG"
DATA "songsSong3.mid","Doctor Who"
DATA "songsSong4.mid","The Flintstones"
DATA "songsSong5.mid","Frosty the Snowman"
DATA "songsSong6.mid","God rest ye"
DATA "songsSong7.mid","O Holy Night"
DATA "songsSong8.mid","Theme from Jaws"
DATA "songsSong9.mid","Jingle Bells"
DATA "songsSong10.mid","Mission Impossible"
DATA "songsSong11.mid","Mister Ed"
DATA "songsSong12.mid","Outerlimits"
DATA "songsSong13.mid","Star Wars"
DATA "songsSong14.mid","Start Trek Voyager"
DATA "songsSong15.mid","What Child is This"
DATA "TitleMusic.mid","Good Song"
DATA "CDAUDIO","CD AUDIO"

REM Some global arrays for storing key information
DIM SongTitle$(17)
DIM FileName$(17)
DIM Selected(1)
DIM MaxSongs(1)


SYNC ON
SYNC RATE 30
MaxSongs(1) = 17
LoadAllMusic()

LOAD BITMAP "imagesackdrop.bmp"
SYNC
SYNC
Selected(1) = 1

DisplayTitles()
' Checking and looping with the ESC key (ASCII value 27)
WHILE INKEY$() <> CHR$(27)
 ' Checking the downkey
 IF DOWNKEY()=1
 WHILE DOWNKEY()=1
 ENDWHILE
 Selected(1) = Selected(1) + 1
 IF Selected(1) > MaxSongs(1) THEN Selected(1) = 1
 DisplayTitles()
ENDIF
' Checking the up key
IF UPKEY()=1
 WHILE UPKEY()=1
 ENDWHILE
 Selected(1) = Selected(1) - 1
 IF Selected(1) < 1 THEN Selected(1) = MaxSongs(1)
 DisplayTitles()
ENDIF
' Checking the Space Bar (ASCII value 32)
IF INKEY$() = CHR$(32)
 IF MUSIC EXIST(1)
 STOP MUSIC (1)
 ENDIF
ENDIF
' Checking the P key
IF INKEY$() = "P" OR INKEY$()="p"
 IF MUSIC EXIST(1) = 1 THEN PAUSE MUSIC 1
ENDIF
' Checking the R key
IF INKEY$() = "R" OR INKEY$()="r"
 WHILE INKEY$() = "R" OR inkey$()="r"
 ENDWHILE
 RESUME MUSIC 1
ENDIF
' Checking the Enter KEY (ASCII value 13)
IF INKEY$() = CHR$(13)
 selector = Selected(1)
 IF FileName$(selector) = "CDAUDIO"
 SYNC
 SYNC
 IF MUSIC EXIST(1)
 IF MUSIC PLAYING(1) THEN STOP MUSIC 1
 DELETE MUSIC 1
 ENDIF
 LOAD CDMUSIC 1,1
 IF MUSIC EXIST(1) THEN PLAY MUSIC 1
 ELSE
 IF MUSIC EXIST(1)
 IF MUSIC PLAYING(1) THEN STOP MUSIC 1
 DELETE MUSIC 1
 ENDIF
 LOAD MUSIC FileName$(selector),1
 PLAY MUSIC 1
 ENDIF
 ENDIF
 'checking to see if the mouse was clicked over a song
 IF MOUSECLICK()=1
 WHILE MOUSECLICK()=1
 SYNC
 ENDWHILE
 newsong = GetNewSongName()
 IF newsong <> 0
 Selected(1) = newsong
 DisplayTitles()
 ENDIF
 ENDIF
 SYNC
ENDWHILE
IF MUSIC EXIST(1)=1
 STOP MUSIC 1
ENDIF
END

' Sets all the music variables
FUNCTION LoadAllMusic()
 FOR Count = 1 TO MaxSongs(1)
 READ Midfile$
 FileName$(Count) = Midfile$
 READ Songname$
 SongTitle$(Count) = Songname$
 NEXT count
ENDFUNCTION

' Display all the music titles
FUNCTION DisplayTitles
 White = RGB(255,255,255)
 Black = RGB(0,0,0)
 Blue = RGB(0,0,255)
 Yellow = RGB(255,255,255)
 SET TEXT OPAQUE
 FOR Count = 1 TO MaxSongs(1)
 IF Count = selected(1)
 INK Yellow,Blue
 TEXT 425,75+((Count-1)*16),SongTitle$(Count)
 ELSE
 INK White,Black
 TEXT 425,75+((Count-1)*16),SongTitle$(Count)
 ENDIF
 NEXT Count
ENDFUNCTION

' checks the mouse cursor to see if it is over a new song.
FUNCTION GetNewSongName()
 xpos = MOUSEX();
 ypos = MOUSEY();
 IF xpos <425 THEN EXITFUNCTION 0
 IF ypos <75 THEN EXITFUNCTION 0
 IF xpos > 611 THEN EXITFUNCTION 0
 IF ypos > 75+MaxSongs(1)*16 THEN EXITFUNCTION 0
 Flag = 0
 FOR Count = 1 TO MaxSongs(1)
 ymin = 75+((Count-1)*16);
 Ymax = 75+((Count)*16);
 IF Ypos > Ymin AND Ypos < Ymax THEN Flag = Count
 NEXT Count
ENDFUNCTION Flag


Summary

In this chapter, you learned the mechanics of playing music in DarkBASIC. DarkBASIC has a thorough selection of commands for loading and playing MIDI files, MP3 files, and CD audio tracks. You learned the importance of setting the mood in a game, as well as how music and sound effects greatly enhance (or detract from) the impression that a game has on players. Enjoy the new power of music you have found within DarkBASIC!


Quiz

The chapter quiz will help reinforce the material you have learned in this chapter, and will provide you with feedback on how well you have learned the subjects that were covered. For the answers to the quiz, refer to Appendix A, "Answers to the Chapter Quizzes."

1.

Which command loads a MIDI file?

  1. LOAD MUSIC
  2. LOAD CDMUSIC
  3. LOAD SOUND
  4. LOAD 3DSOUND

a

2.

Which command stops music from playing?

  1. HALT MUSIC
  2. STOP MUSIC
  3. STOP SOUND
  4. PLEASE MUSIC STOP

b

3.

How many CD tracks can play at one time?

  1. Six
  2. Three
  3. As many as you want
  4. One

d

4.

What does the MUSIC PAUSE command do?

  1. Pauses a MIDI file
  2. Stops a MIDI file
  3. Resumes a MIDI file
  4. Prints a picture of a bear holding a music note

a

5.

How many MIDI files can you store at one time?

  1. 15
  2. 37
  3. 46
  4. 32

b

6.

RESUME MUSIC will continue a song where it was paused.

  1. True
  2. False

a

7.

Which of the following commands will not make music play through the speakers?

  1. PLAY MUSIC
  2. LOOP MUSIC
  3. RESUME MUSIC
  4. PAUSE MUSIC

d

8.

What must you do before you can play a new CD track?

  1. Use the DELETE MUSIC command to delete the old track
  2. Eject the CD from the drive and re-insert it
  3. Nothing
  4. Both A and B

a

9.

Which command will tell you whether a song is looping?

  1. MUSIC PLAYING
  2. MUSIC LOOPING
  3. MUSIC PAUSED
  4. MUSIC EXIST

b

10.

What does the DELETE MUSIC command do?

  1. Deletes the music buffer from memory
  2. Deletes the program you are writing
  3. Hides the mouse
  4. Reveals the mouse

a

Answers

1.

A

2.

B

3.

D

4.

A

5.

B

6.

A

7.

D

8.

A

9.

B

10.

A




Beginner's Guide to DarkBASIC Game Programming
Beginners Guide to DarkBASIC Game Programming (Premier Press Game Development)
ISBN: 1592000096
EAN: 2147483647
Year: 2002
Pages: 203

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net