bool FVoiceCaptureDeviceWindows::Init() { HRESULT hr = DirectSoundCreate8(NULL, &DirectSound, NULL); if (FAILED(hr)) { UE_LOG(LogVoiceCapture, Warning, TEXT("Failed to init DirectSound %d"), hr); return false; } bool bHmdAvailable = IModularFeatures::Get().IsModularFeatureAvailable(IHeadMountedDisplayModule::GetModularFeatureName()); HmdVoiceCaptureDeviceIndex = bHmdAvailable ? CVarHmdDirectSoundVoiceCaptureDeviceIndex.GetValueOnGameThread() : -1; VoiceCaptureDeviceCount = 0; VoiceCaptureDeviceGuid = DSDEVID_DefaultVoiceCapture; hr = DirectSoundCaptureEnumerate((LPDSENUMCALLBACK)CaptureDeviceCallback, this); if (FAILED(hr)) { UE_LOG(LogVoiceCapture, Warning, TEXT("Failed to enumerate capture devices %d"), hr); return false; } bool bDuckingOptOut = false; if (GConfig) { if (!GConfig->GetBool(TEXT("Voice"), TEXT("bDuckingOptOut"), bDuckingOptOut, GEngineIni)) { bDuckingOptOut = false; } } FAudioDuckingWindows::UpdateAudioDucking(bDuckingOptOut); bInitialized = true; return true; }
int SoundManager::Init() { if(active==true) return 1; FONLINE_LOG("SoundManager Init\n"); if (!fm.Init(opt_masterpath.c_str(), opt_critterpath.c_str(), opt_fopath.c_str())) return 0;; if(DirectSoundCreate8(0,&lpDS,0)!=DS_OK) { FONLINE_LOG("Неудалось создать устройство!\n"); return 0; } if(lpDS->SetCooperativeLevel(GetForegroundWindow(),DSSCL_NORMAL)!=DS_OK) { FONLINE_LOG("Неудалось установить уровень кооперации!\n"); return 0; } cur_snd=1; active=true; FONLINE_LOG("SoundManager Init OK\n"); return 1; }
/////////////////////////////////////////////////////////////////////////////// // Function : "InitDirectSound" // // Last Modified : 4/05/2004 // // Input : hWnd - Main Window Handle. // nPrimaryBufferFormat - PB_LOQUAL for Normal, 11khz (default) // PB_MEDQUAL for 22khz // PB_HIQUAL for 44khz // // Return : true, if successful. // // Purpose : Initialize DirectSound and create a Primar Buffer, if needed. /////////////////////////////////////////////////////////////////////////////// bool CSGD_DirectSound::InitDirectSound(HWND hWnd, int nPrimaryBufferFormat) { // Make sure hWnd is valid. if (!hWnd) return false; // Store the Main Window Handle. m_hWnd = hWnd; // Create the main DirectSound Object. if (FAILED(DirectSoundCreate8(NULL, &m_dsObject, NULL))) DSERRBOX("Failed to Create DirectSound Object"); // Set the Cooperative Level for DirectSound. if (nPrimaryBufferFormat != PB_MEDQUAL && nPrimaryBufferFormat != PB_HIQUAL) { // The Default cooperative level. if (FAILED(m_dsObject->SetCooperativeLevel(m_hWnd, DSSCL_NORMAL))) DSERRBOX("Failed to SetCooperativeLevel - NORMAL"); } else { // Attempt to set the primary buffer format // we're doing this because DirectSound likes to convert our samples to its // default low qual format, causing lots of white noise on our samples if (FAILED(m_dsObject->SetCooperativeLevel(m_hWnd, DSSCL_PRIORITY))) DSERRBOX("Failed to SetCooperativeLevel - PRIORITY"); // Make a wave format structure for our primary sound buffer. static WAVEFORMATEX waveFormatEx; memset(&waveFormatEx, 0, sizeof(waveFormatEx)); waveFormatEx.wFormatTag = WAVE_FORMAT_PCM; waveFormatEx.nChannels = 2; // Give us 22050 or 44100 (nPrimaryBufferFormat == PB_MEDQUAL) ? waveFormatEx.nSamplesPerSec = 22050 : waveFormatEx.nSamplesPerSec = 44100; waveFormatEx.nBlockAlign = 4; waveFormatEx.nAvgBytesPerSec = waveFormatEx.nSamplesPerSec * waveFormatEx.nBlockAlign; waveFormatEx.wBitsPerSample = 16; // Setup the DSBUFFERDESC struct. DSBUFFERDESC dsbd; memset(&dsbd, 0, sizeof(dsbd)); dsbd.dwSize = sizeof(dsbd); // Will be making a Primary Buffer. dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER; dsbd.dwBufferBytes = 0; dsbd.lpwfxFormat = NULL; // Must be NULL for primary buffers. // Make the primary sound buffer if (FAILED(m_dsObject->CreateSoundBuffer(&dsbd, &m_dsPrimaryBuffer, NULL))) DSERRBOX("Couldn't Create the Primary Sound Buffer"); // Set the desired format for the buffer. if (FAILED(m_dsPrimaryBuffer->SetFormat(&waveFormatEx))) DSERRBOX("Couldn't Set the Primary Sound Buffer Format"); } // Return success. return true; }
BOOL getCurrentChannelConfig() { IDirectSound8* ds; DWORD speakerConfig; LPGUID guid = NULL; DirectSoundEnumerate((LPDSENUMCALLBACK)DSEnumProc, (VOID*)&guid); if (DirectSoundCreate8(guid, &ds, NULL) != S_OK) { return FALSE; } ds->Initialize(NULL); if (ds->GetSpeakerConfig(&speakerConfig) != S_OK) { PrintLastError("GetSpeakerConfig()"); return FALSE; } if (ds) { ds->Release(); } if (guid) { LocalFree(guid); } switch (DSSPEAKER_CONFIG(speakerConfig)) { case DSSPEAKER_STEREO: currentChannelCount = 2; return TRUE; case DSSPEAKER_QUAD: currentChannelCount = 4; return TRUE; case DSSPEAKER_5POINT1: currentChannelCount = 6; return TRUE; case DSSPEAKER_7POINT1: currentChannelCount = 8; return TRUE; } return FALSE; }
bool cSound::iniDirectSound(HWND hWnd) { for (int i = 0; i < 500; i++) stopped[i] = false; HRESULT result; DSBUFFERDESC bufferDesc; WAVEFORMATEX wavFormat; result=DirectSoundCreate8(NULL, &intSound, NULL); if (FAILED(result))return false; result=intSound->SetCooperativeLevel(hWnd, DSSCL_PRIORITY); if (FAILED(result))return false; bufferDesc.dwSize = sizeof(DSBUFFERDESC); bufferDesc.dwFlags = DSBCAPS_PRIMARYBUFFER | DSBCAPS_CTRLVOLUME; //For primary bufferDesc.dwBufferBytes = 0; //For primary 0 bufferDesc.dwReserved = 0;//For primary 0 bufferDesc.lpwfxFormat = NULL;//For Primary NULL bufferDesc.guid3DAlgorithm = GUID_NULL;//For Primary without 3d functions result=intSound->CreateSoundBuffer(&bufferDesc, &primaryBuffer, NULL); if (FAILED(result))return false; wavFormat.wFormatTag = WAVE_FORMAT_PCM; wavFormat.nSamplesPerSec = 44100; wavFormat.wBitsPerSample = 16; wavFormat.nChannels = 2; wavFormat.nBlockAlign = (wavFormat.wBitsPerSample/8)*wavFormat.nChannels; wavFormat.nAvgBytesPerSec = wavFormat.nSamplesPerSec*wavFormat.nBlockAlign; wavFormat.cbSize = 0; result=primaryBuffer->SetFormat(&wavFormat); if (FAILED(result))return false; return true; }
ClSoundDS::ClSoundDS(HWND hWnd,ClReadFile *readFile,BOOL b3DSound) { HRESULT hr; DSBUFFERDESC dsbdDesc; WAVEFORMATEX wfx; ZeroMemory(this,sizeof(ClSoundDS)); lpReadFile = readFile; hr = DirectSoundCreate8(NULL,&lpDSound,NULL); if(hr!=DS_OK)return; hr = lpDSound->SetCooperativeLevel(hWnd, DSSCL_EXCLUSIVE); if(hr!=DS_OK)return; ZeroMemory(&dsbdDesc,sizeof(DSBUFFERDESC)); dsbdDesc.dwSize = sizeof(DSBUFFERDESC); dsbdDesc.dwFlags = DSBCAPS_PRIMARYBUFFER; dsbdDesc.dwFlags |= DSBCAPS_LOCSOFTWARE; if(b3DSound) dsbdDesc.dwFlags |= DSBCAPS_CTRL3D; dsbdDesc.lpwfxFormat = NULL; hr = lpDSound->CreateSoundBuffer(&dsbdDesc, &lpPrimaryBuffer, NULL); if(DS_OK!=hr) return; wfx.wFormatTag = WAVE_FORMAT_PCM; wfx.nChannels = 2; wfx.nSamplesPerSec = 44100; wfx.wBitsPerSample = 16; wfx.nBlockAlign = wfx.nChannels * (wfx.wBitsPerSample/8); wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign; wfx.cbSize = 0; lpPrimaryBuffer->SetFormat(&wfx); lpDSound->SetCooperativeLevel(hWnd,DSSCL_NORMAL); CreateDummyBuffer(); Enable = TRUE; } // ClSoundDS::ClSoundDS
//----------------------------------------------------------------------------- // Name: CSoundManager::Initialize() // Desc: Initializes the IDirectSound object and also sets the primary buffer // format. This function must be called before any others. //----------------------------------------------------------------------------- HRESULT CSoundManager::Initialize( HWND hWnd, DWORD dwCoopLevel, DWORD dwPrimaryChannels, DWORD dwPrimaryFreq, DWORD dwPrimaryBitRate ) { HRESULT hr; SAFE_RELEASE( m_pDS ); // Create IDirectSound using the primary sound device if( FAILED( hr = DirectSoundCreate8( NULL, &m_pDS, NULL ) ) ) return DXTRACE_ERR( TEXT("DirectSoundCreate8"), hr ); // Set DirectSound coop level if( FAILED( hr = m_pDS->SetCooperativeLevel( hWnd, dwCoopLevel ) ) ) return DXTRACE_ERR( TEXT("SetCooperativeLevel"), hr ); // Set primary buffer format if( FAILED( hr = SetPrimaryBufferFormat( dwPrimaryChannels, dwPrimaryFreq, dwPrimaryBitRate ) ) ) return DXTRACE_ERR( TEXT("SetPrimaryBufferFormat"), hr ); m_dwPrimaryChannels = dwPrimaryChannels; m_dwPrimaryFreq = dwPrimaryFreq; m_dwPrimaryBitRate = dwPrimaryBitRate; return S_OK; }
sBool Init(sInt rate, sInt channels) { #ifndef KSDATAFORMAT_SUBTYPE_PCM const static GUID KSDATAFORMAT_SUBTYPE_PCM = {0x00000001,0x0000,0x0010,{0x80,0x00,0x00,0xaa,0x00,0x38,0x9b,0x71}}; #endif if FAILED(DirectSoundCreate8(0,&DS,0)) return sFALSE; if FAILED(DS->SetCooperativeLevel(sHWND,DSSCL_PRIORITY)) return sFALSE; // this is our output format. WAVEFORMATPCMEX wf; wf.Format.cbSize=22; wf.Format.wFormatTag=WAVE_FORMAT_EXTENSIBLE; wf.Format.nChannels=channels; wf.Format.wBitsPerSample=16; wf.Format.nSamplesPerSec=rate; wf.Format.nBlockAlign=channels*((wf.Format.wBitsPerSample+7)/8); wf.Format.nAvgBytesPerSec=wf.Format.nBlockAlign*rate; wf.dwChannelMask=(1<<channels)-1; // very space opera wf.Samples.wValidBitsPerSample=16; wf.SubFormat=KSDATAFORMAT_SUBTYPE_PCM; // set up some stuff while we're at it BufferSize = sAlign(rate*LATENCY_MS/1000,16); BytesPerSample= wf.Format.nBlockAlign; RenderBuffer = new sF32[channels*BufferSize]; LastPlayPos=0; Channels=channels; Rate=rate; // create primary buffer... DSBUFFERDESC dsbd; sSetMem(&dsbd,0,sizeof(dsbd)); dsbd.dwSize = sizeof(dsbd); dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER; dsbd.dwBufferBytes = 0; dsbd.lpwfxFormat = 0; if FAILED(DS->CreateSoundBuffer(&dsbd,&PrimBuffer,0)) return sFALSE; // set primary buffer format to prepare DirectSound for what's coming (may fail) PrimBuffer->SetFormat((WAVEFORMATEX*)&wf); // create secondary buffer sSetMem(&dsbd,0,sizeof(dsbd)); dsbd.dwSize = sizeof(dsbd); dsbd.dwFlags = DSBCAPS_GLOBALFOCUS|DSBCAPS_STICKYFOCUS|DSBCAPS_GETCURRENTPOSITION2; dsbd.dwBufferBytes = BufferSize*BytesPerSample; dsbd.lpwfxFormat = (WAVEFORMATEX*)&wf; if FAILED(DS->CreateSoundBuffer(&dsbd,&Buffer,0)) return sFALSE; // now that everything seemed to work, let's switch over to the rendering thread Renderers.HintSize(8); Thread = new sThread(ThreadProxy,1,0,this); return sTRUE; }
bool GlobalSoundManager::create(HWND hMainWindow) { if (APP_ERROR(FAILED(DirectSoundCreate8(NULL, &direct_sound_, NULL)) || FAILED(direct_sound_->SetCooperativeLevel(hMainWindow, DSSCL_NORMAL))) ("Couldn't initialize DirectSound")) return false; return true; }
// //////////////////////////////////////////////////////////////////// // // //////////////////////////////////////////////////////////////////// bool DirectSound8Audio::VInitialize() { if(Audio::IsInitialized()) { return (true); } Audio::SetInitialized(false); Release(m_pDS); HRESULT hr; // Create IDirectSound using the primary sound device if(FAILED(hr = DirectSoundCreate8(NULL, &m_pDS, NULL))) { GF_LOG_TRACE_ERR("DirectSound8Audio::VInitialize()", "Failed to initialized the DirectSound interface"); return (false); } if(FAILED(hr = m_pDS->SetCooperativeLevel(m_hWnd, DSSCL_PRIORITY))) { GF_LOG_TRACE_ERR("DirectSound8Audio::VInitialize()", "Failed to set the coop level of the DirectSound interface"); return (false); } if(FAILED(hr = SetPrimaryBufferFormat(8, 44100, 16))) { GF_LOG_TRACE_ERR("DirectSound8Audio::VInitialize()", "Failed to set the primary buffer format of the DirectSound interface"); return (false); } Audio::SetInitialized(true); m_AllSamples.clear(); return (true); }
CMpcAudioRenderer::CMpcAudioRenderer(LPUNKNOWN punk, HRESULT *phr) : CBaseRenderer(__uuidof(this), NAME("MPC - Audio Renderer"), punk, phr) , m_pDSBuffer (NULL ) , m_pSoundTouch (NULL ) , m_pDS (NULL ) , m_dwDSWriteOff (0 ) , m_nDSBufSize (0 ) , m_dRate (1.0 ) , m_pReferenceClock (NULL ) , m_pWaveFileFormat (NULL ) , pAudioClient (NULL ) , pRenderClient (NULL ) , useWASAPI (true ) , bufferFrameCount (0 ) , hnsRequestedDuration (0 ) , hTask (NULL ) { if (useWASAPI) *phr = GetDefaultAudioDevice(&pAudioClient); else { m_pSoundTouch = new soundtouch::SoundTouch(); *phr = DirectSoundCreate8 (NULL, &m_pDS, NULL); } }
HRESULT FrkSound::InitializeSoundClass(HWND windowsHandler) { m_hWindowsHandler = windowsHandler; HRESULT result; // Thu tao thiet bi DS result = DirectSoundCreate8(0, &m_hAudioHandler, 0); result = result | m_hAudioHandler->SetCooperativeLevel(m_hWindowsHandler, DSSCL_PRIORITY); ZeroMemory(&m_hBufferFormat, sizeof(WAVEFORMATEX)); ZeroMemory(&m_hBufferDescription, sizeof(DSBUFFERDESC)); m_hBufferFormat.wFormatTag = AUDIO_FORMAT_TAG; m_hBufferFormat.nChannels = AUDIO_NUM_OF_CHANNEL; m_hBufferFormat.nSamplesPerSec = AUDIO_SAMPLE_SPEED; m_hBufferFormat.wBitsPerSample = AUDIO_BITS_PER_SAMPLE; m_hBufferFormat.nBlockAlign = AUDIO_BLOCK_ALIGN(m_hBufferFormat.wBitsPerSample, m_hBufferFormat.nChannels); m_hBufferFormat.nAvgBytesPerSec = AUDIO_AVERAGE_BPS(m_hBufferFormat.nSamplesPerSec, m_hBufferFormat.nBlockAlign); m_hBufferDescription.dwFlags = AUDIO_FLAGS; m_hBufferDescription.guid3DAlgorithm = AUDIO_GUID; m_hBufferDescription.dwSize = sizeof(DSBUFFERDESC); return result; }
bool WindowsSound::Init() { HRESULT hr; //create the sound device hr = DirectSoundCreate8(NULL, &(this->directSoundDevice), NULL); if(FAILED(hr)) return false; //set cooperative level to PRIORITY (=high) HWND hwndC = GetConsoleWindow() ; hr = this->directSoundDevice->SetCooperativeLevel(hwndC, DSSCL_PRIORITY); if(FAILED(hr)) return false; //create an output wave format WAVEFORMATEX waveFormat; memset(&waveFormat, 0, sizeof(WAVEFORMATEX)); waveFormat.wFormatTag = WAVE_FORMAT_PCM; waveFormat.nChannels = CHANNELS_NUMBER; waveFormat.nSamplesPerSec = SAMPLE_RATE; waveFormat.wBitsPerSample = BITS_PER_SAMPLE; waveFormat.nBlockAlign = BLOCK_ALIGN; waveFormat.nAvgBytesPerSec = AVERAGE_BYTES_PER_SECOND; //msdn recommendation for PCM format //Primary sound buffer DSBUFFERDESC bufferDesc; memset(&bufferDesc, 0, sizeof(DSBUFFERDESC)); bufferDesc.dwSize = sizeof(DSBUFFERDESC); bufferDesc.dwFlags = DSBCAPS_PRIMARYBUFFER|DSBCAPS_STICKYFOCUS; bufferDesc.dwBufferBytes = 0; bufferDesc.lpwfxFormat = NULL; LPDIRECTSOUNDBUFFER primaryDirectSoundBuffer; hr = this->directSoundDevice->CreateSoundBuffer(&bufferDesc, &primaryDirectSoundBuffer, NULL); if(FAILED(hr)) return false; hr = primaryDirectSoundBuffer->SetFormat(&waveFormat); if(FAILED(hr)) return false; //set up a buffer description DSBUFFERDESC soundBufferDescription; memset(&soundBufferDescription, 0, sizeof(DSBUFFERDESC)); soundBufferDescription.dwSize = sizeof(DSBUFFERDESC); //DSBCAPS_STICKYFOCUS: allows to play sound even if we don't have have focus. //DSBCAPS_GETCURRENTPOSITION2: tells DirectSound we'll use the GetPosition method later on that sound buffer. soundBufferDescription.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_STICKYFOCUS; soundBufferDescription.dwBufferBytes = BUFFER_LENGTH_IN_BYTES; soundBufferDescription.lpwfxFormat = &waveFormat; //create the sound buffer hr = this->directSoundDevice->CreateSoundBuffer(&soundBufferDescription, &(this->directSoundBuffer), NULL); if(FAILED(hr)) return false; return true; }
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; }
bool DirectSoundDriver::Initialize() { WriteTrace(TraceAudioDriver, TraceDebug, "Start"); if (!SoundDriverBase::Initialize()) { WriteTrace(TraceAudioDriver, TraceDebug, "Done (res: false)"); return false; } CGuard guard(m_CS); LPDIRECTSOUND8 & lpds = (LPDIRECTSOUND8 &)m_lpds; HRESULT hr = DirectSoundCreate8(NULL, &lpds, NULL); if (FAILED(hr)) { WriteTrace(TraceAudioDriver, TraceWarning, "Unable to DirectSoundCreate (hr: 0x%08X)", hr); WriteTrace(TraceAudioDriver, TraceDebug, "Done (res: false)"); return false; } hr = lpds->SetCooperativeLevel((HWND)g_AudioInfo.hwnd, DSSCL_PRIORITY); if (FAILED(hr)) { WriteTrace(TraceAudioDriver, TraceWarning, "Failed to SetCooperativeLevel (hr: 0x%08X)", hr); WriteTrace(TraceAudioDriver, TraceDebug, "Done (res: false)"); return false; } LPDIRECTSOUNDBUFFER & lpdsbuf = (LPDIRECTSOUNDBUFFER &)m_lpdsbuf; if (lpdsbuf) { IDirectSoundBuffer_Release(lpdsbuf); lpdsbuf = NULL; } DSBUFFERDESC dsPrimaryBuff = { 0 }; dsPrimaryBuff.dwSize = sizeof(DSBUFFERDESC); dsPrimaryBuff.dwFlags = DSBCAPS_PRIMARYBUFFER | DSBCAPS_CTRLVOLUME; dsPrimaryBuff.dwBufferBytes = 0; dsPrimaryBuff.lpwfxFormat = NULL; WAVEFORMATEX wfm = { 0 }; wfm.wFormatTag = WAVE_FORMAT_PCM; wfm.nChannels = 2; wfm.nSamplesPerSec = 48000; wfm.wBitsPerSample = 16; wfm.nBlockAlign = wfm.wBitsPerSample / 8 * wfm.nChannels; wfm.nAvgBytesPerSec = wfm.nSamplesPerSec * wfm.nBlockAlign; LPDIRECTSOUNDBUFFER & lpdsb = (LPDIRECTSOUNDBUFFER &)m_lpdsb; hr = lpds->CreateSoundBuffer(&dsPrimaryBuff, &lpdsb, NULL); if (SUCCEEDED(hr)) { lpdsb->SetFormat(&wfm); lpdsb->Play(0, 0, DSBPLAY_LOOPING); } WriteTrace(TraceAudioDriver, TraceDebug, "Done (res: true)"); return true; }
/*! * @brief Sets up the DirectSound for playback * @details Example code: * @code * HRESULT hr; * struct dsound_data * p_retval = (struct dsound_data*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct dsound_data)); * if (NULL != p_retval) * { * hr = init_ds_data(hWnd, p_WFE, p_retval); * if (SUCCEEDED(hr)) * { HeapFree(GetProcessHeap(), 0, p_tib); * return (DSOUNDPLAY)(p_retval); * } * } * debug_outputln("%s %4.4u", __FILE__, __LINE__); * HeapFree(GetProcessHeap(), 0, p_retval); * return NULL; * @endcode * @param[in] hwnd handle to the window that goes into the call of IDirectSound::SetCooperationLevel. Can be NULL, in which case either * a foreground window or the desktop window will be used. See <a href="http://msdn.microsoft.com/en-us/library/ms898135.aspx">this link</a> for more information. * @param[in] p_WFE pointer to the WAVEFORMATEX structure, describing the data to be played. * @param[out] p_ds_data refernce to the structure, whose members will be filled with DirectSound interface pointers. * @retrun returns the status of the operation, test it with SUCCEEDED() or FAILED() macros. */ static HRESULT init_ds_data(HWND hwnd, WAVEFORMATEX const * p_WFE, dxaudio_player_thread_information_block_t * p_ds_data) { HRESULT hr = E_FAIL; hr = DirectSoundCreate8(&DSDEVID_DefaultVoicePlayback, &p_ds_data->p_direct_sound_8_, NULL); if (FAILED(hr)) { debug_outputln("%s %4.4u : %x", __FILE__, __LINE__, hr); goto error; } if (NULL == hwnd) { hwnd = GetForegroundWindow(); } if (NULL == hwnd) { hwnd = GetDesktopWindow(); } /* Quoting MSDN: */ /* "[..]After creating a device object, you must set the cooperative level */ /* for the device by using the IDirectSound8::SetCooperativeLevel method. */ /* Unless you do this, no sounds will be heard." */ hr = p_ds_data->p_direct_sound_8_->SetCooperativeLevel(hwnd, DSSCL_PRIORITY); if (FAILED(hr)) { debug_outputln("%4.4u %s : 0x%8.8x", __LINE__, __FILE__, hr); goto error; } CopyMemory(&p_ds_data->p_dsound_data->wfe_, p_WFE, sizeof(WAVEFORMATEX)); p_ds_data->p_dsound_data->wfe_.cbSize = sizeof(WAVEFORMATEX); p_ds_data->p_dsound_data->nSingleBufferSize_ = p_ds_data->p_dsound_data->play_settings_.play_buffer_size_; hr = create_buffers(p_ds_data->p_direct_sound_8_, &p_ds_data->p_dsound_data->wfe_, p_ds_data->p_dsound_data->number_of_chunks_, p_ds_data->p_dsound_data->nSingleBufferSize_, &p_ds_data->p_primary_sound_buffer_, &p_ds_data->p_secondary_sound_buffer_ ); if (FAILED(hr)) { debug_outputln("%s %4.4u : %x", __FILE__, __LINE__, hr); goto error; } hr = set_play_notifications(p_ds_data->p_secondary_sound_buffer_, &p_ds_data->notification_array_[0], COUNTOF_ARRAY(p_ds_data->notification_array_)); if (FAILED(hr)) { debug_outputln("%s %4.4u : %x", __FILE__, __LINE__, hr); goto error; } return hr; error: if (NULL != p_ds_data->p_direct_sound_8_) { p_ds_data->p_direct_sound_8_->Release(); p_ds_data->p_direct_sound_8_ = NULL; } return hr; }
/* IDirectMusicPerformance8 Interface part follow: */ static HRESULT WINAPI IDirectMusicPerformance8Impl_InitAudio (LPDIRECTMUSICPERFORMANCE8 iface, IDirectMusic** ppDirectMusic, IDirectSound** ppDirectSound, HWND hWnd, DWORD dwDefaultPathType, DWORD dwPChannelCount, DWORD dwFlags, DMUS_AUDIOPARAMS* pParams) { IDirectSound* dsound = NULL; HRESULT hr = S_OK; IDirectMusicPerformance8Impl *This = (IDirectMusicPerformance8Impl *)iface; FIXME("(%p, %p, %p, %p, %x, %u, %x, %p): to check\n", This, ppDirectMusic, ppDirectSound, hWnd, dwDefaultPathType, dwPChannelCount, dwFlags, pParams); if (This->pDirectMusic || This->pDirectSound) return DMUS_E_ALREADY_INITED; if (NULL != ppDirectSound && NULL != *ppDirectSound) { dsound = *ppDirectSound; } else { hr = DirectSoundCreate8 (NULL, (LPDIRECTSOUND8*) &dsound, NULL); FIXME("return dsound(%p,%d)\n", dsound, hr); if (FAILED(hr) || !dsound) return DSERR_NODRIVER; if (ppDirectSound) *ppDirectSound = dsound; } IDirectMusicPerformance8Impl_Init(iface, ppDirectMusic, dsound, hWnd); /* Init increases the ref count of the dsound object. Decrement it if the app doesn't want a pointer to the object. */ if (NULL == ppDirectSound) { IDirectSound_Release(This->pDirectSound); } /* as seen in msdn we need params init before audio path creation */ if (NULL != pParams) { This->pParams = *pParams; } else { /* TODO, how can i fill the struct as seen on msdn */ memset(&This->pParams, 0, sizeof(DMUS_AUDIOPARAMS)); This->pParams.dwSize = sizeof(DMUS_AUDIOPARAMS); This->pParams.fInitNow = FALSE; This->pParams.dwValidData = DMUS_AUDIOPARAMS_FEATURES | DMUS_AUDIOPARAMS_VOICES | DMUS_AUDIOPARAMS_SAMPLERATE | DMUS_AUDIOPARAMS_DEFAULTSYNTH; This->pParams.dwVoices = 64; This->pParams.dwSampleRate = (DWORD) 22.050; This->pParams.dwFeatures = dwFlags; This->pParams.clsidDefaultSynth = CLSID_DirectMusicSynthSink; } hr = IDirectMusicPerformance8_CreateStandardAudioPath(iface, dwDefaultPathType, dwPChannelCount, FALSE, &This->pDefaultPath); PostMessageToProcessMsgThread(This, PROCESSMSG_START); return hr; }
int DSoundAudioBackend::RunThread() { if (FAILED(DirectSoundCreate8(0, &ds_, 0))) { ds_ = NULL; threadData_ = 2; return 1; } ds_->SetCooperativeLevel(window_, DSSCL_PRIORITY); if (!CreateBuffer()) { ds_->Release(); ds_ = NULL; threadData_ = 2; return 1; } soundSyncEvent_ = CreateEvent(0, false, false, 0); InitializeCriticalSection(&soundCriticalSection); DWORD num1; short *p1; dsBuffer_->Lock(0, bufferSize_, (void **)&p1, &num1, 0, 0, 0); memset(p1, 0, num1); dsBuffer_->Unlock(p1, num1, 0, 0); totalRenderedBytes_ = -bufferSize_; setCurrentThreadName("DSound"); currentPos_ = 0; lastPos_ = 0; dsBuffer_->Play(0,0,DSBPLAY_LOOPING); while (!threadData_) { EnterCriticalSection(&soundCriticalSection); dsBuffer_->GetCurrentPosition((DWORD *)¤tPos_, 0); int numBytesToRender = RoundDown128(ModBufferSize(currentPos_ - lastPos_)); if (numBytesToRender >= 256) { int numBytesRendered = 4 * (*callback_)(realtimeBuffer_, numBytesToRender >> 2, 16, 44100, 2); //We need to copy the full buffer, regardless of what the mixer claims to have filled //If we don't do this then the sound will loop if the sound stops and the mixer writes only zeroes numBytesRendered = numBytesToRender; WriteDataToBuffer(lastPos_, (char *) realtimeBuffer_, numBytesRendered); currentPos_ = ModBufferSize(lastPos_ + numBytesRendered); totalRenderedBytes_ += numBytesRendered; lastPos_ = currentPos_; } LeaveCriticalSection(&soundCriticalSection); WaitForSingleObject(soundSyncEvent_, MAXWAIT); }
bool SoundClass::InitializeDirectSound(HWND hwnd) { HRESULT result; DSBUFFERDESC bufferDesc; WAVEFORMATEX waveFormat; // Initialize the direct sound interface pointer for the default sound device. result = DirectSoundCreate8(NULL, &m_DirectSound, NULL); if (FAILED(result)) { return false; } // Set the cooperative level to priority so the format of the primary sound buffer can be modified. result = m_DirectSound->SetCooperativeLevel(hwnd, DSSCL_PRIORITY); if (FAILED(result)) { return false; } // Setup the primary buffer description. bufferDesc.dwSize = sizeof(DSBUFFERDESC); bufferDesc.dwFlags = DSBCAPS_PRIMARYBUFFER | DSBCAPS_CTRLVOLUME; bufferDesc.dwBufferBytes = 0; bufferDesc.dwReserved = 0; bufferDesc.lpwfxFormat = NULL; bufferDesc.guid3DAlgorithm = GUID_NULL; // Get control of the primary sound buffer on the default sound device. result = m_DirectSound->CreateSoundBuffer(&bufferDesc, &m_primaryBuffer, NULL); if (FAILED(result)) { return false; } // Setup the format of the primary sound bufffer. // In this case it is a .WAV file recorded at 44,100 samples per second in 16-bit stereo (cd audio format). waveFormat.wFormatTag = WAVE_FORMAT_PCM; waveFormat.nSamplesPerSec = 44100; waveFormat.wBitsPerSample = 16; waveFormat.nChannels = 2; waveFormat.nBlockAlign = (waveFormat.wBitsPerSample / 8) * waveFormat.nChannels; waveFormat.nAvgBytesPerSec = waveFormat.nSamplesPerSec * waveFormat.nBlockAlign; waveFormat.cbSize = 0; // Set the primary buffer to be the wave format specified. result = m_primaryBuffer->SetFormat(&waveFormat); if (FAILED(result)) { return false; } return true; }
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; }
int SoundEvent::InitDirectSound(SoundLib soundlib) { // LPDIRECTSOUND8 m_pDS; HRESULT hr; // LPDIRECTSOUNDBUFFER pDSBPrimary = NULL; TRACESETUP("INITDIRECTSOUND"); // Create IDirectSound using the primary sound device hr = DirectSoundCreate8( NULL, &m_pDS, NULL ); if (hr != DS_OK) { TRACE ("ERROR DIRECTSOUNDCREATE") this->makeInvalid(); return (false); } else TRACE("DIRECTSOUND CREATE OK"); // Set DirectSound coop level hr = m_pDS->SetCooperativeLevel( GetActiveWindow() , DSSCL_PRIORITY ) ; if (hr != DS_OK) TRACE ("ERROR DIRECTSOUND SET COOP") else TRACE("DIRECTSOUND SETCOOP OK"); // Get the primary buffer DSBUFFERDESC dsbd; ZeroMemory( &dsbd, sizeof(DSBUFFERDESC) ); dsbd.dwSize = sizeof(DSBUFFERDESC); dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER; dsbd.dwBufferBytes = 0; dsbd.lpwfxFormat = NULL; hr = m_pDS->CreateSoundBuffer( &dsbd, &pDSBPrimary, NULL ); if (hr != DS_OK) TRACE ("ERROR DIRECTSOUND CREATE SOUND BUFFER") else TRACE("DIRECTSOUND CREATE SOUND BUFFER OK"); WAVEFORMATEX wfx; ZeroMemory( &wfx, sizeof(WAVEFORMATEX) ); wfx.wFormatTag = (WORD) WAVE_FORMAT_PCM; wfx.nChannels = (WORD) 2; wfx.nSamplesPerSec = (DWORD) 22050; wfx.wBitsPerSample = (WORD) 16; wfx.nBlockAlign = (WORD) (wfx.wBitsPerSample / 8 * wfx.nChannels); wfx.nAvgBytesPerSec = (DWORD) (wfx.nSamplesPerSec * wfx.nBlockAlign); hr = pDSBPrimary->SetFormat(&wfx); if (hr != DS_OK) TRACE ("ERROR DIRECTSOUND SET FORMAT") else TRACE("DIRECTSOUND SET FORMAT OK"); return(true); }
//================================================================================================================ Sound::Sound(GameDirectory2D* gd, HWND hwnd) : m_DirectSound(0), m_primaryBuffer(0), m_gd(gd), m_soundLoaded(false) { //Initialize the direct sound interface pointer for the default sound device HRESULT result = DirectSoundCreate8(NULL, &m_DirectSound, NULL); //Set the cooperative level to priority so the format of the primary sound buffer can be modified result = m_DirectSound->SetCooperativeLevel(hwnd, DSSCL_PRIORITY); }
bool SoundBuffersMG::Initialize(const HWND hWnd) { hr = DirectSoundCreate8(NULL, &pDSound, NULL); if( FAILED(hr) ) return false; hr = pDSound->SetCooperativeLevel( hWnd, DSSCL_NORMAL); if( FAILED(hr) ) return false; return true; }
bool OggPlayer::InitDirectSound( HWND hWnd ) { HRESULT hr; if (FAILED(hr = DirectSoundCreate8(NULL, &pDS, NULL))) return bInitialized = false; pDS->Initialize(NULL); pDS->SetCooperativeLevel( hWnd, DSSCL_PRIORITY ); bReleaseDS = true; return bInitialized = true; }
Logical SoundEngine::init(HWND hwnd) { if(DirectSoundCreate8(NULL, &lpDS, NULL) != DS_OK) { return no; } if(lpDS->SetCooperativeLevel(hwnd, DSSCL_NORMAL) != DS_OK) { return no; } return yes; }
SoundManager::SoundManager(void) { /// 创建一个设备对象 if( DirectSoundCreate8(NULL, & lpDirectSound, NULL) != DS_OK ) MessageBoxA(0,"DirectSoundCreate8 Failed!","aa",0); HWND hWnd = CreateWindow("BUTTON", "aaa", 0, 0,0,0,0, 0, 0, 0, 0); /// 设置协作水平 if( lpDirectSound->SetCooperativeLevel(hWnd, DSSCL_PRIORITY) != DS_OK ) MessageBoxA(0,"SetCooperativeLevel Failed!","aa",0); play_sound = false; attenuation = 0; }
SoundDevice::SoundDevice(HWND hwnd, int sampleRate, int bufferSize) : dsObject(0), dsBuffer(0), writeOffset(0) { this->sampleRate = sampleRate; this->bufferSize = bufferSize; DirectSoundCreate8(NULL, &dsObject, NULL); dsObject->SetCooperativeLevel(hwnd ? hwnd : GetDesktopWindow(), DSSCL_PRIORITY); if (dsObject) { dsBuffer = createBasicBuffer(sampleRate, 16, bufferSize); if (dsBuffer) { LPVOID ptr1; DWORD bytes1; LPVOID ptr2; DWORD bytes2; HRESULT lockResult = dsBuffer->Lock( 0, 0, &ptr1, &bytes1, &ptr2, &bytes2, DSBLOCK_ENTIREBUFFER); if (lockResult == DS_OK) { ZeroMemory(ptr1, bytes1); if (ptr2) { ZeroMemory(ptr2, bytes2); } dsBuffer->Unlock(ptr1, bytes1, ptr2, bytes2); } dsBuffer->SetCurrentPosition(0); dsBuffer->Play(0, 0, DSBPLAY_LOOPING); } } // TODO: Fix primary buffer sheisse }
/******************************************************************* * initDirectSound * Initializes DirectSound *******************************************************************/ bool D3DUtil::initDirectSound(HWND hwnd) { HRESULT hr; hr = DirectSoundCreate8( NULL, &g_pDS, NULL ); if FAILED (hr) return false; // Set DirectSound cooperative level hr = g_pDS->SetCooperativeLevel( hwnd, DSSCL_PRIORITY ); if FAILED ( hr ) return false; return true; }
/* ================ SoundManager::initDirectSound ================ */ bool SoundManager::initDirectSound( HWND hwnd ) { HRESULT result; DSBUFFERDESC bufferDesc; WAVEFORMATEX waveFormat; // Setup direct sound interface result = DirectSoundCreate8(NULL, &directSound_, NULL); if (FAILED(result)) { return false; } result = directSound_->SetCooperativeLevel(hwnd, DSSCL_PRIORITY); if (FAILED(result)) { return false; } bufferDesc.dwSize = sizeof(DSBUFFERDESC); bufferDesc.dwFlags = DSBCAPS_PRIMARYBUFFER | DSBCAPS_CTRLVOLUME; bufferDesc.dwBufferBytes = 0; bufferDesc.dwReserved = 0; bufferDesc.lpwfxFormat = NULL; bufferDesc.guid3DAlgorithm = GUID_NULL; result = directSound_->CreateSoundBuffer(&bufferDesc, &primaryBuffer_, NULL); if (FAILED(result)) { return false; } // Setup audio format waveFormat.wFormatTag = WAVE_FORMAT_PCM; waveFormat.nSamplesPerSec = 44100; waveFormat.wBitsPerSample = 16; waveFormat.nChannels = 2; waveFormat.nBlockAlign = (waveFormat.wBitsPerSample / 8) * waveFormat.nChannels; waveFormat.nAvgBytesPerSec = waveFormat.nSamplesPerSec * waveFormat.nBlockAlign; waveFormat.cbSize = 0; result = primaryBuffer_->SetFormat(&waveFormat); if (FAILED(result)) { return false; } return true; }
//=============================================== //デバイスの生成 //=============================================== //[input] // なし //[return] // //=============================================== bool CSound::CreateDevice() { HRESULT hr; /*DirectSoundの作成*/ hr = DirectSoundCreate8(NULL, &m_pDirectSound, NULL); if(FAILED(hr) ) { MESSAGE("DirectSoundのインターフェースの取得に失敗"); return false; } /*協調レベルの設定*/ hr = m_pDirectSound->SetCooperativeLevel(Joker::GetHWnd(), DSSCL_PRIORITY); if(FAILED(hr) ) { MESSAGE("協調レベルの設定に失敗"); return false; } DSCAPS caps; ZeroMemory(&caps, sizeof(DSCAPS) ); caps.dwSize = sizeof(DSCAPS); /*デバイス能力の取得*/ hr = m_pDirectSound->GetCaps(&caps); if(FAILED(hr) ) { MESSAGE("デバイス能力の取得に失敗"); return false; } /*スピーカーの設定*/ SetSpeakerConfig(); /*プライマリ・バッファの生成*/ CreatePrimaryBuffer(); return true; }