CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Windows/DSoundStream.cpp
Views: 1401
#include "Common/CommonWindows.h"1#include <MMReg.h>2#include <process.h>34#ifdef __MINGW32__5#define __null6#endif7#include <dsound.h>8#ifdef __MINGW32__9#undef __null10#endif1112#include "Common/Thread/ThreadUtil.h"13#include "Common/Log.h"14#include "Common/OSVersion.h"15#include "Core/ConfigValues.h"16#include "Core/Util/AudioFormat.h"17#include "Windows/W32Util/Misc.h"1819#include "DSoundStream.h"2021inline int RoundDown128(int x) {22return x & (~127);23}2425unsigned int WINAPI DSoundAudioBackend::soundThread(void *param) {26DSoundAudioBackend *dsound = (DSoundAudioBackend *)param;27return dsound->RunThread();28}2930bool DSoundAudioBackend::WriteDataToBuffer(DWORD offset, // Our own write cursor.31char* soundData, // Start of our data.32DWORD soundBytes) { // Size of block to copy.33void *ptr1, *ptr2;34DWORD numBytes1, numBytes2;35// Obtain memory address of write block. This will be in two parts if the block wraps around.36HRESULT hr = dsBuffer_->Lock(offset, soundBytes, &ptr1, &numBytes1, &ptr2, &numBytes2, 0);37// If the buffer was lost, restore and retry lock.38/*39if (DSERR_BUFFERLOST == hr) {40dsBuffer->Restore();41hr=dsBuffer->Lock(dwOffset, dwSoundBytes, &ptr1, &numBytes1, &ptr2, &numBytes2, 0);42} */43if (FAILED(hr)) {44return false;45}4647memcpy(ptr1, soundData, numBytes1);48if (ptr2)49memcpy(ptr2, soundData + numBytes1, numBytes2);50// Release the data back to DirectSound.51dsBuffer_->Unlock(ptr1, numBytes1, ptr2, numBytes2);52return true;53}5455bool DSoundAudioBackend::CreateBuffer() {56PCMWAVEFORMAT pcmwf;57DSBUFFERDESC dsbdesc;5859memset(&pcmwf, 0, sizeof(PCMWAVEFORMAT));60memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));6162bufferSize_ = BUFSIZE;6364pcmwf.wf.wFormatTag = WAVE_FORMAT_PCM;65pcmwf.wf.nChannels = 2;66pcmwf.wf.nSamplesPerSec = sampleRate_;67pcmwf.wf.nBlockAlign = 4;68pcmwf.wf.nAvgBytesPerSec = pcmwf.wf.nSamplesPerSec * pcmwf.wf.nBlockAlign;69pcmwf.wBitsPerSample = 16;7071dsbdesc.dwSize = sizeof(DSBUFFERDESC);72dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_GLOBALFOCUS; // //DSBCAPS_CTRLPAN | DSBCAPS_CTRLVOLUME | DSBCAPS_CTRLFREQUENCY;73dsbdesc.dwBufferBytes = bufferSize_; //FIX32(pcmwf.wf.nAvgBytesPerSec); //change to set buffer size74dsbdesc.lpwfxFormat = (WAVEFORMATEX *)&pcmwf;7576if (SUCCEEDED(ds_->CreateSoundBuffer(&dsbdesc, &dsBuffer_, NULL))) {77dsBuffer_->SetCurrentPosition(0);78return true;79} else {80dsBuffer_ = NULL;81return false;82}83}8485int DSoundAudioBackend::RunThread() {86if (FAILED(DirectSoundCreate8(0, &ds_, 0))) {87ds_ = NULL;88threadData_ = 2;89return 1;90}9192ds_->SetCooperativeLevel(window_, DSSCL_PRIORITY);93if (!CreateBuffer()) {94ds_->Release();95ds_ = NULL;96threadData_ = 2;97return 1;98}99100InitializeCriticalSection(&soundCriticalSection);101102DWORD num1;103int16_t *p1;104105dsBuffer_->Lock(0, bufferSize_, (void **)&p1, &num1, 0, 0, 0);106107memset(p1, 0, num1);108dsBuffer_->Unlock(p1, num1, 0, 0);109totalRenderedBytes_ = -bufferSize_;110111SetCurrentThreadName("DSound");112currentPos_ = 0;113lastPos_ = 0;114115dsBuffer_->Play(0, 0, DSBPLAY_LOOPING);116117auto ModBufferSize = [&](int x) { return (x + bufferSize_) % bufferSize_; };118119while (!threadData_) {120EnterCriticalSection(&soundCriticalSection);121122dsBuffer_->GetCurrentPosition((DWORD *)¤tPos_, 0);123int numBytesToRender = RoundDown128(ModBufferSize(currentPos_ - lastPos_));124125if (numBytesToRender >= 256) {126int numBytesRendered = 4 * (*callback_)(realtimeBuffer_, numBytesToRender >> 2, 44100);127//We need to copy the full buffer, regardless of what the mixer claims to have filled128//If we don't do this then the sound will loop if the sound stops and the mixer writes only zeroes129numBytesRendered = numBytesToRender;130WriteDataToBuffer(lastPos_, (char *) realtimeBuffer_, numBytesRendered);131132currentPos_ = ModBufferSize(lastPos_ + numBytesRendered);133totalRenderedBytes_ += numBytesRendered;134135lastPos_ = currentPos_;136}137138LeaveCriticalSection(&soundCriticalSection);139Sleep(5);140}141dsBuffer_->Stop();142143dsBuffer_->Release();144ds_->Release();145146threadData_ = 2;147return 0;148}149150DSoundAudioBackend::~DSoundAudioBackend() {151if (!ds_)152return;153154if (!dsBuffer_)155return;156157EnterCriticalSection(&soundCriticalSection);158159if (threadData_ == 0) {160threadData_ = 1;161}162163if (hThread_ != NULL) {164WaitForSingleObject(hThread_, 1000);165CloseHandle(hThread_);166hThread_ = NULL;167}168169LeaveCriticalSection(&soundCriticalSection);170DeleteCriticalSection(&soundCriticalSection);171}172173bool DSoundAudioBackend::Init(HWND window, StreamCallback _callback, int sampleRate) {174window_ = window;175callback_ = _callback;176sampleRate_ = sampleRate;177threadData_ = 0;178hThread_ = (HANDLE)_beginthreadex(0, 0, soundThread, (void *)this, 0, 0);179if (!hThread_)180return false;181SetThreadPriority(hThread_, THREAD_PRIORITY_ABOVE_NORMAL);182return true;183}184185186