forked from Nyagamon/HCADecoder
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathHCADecodeService.cpp
244 lines (219 loc) · 6.1 KB
/
HCADecodeService.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include <map>
#include "HCADecodeService.h"
#include "clHCA.h"
HCADecodeService::HCADecodeService()
: numthreads(std::thread::hardware_concurrency() ? std::thread::hardware_concurrency() : 1),
numchannels(0),
chunksize(24),
blocklistsz(0),
blocks(nullptr),
workingrequest(nullptr),
workerthreads(new std::thread[this->numthreads]),
workersem(new Semaphore[this->numthreads]),
datasem(0),
mainsem(0),
channels(new clHCA::stChannel[0x10 * this->numthreads]),
wavebuffer(new float[0x10 * 0x80 * this->numthreads]),
shutdown(false),
stopcurrent(false)
{
for (unsigned int i = 0; i < this->numthreads; ++i)
{
workerthreads[i] = std::thread(&HCADecodeService::Decode_Thread, this, i);
}
dispatchthread = std::thread(&HCADecodeService::Main_Thread, this);
}
HCADecodeService::HCADecodeService(unsigned int numthreads, unsigned int chunksize)
: numthreads(numthreads ? numthreads : (std::thread::hardware_concurrency() ? std::thread::hardware_concurrency() : 1)),
numchannels(0),
chunksize(chunksize ? chunksize : 24),
blocklistsz(0),
blocks(nullptr),
workingrequest(nullptr),
workerthreads(new std::thread[this->numthreads]),
workersem(new Semaphore[this->numthreads]),
datasem(0),
mainsem(0),
channels(new clHCA::stChannel[0x10 * this->numthreads]),
wavebuffer(new float[0x10 * 0x80 * this->numthreads]),
shutdown(false),
stopcurrent(false)
{
for (unsigned int i = 0; i < this->numthreads; ++i)
{
workerthreads[i] = std::thread(&HCADecodeService::Decode_Thread, this, i);
}
dispatchthread = std::thread(&HCADecodeService::Main_Thread, this);
}
HCADecodeService::~HCADecodeService()
{
shutdown = true;
datasem.notify();
dispatchthread.join();
delete[] wavebuffer;
delete[] channels;
delete[] workersem;
delete[] workerthreads;
delete[] blocks;
}
void HCADecodeService::cancel_decode(void *ptr)
{
if (!ptr)
{
return;
}
if (workingrequest == ptr)
{
stopcurrent = true;
}
else
{
std::unique_lock<std::mutex> filelistlock(filelistmtx);
auto it = filelist.find(ptr);
if (it != filelist.end())
{
filelist.erase(it);
datasem.wait();
}
}
}
void HCADecodeService::wait_on_request(void *ptr)
{
if (!ptr)
{
return;
}
else
{
while (true)
{
if (workingrequest == ptr)
{
std::unique_lock<std::mutex> lck(workingmtx);
workingcv.wait(lck);
break;
}
filelistmtx.lock();
auto it = filelist.find(ptr);
if (it != filelist.end())
{
filelistmtx.unlock();
std::unique_lock<std::mutex> lck(workingmtx);
workingcv.wait(lck);
}
else
{
filelistmtx.unlock();
break;
}
}
}
}
void HCADecodeService::wait_for_finish()
{
filelistmtx.lock();
while (!filelist.empty() || workingrequest)
{
filelistmtx.unlock();
std::unique_lock<std::mutex> lck(workingmtx);
workingcv.wait(lck);
filelistmtx.lock();
}
filelistmtx.unlock();
}
std::pair<void *, size_t> HCADecodeService::decode(const char *hcafilename, unsigned int decodefromsample, unsigned int ciphKey1, unsigned int ciphKey2, unsigned int subKey, float volume, int mode, int loop)
{
clHCA hca(ciphKey1, ciphKey2, subKey);
void *wavptr = nullptr;
size_t sz = 0;
hca.Analyze(wavptr, sz, hcafilename, volume, mode, loop);
if (wavptr)
{
unsigned int decodefromblock = decodefromsample / hca.get_channelCount() >> 10;
if (decodefromblock >= hca.get_blockCount())
{
decodefromblock = 0;
}
filelistmtx.lock();
filelist[wavptr].first = std::move(hca);
filelist[wavptr].second = decodefromblock;
filelistmtx.unlock();
datasem.notify();
}
return std::pair<void *, size_t>(wavptr, sz);
}
void HCADecodeService::Main_Thread()
{
while (true)
{
datasem.wait();
if (shutdown)
{
break;
}
filelistmtx.lock();
load_next_request();
filelistmtx.unlock();
numchannels = workingfile.get_channelCount();
populate_block_list();
currindex = 0;
for (unsigned int i = 0; i < numthreads; ++i)
{
workersem[i].notify();
}
mainsem.wait(numthreads);
workingrequest = nullptr;
std::unique_lock<std::mutex> lck(workingmtx);
workingcv.notify_all();
}
join_workers();
}
void HCADecodeService::Decode_Thread(unsigned int id)
{
while (!shutdown)
{
workersem[id].wait();
unsigned int offset = id * numchannels;
workingfile.PrepDecode(channels + offset);
unsigned int bindex = currindex++;
while (bindex < blocklistsz)
{
workingfile.AsyncDecode(channels + offset, wavebuffer + (offset << 7), blocks[bindex], workingrequest, chunksize, stopcurrent);
bindex = currindex++;
}
mainsem.notify();
}
}
void HCADecodeService::load_next_request()
{
auto it = filelist.begin();
workingrequest = it->first;
workingfile = std::move(it->second.first);
startingblock = it->second.second;
filelist.erase(it);
stopcurrent = false;
}
void HCADecodeService::populate_block_list()
{
unsigned int blockCount = workingfile.get_blockCount();
unsigned int sz = blockCount / chunksize + (blockCount % chunksize != 0);
if (sz > blocklistsz)
{
delete[] blocks;
blocks = new unsigned int[sz];
blocklistsz = sz;
}
unsigned int lim = sz * chunksize + startingblock;
for (unsigned int i = (startingblock / chunksize) * chunksize, j = 0; i < lim; i += chunksize, ++j)
{
blocks[j] = i % blockCount;
}
}
void HCADecodeService::join_workers()
{
for (unsigned int i = 0; i < numthreads; ++i)
{
workersem[i].notify();
workerthreads[i].join();
}
}