wasapi gets the audio data

echo161 注册会员
2023-02-25 20:13

the reference answer GPT ᴼ ᴾ ᴱ ᴺ ᴬ ᴵ < br / > when using IMMDevice: : Activate method open audio devices, if the request is not clearly specify, the flow of data format The device's default data flow format is used, the default format parameters specified by the WAVEFORMATEXTENSIBLE structure returned by the GetMixFormat method. If the wFormatTag field in the WAVEFORMATEXTENSIBLE structure equals 65534(0xFFFE), then this indicates that the data stream format is compressed-based streaming audio format, It is called Extended and Variable-Length Compression Format(EXtensible variable-Length Compression Format, or XVID). The
XVID format is a format used to compress audio and includes a 128-bit GUID to identify the format's detailed parameters. Because of its extensibility and variable length, the format can accommodate a variety of compression techniques and audio encoding formats.
If you want to manually specify the audio format, you can do so using the pFormat parameter in the IAudioClient::Initialize method. In this case, you need to use valid format parameters. If you specify an invalid parameter, an E_INVALIDARG error occurs. You can use the member of the WAVEFORMATEXTENSIBLE structure to set specific format parameters and pass them to the pFormat parameter of the Initialize method. For example, the following code demonstrates how to initialize the WAVEFORMATEXTENSIBLE structure to specify the PCM 16-bit, 44.1kHz, 2-channel audio format:

wfx.Format.wFormatTag = WAVE_FORMAT_PCM;
wfx.Format.nChannels = 2;
wfx.Format.nSamplesPerSec = 44100;
wfx.Format.nAvgBytesPerSec = 44100 * 2 * 2;
wfx.Format.nBlockAlign = 2 * 2;
wfx.Format.wBitsPerSample = 16;
wfx.Format.cbSize = 0;
wfx.Samples.wValidBitsPerSample = 16;

If you need to use a custom audio format, then you need to carefully understand the parameters of that format and set up the members of the WAVEFORMATEXTENSIBLE structure correctly.

isabel333 注册会员
2023-02-25 20:13

This answer refers to GPT, GPT_Pro better to solve the problem
If wFormatTag equals 65534, this indicates that you are using an unknown audio format. In this case, you can analyze the variables in the pFormat structure to get the exact audio format.

// 获取pFormat
DWORD wFormatTag = pFormat->wFormatTag;
// 如果wFormatTag等于65534,表示正在使用未知音频格式
if (wFormatTag == 65534){
    // 分析pFormat结构中的变量,以获取确切的音频格式
    DWORD nSamplesPerSec = pFormat->nSamplesPerSec;  // 采样率
    WORD wBitsPerSample = pFormat->wBitsPerSample;  // 比特率
    WORD nChannels = pFormat->nChannels;            // 通道数
    WORD nBlockAlign = pFormat->nBlockAlign;        // 块对齐
    DWORD cbSize = pFormat->cbSize;                 // 比特率大小
    // 根据以上信息,可以定义自己想要的WAVEFORMATEX结构体,然后进行初始化。
    WAVEFORMATEX waveformatex;
    waveformatex.wFormatTag = WAVE_FORMAT_PCM;  // 音频格式  
    waveformatex.nSamplesPerSec = nSamplesPerSec;  // 采样率  
    waveformatex.wBitsPerSample = wBitsPerSample;  // 比特率  
    waveformatex.nChannels = nChannels;            // 通道数  
    waveformatex.nBlockAlign = nBlockAlign;        // 块对齐  
    waveformatex.cbSize = cbSize;                 // 比特率大小  
    // 调用函数初始化
    Initialize(&waveformatex);  // &waveformatex为初始化函数的参数 

By analyzing the variables in the pFormat structure, the sampling rate, bit rate, number of channels, block alignment, bit rate size information can be obtained and used to define the WAVEFORMATEX structure before initialization.
If the answer is helpful, please accept it.