internal void initDirectSound(HWND windowHandle, int32 samplePerSecond, int32 bufferSize) { // Load the library HMODULE directSoundLibrary = LoadLibraryA("dsound.dll"); if ( ! directSoundLibrary ) { OutputDebugStringA("Direct sound dll not found "); return; } // Get a direct sound object direct_sound_create *directSoundCreate = (direct_sound_create*)GetProcAddress(directSoundLibrary, "DirectSoundCreate"); HRESULT error; LPDIRECTSOUND directSound; error = directSoundCreate(0, &directSound, 0); if ( directSoundCreate && SUCCEEDED(error) ) { DSBUFFERDESC bufferDescription = {}; bufferDescription.dwSize = sizeof(bufferDescription); bufferDescription.dwFlags = DSBCAPS_PRIMARYBUFFER; WAVEFORMATEX waveFormat = {}; waveFormat.wFormatTag = WAVE_FORMAT_PCM; waveFormat.nChannels = 2; waveFormat.nSamplesPerSec = samplePerSecond; waveFormat.wBitsPerSample = 16; waveFormat.nBlockAlign = (waveFormat.nChannels*waveFormat.wBitsPerSample) / 8; waveFormat.nAvgBytesPerSec = waveFormat.nSamplesPerSec * waveFormat.nBlockAlign; waveFormat.cbSize = 0; LPDIRECTSOUNDBUFFER primaryBuffer; // create a primary buffer if ( SUCCEEDED(directSound->SetCooperativeLevel(windowHandle, DSSCL_PRIORITY)) ) { if ( SUCCEEDED(error = directSound->CreateSoundBuffer(&bufferDescription, &primaryBuffer, 0)) ) { if ( !(SUCCEEDED(error = primaryBuffer->SetFormat(&waveFormat))) ) { OutputDebugStringA("Primary buffer failed " + error ); } } else { OutputDebugStringA("Primary buffer failed " + error); } DSBUFFERDESC bufferDescription = {}; bufferDescription.dwSize = sizeof(bufferDescription); bufferDescription.dwFlags = 0; bufferDescription.dwBufferBytes = bufferSize; bufferDescription.lpwfxFormat = &waveFormat; // create a secondary buffer if ( SUCCEEDED(directSound->CreateSoundBuffer(&bufferDescription, &globalSecondaryBuffer, 0)) ) { } } // start playing it } else { OutputDebugStringA("Create DSound error " + error); } }
internal void Win32InitDSound(HWND _Window, int32 _SamplerPerSecond, int32 _SecondaryBufferSize) { // NOTE: Load the Library HMODULE dSoundLibrary = LoadLibraryA("dsound.dll"); if (dSoundLibrary) { // NOTE: Get a DirectSound object! direct_sound_create* directSoundCreate = (direct_sound_create*) GetProcAddress(dSoundLibrary, "DirectSoundCreate"); LPDIRECTSOUND directSound; if (directSoundCreate && SUCCEEDED(directSoundCreate(0, &directSound, 0))) { WAVEFORMATEX waveFormat = {}; waveFormat.wFormatTag = WAVE_FORMAT_PCM; waveFormat.nChannels = 2; waveFormat.nSamplesPerSec = _SamplerPerSecond; waveFormat.wBitsPerSample = 16; waveFormat.nBlockAlign = (waveFormat.nChannels * waveFormat.wBitsPerSample) / 8; waveFormat.nAvgBytesPerSec = waveFormat.nSamplesPerSec * waveFormat.nBlockAlign; waveFormat.cbSize = 0; if (SUCCEEDED(directSound->SetCooperativeLevel(_Window, DSSCL_PRIORITY))) { DSBUFFERDESC bufferDescription = {}; bufferDescription.dwSize = sizeof(bufferDescription); bufferDescription.dwFlags = DSBCAPS_PRIMARYBUFFER; LPDIRECTSOUNDBUFFER primaryBuffer; if (SUCCEEDED(directSound->CreateSoundBuffer(&bufferDescription, &primaryBuffer, 0))) { HRESULT error = primaryBuffer->SetFormat(&waveFormat); if (SUCCEEDED(error)) { OutputDebugStringA("primary buffer format was set.\n"); } else { } } else { } } else { } DSBUFFERDESC bufferDescription = {}; bufferDescription.dwSize = sizeof(bufferDescription); bufferDescription.dwFlags = 0; bufferDescription.dwBufferBytes = _SecondaryBufferSize; bufferDescription.lpwfxFormat = &waveFormat; HRESULT error = directSound->CreateSoundBuffer(&bufferDescription, &GlobalSecondaryBuffer, 0); if (SUCCEEDED(error)) { OutputDebugStringA("Secondary buffer created successfully.\n"); } } else { } } else { } }
internal void InitializeSound(HWND window, int32 bufferSize, int32 samplesPerSecond) { // First load the library HMODULE library = LoadLibraryA("dsound.dll"); if(library) { // Get a directSound object. direct_sound_create *directSoundCreate = (direct_sound_create *)GetProcAddress(library, "DirectSoundCreate"); LPDIRECTSOUND directSound; if(directSoundCreate && SUCCEEDED(directSoundCreate(0, &directSound, 0))) { WAVEFORMATEX waveformat; waveformat.wFormatTag = WAVE_FORMAT_PCM; waveformat.nChannels = 2; waveformat.nSamplesPerSec = samplesPerSecond; waveformat.wBitsPerSample = 16; waveformat.nBlockAlign = waveformat.nChannels * waveformat.wBitsPerSample / 8; waveformat.nAvgBytesPerSec = waveformat.nSamplesPerSec * waveformat.nBlockAlign; waveformat.cbSize = 0; if(SUCCEEDED(directSound->SetCooperativeLevel(window, DSSCL_PRIORITY))) { DSBUFFERDESC bufferDescription = {}; bufferDescription.dwSize = sizeof(bufferDescription); bufferDescription.dwFlags = DSBCAPS_PRIMARYBUFFER; bufferDescription.dwBufferBytes = 0; // Responsible for creating a handle to the sound card and configures the sound card using the wave format we defined above. // TODO: Is this even necessary now? LPDIRECTSOUNDBUFFER primaryBuffer; if(SUCCEEDED(directSound->CreateSoundBuffer(&bufferDescription, &primaryBuffer, 0))) { if(SUCCEEDED(primaryBuffer->SetFormat(&waveformat))) { OutputDebugStringA("Primary buffer set/n"); }; } } else{ // TODO: log diagnostic } DSBUFFERDESC bufferDescription = {}; bufferDescription.dwSize = sizeof(bufferDescription); bufferDescription.dwFlags = 0; bufferDescription.dwBufferBytes = bufferSize; bufferDescription.lpwfxFormat = &waveformat; if(SUCCEEDED(directSound->CreateSoundBuffer(&bufferDescription, &audioBuffer, 0))) { OutputDebugStringA("Secondary buffer created/n"); } } else{ // TODO: Log direct sound not loaded. } // Create a primary buffer // Create a secondary buffer which is what we will actually write to // Start playing } }