本文整理汇总了C++中LPDIRECTSOUND8::CreateSoundBuffer方法的典型用法代码示例。如果您正苦于以下问题:C++ LPDIRECTSOUND8::CreateSoundBuffer方法的具体用法?C++ LPDIRECTSOUND8::CreateSoundBuffer怎么用?C++ LPDIRECTSOUND8::CreateSoundBuffer使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类LPDIRECTSOUND8
的用法示例。
在下文中一共展示了LPDIRECTSOUND8::CreateSoundBuffer方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: InitDSound
static int InitDSound(HWND hWnd)
{
HRESULT hr;
pDirectSoundCreate8 SoundCreate;
dsoundDLL = LoadLibrary("dsound.dll");
if (dsoundDLL == NULL) {
WriteLog("not find dsound.dll");
}
SoundCreate = (pDirectSoundCreate8)GetProcAddress(dsoundDLL, "DirectSoundCreate8");
hr = SoundCreate(NULL, &lpDS, NULL);
if (FAILED(hr)) {
goto end;
}
hr = lpDS->SetCooperativeLevel(hWnd, DSSCL_PRIORITY);
if (FAILED(hr)) {
goto end;
}
DSBUFFERDESC desc;
ZeroMemory(&desc,sizeof(DSBUFFERDESC));
desc.dwSize = sizeof(DSBUFFERDESC);
desc.dwFlags = DSBCAPS_PRIMARYBUFFER;
hr = lpDS->CreateSoundBuffer(&desc, &lpDSP, NULL);
if (FAILED(hr)) {
goto end;
}
WriteLog("init sound\n");
return 1;
end:
return 0;
}
示例2: Dsoundinit
bool Dsoundinit( HWND hwnd )
{
// SBArrの初期化
initSBArr();
// DirectSound8 の作成
if (FAILED(DirectSoundCreate8(NULL,&lpDS,NULL)))
{
MessageBox(NULL,"DirectSound オブジェクトの作成に失敗しました。","DirectSoundエラー",MB_OK | MB_ICONSTOP);
return false;
}
// DirectSound の協調レベルを設定
if (FAILED(lpDS->SetCooperativeLevel(hwnd,DSSCL_PRIORITY)))
{
MessageBox(NULL,"DirectSound の協調レベルの設定に失敗しました。","DirectSoundエラー",MB_OK | MB_ICONSTOP);
// 閉じる
SendMessage(hwnd,WM_CLOSE,0,0);
return false;
}
// プライマリ サウンドバッファ作成
DSBUFFERDESC desc;
ZeroMemory(&desc,sizeof(DSBUFFERDESC));
desc.dwSize = sizeof(DSBUFFERDESC);
desc.dwFlags = DSBCAPS_PRIMARYBUFFER;
if (FAILED(lpDS->CreateSoundBuffer(&desc,&lpDSP,NULL)))
{
MessageBox(NULL,"プライマリサウンドバッファの作成に失敗しました。","DirectSoundエラー",MB_OK | MB_ICONSTOP);
// 閉じる
SendMessage(hwnd,WM_CLOSE,0,0);
return false;
}
//ここまでは一回行えばOKの処理
//SEの数だけセカンダリバッファにデータ読み込み
//読み込みファイル名をリストから読み込む
initSBFileList();
int nCount;
for(nCount = 0;nCount <= SE_MAX - 1;nCount++){
// WAVE ファイルの読み込み
// MessageBox(NULL,SBArr[nCount].cSeFileName,"",MB_OK);
SBArr[nCount].SB_lpDSB = LoadWave(SBArr[nCount].cSeFileName); //効果音
if (SBArr[nCount].SB_lpDSB == NULL)
{
MessageBox(NULL,"ファイルの読み込みに失敗しました。","DirectSoundエラー",MB_OK | MB_ICONSTOP);
// 閉じる
SendMessage(hwnd,WM_CLOSE,0,0);
return false;
}
}
return true;
}
示例3: CreateBuffer
SOUNDBUFFER CreateBuffer()
{
LPDIRECTSOUNDBUFFER ret;
ds8->CreateSoundBuffer(&bufdesc, &ret, NULL);
ret->SetVolume(1000);
return ((int*)ret);
}
示例4: OpenSoundFile
static LPDIRECTSOUNDBUFFER OpenSoundFile(char *name)
{
DSBUFFERDESC DSBufDESC;
WAVEFORMATEX waveFormat;
void* buf;
DWORD size;
HMMIO hmmio;
MMCKINFO cParent, cSub;
LPDIRECTSOUNDBUFFER lpDSBuf = NULL;
MMIOINFO mem;
int fileSize;
char *data = LoadFile(name, &fileSize);
memset(&mem, 0, sizeof(MMIOINFO));
mem.pchBuffer = data;
mem.cchBuffer = fileSize;
mem.fccIOProc = FOURCC_MEM;
hmmio = mmioOpen(NULL, &mem, MMIO_ALLOCBUF | MMIO_READ);
cParent.fccType = mmioFOURCC('W','A','V','E');
if (mmioDescend(hmmio, &cParent, NULL, MMIO_FINDRIFF) != MMSYSERR_NOERROR) {
return NULL;
}
cSub.ckid = mmioFOURCC('f','m','t',' ');
if (mmioDescend(hmmio, &cSub, &cParent, MMIO_FINDCHUNK) != MMSYSERR_NOERROR) {
goto end;
}
mmioRead(hmmio, (char*)&waveFormat, cSub.cksize);
mmioAscend(hmmio, &cSub, 0);
if (waveFormat.wFormatTag != WAVE_FORMAT_PCM) {
goto end;
}
cSub.ckid = mmioFOURCC('d','a','t','a');
if (mmioDescend(hmmio, &cSub, &cParent, MMIO_FINDCHUNK) != MMSYSERR_NOERROR) {
goto end;
}
ZeroMemory(&DSBufDESC, sizeof(DSBUFFERDESC));
DSBufDESC.dwSize = sizeof(DSBUFFERDESC);
DSBufDESC.dwFlags = DSBCAPS_CTRLPAN|DSBCAPS_CTRLVOLUME|DSBCAPS_GLOBALFOCUS;
DSBufDESC.dwBufferBytes = cSub.cksize;
DSBufDESC.lpwfxFormat = &waveFormat;
lpDS->CreateSoundBuffer(&DSBufDESC, &lpDSBuf, NULL);
lpDSBuf->Lock(0, cSub.cksize, &buf, &size, NULL, 0, 0);
mmioRead(hmmio, (HPSTR)buf, (LONG)cSub.cksize);
lpDSBuf->Unlock(buf, size, NULL, 0);
mmioClose(hmmio, MMIO_FHOPEN);
return lpDSBuf;
end:
mmioClose(hmmio, MMIO_FHOPEN);
free(data);
return NULL;
}
示例5: InitSound
BOOL InitSound()
{
DSBUFFERDESC dsbdesc;
if (!hWnd)
{
return true;
}
if (!lpds)
{
//Create DirectSound interface
if (DirectSoundCreate8(NULL, &lpds, NULL))
{
Error("DirectSoundCreate8(NULL, &lpds, NULL)");
return true;
}
//Set cooperative level
if (lpds->SetCooperativeLevel(hWnd, DSSCL_NORMAL))
{
Error("lpds->SetCooperativeLevel(hWnd, DSSCL_NORMAL)");
CloseSound();
return true;
}
}
//Get primary buffer and set it to run silently
if (!lpdsbPrimary)
{
ZeroMemory(&dsbdesc, sizeof(dsbdesc));
dsbdesc.dwSize = sizeof(dsbdesc);
dsbdesc.dwFlags = DSBCAPS_PRIMARYBUFFER;
if (lpds->CreateSoundBuffer(&dsbdesc, &lpdsbPrimary, NULL))
{
Error("lpds->CreateSoundBuffer(&dsbdesc, &lpdsbPrimary, NULL)");
CloseSound();
return true;
}
if (lpdsbPrimary->Play(0, 0, DSBPLAY_LOOPING))
{
Error("lpdsbPrimary->Play(0, 0, DSBPLAY_LOOPING)");
CloseSound();
return true;
}
}
Settings.SoundEnabled = true;
return false;
}
示例6: LoadWave
// WAVE ファイルの読み込み
LPDIRECTSOUNDBUFFER LoadWave(char *name)
{
char *buf;
UINT readsize;
WAVEFORMATEX wf;
LPDIRECTSOUNDBUFFER lpDSB = NULL;
DSBUFFERDESC desc;
LPVOID pMem1,pMem2;
DWORD size1,size2;
// WAVE ファイルを開く
if (!YWaveOpen(name))
{
return NULL;
}
// メモリ領域の確保
buf = (char *)malloc(m_ckIn.cksize);
// 読み込み
ZeroMemory(&wf,sizeof(WAVEFORMATEX));
if (!YWaveRead(m_ckIn.cksize,buf,&readsize,&wf))
{
free(buf);
return NULL;
}
// サウンドバッファの作成
ZeroMemory(&desc,sizeof(DSBUFFERDESC));
desc.dwSize = sizeof(DSBUFFERDESC);
desc.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_GLOBALFOCUS | DSBCAPS_LOCDEFER | DSBCAPS_CTRLVOLUME;
desc.dwBufferBytes = readsize;
desc.lpwfxFormat = &wf;
if (FAILED(lpDS->CreateSoundBuffer(&desc,&lpDSB,NULL)))
{
free(buf);
return NULL;
}
// 領域をロック
if (FAILED(lpDSB->Lock(0,readsize,&pMem1,&size1,&pMem2,&size2,0)))
{
free(buf);
return 0;
}
// 書き込み
memcpy(pMem1,buf,size1);
if (size2)
{
memcpy(pMem2,buf + size1,size2);
}
// ロック解除
lpDSB->Unlock(pMem1,size1,pMem2,size2);
free(buf);
// 閉じる
YWaveClose();
return lpDSB;
}
示例7: get3DListener
//获得3D收听对象
HRESULT get3DListener(LPDIRECTSOUND8 lpds, LPDIRECTSOUND3DBUFFER8* ppListener){
DSBUFFERDESC dsbd;
LPDIRECTSOUNDBUFFER lpdsbPrimary;
LPDIRECTSOUND3DLISTENER lp3DListener = NULL;
HRESULT hr;
ZeroMemory(&dsbd, sizeof(DSBUFFERDESC));
dsbd.dwSize = sizeof(DSBUFFERDESC);
dsbd.dwFlags = DSBCAPS_CTRL3D | DSBCAPS_PRIMARYBUFFER;
if (SUCCEEDED(hr = lpds->CreateSoundBuffer(&dsbd, &lpdsbPrimary, NULL))){
hr = lpdsbPrimary->QueryInterface(IID_IDirectSound3DListener8, (LPVOID*)ppListener);
lpdsbPrimary->Release();
}
return hr;
}
示例8: ApplicationCreateWritePrimaryBuffer
BOOL ApplicationCreateWritePrimaryBuffer(
LPDIRECTSOUND8 lpDirectSound,
LPDIRECTSOUNDBUFFER8 *lplpDsb,
LPDWORD lpdwBufferSize,
HWND hwnd
){
DSBUFFERDESC dsbdesc;
DSBCAPS dsbcaps;
HRESULT hr;
WAVEFORMATEX wf;
memset(&wf, 0, sizeof(WAVEFORMATEX));
wf.wFormatTag = WAVE_FORMAT_PCM;
wf.nChannels = 2;
wf.nSamplesPerSec = 22050;
wf.nBlockAlign = 4;
wf.nAvgBytesPerSec = wf.nSamplesPerSec * wf.nBlockAlign;
wf.wBitsPerSample = 16;
memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));
dsbdesc.dwSize = sizeof(DSBUFFERDESC);
dsbdesc.dwFlags = DSBCAPS_PRIMARYBUFFER;
//缓存大小由声音硬件返回
dsbdesc.dwBufferBytes = 0;
dsbdesc.lpwfxFormat = NULL;//对于主缓存来说必须是NULL
//得到写数据协作权限
hr = lpDirectSound->SetCooperativeLevel(hwnd, DSSCL_WRITEPRIMARY);
if (SUCCEEDED(hr)){
hr = lpDirectSound->CreateSoundBuffer(&dsbdesc, lplpDsb, NULL);
if (SUCCEEDED(hr)){
hr = (*lplpDsb)->SetFormat(&wf);
if (SUCCEEDED(hr)){
dsbcaps.dwSize = sizeof(DSBCAPS);
(*lplpDsb)->GetCaps(&dsbcaps);
*lpdwBufferSize = dsbcaps.dwBufferBytes;
return TRUE;
}
}
}
//失败
*lplpDsb = NULL;
*lpdwBufferSize = 0;
return FALSE;
}
示例9: init
CString COggDlg::init(HWND hwnd,int sm)
{
DirectSoundCreate8(NULL,&m_ds,NULL);
if(m_ds==NULL) return _T("DirectSoundを生成できません。\nDirectX7が正常にインストールされているか確認してください。");
if(m_ds->SetCooperativeLevel(hwnd,DSSCL_PRIORITY)!=DS_OK){
return _T("DirectSoundの強調レベルを設定できません。\nDirectX7が正常にインストールされているか確認してください。");
}
hw=0;
// ZeroMemory(&d,sizeof(d));d.dwSize=sizeof(d);HRESULT r =m_ds->GetCaps(&d);
// if(r!=DS_OK){
// return "DirectSoundの情報を獲得出来ません。\nDirectX7が正常にインストールされているか確認してください。";
// }
// if(d.dwFlags & (DSCAPS_SECONDARYSTEREO|DSCAPS_PRIMARYSTEREO |DSCAPS_PRIMARY16BIT) && d.dwFreeHwMemBytes!=0){
// hw=DSBCAPS_LOCHARDWARE;
// }::timeSetEvent
m_p=NULL;
DSBUFFERDESC dss;
ZeroMemory(&dss,sizeof(dss));
dss.dwSize=sizeof(dss);
// dss.dwFlags=DSBCAPS_CTRL3D | DSBCAPS_CTRLVOLUME | DSBCAPS_CTRLPAN | DSBCAPS_CTRLFREQUENCY|DSBCAPS_PRIMARYBUFFER|hw;
dss.dwFlags=DSBCAPS_PRIMARYBUFFER;
dss.lpwfxFormat=NULL;
dss.dwBufferBytes=0;
if(m_ds->CreateSoundBuffer(&dss,&m_p,NULL)!=DS_OK){
return _T("DirectSoundのプライマリバッファを生成できません。\nDirectX7が正常にインストールされているか確認してください。");
}
if(m_p!=NULL){
// //PCMWAVEFORMAT p;
WAVEFORMATEX p;
ZeroMemory(&p,sizeof(p));
p.wFormatTag=WAVE_FORMAT_PCM;
p.nChannels= wavch;
p.nSamplesPerSec= wavbit;
p.wBitsPerSample = wavsam;
p.nBlockAlign = p.nChannels * p.wBitsPerSample / 8;
p.nAvgBytesPerSec = p.nSamplesPerSec * p.nBlockAlign;
p.cbSize = 0;
m_p->SetFormat(&p);
}
//m_p->QueryInterface(IID_IDirectSound3DListener, (LPVOID*)&m_listener);
//m_listener->SetPosition(0.0f, 0.0f, 0.0f, DS3D_IMMEDIATE);
return _T("");
}
示例10: CreateBaseBuffer
//get the DIRECTSOUNDBUFFER8 interface
HRESULT CreateBaseBuffer(LPDIRECTSOUND8 lpDirectSound, LPDIRECTSOUNDBUFFER8 * ppDsb8){
WAVEFORMATEX wfx;
DSBUFFERDESC dsbdesc;
LPDIRECTSOUNDBUFFER pDsb = NULL;
HRESULT hr;
//set up WAV buffer format
memset(&wfx, 0, sizeof(WAVEFORMATEX));
wfx.wFormatTag = WAVE_FORMAT_PCM;
wfx.nChannels = 2;
wfx.nSamplesPerSec = 22050;
wfx.nBlockAlign = 4;
wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
wfx.wBitsPerSample = 16;
//set up DSBUFFERDESC structure
memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));
dsbdesc.dwSize = sizeof(DSBUFFERDESC);
//the rule of using following flags is that use only what you will control
dsbdesc.dwFlags =
DSBCAPS_CTRLPAN |
DSBCAPS_CTRLVOLUME | //IDirectSound::SetVolume() is success only if this flag was set
DSBCAPS_CTRLFREQUENCY |
DSBCAPS_GLOBALFOCUS; //ensure that even if the window is in the backgroud,that mean you can hear the sound when you lose the focus of the window
dsbdesc.dwBufferBytes = 3 * wfx.nAvgBytesPerSec;//create a buffer that is big enough to hold 3 seconds of streaming data
dsbdesc.lpwfxFormat = &wfx;//if the was not specified, ds will place it in hardware-controlled memory.
//you can check the location of the buffer by using GetCaps() function,
//create buffer
hr = lpDirectSound->CreateSoundBuffer(&dsbdesc, &pDsb, NULL);
if (SUCCEEDED(hr)){
hr = pDsb->QueryInterface(IID_IDirectSoundBuffer8, (LPVOID*)ppDsb8);
pDsb->Release();
}
//the created buffer owned by device object,and released when device object release the resources
//you can also create multiple buffer by using function IDirectSound8::DuplicateSoundBuffer(),but the memory is share with original buffer,so if one buffer changed,others will be reflected.
return hr;
}
示例11: main
int main()
{
using namespace flamenco;
LPDIRECTSOUND8 directSound;
// Создаем интерфейс DirectSound.
check_directx(DirectSoundCreate8(&GUID_NULL, &directSound, NULL));
check_directx(directSound->SetCooperativeLevel(GetForegroundWindow(), DSSCL_PRIORITY));
// Получаем первичный буфер.
LPDIRECTSOUNDBUFFER primaryBuffer = NULL;
DSBUFFERDESC descr;
ZeroMemory(&descr, sizeof(DSBUFFERDESC));
descr.dwSize = sizeof(DSBUFFERDESC);
descr.dwFlags = DSBCAPS_PRIMARYBUFFER;
descr.lpwfxFormat = NULL;
check_directx(directSound->CreateSoundBuffer(&descr, &primaryBuffer, NULL));
// Изменяем формат первичного буфера.
WAVEFORMATEX wfx;
ZeroMemory(&wfx, sizeof(WAVEFORMATEX));
wfx.cbSize = sizeof(WAVEFORMATEX);
wfx.wFormatTag = WAVE_FORMAT_PCM;
wfx.nChannels = 2;
wfx.nSamplesPerSec = FREQUENCY;
wfx.wBitsPerSample = 16;
wfx.nBlockAlign = wfx.wBitsPerSample / 8 * wfx.nChannels;
wfx.nAvgBytesPerSec = (u32)wfx.nSamplesPerSec * wfx.nBlockAlign;
check_directx(primaryBuffer->SetFormat(&wfx));
primaryBuffer->Release();
// Формат буфера микшера.
const u32 MIXER_BUFFER_SIZE_IN_BYTES = MIXER_BUFFER_SIZE_IN_SAMPLES * sizeof(s16);
// Размер звукового буфера должен быть больше 100 ms, иначе GetCurrentPosition()
// будет выдавать неправильные данные.
const u32 SOUND_BUFFER_SIZE_IN_BYTES = 10 * MIXER_BUFFER_SIZE_IN_BYTES;
DSBUFFERDESC desc;
ZeroMemory(&desc, sizeof(DSBUFFERDESC));
desc.dwSize = sizeof(DSBUFFERDESC);
desc.dwFlags = DSBCAPS_LOCSOFTWARE | DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_GLOBALFOCUS;
desc.dwBufferBytes = SOUND_BUFFER_SIZE_IN_BYTES;
desc.guid3DAlgorithm = DS3DALG_DEFAULT;
desc.lpwfxFormat = &wfx;
// Создаем буфер.
LPDIRECTSOUNDBUFFER soundBuffer = NULL;
check_directx(directSound->CreateSoundBuffer(&desc, &soundBuffer, NULL));
//reference<stream<ogg_decoder> > sound = stream<ogg_decoder>::create(std::auto_ptr<source>(new file_source("input.ogg")));
// Инициализация flamenco.
mixer & mixer = mixer::singleton();
reference<sound_stream_base> sound = stream<wavpack_decoder>::create(std::auto_ptr<source>(new file_source("input.wv")));
//assert(!src.get());
mixer.attach(sound);
/*
reference<pin> sine = sine::create(400);
reference<ogg> wave = ogg::create("input.ogg");
reference<pin> noise = noise::create();
reference<volume_pan> vp = volume_pan::create(sound, 1.0f, 0.0f);
//mixer.attach(sine);
mixer.attach(vp);
//mixer.attach(noise);
*/
// Заполнение звукового буфера.
s16 * bufferPtr;
u32 bufferSize;
check_directx(soundBuffer->Lock(0, 0, reinterpret_cast<void **>(&bufferPtr), &bufferSize,
NULL, NULL, DSBLOCK_ENTIREBUFFER));
// Заполняем обе половинки буфера.
mixer.mix(bufferPtr);
mixer.mix(bufferPtr + MIXER_BUFFER_SIZE_IN_SAMPLES);
check_directx(soundBuffer->Unlock(bufferPtr, bufferSize, NULL, 0));
// Проигрываем звук и дописываем данные по ходу.
soundBuffer->Play(0, 0, DSBPLAY_LOOPING);
u32 writeOffset = MIXER_BUFFER_SIZE_IN_BYTES * 2;
while (true)
{
u32 cursorPos;
soundBuffer->GetCurrentPosition(&cursorPos, NULL);
// Определяем, нужно ли дописать очередную порцию данных.
u32 offset = (SOUND_BUFFER_SIZE_IN_BYTES + writeOffset - cursorPos) % SOUND_BUFFER_SIZE_IN_BYTES;
if (offset > MIXER_BUFFER_SIZE_IN_BYTES)
{
check_directx(soundBuffer->Lock(writeOffset, MIXER_BUFFER_SIZE_IN_BYTES,
reinterpret_cast<void **>(&bufferPtr), &bufferSize,
NULL, NULL, 0));
mixer.mix(bufferPtr);
check_directx(soundBuffer->Unlock(bufferPtr, bufferSize, NULL, 0));
writeOffset = (writeOffset + MIXER_BUFFER_SIZE_IN_BYTES) % SOUND_BUFFER_SIZE_IN_BYTES;
}
//.........这里部分代码省略.........
示例12: NewSoundBuffer
BOOL NewSoundBuffer(SOUNDBUFFER *pSoundBuffer)
{
DSBPOSITIONNOTIFY dsbpn[3];
DSBUFFERDESC dsbd;
WAVEFORMATEX wfx;
DWORD dw;
wfx.wFormatTag = WAVE_FORMAT_PCM;
wfx.nChannels = 2;
wfx.nSamplesPerSec = 22050;
wfx.wBitsPerSample = 8;
wfx.nBlockAlign = wfx.wBitsPerSample / 8 * wfx.nChannels;
wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
ZeroMemory(&dsbd, sizeof(dsbd));
dsbd.dwSize = sizeof(dsbd);
dsbd.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_GLOBALFOCUS | DSBCAPS_CTRLPOSITIONNOTIFY;
dsbd.lpwfxFormat = &wfx;
dsbd.dwBufferBytes = BufferSize * 3;
if (lpds->CreateSoundBuffer(&dsbd, &pSoundBuffer->lpdsb, NULL))
{
return true;
}
if (!(pSoundBuffer->hEvent[0] = CreateEvent(NULL, false, false, NULL)))
{
DestroySoundBuffer(pSoundBuffer);
return true;
}
if (!(pSoundBuffer->hEvent[1] = CreateEvent(NULL, false, false, NULL)))
{
DestroySoundBuffer(pSoundBuffer);
return true;
}
if (!(pSoundBuffer->hEvent[2] = CreateEvent(NULL, false, false, NULL)))
{
DestroySoundBuffer(pSoundBuffer);
return true;
}
if (pSoundBuffer->lpdsb->QueryInterface(IID_IDirectSoundNotify, (void **)&pSoundBuffer->lpdsn))
{
DestroySoundBuffer(pSoundBuffer);
return true;
}
dsbpn[0].hEventNotify = pSoundBuffer->hEvent[0];
dsbpn[0].dwOffset = 0;
dsbpn[1].hEventNotify = pSoundBuffer->hEvent[1];
dsbpn[1].dwOffset = BufferSize;
dsbpn[2].hEventNotify = pSoundBuffer->hEvent[2];
dsbpn[2].dwOffset = BufferSize * 2;
if (pSoundBuffer->lpdsn->SetNotificationPositions(3, dsbpn))
{
DestroySoundBuffer(pSoundBuffer);
return true;
}
InitializeCriticalSection(&pSoundBuffer->csSound);
if (!CreateThread(NULL, NULL, SoundThreadProc, pSoundBuffer, NULL, &dw))
{
DestroySoundBuffer(pSoundBuffer);
return true;
}
return false;
}
示例13: create_buffers
/**
* @brief Creates the primary buffer and the secondary buffers.
* @param[in] p_direct_sound_8 pointer to the IDirectSound8 interface. This interface is a factory for creating both the primary buffer and the secondary buffers.
* @param[out] pp_primary_buffer pointer to the memory location which will be written with the primary buffer interface pointer.
* @param[out] pp_secondary_buffer pointer to the memory location which will be written with the secondary buffer interface pointer.
* @param[in] p_wfe pointer to the WAVEFORMATEX structure. This parameter defines the buffer format (number of samples per second, how many bytes per sample and so on).
* @param[in] single_buffer_size size of the single play buffer
* @return
*/
static HRESULT create_buffers(LPDIRECTSOUND8 p_ds,
WAVEFORMATEX * p_wfex,
size_t number_of_chunks,
size_t single_buffer_size,
LPDIRECTSOUNDBUFFER * pp_primary_sound_buffer,
LPDIRECTSOUNDBUFFER8 * pp_secondary_sound_buffer)
{
HRESULT hr;
DSBUFFERDESC bufferDesc;
LPDIRECTSOUNDBUFFER lpDSB = NULL;
if (NULL != *pp_primary_sound_buffer || NULL != *pp_secondary_sound_buffer)
{
debug_outputln("%s %4.4u : %p %p", __FILE__, __LINE__, *pp_primary_sound_buffer, *pp_secondary_sound_buffer);
return E_INVALIDARG;
}
ZeroMemory(&bufferDesc, sizeof(bufferDesc));
bufferDesc.dwSize = sizeof(bufferDesc);
bufferDesc.dwFlags = DSBCAPS_PRIMARYBUFFER;
bufferDesc.guid3DAlgorithm = DS3DALG_DEFAULT;
/* All others must be null for primary buffer */
hr = p_ds->CreateSoundBuffer(&bufferDesc, pp_primary_sound_buffer, NULL);
if (FAILED(hr))
{
debug_outputln("%s %4.4u : %x", __FILE__, __LINE__, hr);
goto error;
}
hr = (*pp_primary_sound_buffer)->SetFormat(p_wfex);
if (FAILED(hr))
{
debug_outputln("%s %4.4u : %8.8x", __FILE__, __LINE__, hr);
goto error;
}
get_buffer_caps("Primary: ", *pp_primary_sound_buffer);
/* Secondary buffer */
bufferDesc.dwFlags = DSBCAPS_CTRLVOLUME /* The buffer has volume control capability. */
| DSBCAPS_CTRLPAN /* The buffer has pan control capability. */
| DSBCAPS_CTRLFREQUENCY /* The buffer has frequency control capability. */
| DSBCAPS_GLOBALFOCUS /* With this flag set, an application using DirectSound can continue to play its buffers if the user switches focus to another application, even if the new application uses DirectSound. */
| DSBCAPS_CTRLPOSITIONNOTIFY; /* The buffer has position notification capability. */
/* double buffering - one buffer being played, whereas the other is being filled in */
bufferDesc.dwBufferBytes = number_of_chunks * single_buffer_size;
bufferDesc.lpwfxFormat = p_wfex;
hr = p_ds->CreateSoundBuffer(&bufferDesc, &lpDSB, NULL);
if (FAILED(hr))
{
debug_outputln("%s %4.4u : %x", __FILE__, __LINE__, hr);
goto error;
}
hr = lpDSB->QueryInterface(IID_IDirectSoundBuffer8, (LPVOID*)pp_secondary_sound_buffer);
if (FAILED(hr))
{
debug_outputln("%s %4.4u : %x", __FILE__, __LINE__, hr);
goto error;
}
get_buffer_caps("Secondary: ", *pp_secondary_sound_buffer);
if (NULL != lpDSB)
{
lpDSB->Release();
lpDSB = NULL;
}
return hr;
error:
if (NULL != *pp_secondary_sound_buffer)
{
(*pp_secondary_sound_buffer)->Release();
*pp_secondary_sound_buffer = NULL;
}
if (NULL != *pp_primary_sound_buffer)
{
(*pp_primary_sound_buffer)->Release();
*pp_primary_sound_buffer = NULL;
}
if (NULL != lpDSB)
{
lpDSB->Release();
lpDSB = NULL;
}
return hr;
}
示例14: run
void DXAudioOutput::run() {
HRESULT hr;
DSBUFFERDESC dsbdesc;
WAVEFORMATEXTENSIBLE wfx;
WAVEFORMATEXTENSIBLE wfxSet;
int ns = 0;
unsigned int chanmasks[32];
LPDIRECTSOUND8 pDS = NULL;
LPDIRECTSOUNDBUFFER pDSBPrimary = NULL;
LPDIRECTSOUNDBUFFER pDSBOutput = NULL;
LPDIRECTSOUNDNOTIFY8 pDSNotify = NULL;
DWORD dwBufferSize;
DWORD dwLastWritePos;
DWORD dwLastPlayPos;
DWORD dwTotalPlayPos;
int iLastwriteblock;
LPVOID aptr1, aptr2;
DWORD nbytes1, nbytes2;
int playblock;
int nowriteblock;
DWORD dwPlayPosition, dwWritePosition;
unsigned int iByteSize;
bool bOk;
DWORD dwSpeakerConfig;
bool failed = false;
bOk = false;
DWORD dwMask = 0;
bool bHead = false;
ZeroMemory(&dsbdesc, sizeof(DSBUFFERDESC));
dsbdesc.dwSize = sizeof(DSBUFFERDESC);
dsbdesc.dwFlags = DSBCAPS_PRIMARYBUFFER;
if (! g.s.qbaDXOutput.isEmpty()) {
LPGUID lpguid = reinterpret_cast<LPGUID>(g.s.qbaDXOutput.data());
if (FAILED(hr = DirectSoundCreate8(lpguid, &pDS, NULL))) {
failed = true;
}
}
if (! pDS && FAILED(hr = DirectSoundCreate8(&DSDEVID_DefaultVoicePlayback, &pDS, NULL))) {
qWarning("DXAudioOutput: DirectSoundCreate failed: hr=0x%08lx", hr);
goto cleanup;
} else if (FAILED(hr = pDS->SetCooperativeLevel(g.mw->winId(), DSSCL_PRIORITY))) {
qWarning("DXAudioOutput: SetCooperativeLevel failed: hr=0x%08lx", hr);
goto cleanup;
} else if (FAILED(hr = pDS->CreateSoundBuffer(&dsbdesc, &pDSBPrimary, NULL))) {
qWarning("DXAudioOutput: CreateSoundBuffer (Primary) failed: hr=0x%08lx", hr);
goto cleanup;
}
pDS->GetSpeakerConfig(&dwSpeakerConfig);
switch (DSSPEAKER_CONFIG(dwSpeakerConfig)) {
case DSSPEAKER_HEADPHONE:
dwMask = KSAUDIO_SPEAKER_STEREO;
bHead = true;
break;
case DSSPEAKER_MONO:
dwMask = KSAUDIO_SPEAKER_MONO;
break;
case DSSPEAKER_QUAD:
dwMask = KSAUDIO_SPEAKER_QUAD;
break;
case DSSPEAKER_STEREO:
dwMask = KSAUDIO_SPEAKER_STEREO;
break;
case DSSPEAKER_SURROUND:
dwMask = KSAUDIO_SPEAKER_SURROUND;
break;
case DSSPEAKER_5POINT1:
dwMask = KSAUDIO_SPEAKER_5POINT1;
break;
case DSSPEAKER_7POINT1:
dwMask = KSAUDIO_SPEAKER_7POINT1;
break;
case DSSPEAKER_7POINT1_SURROUND:
dwMask = KSAUDIO_SPEAKER_7POINT1_SURROUND;
break;
case DSSPEAKER_5POINT1_SURROUND:
dwMask = KSAUDIO_SPEAKER_5POINT1_SURROUND;
break;
default:
dwMask = 0;
break;
}
if (! g.s.doPositionalAudio())
dwMask = KSAUDIO_SPEAKER_MONO;
for (int i=0;i<32;i++) {
if (dwMask & (1 << i)) {
//.........这里部分代码省略.........
示例15: init
bool DirectSound::init(long sampleRate)
{
HRESULT hr;
DWORD freq;
DSBUFFERDESC dsbdesc;
int i;
hr = CoCreateInstance( CLSID_DirectSound8, NULL, CLSCTX_INPROC_SERVER, IID_IDirectSound8, (LPVOID *)&pDirectSound );
if( hr != S_OK ) {
systemMessage( IDS_CANNOT_CREATE_DIRECTSOUND, NULL, hr );
return false;
}
pDirectSound->Initialize( &DSDEVID_DefaultPlayback );
if( hr != DS_OK ) {
systemMessage( IDS_CANNOT_CREATE_DIRECTSOUND, NULL, hr );
return false;
}
if( FAILED( hr = pDirectSound->SetCooperativeLevel( theApp.m_pMainWnd->GetSafeHwnd(), DSSCL_EXCLUSIVE ) ) ) {
systemMessage( IDS_CANNOT_SETCOOPERATIVELEVEL, _T("Cannot SetCooperativeLevel %08x"), hr );
return false;
}
// Create primary sound buffer
ZeroMemory( &dsbdesc, sizeof(DSBUFFERDESC) );
dsbdesc.dwSize = sizeof(DSBUFFERDESC);
dsbdesc.dwFlags = DSBCAPS_PRIMARYBUFFER;
if( dsoundDisableHardwareAcceleration ) {
dsbdesc.dwFlags |= DSBCAPS_LOCSOFTWARE;
}
if( FAILED( hr = pDirectSound->CreateSoundBuffer( &dsbdesc, &dsbPrimary, NULL ) ) ) {
systemMessage(IDS_CANNOT_CREATESOUNDBUFFER, _T("Cannot CreateSoundBuffer %08x"), hr);
return false;
}
freq = sampleRate;
// calculate the number of samples per frame first
// then multiply it with the size of a sample frame (16 bit * stereo)
soundBufferLen = ( freq / 60 ) * 4;
soundBufferTotalLen = soundBufferLen * 10;
soundNextPosition = 0;
ZeroMemory( &wfx, sizeof(WAVEFORMATEX) );
wfx.wFormatTag = WAVE_FORMAT_PCM;
wfx.nChannels = 2;
wfx.nSamplesPerSec = freq;
wfx.wBitsPerSample = 16;
wfx.nBlockAlign = wfx.nChannels * wfx.wBitsPerSample / 8;
wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
if( FAILED( hr = dsbPrimary->SetFormat( &wfx ) ) ) {
systemMessage( IDS_CANNOT_SETFORMAT_PRIMARY, _T("CreateSoundBuffer(primary) failed %08x"), hr );
return false;
}
// Create secondary sound buffer
ZeroMemory( &dsbdesc, sizeof(DSBUFFERDESC) );
dsbdesc.dwSize = sizeof(DSBUFFERDESC);
dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_CTRLPOSITIONNOTIFY | DSBCAPS_GLOBALFOCUS;
if( dsoundDisableHardwareAcceleration ) {
dsbdesc.dwFlags |= DSBCAPS_LOCSOFTWARE;
}
dsbdesc.dwBufferBytes = soundBufferTotalLen;
dsbdesc.lpwfxFormat = &wfx;
if( FAILED( hr = pDirectSound->CreateSoundBuffer( &dsbdesc, &dsbSecondary, NULL ) ) ) {
systemMessage( IDS_CANNOT_CREATESOUNDBUFFER, _T("CreateSoundBuffer(secondary) failed %08x"), hr );
return false;
}
if( FAILED( hr = dsbSecondary->SetCurrentPosition( 0 ) ) ) {
systemMessage( 0, _T("dsbSecondary->SetCurrentPosition failed %08x"), hr );
return false;
}
if( SUCCEEDED( hr = dsbSecondary->QueryInterface( IID_IDirectSoundNotify8, (LPVOID*)&dsbNotify ) ) ) {
dsbEvent = CreateEvent( NULL, FALSE, FALSE, NULL );
DSBPOSITIONNOTIFY notify[10];
for( i = 0; i < 10; i++ ) {
notify[i].dwOffset = i * soundBufferLen;
notify[i].hEventNotify = dsbEvent;
}
if( FAILED( dsbNotify->SetNotificationPositions( 10, notify ) ) ) {
dsbNotify->Release();
dsbNotify = NULL;
CloseHandle(dsbEvent);
dsbEvent = NULL;
}
}
// Play primary buffer
if( FAILED( hr = dsbPrimary->Play( 0, 0, DSBPLAY_LOOPING ) ) ) {
systemMessage( IDS_CANNOT_PLAY_PRIMARY, _T("Cannot Play primary %08x"), hr );
//.........这里部分代码省略.........