| 1 | #include "dsound/DSoundRender.h" |
| 2 | |
| 3 | BOOL DSoundRender::Init(HWND hWnd, int sample_rate, int channels, int bits_per_sample) { |
| 4 | Close(); |
| 5 | std::lock_guard<std::mutex> _(m_mutex); |
| 6 | if (FAILED(DirectSoundCreate8(NULL, &m_pDS, NULL))) |
| 7 | { |
| 8 | #ifdef _DEBUG |
| 9 | OutputDebugString(_T("DirectSoundCreate8 error!\n")); |
| 10 | #endif |
| 11 | return FALSE; |
| 12 | } |
| 13 | if (FAILED(m_pDS->SetCooperativeLevel(hWnd, DSSCL_NORMAL))) |
| 14 | { |
| 15 | #ifdef _DEBUG |
| 16 | OutputDebugString(_T("SetCooperativeLevel error!\n")); |
| 17 | #endif |
| 18 | return FALSE; |
| 19 | } |
| 20 | |
| 21 | m_channels = channels; |
| 22 | m_sample_rate = sample_rate; |
| 23 | m_bits_per_sample = bits_per_sample; |
| 24 | |
| 25 | DSBUFFERDESC dsbd; |
| 26 | memset(&dsbd, 0, sizeof(dsbd)); |
| 27 | dsbd.dwSize = sizeof(dsbd); |
| 28 | dsbd.dwFlags = DSBCAPS_GLOBALFOCUS | DSBCAPS_CTRLPOSITIONNOTIFY | DSBCAPS_GETCURRENTPOSITION2; |
| 29 | dsbd.dwBufferBytes = MAX_AUDIO_BUF * BUFFERNOTIFYSIZE; |
| 30 | dsbd.lpwfxFormat = (WAVEFORMATEX*)new WAVEFORMATEX; |
| 31 | dsbd.lpwfxFormat->wFormatTag = WAVE_FORMAT_PCM; |
| 32 | dsbd.lpwfxFormat->nChannels = channels; |
| 33 | dsbd.lpwfxFormat->nSamplesPerSec = sample_rate; |
| 34 | dsbd.lpwfxFormat->nAvgBytesPerSec = sample_rate * (bits_per_sample / 8)*channels; |
| 35 | dsbd.lpwfxFormat->nBlockAlign = (bits_per_sample / 8)*channels; |
| 36 | dsbd.lpwfxFormat->wBitsPerSample = bits_per_sample; |
| 37 | dsbd.lpwfxFormat->cbSize = 0; |
| 38 | |
| 39 | if (FAILED(m_pDS->CreateSoundBuffer(&dsbd, &m_pDSBuffer, NULL))) { |
| 40 | #ifdef _DEBUG |
| 41 | OutputDebugString(_T("SetCooperativeLevel error!\n")); |
| 42 | #endif |
| 43 | return FALSE; |
| 44 | } |
| 45 | if (FAILED(m_pDSBuffer->QueryInterface(IID_IDirectSoundBuffer8, (LPVOID*)&m_pDSBuffer8))) { |
| 46 | #ifdef _DEBUG |
| 47 | OutputDebugString(_T("SetCooperativeLevel error!\n")); |
| 48 | #endif |
| 49 | return FALSE; |
| 50 | } |
| 51 | if (FAILED(m_pDSBuffer8->QueryInterface(IID_IDirectSoundNotify, (LPVOID*)&m_pDSNotify))) { |
| 52 | #ifdef _DEBUG |
| 53 | OutputDebugString(_T("SetCooperativeLevel error!\n")); |
| 54 | #endif |
| 55 | return FALSE; |
| 56 | } |
| 57 | for (int i = 0; i < MAX_AUDIO_BUF; i++) { |
| 58 | m_pDSPosNotify[i].dwOffset = i * BUFFERNOTIFYSIZE; |
| 59 | m_event[i] = ::CreateEvent(NULL, FALSE, FALSE, NULL); |
| 60 | m_pDSPosNotify[i].hEventNotify = m_event[i]; |
no test coverage detected