[ Pobierz całość w formacie PDF ]
.The previous chapters covered image mani-pulation under Windows.This chapter describes how to play musical notesusing a number of Windows API functions.Sound under WindowsCompared to the Apple Macintosh, the sound generation capabilities of theIBM-compatible PCs are rather limited.Essentially, all you can do with the PC sspeaker is play single notes you cannot even vary the volume (loudness).One way to improve the sound output under Windows (and DOS) is to in-stall a sound card that can synthesize a wide range of sounds.Some of thepopular sound cards are Sound Blaster, Media Vision, and Microsoft WindowsSound System.These cards convert the analog (continuously varying) soundwaves into 8-bit or 16-bit numbers, sampling the wave at rates from 4 to 44KHz(22,000 times a second).Higher sampling rates and higher number of bits(16-bit) provide better quality, but you need more disk space to store high-quality sound.Programming for SoundLike any other device, the sound cards are controlled through drivers.Thesound driver provides a standard programming interface for all sound boards.If you look at the SYSTEM.INI file in your system s Windows directory, you mightfind this line:sound.drv=sound.drvThis tells Microsoft Windows that the sound output is to be performed throughthe driver named sound.drv, which is the default driver for the PC s built-inspeaker.The right side of the line is different if you have a sound card installedin your system it is the name of the driver that Windows uses to control thatsound card.190ChapterGenerating Sound6Once the sound driver is installed, you can use a small set of Windows func-tions to generate sound.Windows 3.1 includes another simpler API, called themultimedia API, for sound cards and other multimedia devices such as the CD-ROM drive and video output device.The multimedia API relies on a dynamiclink library (DLL) MMSYSTEM, which provides a high-level set of commandscalled Media Control Interface (MCI).As a programmer, you can control a mul-timedia device by sending commands using the mciSendCommand function.Seethe Further Reading section at the end of this chapter for sources of infor-mation on the MCI functions.This chapter covers sound generation under Windows with the PC s built-in speaker or a sound board with a driver that responds to the Windows soundfunctions.Although Microsoft recommends that programmers use the MCIfunctions for controlling sound devices, using the old API functions is the onlyway to guarantee that sound output will work in all PCs whether they havea sound card or they rely on the PC s built-in speaker.Windows Sound ModelWhenever a device is controlled through a driver, the driver presents an ab-stract model of the device to the programmer.The Windows sound driversmodel each sound as a voice.You can think of each voice as a queue with anumber of notes that are to be played in sequence (see Figure 6.1).At any in-stant of time, the sound card plays notes from all the voices simultaneously.The PC s speaker can handle only one voice, but most sound cards can handlefrom 8 to 16 voices.Voice 1Voice 2Queues of notesVoice 3"""Figure 6.1.Voices in Windows.191Programming Windows Games with Borland C++To generate sound, you have to follow these steps:1.Open the sound driver by calling OpenSound.If the sound device is notbeing used by another application, OpenSound returns the number ofvoices available.Otherwise, it returns a negative number.Thus, youstart the sound generation code withif(OpenSound() > 0){// Sound driver successfully opened.OK to proceed}2.Call SetVoiceQueueSize to set the size of queues for each voice.Youhave to specify the size in number of bytes.Each note requires 6 bytesof memory.To make room for n notes in the first voice s queue, useSetVoiceQueueSize(1, 6*n);3.Call SetVoiceAccent to set the tempo, volume, and mode (legato,normal, or staccato) of the notes in a voice.4.Call SetVoiceNote to add notes to a voice queue.For each note, you canspecify the following characteristics:The note number (between 1 and 84)Duration of the note (1 for a whole note, 2 for a half-note, 4 for aquarter-note, and so on)The number of quarter-note durations to add to the note sduration5.Call StartSound to begin the sound output.6.You can proceed with other programming tasks while the sound plays.At any time, you can call CountVoiceNotes to check how many notesremain in a voice s queue.Once the queue is empty, you should callStopSound followed by CloseSound to release the sound device for useby other processes.C++ Structures for Sound under WindowsFor use in C++ programs, it makes sense to define a number of object types tohelp with sound output.Listing 6.1 shows the header file sounds.h that defines192ChapterGenerating Sound6the structures Note and Music
[ Pobierz całość w formacie PDF ]