1 |
schoenebeck |
53 |
/*************************************************************************** |
2 |
|
|
* * |
3 |
|
|
* LinuxSampler - modular, streaming capable sampler * |
4 |
|
|
* * |
5 |
schoenebeck |
56 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
schoenebeck |
1879 |
* Copyright (C) 2005 - 2009 Christian Schoenebeck * |
7 |
schoenebeck |
53 |
* * |
8 |
|
|
* 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 * |
10 |
|
|
* the Free Software Foundation; either version 2 of the License, or * |
11 |
|
|
* (at your option) any later version. * |
12 |
|
|
* * |
13 |
|
|
* This program is distributed in the hope that it will be useful, * |
14 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
15 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
16 |
|
|
* GNU General Public License for more details. * |
17 |
|
|
* * |
18 |
|
|
* You should have received a copy of the GNU General Public License * |
19 |
|
|
* along with this program; if not, write to the Free Software * |
20 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
21 |
|
|
* MA 02111-1307 USA * |
22 |
|
|
***************************************************************************/ |
23 |
|
|
|
24 |
|
|
#ifndef __LS_GIG_DISKTHREAD_H__ |
25 |
|
|
#define __LS_GIG_DISKTHREAD_H__ |
26 |
|
|
|
27 |
schoenebeck |
1424 |
#include "../../common/global_private.h" |
28 |
schoenebeck |
53 |
|
29 |
schoenebeck |
505 |
#include <gig.h> |
30 |
|
|
|
31 |
schoenebeck |
53 |
#include "../../common/Thread.h" |
32 |
|
|
#include "../../common/RingBuffer.h" |
33 |
schoenebeck |
1879 |
#include "../../common/atomic.h" |
34 |
schoenebeck |
53 |
#include "Stream.h" |
35 |
|
|
#include "Voice.h" |
36 |
|
|
|
37 |
|
|
namespace LinuxSampler { namespace gig { |
38 |
|
|
|
39 |
|
|
int CompareStreamWriteSpace(const void* a, const void* b); |
40 |
|
|
|
41 |
schoenebeck |
563 |
/** @brief Disk Reader Thread |
42 |
|
|
* |
43 |
|
|
* The disk reader thread is responsible for periodically refilling |
44 |
|
|
* disk streams in parallel to the audio thread's rendering process. |
45 |
persson |
1038 |
* |
46 |
|
|
* There is also a function for releasing parts of instruments not |
47 |
|
|
* in use anymore (as this is not real time safe, the audio thread |
48 |
|
|
* cannot do it directly). |
49 |
schoenebeck |
563 |
*/ |
50 |
schoenebeck |
53 |
class DiskThread : public Thread { |
51 |
|
|
public: |
52 |
|
|
// Methods |
53 |
schoenebeck |
1800 |
DiskThread(int MaxStreams, uint BufferWrapElements, InstrumentResourceManager* pInstruments); |
54 |
letz |
502 |
virtual ~DiskThread(); |
55 |
schoenebeck |
53 |
void Reset(); |
56 |
|
|
String GetBufferFillBytes(); |
57 |
|
|
String GetBufferFillPercentage(); |
58 |
persson |
865 |
int OrderNewStream(Stream::reference_t* pStreamRef, ::gig::DimensionRegion* pDimRgn, unsigned long SampleOffset, bool DoLoop); |
59 |
schoenebeck |
1321 |
int OrderDeletionOfStream(Stream::reference_t* pStreamRef, bool bRequestNotification = false); |
60 |
persson |
1038 |
int OrderDeletionOfDimreg(::gig::DimensionRegion* dimreg); |
61 |
persson |
1924 |
int OrderProgramChange(uint8_t Program, EngineChannel* pEngineChannel); |
62 |
schoenebeck |
53 |
Stream* AskForCreatedStream(Stream::OrderID_t StreamOrderID); |
63 |
schoenebeck |
1321 |
Stream::Handle AskForDeletedStream(); |
64 |
schoenebeck |
53 |
|
65 |
|
|
// the number of streams currently in usage |
66 |
|
|
// printed on the console the main thread (along with the active voice count) |
67 |
iliev |
1789 |
uint GetActiveStreamCount() { return atomic_read(&ActiveStreamCount); } |
68 |
|
|
void SetActiveStreamCount(uint Streams) { atomic_set(&ActiveStreamCount, Streams); } |
69 |
schoenebeck |
53 |
int ActiveStreamCountMax; |
70 |
|
|
|
71 |
|
|
protected: |
72 |
schoenebeck |
385 |
::gig::buffer_t DecompressionBuffer; ///< Used for thread safe streaming. |
73 |
|
|
|
74 |
schoenebeck |
53 |
int Main(); ///< Implementation of virtual method from class Thread |
75 |
schoenebeck |
385 |
|
76 |
|
|
friend class Stream; |
77 |
schoenebeck |
53 |
private: |
78 |
|
|
// Private Types |
79 |
|
|
struct create_command_t { |
80 |
|
|
Stream::OrderID_t OrderID; |
81 |
|
|
Stream::Handle hStream; |
82 |
|
|
Stream::reference_t* pStreamRef; |
83 |
persson |
865 |
::gig::DimensionRegion* pDimRgn; |
84 |
schoenebeck |
53 |
unsigned long SampleOffset; |
85 |
|
|
bool DoLoop; |
86 |
|
|
}; |
87 |
|
|
struct delete_command_t { |
88 |
|
|
Stream* pStream; |
89 |
|
|
Stream::Handle hStream; |
90 |
|
|
Stream::OrderID_t OrderID; |
91 |
schoenebeck |
1321 |
bool bNotify; |
92 |
schoenebeck |
53 |
}; |
93 |
persson |
1924 |
struct program_change_command_t { |
94 |
|
|
uint8_t Program; |
95 |
|
|
EngineChannel* pEngineChannel; |
96 |
|
|
}; |
97 |
schoenebeck |
53 |
// Attributes |
98 |
|
|
bool IsIdle; |
99 |
|
|
uint Streams; |
100 |
schoenebeck |
970 |
RingBuffer<create_command_t,false>* CreationQueue; ///< Contains commands to create streams |
101 |
|
|
RingBuffer<delete_command_t,false>* DeletionQueue; ///< Contains commands to delete streams |
102 |
schoenebeck |
1321 |
RingBuffer<delete_command_t,false>* GhostQueue; ///< Contains handles to streams that are not used anymore and weren't deletable immediately |
103 |
|
|
RingBuffer<Stream::Handle,false> DeletionNotificationQueue; ///< In case the original sender requested a notification for its stream deletion order, this queue will receive the handle of the respective stream once actually be deleted by the disk thread. |
104 |
persson |
1038 |
RingBuffer< ::gig::DimensionRegion*,false>* DeleteDimregQueue; ///< Contains dimension regions that are not used anymore and should be handed back to the instrument resource manager |
105 |
persson |
1924 |
RingBuffer<program_change_command_t,false> ProgramChangeQueue; ///< Contains requests for MIDI program change |
106 |
schoenebeck |
53 |
unsigned int RefillStreamsPerRun; ///< How many streams should be refilled in each loop run |
107 |
schoenebeck |
1800 |
Stream** pStreams; ///< Contains all disk streams (whether used or unused) |
108 |
|
|
Stream** pCreatedStreams; ///< This is where the voice (audio thread) picks up it's meanwhile hopefully created disk stream. |
109 |
schoenebeck |
53 |
static Stream* SLOT_RESERVED; ///< This value is used to mark an entry in pCreatedStreams[] as reserved. |
110 |
persson |
1038 |
InstrumentResourceManager* pInstruments; ///< The instrument resource manager of the engine that is using this disk thread. Used by the dimension region deletion feature. |
111 |
schoenebeck |
53 |
|
112 |
|
|
// Methods |
113 |
|
|
void CreateStream(create_command_t& Command); |
114 |
|
|
void DeleteStream(delete_command_t& Command); |
115 |
|
|
void RefillStreams(); |
116 |
|
|
Stream::Handle CreateHandle(); |
117 |
|
|
Stream::OrderID_t CreateOrderID(); |
118 |
iliev |
1789 |
|
119 |
|
|
atomic_t ActiveStreamCount; |
120 |
schoenebeck |
53 |
}; |
121 |
|
|
|
122 |
|
|
}} // namespace LinuxSampler::gig |
123 |
|
|
|
124 |
|
|
#endif // __LS_GIG_DISKTHREAD_H__ |