70 likes | 174 Vues
GAM666 – Introduction To Game Programming. DirectX Audio. DirectX Audio, first appearing in DirectX 8, is the union of DirectSound – low level control of the audio hardware DirectMusic – high level management of sounds
E N D
GAM666 – Introduction To Game Programming DirectX Audio DirectX Audio, first appearing in DirectX 8, is the union of • DirectSound – low level control of the audio hardware • DirectMusic – high level management of sounds Sample6 uses DirectMusic for almost everything, except to change the pitch of the “engine” noise, which DirectMusic doesn't let us control Sample7 adds 3D sounds, which also require DirectSound control
GAM666 – Introduction To Game Programming Digital Audio Basics • Digital audio deals with digital samples of sound waves. Sound cards contain Digital/Analog Converters (DACs) to sample and playback sounds (which can be stereo or mono) • Sample rate is the frequency at which samples are taken, and should be twice the highest frequency you want to hear (22kHz is common, but 44kHz is better, since good ears can hear to 20kHz) • Sample size is the number of bits reserved for each sample (16 bits is common, but 24 or 32 is better giving greater dynamic range)
GAM666 – Introduction To Game Programming DirectX Audio Unlike Direct3D, which provides dedicated functions for creating the necessary COM object, DirectX Audio uses standard COM methods. This means: • Calling CoInitialize() once at the beginning, before any COM objects are instantiated • Calling CoUninitialize() at the end of the program after all the COM objects are Released • Using CoCreateInstance() to create many of the COM objects, specifying both the Class ID and Interface ID for the object
GAM666 – Introduction To Game Programming DirectX Audio DirectMusic objects: • IDirectMusicPerformance8 – directs the entire combination of sounds that will play • IDirectMusicAudioPath8 – directs a set of sounds that you want to control together • IDirectMusicSegment8 – represents a specific sound effect to be played on one of the audio paths of the performance • IDirectMusicLoader8 – used to load a sound effect into a segment
GAM666 – Introduction To Game Programming DirectX Audio • You can play a sound effect by using the Performance function PlaySegment() or PlaySegmentEx(), controlling that sound by manipulating the Audiopath for that segment • If the Audiopath doesn't give you enough control, you may need to get its low level sound buffer (using the Audiopath GetObjectInPath() function), which is an instance of the DirectSound interface: IDirectSoundBuffer8 • Sample6 uses the sound buffer to change the playback frequency, effectively changing pitch
GAM666 – Introduction To Game Programming DirectX Audio • Before playing a sound effect, it must be sent to the sound card, using the Segment function Download() - once downloaded, it can be played over and over again • The Performance functions Stop() and StopEx() are used to stop playback of one or all playing segments • All segments must be stopped before Releasing the COM objects, which are sensitive to the order of destruction
GAM666 – Introduction To Game Programming 3D Audio DirectX Audio is capable of managing sounds in 3D space, and even compensating for such effects as the Doppler effect (where the perceived pitch varies depending on the speed difference between the sound and the listener) – you will need: • An IDirectSound3DListener8 instance, representing the user's position in space • An IDirectSound3DBuffer8 instance for each segment you want to locate in 3D space • Both the 3D sound buffer and the listener are used to calculate the volume and position of what you hear