void I_InitSound(void) { // proff 07/04/98: Added for CYGWIN32 compatibility #ifdef HAVE_LIBDSOUND HRESULT error; int c; int i; #endif // HAVE_LIBDSOUND // proff 07/01/98: Added I_InitMusic if (!nomusicparm) I_InitMusic(); if (nosfxparm) return; // proff 07/04/98: Added for CYGWIN32 compatibility #ifdef HAVE_LIBDSOUND // proff 11/21/98: Added DirectSound device selection i = M_CheckParm ("-dsounddevice"); if ((i) || (snd_dsounddevice)) { printf("I_InitSound: Sound Devices\n"); DirectSoundEnumerate(&DSEnumCallback,NULL); DSoundDevice=snd_dsounddevice; if (i) DSoundDevice=atoi(myargv[i+1]); DSoundDevice= (DSoundDevice < 0) ? 0 : (DSoundDevice >= DSDeviceCount) ? 0 : DSoundDevice; } printf("I_InitSound: "); error = DirectSoundCreate(DSDeviceGUIDs[DSoundDevice], &lpDS, NULL); // proff 11/21/98: End of additions and changes if (error == DSERR_NODRIVER) { lpDS = NULL; printf("no sounddevice found\n"); noDSound = true; return; } noDSound = false; if (error != DS_OK) { noDSound = true; return; } // open the hidden sound window we use to play sounds through OpenSoundWnd(); printf("created DirectSound. Selected Device: %i\n", DSoundDevice); atexit(I_ShutdownSound); error = IDirectSound_SetCooperativeLevel(lpDS, soundWnd , DSSCL_EXCLUSIVE); if (error != DS_OK) { noDSound = true; return; } printf("I_InitSound: "); printf("CooperativeLevel set\n"); printf("I_InitSound: "); // error is returned but it works ... ?? error = CreatePrimaryBuffer(); // if (error != DS_OK) // { // printf("ERROR: Couldnt create primary buffer\n"); // noDSound = true; // return; // } numChannels = default_numChannels; lpSecondaryDSB = malloc(sizeof(LPDIRECTSOUNDBUFFER)*numChannels); if (lpSecondaryDSB) { memset(lpSecondaryDSB, 0, sizeof(LPDIRECTSOUNDBUFFER) * numChannels); printf ("Channels : %i\n", numChannels); } else { noDSound = true; return; } ChannelInfo = malloc(sizeof(channel_info_t)*numChannels); if (ChannelInfo) { // proff 11/09/98: Added for security memset(ChannelInfo, 0, sizeof(channel_info_t) * numChannels); } else { noDSound = true; return; } for (c=0; c<numChannels; c++) { error = CreateSecondaryBuffer(&lpSecondaryDSB[c], 65535); if (error != DS_OK) { noDSound = true; return; } } printf("DirectSound initialised ok!\n"); #endif // HAVE_LIBDSOUND }
bool SoundLibrary3dDirectSound::Initialise(int _mixFreq, int _numChannels, bool _hw3d, int _mainBufNumSamples, int _musicBufNumSamples ) { int errCode; AppReleaseAssert( g_systemInfo->m_directXVersion >= 9.0f, APP_NAME " requires DirectX9 or Greater" ); AppReleaseAssert( _numChannels > 0, "SoundLibrary3d asked to create too few channels" ); // // Initialise COM // We need to do this in order to use the directX FX stuff errCode = CoInitialize( NULL ); // // Create Direct Sound Device errCode = DirectSoundCreate8(NULL, // Specifies default device &m_directSound->m_device, NULL); // Has to be NULL - stupid, stupid Microsoft SOUNDASSERT(errCode, "Direct Sound couldn't create a sound device"); errCode = m_directSound->m_device->SetCooperativeLevel((HWND)g_windowManager->Window(), DSSCL_PRIORITY); SOUNDASSERT(errCode, "Direct Sound couldn't set the cooperative level"); RefreshCapabilities(); m_sampleRate = _mixFreq; m_hw3dDesired = _hw3d; m_numChannels = min( _numChannels, GetMaxChannels() ) - 1; // Reserve one channel for the music m_musicChannelId = -1; // // Create the Primary Sound Buffer { int flags = DSBCAPS_PRIMARYBUFFER | DSBCAPS_CTRL3D; if( Hardware3DSupport() && m_hw3dDesired ) flags |= DSBCAPS_LOCHARDWARE; else flags |= DSBCAPS_LOCSOFTWARE; DSBUFFERDESC dsbd; ZeroMemory( &dsbd, sizeof(DSBUFFERDESC) ); dsbd.dwSize = sizeof(DSBUFFERDESC); dsbd.dwFlags = flags; dsbd.dwBufferBytes = 0; dsbd.lpwfxFormat = NULL; errCode = m_directSound->m_device->CreateSoundBuffer(&dsbd, &m_directSound->m_primaryBuffer, NULL); SOUNDASSERT(errCode, "Direct sound couldn't create the primary sound buffer"); WAVEFORMATEX wfx; ZeroMemory( &wfx, sizeof(WAVEFORMATEX) ); wfx.wFormatTag = (WORD) WAVE_FORMAT_PCM; wfx.nChannels = 2; wfx.nSamplesPerSec = _mixFreq; wfx.wBitsPerSample = 16; wfx.nBlockAlign = (WORD) (wfx.wBitsPerSample / 8 * wfx.nChannels); wfx.nAvgBytesPerSec = (DWORD) (wfx.nSamplesPerSec * wfx.nBlockAlign); errCode = m_directSound->m_primaryBuffer->SetFormat(&wfx); SOUNDASSERT(errCode, "Direct sound couldn't set the primary sound buffer format"); } // // Create a streaming secondary buffer for each channel m_channels = new DirectSoundChannel[ m_numChannels ]; for( int i = 0; i < m_numChannels; ++i ) { m_channels[i].m_bufferInterface = CreateSecondaryBuffer(_mainBufNumSamples); // // Earlier return if CreateSecondaryBuffer fails if( !m_channels[i].m_bufferInterface ) { return false; } m_channels[i].m_numBufferSamples = _mainBufNumSamples; m_channels[i].m_lastSampleWritten = m_channels[i].m_numBufferSamples - 1; // We just filled all of it with zeros remember m_channels[i].m_simulatedPlayCursor = -1; m_channels[i].m_freq = _mixFreq; // Get the DirectSound3DBuffer interface errCode = m_channels[i].m_bufferInterface->QueryInterface( IID_IDirectSound3DBuffer, (void **) &m_channels[i].m_buffer3DInterface ); SOUNDASSERT(errCode, "Direct sound couldn't get Sound3DBuffer interface"); } // // Create a streaming secondary buffer for music m_musicChannel = new DirectSoundChannel; m_musicChannel->m_bufferInterface = CreateSecondaryBuffer(_musicBufNumSamples); // // Earlier return if CreateSecondaryBuffer fails if( !m_musicChannel->m_bufferInterface ) { return false; } m_musicChannel->m_numBufferSamples = _musicBufNumSamples; m_musicChannel->m_lastSampleWritten = m_musicChannel->m_numBufferSamples - 1; m_musicChannel->m_simulatedPlayCursor = -1; m_musicChannel->m_freq = _mixFreq; // Get the DirectSound3DBuffer interface errCode = m_musicChannel->m_bufferInterface->QueryInterface( IID_IDirectSound3DBuffer, (void **)&(m_musicChannel->m_buffer3DInterface) ); SOUNDASSERT(errCode, "Direct sound couldn't get Sound3DBuffer interface"); SetChannel3DMode(m_musicChannelId, 2); SetChannelVolume(m_musicChannelId, 10.0f); SetChannelFrequency(m_musicChannelId, 44100); // // Set our listener properties IDirectSound3DListener *listener = NULL; errCode = m_directSound->m_primaryBuffer->QueryInterface( IID_IDirectSound3DListener, (void **) &listener ); SOUNDASSERT(errCode, "Direct sound couldn't get Sound3DListener interface"); errCode = listener->SetDopplerFactor( 0.0f, DS3D_IMMEDIATE ); return true; }