09 02


Delphi Graphics and Game Programming Exposed! with DirectX For versions 5.0-7.0:Sound and Music                       Search Tips   Advanced Search        Title Author Publisher ISBN    Please Select ----------- Artificial Intel Business & Mgmt Components Content Mgmt Certification Databases Enterprise Mgmt Fun/Games Groupware Hardware IBM Redbooks Intranet Dev Middleware Multimedia Networks OS Productivity Apps Programming Langs Security Soft Engineering UI Web Services Webmaster Y2K ----------- New Arrivals









Delphi Graphics and Game Programming Exposed with DirectX 7.0

by John Ayres

Wordware Publishing, Inc.

ISBN: 1556226373   Pub Date: 12/01/99














Search this book:
 



Previous Table of Contents Next Listing 9-1: Playing sound effects with PlaySound implementation uses MMSystem; var WaveFile: TMemoryStream; {$R *.DFM} {$R WaveRes.res} procedure TForm1.FormCreate(Sender: TObject); begin {create the image of a WAV file in memory} WaveFile := TMemoryStream.Create; WaveFile.LoadFromFile(‘Memory.wav’); end; procedure TForm1.FormDestroy(Sender: TObject); begin {delete the in memory WAV file} WaveFile.Free; end; procedure TForm1.btnPlayFileClick(Sender: TObject); begin {play a sound from a WAV file} PlaySound(‘File.wav’, 0, SND_FILENAME or SND_ASYNC); end; procedure TForm1.btnPlayResClick(Sender: TObject); begin {play a sound from a resource (see the WaveRes.rc file for the resource script)} PlaySound(‘WaveResource’, hInstance, SND_RESOURCE or SND_ASYNC); end; procedure TForm1.btnPlayMemClick(Sender: TObject); begin {play a sound from a WAV file image in memory} PlaySound(WaveFile.Memory, 0, SND_MEMORY or SND_ASYNC); end; procedure TForm1.btnPlayLoopClick(Sender: TObject); begin {play a continuously looping sound from a file} PlaySound(‘Loop.wav’, 0, SND_FILENAME or SND_ASYNC or SND_LOOP); btnPlayStop.Enabled := TRUE; end; procedure TForm1.btnPlayStopClick(Sender: TObject); begin {stop all sound output} PlaySound(nil, 0, SND_PURGE); btnPlayStop.Enabled := FALSE; end; As illustrated, it is relatively simple to play a sound file by simply passing the PlaySound function the name of the WAV file. Playing a sound from a resource is almost as simple. The WAV files must be compiled into a resource by using the BRCC32.exe resource compiler included with Delphi (see the WaveRes.rc file in the directory containing the above example on the companion CD). Once this is complete, the resource file is linked into the executable using the $R compiler directive. Then, it’s a simple matter of passing the PlaySound function the name of the WAV resource (as opposed to the name of a WAV file), the handle of the module containing the resources (useful for putting WAV resources into DLLs), and the SND_RESOURCE flag. Note: WAV files must be compiled using a resource type of “WAVE” in the resource script file before PlaySound can find them. Playing sounds from memory is easy enough, and improves the performance of your application as PlaySound does not have to reload the WAV file or resource each time it is called. By using the TMemoryStream object, a WAV file can easily be loaded into memory and passed to the PlaySound function via its Memory property. Together with the ability to continuously play a sound (by using the SND_LOOP flag) or stop all sound output (by using the SND_PURGE flag), the PlaySound function is relatively powerful. If your game or application will not require the ability to play multiple sounds simultaneously, the PlaySound function will probably provide all the functionality required. However, to optimize sound output performance, play a virtually unlimited number of sounds simultaneously, or produce special effects, DirectSound is the way to go. DirectSound DirectSound is that portion of DirectX that is responsible for the manipulation and playback of digitized sound (music is handled via other API functions described later in the chapter). While not nearly as simplistic as the PlaySound function, the methods in the DirectSound API provide the developer with plenty of audio playback control while not being nearly as difficult as writing your own mixing and manipulation code with low-level audio API calls. Features DirectSound offers many features above and beyond what is available through the Win32 API. As with all DirectX components, DirectSound gives the developer more control over sound output than the Windows API, while also providing an improvement in performance. DirectSound provides the following features of specific interest to game developers: •  Automatic use of hardware acceleration (when available) •  Automatic conversion of WAV formats •  Simultaneous output of a virtually unlimited number of sounds •  Special effects such as panning, volume control, frequency adjustment, and 3-D placement In the following sections we will discuss how the Delphi game developer can take advantage of each of these features to create rich sound output for applications or games. Functional Overview Similar to DirectDraw, DirectSound programming consists of creating a DirectSound object and several sound buffers. The DirectSound object itself represents the developer’s direct interface to the audio hardware. Through the IDirectSound interface, the application can query the audio hardware’s capabilities, create sound buffers, and control other aspects of the hardware itself. Of primary interest to DirectSound programmers is the secondary sound buffer object (or specifically the IDirectSoundBuffer object). A secondary sound buffer object represents a sound, or WAV file, in memory. It is a similar relationship as surfaces are to bitmaps in DirectDraw programming. This memory is typically located in system RAM. However, some sound cards contain hardware RAM for the purpose of accelerating sound output. DirectSound will automatically take advantage of this hardware memory by placing secondary sound buffers in the sound card RAM. All of this is transparent to the developer. The majority of time spent in programming DirectSound will be in creation and manipulation of these secondary sound buffers. When a secondary sound buffer is played, its sound information is mixed with other secondary sound buffers and placed in the primary buffer. The primary buffer represents the conglomeration of all the sounds that will be played simultaneously, and is constantly streamed out to the sound output hardware. As secondary buffers are mixed into the primary buffer, DirectSound automatically converts their format into that of the primary buffer. This makes it easy for the developer to use sounds in varying formats, such as 8- and 16-bit sound, or sounds recorded at different sampling rates. As this mixing occurs, any special effects are applied to the sound, such as panning, volume, or 3-D positioning. The number of buffers that DirectSound can mix is limited only by processing time. DirectSound provides a software mixer for combining system memory buffers and hardware buffers into the primary buffer. System memory buffers are always mixed in software, but if a hardware mixer is available, the mixed system buffers are streamed to the hardware mixer, along with the hardware buffers. This elaborate process is illustrated in Figure 9-1. Figure 9-1:  DirectSound functional overview Previous Table of Contents Next Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.

Wyszukiwarka

Podobne podstrony:
09 02
German Top 100 Single Charts 09 02 2015 (2015) Tracklista
Kody wybranych ofert od 2013 09 02
09 02 2010 analiza
trouble shooting guide TPA400 with multimeter 09 02 2004 ATM 000 001 0
Wycofanie wojsk USA z Iraku wstrzymane (09 02 2009)
TI 02 09 02 T pl(1)
09 02
WSM 02 09 pl(2)
od 02 07 09 do 10 07 09
02 10 09 (7)
technik dentystyczny22[09] z3 02 u

więcej podobnych podstron