CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Windows/DSoundStream.cpp
Views: 1401
1
#include "Common/CommonWindows.h"
2
#include <MMReg.h>
3
#include <process.h>
4
5
#ifdef __MINGW32__
6
#define __null
7
#endif
8
#include <dsound.h>
9
#ifdef __MINGW32__
10
#undef __null
11
#endif
12
13
#include "Common/Thread/ThreadUtil.h"
14
#include "Common/Log.h"
15
#include "Common/OSVersion.h"
16
#include "Core/ConfigValues.h"
17
#include "Core/Util/AudioFormat.h"
18
#include "Windows/W32Util/Misc.h"
19
20
#include "DSoundStream.h"
21
22
inline int RoundDown128(int x) {
23
return x & (~127);
24
}
25
26
unsigned int WINAPI DSoundAudioBackend::soundThread(void *param) {
27
DSoundAudioBackend *dsound = (DSoundAudioBackend *)param;
28
return dsound->RunThread();
29
}
30
31
bool DSoundAudioBackend::WriteDataToBuffer(DWORD offset, // Our own write cursor.
32
char* soundData, // Start of our data.
33
DWORD soundBytes) { // Size of block to copy.
34
void *ptr1, *ptr2;
35
DWORD numBytes1, numBytes2;
36
// Obtain memory address of write block. This will be in two parts if the block wraps around.
37
HRESULT hr = dsBuffer_->Lock(offset, soundBytes, &ptr1, &numBytes1, &ptr2, &numBytes2, 0);
38
// If the buffer was lost, restore and retry lock.
39
/*
40
if (DSERR_BUFFERLOST == hr) {
41
dsBuffer->Restore();
42
hr=dsBuffer->Lock(dwOffset, dwSoundBytes, &ptr1, &numBytes1, &ptr2, &numBytes2, 0);
43
} */
44
if (FAILED(hr)) {
45
return false;
46
}
47
48
memcpy(ptr1, soundData, numBytes1);
49
if (ptr2)
50
memcpy(ptr2, soundData + numBytes1, numBytes2);
51
// Release the data back to DirectSound.
52
dsBuffer_->Unlock(ptr1, numBytes1, ptr2, numBytes2);
53
return true;
54
}
55
56
bool DSoundAudioBackend::CreateBuffer() {
57
PCMWAVEFORMAT pcmwf;
58
DSBUFFERDESC dsbdesc;
59
60
memset(&pcmwf, 0, sizeof(PCMWAVEFORMAT));
61
memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));
62
63
bufferSize_ = BUFSIZE;
64
65
pcmwf.wf.wFormatTag = WAVE_FORMAT_PCM;
66
pcmwf.wf.nChannels = 2;
67
pcmwf.wf.nSamplesPerSec = sampleRate_;
68
pcmwf.wf.nBlockAlign = 4;
69
pcmwf.wf.nAvgBytesPerSec = pcmwf.wf.nSamplesPerSec * pcmwf.wf.nBlockAlign;
70
pcmwf.wBitsPerSample = 16;
71
72
dsbdesc.dwSize = sizeof(DSBUFFERDESC);
73
dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_GLOBALFOCUS; // //DSBCAPS_CTRLPAN | DSBCAPS_CTRLVOLUME | DSBCAPS_CTRLFREQUENCY;
74
dsbdesc.dwBufferBytes = bufferSize_; //FIX32(pcmwf.wf.nAvgBytesPerSec); //change to set buffer size
75
dsbdesc.lpwfxFormat = (WAVEFORMATEX *)&pcmwf;
76
77
if (SUCCEEDED(ds_->CreateSoundBuffer(&dsbdesc, &dsBuffer_, NULL))) {
78
dsBuffer_->SetCurrentPosition(0);
79
return true;
80
} else {
81
dsBuffer_ = NULL;
82
return false;
83
}
84
}
85
86
int DSoundAudioBackend::RunThread() {
87
if (FAILED(DirectSoundCreate8(0, &ds_, 0))) {
88
ds_ = NULL;
89
threadData_ = 2;
90
return 1;
91
}
92
93
ds_->SetCooperativeLevel(window_, DSSCL_PRIORITY);
94
if (!CreateBuffer()) {
95
ds_->Release();
96
ds_ = NULL;
97
threadData_ = 2;
98
return 1;
99
}
100
101
InitializeCriticalSection(&soundCriticalSection);
102
103
DWORD num1;
104
int16_t *p1;
105
106
dsBuffer_->Lock(0, bufferSize_, (void **)&p1, &num1, 0, 0, 0);
107
108
memset(p1, 0, num1);
109
dsBuffer_->Unlock(p1, num1, 0, 0);
110
totalRenderedBytes_ = -bufferSize_;
111
112
SetCurrentThreadName("DSound");
113
currentPos_ = 0;
114
lastPos_ = 0;
115
116
dsBuffer_->Play(0, 0, DSBPLAY_LOOPING);
117
118
auto ModBufferSize = [&](int x) { return (x + bufferSize_) % bufferSize_; };
119
120
while (!threadData_) {
121
EnterCriticalSection(&soundCriticalSection);
122
123
dsBuffer_->GetCurrentPosition((DWORD *)&currentPos_, 0);
124
int numBytesToRender = RoundDown128(ModBufferSize(currentPos_ - lastPos_));
125
126
if (numBytesToRender >= 256) {
127
int numBytesRendered = 4 * (*callback_)(realtimeBuffer_, numBytesToRender >> 2, 44100);
128
//We need to copy the full buffer, regardless of what the mixer claims to have filled
129
//If we don't do this then the sound will loop if the sound stops and the mixer writes only zeroes
130
numBytesRendered = numBytesToRender;
131
WriteDataToBuffer(lastPos_, (char *) realtimeBuffer_, numBytesRendered);
132
133
currentPos_ = ModBufferSize(lastPos_ + numBytesRendered);
134
totalRenderedBytes_ += numBytesRendered;
135
136
lastPos_ = currentPos_;
137
}
138
139
LeaveCriticalSection(&soundCriticalSection);
140
Sleep(5);
141
}
142
dsBuffer_->Stop();
143
144
dsBuffer_->Release();
145
ds_->Release();
146
147
threadData_ = 2;
148
return 0;
149
}
150
151
DSoundAudioBackend::~DSoundAudioBackend() {
152
if (!ds_)
153
return;
154
155
if (!dsBuffer_)
156
return;
157
158
EnterCriticalSection(&soundCriticalSection);
159
160
if (threadData_ == 0) {
161
threadData_ = 1;
162
}
163
164
if (hThread_ != NULL) {
165
WaitForSingleObject(hThread_, 1000);
166
CloseHandle(hThread_);
167
hThread_ = NULL;
168
}
169
170
LeaveCriticalSection(&soundCriticalSection);
171
DeleteCriticalSection(&soundCriticalSection);
172
}
173
174
bool DSoundAudioBackend::Init(HWND window, StreamCallback _callback, int sampleRate) {
175
window_ = window;
176
callback_ = _callback;
177
sampleRate_ = sampleRate;
178
threadData_ = 0;
179
hThread_ = (HANDLE)_beginthreadex(0, 0, soundThread, (void *)this, 0, 0);
180
if (!hThread_)
181
return false;
182
SetThreadPriority(hThread_, THREAD_PRIORITY_ABOVE_NORMAL);
183
return true;
184
}
185
186