/* returns pointer to buffer */ void* DS_createSoundBuffer(DS_Info* info, float sampleRate, /* 44100 */ int sampleSizeInBits, /* 16 */ int channels /* 2 */, int bufferSizeInBytes/* 88200 0.5 sec */) { DSBUFFERDESC dsbdesc; //创建DSBUFFERDESC结构体 DSCBUFFERDESC dscbdesc; HRESULT res; WAVEFORMATEXTENSIBLE format; void* buffer; //TRACE1("Creating secondary buffer for device %d\n", info->deviceID); //设置主声音缓冲区的波形音频格式 createWaveFormat(&format, (int) sampleRate, channels, info->frameSize / channels * 8, sampleSizeInBits); /* 2 second secondary buffer */ info->dsBufferSizeInBytes = 2 * ((int) sampleRate) * info->frameSize; if (bufferSizeInBytes > info->dsBufferSizeInBytes / 2) { bufferSizeInBytes = info->dsBufferSizeInBytes / 2; } bufferSizeInBytes = (bufferSizeInBytes / info->frameSize) * info->frameSize; info->bufferSizeInBytes = bufferSizeInBytes; if (info->isSource) { memset(&dsbdesc, 0, sizeof(DSBUFFERDESC)); //清零 dsbdesc.dwSize = sizeof(DSBUFFERDESC); //结构大小 //缓冲区性能标志常量 dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2 //获取当前位置2 | DSBCAPS_GLOBALFOCUS //有全局焦点的缓冲区 | DSBCAPS_CTRLPAN //声音可以从左声道移动到右声道 | DSBCAPS_CTRLVOLUME //可以改变音量大小 | DSBCAPS_CTRLPOSITIONNOTIFY //可以在缓冲区设置通知的位置 | DSBCAPS_CTRLFX; //设置增强音效特技 dsbdesc.dwBufferBytes = info->dsBufferSizeInBytes; dsbdesc.lpwfxFormat = (WAVEFORMATEX*) &format; //设置缓冲区格式 res = DEV_PLAY(info->deviceID)->CreateSoundBuffer(&dsbdesc, (LPDIRECTSOUNDBUFFER*) &buffer, NULL); //创建辅助缓冲区 } else { memset(&dscbdesc, 0, sizeof(DSCBUFFERDESC)); dscbdesc.dwSize = sizeof(DSCBUFFERDESC); dscbdesc.dwFlags = 0; dscbdesc.dwBufferBytes = info->dsBufferSizeInBytes; dscbdesc.lpwfxFormat = (WAVEFORMATEX*) &format; res = DEV_CAPTURE(info->deviceID)->CreateCaptureBuffer(&dscbdesc, (LPDIRECTSOUNDCAPTUREBUFFER*) &buffer, NULL); } if (FAILED(res)) { ERROR1("DS_createSoundBuffer: ERROR: Failed to create sound buffer: %s", TranslateDSError(res)); return NULL; } return buffer; }
int HAE_GetSupportedCaptureFormats(INT32 deviceID, UINT32 *encodings, UINT32 *sampleRates, UINT32 *channels, UINT32 *bits, int maxFormats) { MMRESULT theErr; WAVEFORMATEXTENSIBLE format; int rateIndex, channelIndex, bitIndex; int numSupportedFormats = 0; /* $$fb 2002-04-10: fix for 4514334: JavaSoundDemo Capture not works on Windows2000 with USB Port */ if (deviceID == 0) { deviceID = WAVE_MAPPER; } else { deviceID--; } for (rateIndex = 0; rateIndex < SAMPLERATE_COUNT; rateIndex++) { for (channelIndex = 0; channelIndex < CHANNELS_COUNT; channelIndex++) { for (bitIndex = 0; bitIndex < BITS_COUNT; bitIndex++) { createWaveFormat(&format, sampleRateArray[rateIndex], channelsArray[channelIndex], bitsArray[bitIndex]); theErr = waveInOpen(0, deviceID, (WAVEFORMATEX*) &format, 0, 0, WAVE_FORMAT_QUERY | WAVE_FORMAT_DIRECT); if (theErr == MMSYSERR_NOERROR) { // this format is supported!! encodings[numSupportedFormats] = PCM; sampleRates[numSupportedFormats] = sampleRateArray[rateIndex]; channels[numSupportedFormats] = channelsArray[channelIndex]; bits[numSupportedFormats] = bitsArray[bitIndex]; numSupportedFormats++; if (numSupportedFormats >= maxFormats) return numSupportedFormats; } } } } return numSupportedFormats; }