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