扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:智慧的鱼 来源:天极开发 2007年10月16日
关键字:
LPDIRECTSOUND8 g_pDsd = NULL; //Directsound 对象指针 LPDIRECTSOUNDBUFFER g_pDSBuffer = NULL; //辅助缓冲区指针 LPDIRECTSOUND3DBUFFER g_pDS3DBuffer = NULL; // 3D 声源对象指针 LPDIRECTSOUND3DLISTENER g_pDSListener = NULL; // 3D 听者对象指针 DS3DBUFFER g_dsBufferParams; // 3D buffer properties DS3DLISTENER g_dsListenerParams; // Listener properties CWaveFile *g_pWaveFile= NULL; //读写wave文件用到的一个类对象 BOOL g_bPlaying = FALSE; //是否正在播放 |
//初始化Dsound HRESULT hr; if(FAILED(hr = DirectSoundCreate8(NULL,&g_pDsd,NULL))) return FALSE; if(FAILED(hr = g_pDsd->SetCooperativeLevel(m_hWnd,DSSCL_PRIORITY))) return FALSE; //初始化Directsound 的主缓冲区,并设置格式 LPDIRECTSOUNDBUFFER pDSBPrimary = NULL; DSBUFFERDESC dsbdesc ; ZeroMemory(&dsbdesc,sizeof(DSBUFFERDESC)); dsbdesc.dwSize = sizeof(DSBUFFERDESC); dsbdesc.dwFlags = DSBCAPS_CTRL3D | DSBCAPS_PRIMARYBUFFER;//一定不要忘记创建主缓冲区的时候要设置DSBCAPS_CTRL3D标志。只有指定了这个标志,后面你才能从这个辅助缓冲区中请求到3D的buffer指针。 if(FAILED(hr = g_pDsd->CreateSoundBuffer(&dsbdesc,&pDSBPrimary ,NULL))) return FALSE; //获取3D 模拟世界中的听者指针。 if( FAILED( hr = pDSBPrimary->QueryInterface( IID_IDirectSound3DListener, (VOID**)&g_pDSListener ) ) ) return FALSE; WAVEFORMATEX wfx; ZeroMemory( &wfx, sizeof(WAVEFORMATEX) ); wfx.wFormatTag = (WORD) WAVE_FORMAT_PCM; wfx.nChannels = WAVECHANNEL ; wfx.nSamplesPerSec = WAVESAMPLEPERSEC ; wfx.wBitsPerSample = WAVEBITSPERSAMPLE ; wfx.nBlockAlign = (WORD) (wfx.wBitsPerSample / 8 * wfx.nChannels); wfx.nAvgBytesPerSec = (DWORD) (wfx.nSamplesPerSec * wfx.nBlockAlign); if( FAILED( hr = pDSBPrimary->SetFormat(&wfx) ) ) //设置缓冲区的音频格式。 return FALSE; |
DSBUFFERDESC dsbd; ZeroMemory( &dsbd, sizeof(DSBUFFERDESC) ); dsbd.dwSize = sizeof(DSBUFFERDESC); dsbd.dwFlags = DSBCAPS_CTRL3D| DSBCAPS_GLOBALFOCUS | DSBCAPS_CTRLPOSITIONNOTIFY |DSBCAPS_GETCURRENTPOSITION2; //看看创建辅助缓冲区的buffer时设置的标志,3D的标志自然是需要的,还有一个标志需要注意,DSBCAPS_CTRLPOSITIONNOTIFY,如果你采用的流(stream)buffer的话,就需要边播放,边向buffer中填充数据,就需要设置这个标志,这样,在directsound播放到指定位置时,就会触发事件。 //dsbd.dwBufferBytes =MAX_AUDIO_BUF * BUFFERNOTIFYSIZE ;//如果采用流buffer,可以设置适当的buffer大小。 dsbd.dwBufferBytes =g_pWaveFile->GetSize(); // 如果采用静态buffer,那么buffer的大小就是文件的大小了。 dsbd.guid3DAlgorithm = guid3DAlgorithm; dsbd.lpwfxFormat = g_pWaveFile->m_pwfx; if(FAILED(hr = g_pDsd->CreateSoundBuffer(&dsbd,&g_pDSBuffer,NULL))) return ; //通过辅助缓冲区,来获取3D buffer的指针。 if(FAILED(hr = g_pDSBuffer->QueryInterface(IID_IDirectSound3DBuffer, (VOID**)&g_pDS3DBuffer ))) return ; g_dsBufferParams.dwSize = sizeof(DS3DBUFFER); g_pDS3DBuffer->GetAllParameters( &g_dsBufferParams ); //设置3Dbuffer的属性。 g_dsBufferParams.dwMode = DS3DMODE_HEADRELATIVE; g_pDS3DBuffer->SetAllParameters( &g_dsBufferParams, DS3D_IMMEDIATE ); |
FLOAT fDopplerFactor; FLOAT fRolloffFactor; FLOAT fMinDistance; FLOAT fMaxDistance; CSliderCtrl *pDopplerSlider = (CSliderCtrl*)GetDlgItem(IDC_DOPPLER_SLIDER); fDopplerFactor = pDopplerSlider->GetPos(); CSliderCtrl *pRolloffSlider= (CSliderCtrl*)GetDlgItem(IDC_ROLLOFF_SLIDER); fRolloffFactor = pRolloffSlider->GetPos(); CSliderCtrl *pMinDistSlider = (CSliderCtrl*)GetDlgItem(IDC_MINDISTANCE_SLIDER ); fMinDistance = pMinDistSlider->GetPos(); CSliderCtrl *pMaxDistSlider = (CSliderCtrl*)GetDlgItem(IDC_MAXDISTANCE_SLIDER ); fMaxDistance = pMaxDistSlider->GetPos(); g_dsListenerParams.flDopplerFactor = fDopplerFactor; g_dsListenerParams.flRolloffFactor = fRolloffFactor; if( g_pDSListener ) { g_pDSListener->SetAllParameters( &g_dsListenerParams, DS3D_DEFERRED ); g_pDSListener->CommitDeferredSettings(); } g_dsBufferParams.flMinDistance = fMinDistance; g_dsBufferParams.flMaxDistance = fMaxDistance; if( g_pDS3DBuffer ) g_pDS3DBuffer->SetAllParameters( &g_dsBufferParams, DS3D_DEFERRED ); |
DWORD res; LPVOID lplockbuf; DWORD len; DWORD dwWrite; g_pDSBuffer->Lock(0,0,&lplockbuf,&len,NULL,NULL,DSBLOCK_ENTIREBUFFER); g_pWaveFile->Read((BYTE*)lplockbuf,len,&dwWrite); g_pDSBuffer->Unlock(lplockbuf,len,NULL,0); g_pDSBuffer->SetCurrentPosition(0); g_pDSBuffer->Play(0,0,DSBPLAY_LOOPING); |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者