3 |
* LinuxSampler - modular, streaming capable sampler * |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
* Copyright (C) 2005 Christian Schoenebeck * |
* Copyright (C) 2005 - 2007 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 * |
58 |
GhostQueue->init(); |
GhostQueue->init(); |
59 |
CreationQueue->init(); |
CreationQueue->init(); |
60 |
DeletionQueue->init(); |
DeletionQueue->init(); |
61 |
|
DeletionNotificationQueue.init(); |
62 |
|
DeleteDimregQueue->init(); |
63 |
ActiveStreamCount = 0; |
ActiveStreamCount = 0; |
64 |
ActiveStreamCountMax = 0; |
ActiveStreamCountMax = 0; |
65 |
if (running) this->StartThread(); // start thread only if it was running before |
if (running) this->StartThread(); // start thread only if it was running before |
105 |
* 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 |
106 |
* called by audio thread within the voice class). |
* called by audio thread within the voice class). |
107 |
*/ |
*/ |
108 |
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) { |
109 |
dmsg(4,("Disk Thread: new stream ordered\n")); |
dmsg(4,("Disk Thread: new stream ordered\n")); |
110 |
if (CreationQueue->write_space() < 1) { |
if (CreationQueue->write_space() < 1) { |
111 |
dmsg(1,("DiskThread: Order queue full!\n")); |
dmsg(1,("DiskThread: Order queue full!\n")); |
127 |
cmd.OrderID = pStreamRef->OrderID; |
cmd.OrderID = pStreamRef->OrderID; |
128 |
cmd.hStream = pStreamRef->hStream; |
cmd.hStream = pStreamRef->hStream; |
129 |
cmd.pStreamRef = pStreamRef; |
cmd.pStreamRef = pStreamRef; |
130 |
cmd.pSample = pSample; |
cmd.pDimRgn = pDimRgn; |
131 |
cmd.SampleOffset = SampleOffset; |
cmd.SampleOffset = SampleOffset; |
132 |
cmd.DoLoop = DoLoop; |
cmd.DoLoop = DoLoop; |
133 |
|
|
136 |
} |
} |
137 |
|
|
138 |
/** |
/** |
139 |
* 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 |
140 |
* thread within the voice class). |
* will return immediately, thus it won't block until the respective voice |
141 |
|
* was actually deleted. (Called by audio thread within the Voice class). |
142 |
|
* |
143 |
|
* @param pStreamRef - stream that shall be deleted |
144 |
|
* @param bRequestNotification - set to true in case you want to receive a |
145 |
|
* notification once the stream has actually |
146 |
|
* been deleted |
147 |
|
* @returns 0 on success, -1 if command queue is full |
148 |
|
* @see AskForDeletedStream() |
149 |
*/ |
*/ |
150 |
int DiskThread::OrderDeletionOfStream(Stream::reference_t* pStreamRef) { |
int DiskThread::OrderDeletionOfStream(Stream::reference_t* pStreamRef, bool bRequestNotification) { |
151 |
dmsg(4,("Disk Thread: stream deletion ordered\n")); |
dmsg(4,("Disk Thread: stream deletion ordered\n")); |
152 |
if (DeletionQueue->write_space() < 1) { |
if (DeletionQueue->write_space() < 1) { |
153 |
dmsg(1,("DiskThread: Deletion queue full!\n")); |
dmsg(1,("DiskThread: Deletion queue full!\n")); |
158 |
cmd.pStream = pStreamRef->pStream; |
cmd.pStream = pStreamRef->pStream; |
159 |
cmd.hStream = pStreamRef->hStream; |
cmd.hStream = pStreamRef->hStream; |
160 |
cmd.OrderID = pStreamRef->OrderID; |
cmd.OrderID = pStreamRef->OrderID; |
161 |
|
cmd.bNotify = bRequestNotification; |
162 |
|
|
163 |
DeletionQueue->push(&cmd); |
DeletionQueue->push(&cmd); |
164 |
return 0; |
return 0; |
165 |
} |
} |
166 |
|
|
167 |
/** |
/** |
168 |
|
* Tell the disk thread to release a dimension region that belongs |
169 |
|
* to an instrument which isn't loaded anymore. The disk thread |
170 |
|
* will hand back the dimension region to the instrument resource |
171 |
|
* manager. (OrderDeletionOfDimreg is called from the audio thread |
172 |
|
* when a voice dies.) |
173 |
|
*/ |
174 |
|
int DiskThread::OrderDeletionOfDimreg(::gig::DimensionRegion* dimreg) { |
175 |
|
dmsg(4,("Disk Thread: dimreg deletion ordered\n")); |
176 |
|
if (DeleteDimregQueue->write_space() < 1) { |
177 |
|
dmsg(1,("DiskThread: DeleteDimreg queue full!\n")); |
178 |
|
return -1; |
179 |
|
} |
180 |
|
DeleteDimregQueue->push(&dimreg); |
181 |
|
return 0; |
182 |
|
} |
183 |
|
|
184 |
|
/** |
185 |
* Returns the pointer to a disk stream if the ordered disk stream |
* Returns the pointer to a disk stream if the ordered disk stream |
186 |
* represented by the \a StreamOrderID was already activated by the disk |
* represented by the \a StreamOrderID was already activated by the disk |
187 |
* thread, returns NULL otherwise. If the call was successful, thus if it |
* thread, returns NULL otherwise. If the call was successful, thus if it |
206 |
return NULL; |
return NULL; |
207 |
} |
} |
208 |
|
|
209 |
|
/** |
210 |
|
* In case the original sender requested a notification with his stream |
211 |
|
* deletion order, he can use this method to poll if the respective stream |
212 |
|
* has actually been deleted. |
213 |
|
* |
214 |
|
* @returns handle / identifier of the deleted stream, or |
215 |
|
* Stream::INVALID_HANDLE if no notification present |
216 |
|
*/ |
217 |
|
Stream::Handle DiskThread::AskForDeletedStream() { |
218 |
|
if (DeletionNotificationQueue.read_space()) { |
219 |
|
Stream::Handle hStream; |
220 |
|
DeletionNotificationQueue.pop(&hStream); |
221 |
|
return hStream; |
222 |
|
} else return Stream::INVALID_HANDLE; // no notification received yet |
223 |
|
} |
224 |
|
|
225 |
|
|
226 |
|
|
227 |
// ######################################################################### |
// ######################################################################### |
229 |
// # (following code should only be executed by the disk thread) |
// # (following code should only be executed by the disk thread) |
230 |
|
|
231 |
|
|
232 |
DiskThread::DiskThread(uint BufferWrapElements) : Thread(true, false, 1, -2) { |
DiskThread::DiskThread(uint BufferWrapElements, InstrumentResourceManager* pInstruments) : |
233 |
|
Thread(true, false, 1, -2), |
234 |
|
pInstruments(pInstruments), |
235 |
|
DeletionNotificationQueue(4*CONFIG_MAX_STREAMS) |
236 |
|
{ |
237 |
DecompressionBuffer = ::gig::Sample::CreateDecompressionBuffer(CONFIG_STREAM_MAX_REFILL_SIZE); |
DecompressionBuffer = ::gig::Sample::CreateDecompressionBuffer(CONFIG_STREAM_MAX_REFILL_SIZE); |
238 |
CreationQueue = new RingBuffer<create_command_t>(1024); |
CreationQueue = new RingBuffer<create_command_t,false>(4*CONFIG_MAX_STREAMS); |
239 |
DeletionQueue = new RingBuffer<delete_command_t>(1024); |
DeletionQueue = new RingBuffer<delete_command_t,false>(4*CONFIG_MAX_STREAMS); |
240 |
GhostQueue = new RingBuffer<Stream::Handle>(CONFIG_MAX_STREAMS); |
GhostQueue = new RingBuffer<delete_command_t,false>(CONFIG_MAX_STREAMS); |
241 |
|
DeleteDimregQueue = new RingBuffer< ::gig::DimensionRegion*,false>(4*CONFIG_MAX_STREAMS); |
242 |
Streams = CONFIG_MAX_STREAMS; |
Streams = CONFIG_MAX_STREAMS; |
243 |
RefillStreamsPerRun = CONFIG_REFILL_STREAMS_PER_RUN; |
RefillStreamsPerRun = CONFIG_REFILL_STREAMS_PER_RUN; |
244 |
for (int i = 0; i < CONFIG_MAX_STREAMS; i++) { |
for (int i = 0; i < CONFIG_MAX_STREAMS; i++) { |
247 |
for (int i = 1; i <= CONFIG_MAX_STREAMS; i++) { |
for (int i = 1; i <= CONFIG_MAX_STREAMS; i++) { |
248 |
pCreatedStreams[i] = NULL; |
pCreatedStreams[i] = NULL; |
249 |
} |
} |
250 |
|
ActiveStreamCountMax = 0; |
251 |
} |
} |
252 |
|
|
253 |
DiskThread::~DiskThread() { |
DiskThread::~DiskThread() { |
257 |
if (CreationQueue) delete CreationQueue; |
if (CreationQueue) delete CreationQueue; |
258 |
if (DeletionQueue) delete DeletionQueue; |
if (DeletionQueue) delete DeletionQueue; |
259 |
if (GhostQueue) delete GhostQueue; |
if (GhostQueue) delete GhostQueue; |
260 |
|
if (DeleteDimregQueue) delete DeleteDimregQueue; |
261 |
::gig::Sample::DestroyDecompressionBuffer(DecompressionBuffer); |
::gig::Sample::DestroyDecompressionBuffer(DecompressionBuffer); |
262 |
} |
} |
263 |
|
|
264 |
int DiskThread::Main() { |
int DiskThread::Main() { |
265 |
dmsg(3,("Disk thread running\n")); |
dmsg(3,("Disk thread running\n")); |
266 |
while (true) { |
while (true) { |
267 |
|
#if !defined(WIN32) |
268 |
pthread_testcancel(); // mandatory for OSX |
pthread_testcancel(); // mandatory for OSX |
269 |
|
#endif |
270 |
IsIdle = true; // will be set to false if a stream got filled |
IsIdle = true; // will be set to false if a stream got filled |
271 |
|
|
272 |
// if there are ghost streams, delete them |
// if there are ghost streams, delete them |
273 |
for (int i = 0; i < GhostQueue->read_space(); i++) { //FIXME: unefficient |
for (int i = 0; i < GhostQueue->read_space(); i++) { //FIXME: unefficient |
274 |
Stream::Handle hGhostStream; |
delete_command_t ghostStream; |
275 |
GhostQueue->pop(&hGhostStream); |
GhostQueue->pop(&ghostStream); |
276 |
bool found = false; |
bool found = false; |
277 |
for (int i = 0; i < this->Streams; i++) { |
for (int i = 0; i < this->Streams; i++) { |
278 |
if (pStreams[i]->GetHandle() == hGhostStream) { |
if (pStreams[i]->GetHandle() == ghostStream.hStream) { |
279 |
pStreams[i]->Kill(); |
pStreams[i]->Kill(); |
280 |
found = true; |
found = true; |
281 |
|
// if original sender requested a notification, let him know now |
282 |
|
if (ghostStream.bNotify) |
283 |
|
DeletionNotificationQueue.push(&ghostStream.hStream); |
284 |
break; |
break; |
285 |
} |
} |
286 |
} |
} |
287 |
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 |
288 |
} |
} |
289 |
|
|
290 |
// if there are creation commands, create new streams |
// if there are creation commands, create new streams |
301 |
DeleteStream(command); |
DeleteStream(command); |
302 |
} |
} |
303 |
|
|
304 |
|
// release DimensionRegions that belong to instruments |
305 |
|
// that are no longer loaded |
306 |
|
while (DeleteDimregQueue->read_space() > 0) { |
307 |
|
::gig::DimensionRegion* dimreg; |
308 |
|
DeleteDimregQueue->pop(&dimreg); |
309 |
|
pInstruments->HandBackDimReg(dimreg); |
310 |
|
} |
311 |
|
|
312 |
RefillStreams(); // refill the most empty streams |
RefillStreams(); // refill the most empty streams |
313 |
|
|
314 |
// if nothing was done during this iteration (eg no streambuffer |
// if nothing was done during this iteration (eg no streambuffer |
315 |
// filled with data) then sleep for 50ms |
// filled with data) then sleep for 30ms |
316 |
if (IsIdle) usleep(30000); |
if (IsIdle) usleep(30000); |
317 |
|
|
318 |
int streamsInUsage = 0; |
int streamsInUsage = 0; |
339 |
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; |
340 |
return; |
return; |
341 |
} |
} |
342 |
newstream->Launch(Command.hStream, Command.pStreamRef, Command.pSample, Command.SampleOffset, Command.DoLoop); |
newstream->Launch(Command.hStream, Command.pStreamRef, Command.pDimRgn, Command.SampleOffset, Command.DoLoop); |
343 |
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)); |
344 |
if (pCreatedStreams[Command.OrderID] != SLOT_RESERVED) { |
if (pCreatedStreams[Command.OrderID] != SLOT_RESERVED) { |
345 |
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; |
358 |
if (pStream && pStream != SLOT_RESERVED) { |
if (pStream && pStream != SLOT_RESERVED) { |
359 |
pStream->Kill(); |
pStream->Kill(); |
360 |
pCreatedStreams[Command.OrderID] = NULL; // free slot for new order |
pCreatedStreams[Command.OrderID] = NULL; // free slot for new order |
361 |
|
// if original sender requested a notification, let him know now |
362 |
|
if (Command.bNotify) |
363 |
|
DeletionNotificationQueue.push(&Command.hStream); |
364 |
return; |
return; |
365 |
} |
} |
366 |
|
|
367 |
// the stream was not created yet |
// the stream was not created yet |
368 |
if (GhostQueue->write_space() > 0) { |
if (GhostQueue->write_space() > 0) { |
369 |
GhostQueue->push(&Command.hStream); |
GhostQueue->push(&Command); |
370 |
|
} else { // error, queue full |
371 |
|
if (Command.bNotify) { |
372 |
|
dmsg(1,("DiskThread: GhostQueue full! (might lead to dead lock with instrument editor!)\n")); |
373 |
|
} else { |
374 |
|
dmsg(1,("DiskThread: GhostQueue full!\n")); |
375 |
|
} |
376 |
} |
} |
|
else dmsg(1,("DiskThread: GhostQueue full!\n")); |
|
377 |
} |
} |
378 |
} |
} |
379 |
|
|