Windows/MFC

WaveOut api

aucd29 2013. 10. 2. 18:39
http://glo2my.tistory.com/8

WaveOut api들을 사용하기 위해서
WaveOutOpen -> WaveOutPrepareHeader -> WaveOutWrite -> WaveOutClose

함수들을 사용합니다.

여기서 사람들이 어려워 하는 것중에 하나가

WaveOutOpen할 때 함수의 Argument로 주게 되는

WAVEFORMATEX 정보를 어떻게 줘야 하느냐 인데요.




간단한 설명입니다.


우선 Structure의 구성입니다.
typedef struct {
    WORD wFormatTag;
    WORD nChannels;
    DWORD nSamplesPerSec;
    DWORD nAvgBytesPerSec;
    WORD nBlockAlign;
    WORD wBitsPerSample;
    WORD cbSize;
} WAVEFORMATEX;





wFormatTag

Waveform-audio format type. Format tags are registered with Microsoft Corporation for many compression algorithms. A complete list of format tags can be found in the Mmreg.h header file. For one- or two-channel PCM data, this value should be WAVE_FORMAT_PCM. When this structure is included in a WAVEFORMATEXTENSIBLE structure, this value must be WAVE_FORMAT_EXTENSIBLE.

- Format을 알려주는 건데요, 보통 Waveout을 할 때는 PCM으로 하기 때문에

그냥 WAVE_FORMAT_PCM으로 해주면 됩니다.



nChannels

Number of channels in the waveform-audio data. Monaural data uses one channel and stereo data uses two channels.

- 재생할 Stream이 MONO인지, STEREO인지를 정해주는 겁니다.

재생할 Wav파일이나 Decoding한 MP3 파일같은 것이 Mono면 1, Stereo면 2 로 설정해주면 됩니다.



nSamplesPerSec

Sample rate, in samples per second (hertz). If wFormatTag is WAVE_FORMAT_PCM, then common values for nSamplesPerSec are 8.0 kHz, 11.025 kHz, 22.05 kHz, and 44.1 kHz. For non-PCM formats, this member must be computed according to the manufacturer's specification of the format tag.

- Sampling Rate 값을 적어주면 됩니다. 보편적으로는 8000 ~ 48000 사이 정보겠죠 :)

8000, 11025, 16000, 22050, 24000. 44100, 48000 같은 놈들이 대부분입니다.



nAvgBytesPerSec

Required average data-transfer rate, in bytes per second, for the format tag. If wFormatTag is WAVE_FORMAT_PCM, nAvgBytesPerSec should be equal to the product of nSamplesPerSec and nBlockAlign. For non-PCM formats, this member must be computed according to the manufacturer's specification of the format tag.

- 말 그대로 1초당 평균적은 샘플의 크기입니다.

이건 nBlockAlign * nSamplesPerSec 을 해주면 됩니다. :)

44100 Hz, 16bit, Stereo를 예로 들으면

44100 * nBlockAlign 이구요, nBlockAlign은 nChannel * wBitPerSample / 8 이니까

44100 * 2 * 16/8 = 176400 이 됩니다.




nBlockAlign

Block alignment, in bytes. The block alignment is the minimum atomic unit of data for the wFormatTag format type. If wFormatTag is WAVE_FORMAT_PCM or WAVE_FORMAT_EXTENSIBLE, nBlockAlign must be equal to the product of nChannels and wBitsPerSample divided by 8 (bits per byte). For non-PCM formats, this member must be computed according to the manufacturer's specification of the format tag.

Software must process a multiple of nBlockAlign bytes of data at a time. Data written to and read from a device must always start at the beginning of a block. For example, it is illegal to start playback of PCM data in the middle of a sample (that is, on a non-block-aligned boundary).

- 설명대로 하자면, nChannel * wBitPerSample / 8 값입니다.

Software가 Sample을 읽어갈 때 Align을 맞춰서 읽어가야 제대로 된 정보를 읽어갈 수 있습니다.



wBitsPerSample

Bits per sample for the wFormatTag format type. If wFormatTag is WAVE_FORMAT_PCM, then wBitsPerSample should be equal to 8 or 16. For non-PCM formats, this member must be set according to the manufacturer's specification of the format tag. If wFormatTag is WAVE_FORMAT_EXTENSIBLE, this value can be any integer multiple of 8 and represents the container size, not necessarily the sample size; for example, a 20-bit sample size is in a 24-bit container. Some compression schemes cannot define a value for wBitsPerSample, so this member can be 0.

- Bits Per Sample 말 그대로 한 Sample당 Bit의 수입니다.

8bits, 16bits로 보통 구성되어있죠.

대부분의 음악들은 16bits입니다.




cbSize

Size, in bytes, of extra format information appended to the end of the WAVEFORMATEX structure. This information can be used by non-PCM formats to store extra attributes for the wFormatTag. If no extra information is required by the wFormatTag, this member must be set to 0. For WAVE_FORMAT_PCM formats (and only WAVE_FORMAT_PCM formats), this member is ignored. When this structure is included in a WAVEFORMATEXTENSIBLE structure, this value must be at least 22.



- 혹시나 WAVEFORMATEX 스트럭쳐 뒤에 다른 정보를 넘겨줘야 할 때

(PCM이 아닐경우는 그런 상황이 있습니다. ) 뒤에 덧붙여지는 정보의 크기를 말합니다.

보통은 0으로 써버리면 됩니다.



이제 쉽게 WaveOutOpen을 해봅시다 ^^