chapter 13 directsound 로 잡음 만들기
DESCRIPTION
Chapter 13 DirectSound 로 잡음 만들기. History of Sound Programming. Sound programming always gets put off until the end DOS Third party sound libraries: Miles Sound System, Diamondware Sound Toolkit… high price Windows Sound and multimedia support: not for real-time video game - PowerPoint PPT PresentationTRANSCRIPT
Chapter 13Chapter 13
DirectSoundDirectSound 로 잡음 만들기 로 잡음 만들기
2
History of Sound ProgrammingHistory of Sound Programming
• Sound programming always gets put off until the end• DOS
– Third party sound libraries: Miles Sound System, Diamondware Sound Toolkit… high price
• Windows– Sound and multimedia support: not for real-time video game– DirectSound and DirectMusic
• free• high performance• Support for million different sound card
3
Extension of DirectSound and Extension of DirectSound and MusicMusic• DirectSound3D
– 3D sound support
• DirectMusic– Playing MIDI files– DLS (Downloadable Sounds) data– Sound can be selected by game users on-the-fly.
4
DirectSoundDirectSound
• Related components– DirectSound3D– DirectSoundCapture
5
DirectSound ComponentsDirectSound Components
• Run-time .DLL• Compile-time library and header (DSOUND.LIB and
DSOUND.H)
6
DirectSound Interfaces - 1DirectSound Interfaces - 1
7
DirectSound Interfaces - 2DirectSound Interfaces - 2
• IUnknown• IDirectSound
– main COM object, one per a sound card
• IDirectSoundBuffer– primary buffer : currently playing sound– secondary buffer : stored sound, system memory or SRAM (s
ound RAM)
• IDirectSoundCapture– capture (record), ex) record player’s name!, voice recognition
• IDirectSoundNotify– Send message back to DirectSound
8
Sound BufferSound Buffer
9
Starting Up DirectSoundStarting Up DirectSound
LPDIRECTSOUND lpds;
DirectSoundCreate(NULL, &lpds, NULL); // lpGuid, lpDS, …
// lpGuid = NULL : default sound card
10
Understanding Cooperation LevelUnderstanding Cooperation Level
• Normal Cooperation– Play sound for the game application + other applications– Default primary buffer of 22KHz, stereo, 8-bit 가 제공됨
• Priority Cooperation– First access to all the h/w– Only necessary for changing the data format of the primary bu
ffer: ex) 16-bit sample ..
• Exclusive Cooperation– Same as Priority, your application will be audible only when it’
s in the foreground.
• Write-Primary Cooperation– Total control for the primary buffer.
11
Setting the Cooperation LevelSetting the Cooperation Level
if (FAILED(lpds->SetCooperativeLevel(main_window_handle, DSSCL_NORMAL))) { … }
12
Primary Sound BufferPrimary Sound Buffer
• Mixing h/w (or s/w) on the sound card• Processes all the time • You don’t need to manually control the primary buffer• DirectSound create one primary buffer for you (when
we use DSSCL_NORMAL). • 22 KHZ stereo in 8-bit (default)
13
Secondary BuffersSecondary Buffers
• Can be any size• SRAM or System memory?
– Sounds on SRAM: low-cost for processing
• Two Kinds of Secondary Buffers– Static – Streaming
14
Creating Secondary Sound BuffersCreating Secondary Sound Buffers
// After filling the sound buffer description structure here. ….
CreateSoundBuffer(LPDSBUFFERDESC lpdsbd,
LPDIRECTSOUNDBUFFER lpdsbuffer,
NULL);
15
Sound Buffer DescriptionSound Buffer Description
typedef struct {
DWORD dwSize;
DWORD dwFlags;
DWORD dwBufferBytes; // buffer size in bytes
DWORD dwReserved; // unused
LPWAVEFORMATEX lpwfxFormat; // the wave format
} DSBUFFERDESC;
• Usual Flag Combination– DSBCAPS_CTRLDEFAULT : default control (volume, pan, frequency)– DSBCAPS_STATIC : static (not streaming)– DSBCAPS_LOCSOFTARE : system memory
16
WAVEFORMAT structureWAVEFORMAT structure
typedef structure { WORD wFormatTag; // always WAVE_FORMAT_PCM WORD nChannels; // 1 for mono, 2 for stereo DWORD nSamplesPerSec; // Samples per second 11025 or 22050 … DWORD nAvgBytesPerSec; // average data rate (SamplePerSec * nBlockAlign) WORD nBlockAlign; // nchannels * bytespersample WORD wBitsPerSample; // Bits per sample WORD cbSize; // advanced, 0} WAVEFORMATEX;
17
Sound Buffer Creation Example - 1Sound Buffer Creation Example - 1
ex) 11 KHz, mono, 8-bit with enough storage for 2 secs
LPDIRECTSOUNDBUFFER lpdsbuffer;
DSBUFFERDESC dsbd; // description
WAVEFORMATEX pcmwf; // format description
// WAVEFORMAT Creation
memset(&pcmwf, 0, sizeof(WAVEFORMATEX));
pcmwf.wFormatTag = WAVE_FORMAT_PCM; // always
pcmwf.nChannels = 1; // Mono
pcmwf.nSamplesPerSec = 11025; // 11 kHz sampling
pcmwf.nBlockAlign = 1; // 1 channel * 1 bytes / sample
pcmwf.nAvgBytesPerSec = pcmwf.nSamplesPerSec * pcmwf.nBlockAlign;
pcmwf.wBitsPerSample = 8; // 8bits / sample
pcmwf.cbSize = 0; // always 0
18
Sound Buffer Creation Example - 2Sound Buffer Creation Example - 2
// Create DSBUFFERDESC
memset(dsbd, 0, sizeof(DSBUFFERDESC));
dsbd.dwSize = sizeof(DSBUFFERDESC);
dsbd.dwFlags = DSBCAPS_CTRLDEFAULT | DSBCAPS_STATIC | DSBCAPS_LOCSOFTARE;
dsbd.dwBufferBytes = 22050; // for 2 seconds with 11 kHz
dsbd.lpwfxFormat = &pcmwf;
// Create the buffer
if (FAILED(lpds->CreateSoundBuffer(&dsbd, &lpdsbuffer, NULL)))
{ /* error */ }
19
Writing Data to Secondary BuffersWriting Data to Secondary Buffers
• Lock the buffer• Fill the buffer• Unlock the buffer
UCHAR *audio_ptr_1, *audio_ptr_2; int audio_length_1, audio_length_2;
if (FAILED(lpdsbuffer->Lock(0, 1000, (void **)audio_ptr_1, &audio_length_1, (void **)audio_ptr_2, &audio_length_2, DSBLOCK_ENTIREBUFFER))) {…}…. if (FAILED(lpdsbuffer->Unlock(audio_ptr_1, audio_length_1, audio_ptr_2, au
dio_length_2))) { … }
20
Locking Sound BufferLocking Sound Buffer
21
Rendering SoundsRendering Sounds
• Playing
HRESULT Play(
DWORD dwReserved1, DWORD dwReserved2, // both 0
DWORD dwFlags); // control flags to play
// Examplelpdsbuffer->Play(0, 0, DSBPLAY_LOOPING); // or flag=0 (no looping)
• Stopping
lpdsbuffer->Stop( )
22
DEMO: PROG13_2.CPPDEMO: PROG13_2.CPP
23
Controlling Sound - 1Controlling Sound - 1
• Controlling the Volumelpdsbuffer->SetVolume(LONG lVolume)lVolume = 0 : max sound (0 dB)lVolume = -10000 : no sound (-100 dB)
• Volume control macro (0 ~ 100)#define DSVOLUME_TO_DB(volume) ((DWORD)(-30*(100-volume)))lpdsbuffer->SetVolume(DSVOLUME_TO_DB(50)); // 50% volume// -30 * (100 – 50) = -1500 (50%)// -30 * (100 – 100) = 0 (maximum)// -30 * (100 – 0) = -30 * 100 = -3000 (minimum)
24
Controlling Sounds - 4Controlling Sounds - 4
• Frequency– lpdsbuffer->SetFrequency(22050) : 만약 원래가 11025 Hz
라면 속도가 2 배로 된다 .
• Panning– lpdsbuffer->SetPan(0) : center– lpdsbuffer->SetPan(-10000) : right channel = -100dB– lpdsbuffer->SetPan(10000) : left channel = -100dB– lpdsbuffer->SetPan(-500) : right channel = -5 dB (more sound
to left channel)
25
Getting Information - 1Getting Information - 1
• Determining the Capability of the H/W– GetCaps(LPDSCAPS lpDSCaps); – DSCAPS structure
typedef {
DWORD dwSize;
…
DWORD dwMaxSecondarySampleRate;
…
} DSCAPS;
26
Getting Information - 2Getting Information - 2
• Determining the Info of Buffer GetCaps(LPDSBCAPS lpDSBCaps);
typdef struct {
…
DWORD dwBufferBytes;
DWORD UnlockTransferRate; // Sample rate
DWORD dwPlayCpuOverhead; // % of processor to mix this sound …
} DSBCAPS;
27
Getting Information - 3Getting Information - 3
• Determining the Status of Sound Play– lpdsbuffer->GetStatus(&status); – Status
• DSBSTATUS_BUFFERLOST : something wrong!
• DSBSTATUS_LOOPING : looped mode
• DSBSTATUS_PLAYING : sound is now playing
• Other Information– Retrieve volume, pan, frequency setting etc.. – See DirectSound documents