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 - 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 * |
24 |
#ifndef __LS_GIG_DISKTHREAD_H__ |
#ifndef __LS_GIG_DISKTHREAD_H__ |
25 |
#define __LS_GIG_DISKTHREAD_H__ |
#define __LS_GIG_DISKTHREAD_H__ |
26 |
|
|
27 |
#include "../../common/global.h" |
#include "../../common/global_private.h" |
28 |
|
|
29 |
#if DEBUG_HEADERS |
#include <gig.h> |
|
# warning DiskThread.h included |
|
|
#endif // DEBUG_HEADERS |
|
|
|
|
|
#define REFILL_STREAMS_PER_RUN 4 ///< number of streams that should be refilled with each disk thread cycle |
|
|
#define MIN_REFILL_SIZE 1024 ///< if no buffer was filled up more than this bottom limit, the disk thread will go to sleep |
|
|
#define MAX_REFILL_SIZE 65536 ///< maximum of samples a buffer should be refilled in one cycle (256kB, as 16 bit stereo) |
|
|
#define STREAM_BUFFER_SIZE 131072 ///< the diskstream ringbuffer size (256kB as sample_t is 16bit) |
|
|
#define MAX_INPUT_STREAMS 100 ///< number of streams that should be allocated |
|
30 |
|
|
31 |
#include "../../common/Thread.h" |
#include "../../common/Thread.h" |
32 |
#include "../../common/RingBuffer.h" |
#include "../../common/RingBuffer.h" |
37 |
|
|
38 |
int CompareStreamWriteSpace(const void* a, const void* b); |
int CompareStreamWriteSpace(const void* a, const void* b); |
39 |
|
|
40 |
|
/** @brief Disk Reader Thread |
41 |
|
* |
42 |
|
* The disk reader thread is responsible for periodically refilling |
43 |
|
* disk streams in parallel to the audio thread's rendering process. |
44 |
|
* |
45 |
|
* There is also a function for releasing parts of instruments not |
46 |
|
* in use anymore (as this is not real time safe, the audio thread |
47 |
|
* cannot do it directly). |
48 |
|
*/ |
49 |
class DiskThread : public Thread { |
class DiskThread : public Thread { |
50 |
public: |
public: |
51 |
// Methods |
// Methods |
52 |
DiskThread(uint BufferWrapElements); |
DiskThread(uint BufferWrapElements, InstrumentResourceManager* pInstruments); |
53 |
~DiskThread(); |
virtual ~DiskThread(); |
54 |
void Reset(); |
void Reset(); |
55 |
String GetBufferFillBytes(); |
String GetBufferFillBytes(); |
56 |
String GetBufferFillPercentage(); |
String GetBufferFillPercentage(); |
57 |
int OrderNewStream(Stream::reference_t* pStreamRef, ::gig::Sample* pSample, unsigned long SampleOffset, bool DoLoop); |
int OrderNewStream(Stream::reference_t* pStreamRef, ::gig::DimensionRegion* pDimRgn, unsigned long SampleOffset, bool DoLoop); |
58 |
int OrderDeletionOfStream(Stream::reference_t* pStreamRef); |
int OrderDeletionOfStream(Stream::reference_t* pStreamRef, bool bRequestNotification = false); |
59 |
|
int OrderDeletionOfDimreg(::gig::DimensionRegion* dimreg); |
60 |
Stream* AskForCreatedStream(Stream::OrderID_t StreamOrderID); |
Stream* AskForCreatedStream(Stream::OrderID_t StreamOrderID); |
61 |
|
Stream::Handle AskForDeletedStream(); |
62 |
|
|
63 |
// the number of streams currently in usage |
// the number of streams currently in usage |
64 |
// printed on the console the main thread (along with the active voice count) |
// printed on the console the main thread (along with the active voice count) |
66 |
int ActiveStreamCountMax; |
int ActiveStreamCountMax; |
67 |
|
|
68 |
protected: |
protected: |
69 |
|
::gig::buffer_t DecompressionBuffer; ///< Used for thread safe streaming. |
70 |
|
|
71 |
int Main(); ///< Implementation of virtual method from class Thread |
int Main(); ///< Implementation of virtual method from class Thread |
72 |
|
|
73 |
|
friend class Stream; |
74 |
private: |
private: |
75 |
// Private Types |
// Private Types |
76 |
struct create_command_t { |
struct create_command_t { |
77 |
Stream::OrderID_t OrderID; |
Stream::OrderID_t OrderID; |
78 |
Stream::Handle hStream; |
Stream::Handle hStream; |
79 |
Stream::reference_t* pStreamRef; |
Stream::reference_t* pStreamRef; |
80 |
::gig::Sample* pSample; |
::gig::DimensionRegion* pDimRgn; |
81 |
unsigned long SampleOffset; |
unsigned long SampleOffset; |
82 |
bool DoLoop; |
bool DoLoop; |
83 |
}; |
}; |
85 |
Stream* pStream; |
Stream* pStream; |
86 |
Stream::Handle hStream; |
Stream::Handle hStream; |
87 |
Stream::OrderID_t OrderID; |
Stream::OrderID_t OrderID; |
88 |
|
bool bNotify; |
89 |
}; |
}; |
90 |
|
|
91 |
// Attributes |
// Attributes |
92 |
bool IsIdle; |
bool IsIdle; |
93 |
uint Streams; |
uint Streams; |
94 |
RingBuffer<create_command_t>* CreationQueue; ///< Contains commands to create streams |
RingBuffer<create_command_t,false>* CreationQueue; ///< Contains commands to create streams |
95 |
RingBuffer<delete_command_t>* DeletionQueue; ///< Contains commands to delete streams |
RingBuffer<delete_command_t,false>* DeletionQueue; ///< Contains commands to delete streams |
96 |
RingBuffer<Stream::Handle>* GhostQueue; ///< Contains handles to streams that are not used anymore and weren't deletable immediately |
RingBuffer<delete_command_t,false>* GhostQueue; ///< Contains handles to streams that are not used anymore and weren't deletable immediately |
97 |
|
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. |
98 |
|
RingBuffer< ::gig::DimensionRegion*,false>* DeleteDimregQueue; ///< Contains dimension regions that are not used anymore and should be handed back to the instrument resource manager |
99 |
unsigned int RefillStreamsPerRun; ///< How many streams should be refilled in each loop run |
unsigned int RefillStreamsPerRun; ///< How many streams should be refilled in each loop run |
100 |
Stream* pStreams[MAX_INPUT_STREAMS]; ///< Contains all disk streams (wether used or unused) |
Stream* pStreams[CONFIG_MAX_STREAMS]; ///< Contains all disk streams (whether used or unused) |
101 |
Stream* pCreatedStreams[MAX_INPUT_STREAMS + 1]; ///< This is where the voice (audio thread) picks up it's meanwhile hopefully created disk stream. |
Stream* pCreatedStreams[CONFIG_MAX_STREAMS + 1]; ///< This is where the voice (audio thread) picks up it's meanwhile hopefully created disk stream. |
102 |
static Stream* SLOT_RESERVED; ///< This value is used to mark an entry in pCreatedStreams[] as reserved. |
static Stream* SLOT_RESERVED; ///< This value is used to mark an entry in pCreatedStreams[] as reserved. |
103 |
|
InstrumentResourceManager* pInstruments; ///< The instrument resource manager of the engine that is using this disk thread. Used by the dimension region deletion feature. |
104 |
|
|
105 |
// Methods |
// Methods |
106 |
void CreateStream(create_command_t& Command); |
void CreateStream(create_command_t& Command); |