我一直在使用WAVEFORMATEX
WaveOut 在 Windows 中以 44.1KHz 到 192KHz 的速率播放音频WAVE_FORMAT_IEEE_FLOAT
。该程序用 C++ 编写并在 MinGW 中编译。这一切正常:
现在我正在尝试扩展到四声道多声道输出,这似乎需要WAVEFORMATEXTENSIBLE
. 的超集WAVEFORMATEX
。以下是应用了这些更改的相关代码:
WAVEFORMATEXTENSIBLE wfx;
memset( &wfx, 0, sizeof(wfx) );
wfx.Format.nChannels = want.channels;
wfx.dwChannelMask = (want.channels == 4) ? 0x33 : ((want.channels == 1) ? 0x4 : 0x3);
wfx.Format.nSamplesPerSec = want.freq;
wfx.Format.cbSize = sizeof(wfx) - sizeof(wfx.Format);
MMRESULT wave_out_result = ~MMSYSERR_NOERROR;
if( userdata.HighRes )
{
wfx.Format.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
wfx.SubFormat = {0x00000003,0x0000,0x0010,0x80,0x00,0x00,0xAA,0x00,0x38,0x9B,0x71}; //KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
wfx.Format.wBitsPerSample = 32;
wfx.Samples.wValidBitsPerSample = wfx.Format.wBitsPerSample;
wfx.Format.nBlockAlign = wfx.Format.nChannels * wfx.Format.wBitsPerSample / 8;
wfx.Format.nAvgBytesPerSec = wfx.Format.nBlockAlign * wfx.Format.nSamplesPerSec;
wave_out_result = waveOutOpen( &WaveOutHandle, WAVE_MAPPER, &(wfx.Format), (DWORD_PTR) &WaveOutCallback, 0, CALLBACK_FUNCTION );
}
if( wave_out_result != MMSYSERR_NOERROR )
{
if( userdata.HighRes )
fprintf( stderr, "waveOutOpen returned %i%s when attempting float output\n", wave_out_result, (wave_out_result == WAVERR_BADFORMAT)?" (WAVERR_BADFORMAT)":"" );
wfx.Format.wFormatTag = WAVE_FORMAT_PCM;
wfx.SubFormat = {0x00000001,0x0000,0x0010,0x80,0x00,0x00,0xAA,0x00,0x38,0x9B,0x71}; //KSDATAFORMAT_SUBTYPE_PCM
wfx.Format.wBitsPerSample = 16;
wfx.Samples.wValidBitsPerSample = wfx.Format.wBitsPerSample;
wfx.Format.nBlockAlign = wfx.Format.nChannels * wfx.Format.wBitsPerSample / 8;
wfx.Format.nAvgBytesPerSec = wfx.Format.nBlockAlign * wfx.Format.nSamplesPerSec;
wave_out_result = waveOutOpen( &WaveOutHandle, WAVE_MAPPER, &(wfx.Format), (DWORD_PTR) &WaveOutCallback, 0, CALLBACK_FUNCTION );
if( wave_out_result == MMSYSERR_NOERROR )
userdata.HighRes = false;
else
fprintf( stderr, "waveOutOpen returned %i%s when attempting int16 output\n", wave_out_result, (wave_out_result == WAVERR_BADFORMAT)?" (WAVERR_BADFORMAT)":"" );
}
如果我设置Format.nChannels = 2; dwChannelMask = 0x3;
为立体声,则waveOutOpen
使用 IEEE-float 格式的第一次尝试失败并返回代码WAVERR_BADFORMAT
,但使用 PCM 格式的第二次尝试成功。
如果我尝试Format.nChannels = 4; dwChannelMask = 0x33;
四声道,IEEE-float 和 PCMwaveOutOpen
尝试都会失败,并显示WAVERR_BADFORMAT
.
但是,如果我设置Format.cbSize = 0;
,那么任何一种格式的 2 个通道都可以正常工作,这是有道理的,因为这基本上是我之前一直在做的事情WAVEFORMATEX
。但这不适用于 4 个通道。
我在这里做错了什么? 我的最终目标是 IEEE 浮点格式的四声道或 5.1 环绕声输出。我特别困惑为什么我什至无法使用立体声 IEEE-float 输出,WAVEFORMATEXTENSIBLE
但它与WAVEFORMATEX
.