Utilisation de la structure WAVEFORMATEX
Pour les données audio PCM sur un maximum de deux canaux et avec des exemples de 8 ou 16 bits, utilisez la structure WAVEFORMATEX pour spécifier le format de données.
L’exemple suivant montre comment configurer une structure WAVEFORMATEX pour une chaîne mono 8 bits 11,025 kilohertz (kHz) et pour une chaîne stéréo de 44,1 kHz 16 bits. Après la configuration de WAVEFORMATEX, l’exemple appelle la fonction IsFormatSupported pour vérifier que l’appareil de sortie Waveform PCM prend en charge le format. Le code source pour IsFormatSupported est présenté dans un exemple pour déterminer la prise en charge des formats non standard.
UINT wReturn;
WAVEFORMATEX pcmWaveFormat;
// Set up WAVEFORMATEX for 11 kHz 8-bit mono.
pcmWaveFormat.wFormatTag = WAVE_FORMAT_PCM;
pcmWaveFormat.nChannels = 1;
pcmWaveFormat.nSamplesPerSec = 11025L;
pcmWaveFormat.nAvgBytesPerSec = 11025L;
pcmWaveFormat.nBlockAlign = 1;
pcmWaveFormat.wBitsPerSample = 8;
pcmWaveFormat.cbSize = 0;
// See if format is supported by any device in system.
wReturn = IsFormatSupported(&pcmWaveFormat, WAVE_MAPPER);
// Report results.
if (wReturn == 0)
MessageBox(hMainWnd, "11 kHz 8-bit mono is supported.",
"", MB_ICONINFORMATION);
else if (wReturn == WAVERR_BADFORMAT)
MessageBox(hMainWnd, "11 kHz 8-bit mono NOT supported.",
"", MB_ICONINFORMATION);
else
MessageBox(hMainWnd, "Error opening waveform device.",
"Error", MB_ICONEXCLAMATION);
// Set up WAVEFORMATEX for 44.1 kHz 16-bit stereo.
pcmWaveFormat.wFormatTag = WAVE_FORMAT_PCM;
pcmWaveFormat.nChannels = 2;
pcmWaveFormat.nSamplesPerSec = 44100L;
pcmWaveFormat.nAvgBytesPerSec = 176400L;
pcmWaveFormat.nBlockAlign = 4;
pcmWaveFormat.wBitsPerSample = 16;
pcmWaveFormat.cbSize = 0;
// See if format is supported by any device in the system.
wReturn = IsFormatSupported(&pcmWaveFormat, WAVE_MAPPER);
// Report results.
if (wReturn == 0)
MessageBox(hMainWnd, "44.1 kHz 16-bit stereo is supported.",
"", MB_ICONINFORMATION);
else if (wReturn == WAVERR_BADFORMAT)
MessageBox(hMainWnd, "44.1 kHz 16-bit stereo NOT supported.",
"", MB_ICONINFORMATION);
else
MessageBox(hMainWnd, "Error opening waveform device.",
"Error", MB_ICONEXCLAMATION);