2 |
* * |
* * |
3 |
* LinuxSampler - modular, streaming capable sampler * |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003 by Benno Senoner * |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
|
* Copyright (C) 2005 - 2008 Christian Schoenebeck * |
7 |
* * |
* * |
8 |
* This program is free software; you can redistribute it and/or modify * |
* This program is free software; you can redistribute it and/or modify * |
9 |
* it under the terms of the GNU General Public License as published by * |
* it under the terms of the GNU General Public License as published by * |
49 |
void DiskThread::Reset() { |
void DiskThread::Reset() { |
50 |
bool running = this->IsRunning(); |
bool running = this->IsRunning(); |
51 |
if (running) this->StopThread(); |
if (running) this->StopThread(); |
52 |
for (int i = 0; i < MAX_INPUT_STREAMS; i++) { |
for (int i = 0; i < CONFIG_MAX_STREAMS; i++) { |
53 |
pStreams[i]->Kill(); |
pStreams[i]->Kill(); |
54 |
} |
} |
55 |
for (int i = 1; i <= MAX_INPUT_STREAMS; i++) { |
for (int i = 1; i <= CONFIG_MAX_STREAMS; i++) { |
56 |
pCreatedStreams[i] = NULL; |
pCreatedStreams[i] = NULL; |
57 |
} |
} |
58 |
GhostQueue->init(); |
GhostQueue->init(); |
59 |
CreationQueue->init(); |
CreationQueue->init(); |
60 |
DeletionQueue->init(); |
DeletionQueue->init(); |
61 |
ActiveStreamCount = 0; |
DeletionNotificationQueue.init(); |
62 |
|
|
63 |
|
// make sure that all DimensionRegions are released |
64 |
|
while (DeleteDimregQueue->read_space() > 0) { |
65 |
|
::gig::DimensionRegion* dimreg; |
66 |
|
DeleteDimregQueue->pop(&dimreg); |
67 |
|
pInstruments->HandBackDimReg(dimreg); |
68 |
|
} |
69 |
|
DeleteDimregQueue->init(); |
70 |
|
SetActiveStreamCount(0); |
71 |
ActiveStreamCountMax = 0; |
ActiveStreamCountMax = 0; |
72 |
if (running) this->StartThread(); // start thread only if it was running before |
if (running) this->StartThread(); // start thread only if it was running before |
73 |
} |
} |
95 |
std::stringstream ss; |
std::stringstream ss; |
96 |
for (uint i = 0; i < this->Streams; i++) { |
for (uint i = 0; i < this->Streams; i++) { |
97 |
if (pStreams[i]->GetState() == Stream::state_unused) continue; |
if (pStreams[i]->GetState() == Stream::state_unused) continue; |
98 |
uint bufferfill = (uint) ((float) pStreams[i]->GetReadSpace() / (float) STREAM_BUFFER_SIZE * 100); |
uint bufferfill = (uint) ((float) pStreams[i]->GetReadSpace() / (float) CONFIG_STREAM_BUFFER_SIZE * 100); |
99 |
uint streamid = (uint) pStreams[i]->GetHandle(); |
uint streamid = (uint) pStreams[i]->GetHandle(); |
100 |
if (!streamid) continue; |
if (!streamid) continue; |
101 |
|
|
112 |
* Returns -1 if command queue or pickup pool is full, 0 on success (will be |
* Returns -1 if command queue or pickup pool is full, 0 on success (will be |
113 |
* called by audio thread within the voice class). |
* called by audio thread within the voice class). |
114 |
*/ |
*/ |
115 |
int DiskThread::OrderNewStream(Stream::reference_t* pStreamRef, ::gig::Sample* pSample, unsigned long SampleOffset, bool DoLoop) { |
int DiskThread::OrderNewStream(Stream::reference_t* pStreamRef, ::gig::DimensionRegion* pDimRgn, unsigned long SampleOffset, bool DoLoop) { |
116 |
dmsg(4,("Disk Thread: new stream ordered\n")); |
dmsg(4,("Disk Thread: new stream ordered\n")); |
117 |
if (CreationQueue->write_space() < 1) { |
if (CreationQueue->write_space() < 1) { |
118 |
dmsg(1,("DiskThread: Order queue full!\n")); |
dmsg(1,("DiskThread: Order queue full!\n")); |
119 |
return -1; |
return -1; |
120 |
} |
} |
121 |
|
|
122 |
|
const Stream::OrderID_t newOrder = CreateOrderID(); |
123 |
|
if (!newOrder) { |
124 |
|
dmsg(1,("DiskThread: there was no free slot\n")); |
125 |
|
return -1; // there was no free slot |
126 |
|
} |
127 |
|
|
128 |
pStreamRef->State = Stream::state_active; |
pStreamRef->State = Stream::state_active; |
129 |
pStreamRef->OrderID = CreateOrderID(); |
pStreamRef->OrderID = newOrder; |
130 |
pStreamRef->hStream = CreateHandle(); |
pStreamRef->hStream = CreateHandle(); |
131 |
pStreamRef->pStream = NULL; // a stream has to be activated by the disk thread first |
pStreamRef->pStream = NULL; // a stream has to be activated by the disk thread first |
132 |
|
|
|
if (!pStreamRef->OrderID) return -1; // there was no free slot |
|
|
|
|
133 |
create_command_t cmd; |
create_command_t cmd; |
134 |
cmd.OrderID = pStreamRef->OrderID; |
cmd.OrderID = pStreamRef->OrderID; |
135 |
cmd.hStream = pStreamRef->hStream; |
cmd.hStream = pStreamRef->hStream; |
136 |
cmd.pStreamRef = pStreamRef; |
cmd.pStreamRef = pStreamRef; |
137 |
cmd.pSample = pSample; |
cmd.pDimRgn = pDimRgn; |
138 |
cmd.SampleOffset = SampleOffset; |
cmd.SampleOffset = SampleOffset; |
139 |
cmd.DoLoop = DoLoop; |
cmd.DoLoop = DoLoop; |
140 |
|
|
143 |
} |
} |
144 |
|
|
145 |
/** |
/** |
146 |
* Returns -1 if command queue is full, 0 on success (will be called by audio |
* Request the disk thread to delete the given disk stream. This method |
147 |
* thread within the voice class). |
* will return immediately, thus it won't block until the respective voice |
148 |
|
* was actually deleted. (Called by audio thread within the Voice class). |
149 |
|
* |
150 |
|
* @param pStreamRef - stream that shall be deleted |
151 |
|
* @param bRequestNotification - set to true in case you want to receive a |
152 |
|
* notification once the stream has actually |
153 |
|
* been deleted |
154 |
|
* @returns 0 on success, -1 if command queue is full |
155 |
|
* @see AskForDeletedStream() |
156 |
*/ |
*/ |
157 |
int DiskThread::OrderDeletionOfStream(Stream::reference_t* pStreamRef) { |
int DiskThread::OrderDeletionOfStream(Stream::reference_t* pStreamRef, bool bRequestNotification) { |
158 |
dmsg(4,("Disk Thread: stream deletion ordered\n")); |
dmsg(4,("Disk Thread: stream deletion ordered\n")); |
159 |
if (DeletionQueue->write_space() < 1) { |
if (DeletionQueue->write_space() < 1) { |
160 |
dmsg(1,("DiskThread: Deletion queue full!\n")); |
dmsg(1,("DiskThread: Deletion queue full!\n")); |
165 |
cmd.pStream = pStreamRef->pStream; |
cmd.pStream = pStreamRef->pStream; |
166 |
cmd.hStream = pStreamRef->hStream; |
cmd.hStream = pStreamRef->hStream; |
167 |
cmd.OrderID = pStreamRef->OrderID; |
cmd.OrderID = pStreamRef->OrderID; |
168 |
|
cmd.bNotify = bRequestNotification; |
169 |
|
|
170 |
DeletionQueue->push(&cmd); |
DeletionQueue->push(&cmd); |
171 |
return 0; |
return 0; |
172 |
} |
} |
173 |
|
|
174 |
/** |
/** |
175 |
|
* Tell the disk thread to release a dimension region that belongs |
176 |
|
* to an instrument which isn't loaded anymore. The disk thread |
177 |
|
* will hand back the dimension region to the instrument resource |
178 |
|
* manager. (OrderDeletionOfDimreg is called from the audio thread |
179 |
|
* when a voice dies.) |
180 |
|
*/ |
181 |
|
int DiskThread::OrderDeletionOfDimreg(::gig::DimensionRegion* dimreg) { |
182 |
|
dmsg(4,("Disk Thread: dimreg deletion ordered\n")); |
183 |
|
if (DeleteDimregQueue->write_space() < 1) { |
184 |
|
dmsg(1,("DiskThread: DeleteDimreg queue full!\n")); |
185 |
|
return -1; |
186 |
|
} |
187 |
|
DeleteDimregQueue->push(&dimreg); |
188 |
|
return 0; |
189 |
|
} |
190 |
|
|
191 |
|
/** |
192 |
* Returns the pointer to a disk stream if the ordered disk stream |
* Returns the pointer to a disk stream if the ordered disk stream |
193 |
* represented by the \a StreamOrderID was already activated by the disk |
* represented by the \a StreamOrderID was already activated by the disk |
194 |
* thread, returns NULL otherwise. If the call was successful, thus if it |
* thread, returns NULL otherwise. If the call was successful, thus if it |
213 |
return NULL; |
return NULL; |
214 |
} |
} |
215 |
|
|
216 |
|
/** |
217 |
|
* In case the original sender requested a notification with his stream |
218 |
|
* deletion order, he can use this method to poll if the respective stream |
219 |
|
* has actually been deleted. |
220 |
|
* |
221 |
|
* @returns handle / identifier of the deleted stream, or |
222 |
|
* Stream::INVALID_HANDLE if no notification present |
223 |
|
*/ |
224 |
|
Stream::Handle DiskThread::AskForDeletedStream() { |
225 |
|
if (DeletionNotificationQueue.read_space()) { |
226 |
|
Stream::Handle hStream; |
227 |
|
DeletionNotificationQueue.pop(&hStream); |
228 |
|
return hStream; |
229 |
|
} else return Stream::INVALID_HANDLE; // no notification received yet |
230 |
|
} |
231 |
|
|
232 |
|
|
233 |
|
|
234 |
// ######################################################################### |
// ######################################################################### |
236 |
// # (following code should only be executed by the disk thread) |
// # (following code should only be executed by the disk thread) |
237 |
|
|
238 |
|
|
239 |
DiskThread::DiskThread(uint BufferWrapElements) : Thread(false, 1, -2) { |
DiskThread::DiskThread(uint BufferWrapElements, InstrumentResourceManager* pInstruments) : |
240 |
CreationQueue = new RingBuffer<create_command_t>(1024); |
Thread(true, false, 1, -2), |
241 |
DeletionQueue = new RingBuffer<delete_command_t>(1024); |
pInstruments(pInstruments), |
242 |
GhostQueue = new RingBuffer<Stream::Handle>(MAX_INPUT_STREAMS); |
DeletionNotificationQueue(4*CONFIG_MAX_STREAMS) |
243 |
Streams = MAX_INPUT_STREAMS; |
{ |
244 |
RefillStreamsPerRun = REFILL_STREAMS_PER_RUN; |
DecompressionBuffer = ::gig::Sample::CreateDecompressionBuffer(CONFIG_STREAM_MAX_REFILL_SIZE); |
245 |
for (int i = 0; i < MAX_INPUT_STREAMS; i++) { |
CreationQueue = new RingBuffer<create_command_t,false>(4*CONFIG_MAX_STREAMS); |
246 |
pStreams[i] = new Stream(STREAM_BUFFER_SIZE, BufferWrapElements); // 131072 sample words |
DeletionQueue = new RingBuffer<delete_command_t,false>(4*CONFIG_MAX_STREAMS); |
247 |
|
GhostQueue = new RingBuffer<delete_command_t,false>(CONFIG_MAX_STREAMS); |
248 |
|
DeleteDimregQueue = new RingBuffer< ::gig::DimensionRegion*,false>(4*CONFIG_MAX_STREAMS); |
249 |
|
Streams = CONFIG_MAX_STREAMS; |
250 |
|
RefillStreamsPerRun = CONFIG_REFILL_STREAMS_PER_RUN; |
251 |
|
for (int i = 0; i < CONFIG_MAX_STREAMS; i++) { |
252 |
|
pStreams[i] = new Stream(&DecompressionBuffer, CONFIG_STREAM_BUFFER_SIZE, BufferWrapElements); // 131072 sample words |
253 |
} |
} |
254 |
for (int i = 1; i <= MAX_INPUT_STREAMS; i++) { |
for (int i = 1; i <= CONFIG_MAX_STREAMS; i++) { |
255 |
pCreatedStreams[i] = NULL; |
pCreatedStreams[i] = NULL; |
256 |
} |
} |
257 |
|
ActiveStreamCountMax = 0; |
258 |
} |
} |
259 |
|
|
260 |
DiskThread::~DiskThread() { |
DiskThread::~DiskThread() { |
261 |
for (int i = 0; i < MAX_INPUT_STREAMS; i++) { |
for (int i = 0; i < CONFIG_MAX_STREAMS; i++) { |
262 |
if (pStreams[i]) delete pStreams[i]; |
if (pStreams[i]) delete pStreams[i]; |
263 |
} |
} |
264 |
if (CreationQueue) delete CreationQueue; |
if (CreationQueue) delete CreationQueue; |
265 |
if (DeletionQueue) delete DeletionQueue; |
if (DeletionQueue) delete DeletionQueue; |
266 |
if (GhostQueue) delete GhostQueue; |
if (GhostQueue) delete GhostQueue; |
267 |
|
if (DeleteDimregQueue) delete DeleteDimregQueue; |
268 |
|
::gig::Sample::DestroyDecompressionBuffer(DecompressionBuffer); |
269 |
} |
} |
270 |
|
|
271 |
int DiskThread::Main() { |
int DiskThread::Main() { |
272 |
dmsg(3,("Disk thread running\n")); |
dmsg(3,("Disk thread running\n")); |
273 |
while (true) { |
while (true) { |
274 |
|
#if !defined(WIN32) |
275 |
|
pthread_testcancel(); // mandatory for OSX |
276 |
|
#endif |
277 |
|
#if CONFIG_PTHREAD_TESTCANCEL |
278 |
|
TestCancel(); |
279 |
|
#endif |
280 |
IsIdle = true; // will be set to false if a stream got filled |
IsIdle = true; // will be set to false if a stream got filled |
281 |
|
|
282 |
// if there are ghost streams, delete them |
// if there are ghost streams, delete them |
283 |
for (int i = 0; i < GhostQueue->read_space(); i++) { //FIXME: unefficient |
for (int i = 0; i < GhostQueue->read_space(); i++) { //FIXME: unefficient |
284 |
Stream::Handle hGhostStream; |
delete_command_t ghostStream; |
285 |
GhostQueue->pop(&hGhostStream); |
GhostQueue->pop(&ghostStream); |
286 |
bool found = false; |
bool found = false; |
287 |
for (int i = 0; i < this->Streams; i++) { |
for (int i = 0; i < this->Streams; i++) { |
288 |
if (pStreams[i]->GetHandle() == hGhostStream) { |
if (pStreams[i]->GetHandle() == ghostStream.hStream) { |
289 |
pStreams[i]->Kill(); |
pStreams[i]->Kill(); |
290 |
found = true; |
found = true; |
291 |
|
// if original sender requested a notification, let him know now |
292 |
|
if (ghostStream.bNotify) |
293 |
|
DeletionNotificationQueue.push(&ghostStream.hStream); |
294 |
break; |
break; |
295 |
} |
} |
296 |
} |
} |
297 |
if (!found) GhostQueue->push(&hGhostStream); // put ghost stream handle back to the queue |
if (!found) GhostQueue->push(&ghostStream); // put ghost stream handle back to the queue |
298 |
} |
} |
299 |
|
|
300 |
// if there are creation commands, create new streams |
// if there are creation commands, create new streams |
305 |
} |
} |
306 |
|
|
307 |
// if there are deletion commands, delete those streams |
// if there are deletion commands, delete those streams |
308 |
while (Stream::UnusedStreams < Streams && DeletionQueue->read_space() > 0) { |
while (Stream::UnusedStreams < Stream::TotalStreams && DeletionQueue->read_space() > 0) { |
309 |
delete_command_t command; |
delete_command_t command; |
310 |
DeletionQueue->pop(&command); |
DeletionQueue->pop(&command); |
311 |
DeleteStream(command); |
DeleteStream(command); |
312 |
} |
} |
313 |
|
|
314 |
|
// release DimensionRegions that belong to instruments |
315 |
|
// that are no longer loaded |
316 |
|
while (DeleteDimregQueue->read_space() > 0) { |
317 |
|
::gig::DimensionRegion* dimreg; |
318 |
|
DeleteDimregQueue->pop(&dimreg); |
319 |
|
pInstruments->HandBackDimReg(dimreg); |
320 |
|
} |
321 |
|
|
322 |
RefillStreams(); // refill the most empty streams |
RefillStreams(); // refill the most empty streams |
323 |
|
|
324 |
// if nothing was done during this iteration (eg no streambuffer |
// if nothing was done during this iteration (eg no streambuffer |
325 |
// filled with data) then sleep for 50ms |
// filled with data) then sleep for 30ms |
326 |
if (IsIdle) usleep(30000); |
if (IsIdle) usleep(30000); |
327 |
|
|
328 |
int streamsInUsage = 0; |
int streamsInUsage = 0; |
329 |
for (int i = Streams - 1; i >= 0; i--) { |
for (int i = Streams - 1; i >= 0; i--) { |
330 |
if (pStreams[i]->GetState() != Stream::state_unused) streamsInUsage++; |
if (pStreams[i]->GetState() != Stream::state_unused) streamsInUsage++; |
331 |
} |
} |
332 |
ActiveStreamCount = streamsInUsage; |
SetActiveStreamCount(streamsInUsage); |
333 |
if (streamsInUsage > ActiveStreamCountMax) ActiveStreamCountMax = streamsInUsage; |
if (streamsInUsage > ActiveStreamCountMax) ActiveStreamCountMax = streamsInUsage; |
334 |
} |
} |
335 |
|
|
349 |
std::cerr << "No unused stream found (OrderID:" << Command.OrderID << ") - report if this happens, this is a bug!\n" << std::flush; |
std::cerr << "No unused stream found (OrderID:" << Command.OrderID << ") - report if this happens, this is a bug!\n" << std::flush; |
350 |
return; |
return; |
351 |
} |
} |
352 |
newstream->Launch(Command.hStream, Command.pStreamRef, Command.pSample, Command.SampleOffset, Command.DoLoop); |
newstream->Launch(Command.hStream, Command.pStreamRef, Command.pDimRgn, Command.SampleOffset, Command.DoLoop); |
353 |
dmsg(4,("new Stream launched by disk thread (OrderID:%d,StreamHandle:%d)\n", Command.OrderID, Command.hStream)); |
dmsg(4,("new Stream launched by disk thread (OrderID:%d,StreamHandle:%d)\n", Command.OrderID, Command.hStream)); |
354 |
if (pCreatedStreams[Command.OrderID] != SLOT_RESERVED) { |
if (pCreatedStreams[Command.OrderID] != SLOT_RESERVED) { |
355 |
std::cerr << "DiskThread: Slot " << Command.OrderID << " already occupied! Please report this!\n" << std::flush; |
std::cerr << "DiskThread: Slot " << Command.OrderID << " already occupied! Please report this!\n" << std::flush; |
368 |
if (pStream && pStream != SLOT_RESERVED) { |
if (pStream && pStream != SLOT_RESERVED) { |
369 |
pStream->Kill(); |
pStream->Kill(); |
370 |
pCreatedStreams[Command.OrderID] = NULL; // free slot for new order |
pCreatedStreams[Command.OrderID] = NULL; // free slot for new order |
371 |
|
// if original sender requested a notification, let him know now |
372 |
|
if (Command.bNotify) |
373 |
|
DeletionNotificationQueue.push(&Command.hStream); |
374 |
return; |
return; |
375 |
} |
} |
376 |
|
|
377 |
// the stream was not created yet |
// the stream was not created yet |
378 |
if (GhostQueue->write_space() > 0) { |
if (GhostQueue->write_space() > 0) { |
379 |
GhostQueue->push(&Command.hStream); |
GhostQueue->push(&Command); |
380 |
|
} else { // error, queue full |
381 |
|
if (Command.bNotify) { |
382 |
|
dmsg(1,("DiskThread: GhostQueue full! (might lead to dead lock with instrument editor!)\n")); |
383 |
|
} else { |
384 |
|
dmsg(1,("DiskThread: GhostQueue full!\n")); |
385 |
|
} |
386 |
} |
} |
|
else dmsg(1,("DiskThread: GhostQueue full!\n")); |
|
387 |
} |
} |
388 |
} |
} |
389 |
|
|
403 |
|
|
404 |
int capped_writespace = writespace; |
int capped_writespace = writespace; |
405 |
// if there is too much buffer space available then cut the read/write |
// if there is too much buffer space available then cut the read/write |
406 |
// size to MAX_REFILL_SIZE which is by default 65536 samples = 256KBytes |
// size to CONFIG_STREAM_MAX_REFILL_SIZE which is by default 65536 samples = 256KBytes |
407 |
if (writespace > MAX_REFILL_SIZE) capped_writespace = MAX_REFILL_SIZE; |
if (writespace > CONFIG_STREAM_MAX_REFILL_SIZE) capped_writespace = CONFIG_STREAM_MAX_REFILL_SIZE; |
408 |
|
|
409 |
// adjust the amount to read in order to ensure that the buffer wraps correctly |
// adjust the amount to read in order to ensure that the buffer wraps correctly |
410 |
int read_amount = pStreams[i]->AdjustWriteSpaceToAvoidBoundary(writespace, capped_writespace); |
int read_amount = pStreams[i]->AdjustWriteSpaceToAvoidBoundary(writespace, capped_writespace); |
411 |
// if we wasn't able to refill one of the stream buffers by more than |
// if we wasn't able to refill one of the stream buffers by more than |
412 |
// MIN_REFILL_SIZE we'll send the disk thread to sleep later |
// CONFIG_STREAM_MIN_REFILL_SIZE we'll send the disk thread to sleep later |
413 |
if (pStreams[i]->ReadAhead(read_amount) > MIN_REFILL_SIZE) this->IsIdle = false; |
if (pStreams[i]->ReadAhead(read_amount) > CONFIG_STREAM_MIN_REFILL_SIZE) this->IsIdle = false; |
414 |
} |
} |
415 |
} |
} |
416 |
} |
} |
425 |
|
|
426 |
/// order ID Generator |
/// order ID Generator |
427 |
Stream::OrderID_t DiskThread::CreateOrderID() { |
Stream::OrderID_t DiskThread::CreateOrderID() { |
428 |
static uint32_t counter = 0; |
static Stream::OrderID_t counter(0); |
429 |
for (int i = 0; i < MAX_INPUT_STREAMS; i++) { |
for (int i = 0; i < CONFIG_MAX_STREAMS; i++) { |
430 |
if (counter == MAX_INPUT_STREAMS) counter = 1; // we use '0' as 'invalid order' only, so we skip 0 |
if (counter == CONFIG_MAX_STREAMS) counter = 1; // we use '0' as 'invalid order' only, so we skip 0 |
431 |
else counter++; |
else counter++; |
432 |
if (!pCreatedStreams[counter]) { |
if (!pCreatedStreams[counter]) { |
433 |
pCreatedStreams[counter] = SLOT_RESERVED; // mark this slot as reserved |
pCreatedStreams[counter] = SLOT_RESERVED; // mark this slot as reserved |