chapter 13 directsound 로 잡음 만들기. 2 history of sound programming sound programming always...
TRANSCRIPT
![Page 1: Chapter 13 DirectSound 로 잡음 만들기. 2 History of Sound Programming Sound programming always gets put off until the end DOS –Third party sound libraries:](https://reader035.vdocuments.pub/reader035/viewer/2022062309/56649f3e5503460f94c5f0c2/html5/thumbnails/1.jpg)
Chapter 13Chapter 13
DirectSoundDirectSound 로 잡음 만들기 로 잡음 만들기
![Page 2: Chapter 13 DirectSound 로 잡음 만들기. 2 History of Sound Programming Sound programming always gets put off until the end DOS –Third party sound libraries:](https://reader035.vdocuments.pub/reader035/viewer/2022062309/56649f3e5503460f94c5f0c2/html5/thumbnails/2.jpg)
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
![Page 3: Chapter 13 DirectSound 로 잡음 만들기. 2 History of Sound Programming Sound programming always gets put off until the end DOS –Third party sound libraries:](https://reader035.vdocuments.pub/reader035/viewer/2022062309/56649f3e5503460f94c5f0c2/html5/thumbnails/3.jpg)
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.
![Page 4: Chapter 13 DirectSound 로 잡음 만들기. 2 History of Sound Programming Sound programming always gets put off until the end DOS –Third party sound libraries:](https://reader035.vdocuments.pub/reader035/viewer/2022062309/56649f3e5503460f94c5f0c2/html5/thumbnails/4.jpg)
4
DirectSoundDirectSound
• Related components– DirectSound3D– DirectSoundCapture
![Page 5: Chapter 13 DirectSound 로 잡음 만들기. 2 History of Sound Programming Sound programming always gets put off until the end DOS –Third party sound libraries:](https://reader035.vdocuments.pub/reader035/viewer/2022062309/56649f3e5503460f94c5f0c2/html5/thumbnails/5.jpg)
5
DirectSound ComponentsDirectSound Components
• Run-time .DLL• Compile-time library and header (DSOUND.LIB and
DSOUND.H)
![Page 6: Chapter 13 DirectSound 로 잡음 만들기. 2 History of Sound Programming Sound programming always gets put off until the end DOS –Third party sound libraries:](https://reader035.vdocuments.pub/reader035/viewer/2022062309/56649f3e5503460f94c5f0c2/html5/thumbnails/6.jpg)
6
DirectSound Interfaces - 1DirectSound Interfaces - 1
![Page 7: Chapter 13 DirectSound 로 잡음 만들기. 2 History of Sound Programming Sound programming always gets put off until the end DOS –Third party sound libraries:](https://reader035.vdocuments.pub/reader035/viewer/2022062309/56649f3e5503460f94c5f0c2/html5/thumbnails/7.jpg)
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
![Page 8: Chapter 13 DirectSound 로 잡음 만들기. 2 History of Sound Programming Sound programming always gets put off until the end DOS –Third party sound libraries:](https://reader035.vdocuments.pub/reader035/viewer/2022062309/56649f3e5503460f94c5f0c2/html5/thumbnails/8.jpg)
8
Sound BufferSound Buffer
![Page 9: Chapter 13 DirectSound 로 잡음 만들기. 2 History of Sound Programming Sound programming always gets put off until the end DOS –Third party sound libraries:](https://reader035.vdocuments.pub/reader035/viewer/2022062309/56649f3e5503460f94c5f0c2/html5/thumbnails/9.jpg)
9
Starting Up DirectSoundStarting Up DirectSound
LPDIRECTSOUND lpds;
DirectSoundCreate(NULL, &lpds, NULL); // lpGuid, lpDS, …
// lpGuid = NULL : default sound card
![Page 10: Chapter 13 DirectSound 로 잡음 만들기. 2 History of Sound Programming Sound programming always gets put off until the end DOS –Third party sound libraries:](https://reader035.vdocuments.pub/reader035/viewer/2022062309/56649f3e5503460f94c5f0c2/html5/thumbnails/10.jpg)
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.
![Page 11: Chapter 13 DirectSound 로 잡음 만들기. 2 History of Sound Programming Sound programming always gets put off until the end DOS –Third party sound libraries:](https://reader035.vdocuments.pub/reader035/viewer/2022062309/56649f3e5503460f94c5f0c2/html5/thumbnails/11.jpg)
11
Setting the Cooperation LevelSetting the Cooperation Level
if (FAILED(lpds->SetCooperativeLevel(main_window_handle, DSSCL_NORMAL))) { … }
![Page 12: Chapter 13 DirectSound 로 잡음 만들기. 2 History of Sound Programming Sound programming always gets put off until the end DOS –Third party sound libraries:](https://reader035.vdocuments.pub/reader035/viewer/2022062309/56649f3e5503460f94c5f0c2/html5/thumbnails/12.jpg)
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)
![Page 13: Chapter 13 DirectSound 로 잡음 만들기. 2 History of Sound Programming Sound programming always gets put off until the end DOS –Third party sound libraries:](https://reader035.vdocuments.pub/reader035/viewer/2022062309/56649f3e5503460f94c5f0c2/html5/thumbnails/13.jpg)
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
![Page 14: Chapter 13 DirectSound 로 잡음 만들기. 2 History of Sound Programming Sound programming always gets put off until the end DOS –Third party sound libraries:](https://reader035.vdocuments.pub/reader035/viewer/2022062309/56649f3e5503460f94c5f0c2/html5/thumbnails/14.jpg)
14
Creating Secondary Sound BuffersCreating Secondary Sound Buffers
// After filling the sound buffer description structure here. ….
CreateSoundBuffer(LPDSBUFFERDESC lpdsbd,
LPDIRECTSOUNDBUFFER lpdsbuffer,
NULL);
![Page 15: Chapter 13 DirectSound 로 잡음 만들기. 2 History of Sound Programming Sound programming always gets put off until the end DOS –Third party sound libraries:](https://reader035.vdocuments.pub/reader035/viewer/2022062309/56649f3e5503460f94c5f0c2/html5/thumbnails/15.jpg)
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
![Page 16: Chapter 13 DirectSound 로 잡음 만들기. 2 History of Sound Programming Sound programming always gets put off until the end DOS –Third party sound libraries:](https://reader035.vdocuments.pub/reader035/viewer/2022062309/56649f3e5503460f94c5f0c2/html5/thumbnails/16.jpg)
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;
![Page 17: Chapter 13 DirectSound 로 잡음 만들기. 2 History of Sound Programming Sound programming always gets put off until the end DOS –Third party sound libraries:](https://reader035.vdocuments.pub/reader035/viewer/2022062309/56649f3e5503460f94c5f0c2/html5/thumbnails/17.jpg)
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
![Page 18: Chapter 13 DirectSound 로 잡음 만들기. 2 History of Sound Programming Sound programming always gets put off until the end DOS –Third party sound libraries:](https://reader035.vdocuments.pub/reader035/viewer/2022062309/56649f3e5503460f94c5f0c2/html5/thumbnails/18.jpg)
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 */ }
![Page 19: Chapter 13 DirectSound 로 잡음 만들기. 2 History of Sound Programming Sound programming always gets put off until the end DOS –Third party sound libraries:](https://reader035.vdocuments.pub/reader035/viewer/2022062309/56649f3e5503460f94c5f0c2/html5/thumbnails/19.jpg)
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))) { … }
![Page 20: Chapter 13 DirectSound 로 잡음 만들기. 2 History of Sound Programming Sound programming always gets put off until the end DOS –Third party sound libraries:](https://reader035.vdocuments.pub/reader035/viewer/2022062309/56649f3e5503460f94c5f0c2/html5/thumbnails/20.jpg)
20
Locking Sound BufferLocking Sound Buffer
![Page 21: Chapter 13 DirectSound 로 잡음 만들기. 2 History of Sound Programming Sound programming always gets put off until the end DOS –Third party sound libraries:](https://reader035.vdocuments.pub/reader035/viewer/2022062309/56649f3e5503460f94c5f0c2/html5/thumbnails/21.jpg)
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( )
![Page 22: Chapter 13 DirectSound 로 잡음 만들기. 2 History of Sound Programming Sound programming always gets put off until the end DOS –Third party sound libraries:](https://reader035.vdocuments.pub/reader035/viewer/2022062309/56649f3e5503460f94c5f0c2/html5/thumbnails/22.jpg)
22
DEMO: PROG13_2.CPPDEMO: PROG13_2.CPP
![Page 23: Chapter 13 DirectSound 로 잡음 만들기. 2 History of Sound Programming Sound programming always gets put off until the end DOS –Third party sound libraries:](https://reader035.vdocuments.pub/reader035/viewer/2022062309/56649f3e5503460f94c5f0c2/html5/thumbnails/23.jpg)
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)
![Page 24: Chapter 13 DirectSound 로 잡음 만들기. 2 History of Sound Programming Sound programming always gets put off until the end DOS –Third party sound libraries:](https://reader035.vdocuments.pub/reader035/viewer/2022062309/56649f3e5503460f94c5f0c2/html5/thumbnails/24.jpg)
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)
![Page 25: Chapter 13 DirectSound 로 잡음 만들기. 2 History of Sound Programming Sound programming always gets put off until the end DOS –Third party sound libraries:](https://reader035.vdocuments.pub/reader035/viewer/2022062309/56649f3e5503460f94c5f0c2/html5/thumbnails/25.jpg)
25
Getting Information - 1Getting Information - 1
• Determining the Capability of the H/W– GetCaps(LPDSCAPS lpDSCaps); – DSCAPS structure
typedef {
DWORD dwSize;
…
DWORD dwMaxSecondarySampleRate;
…
} DSCAPS;
![Page 26: Chapter 13 DirectSound 로 잡음 만들기. 2 History of Sound Programming Sound programming always gets put off until the end DOS –Third party sound libraries:](https://reader035.vdocuments.pub/reader035/viewer/2022062309/56649f3e5503460f94c5f0c2/html5/thumbnails/26.jpg)
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;
![Page 27: Chapter 13 DirectSound 로 잡음 만들기. 2 History of Sound Programming Sound programming always gets put off until the end DOS –Third party sound libraries:](https://reader035.vdocuments.pub/reader035/viewer/2022062309/56649f3e5503460f94c5f0c2/html5/thumbnails/27.jpg)
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