/[svn]/linuxsampler/trunk/src/engines/gig/Engine.h
ViewVC logotype

Contents of /linuxsampler/trunk/src/engines/gig/Engine.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1644 - (show annotations) (download) (as text)
Sat Jan 19 16:55:03 2008 UTC (16 years, 3 months ago) by persson
File MIME type: text/x-c++hdr
File size: 12280 byte(s)
* fixed memory leaks that occurred when liblinuxsampler was unloaded
* fixed a memory leak that could happen when a channel was deleted
  while notes were playing
* fixed memory management bug in ASIO driver
* optimized the SynchronizedConfig class so it doesn't wait
  unnecessarily long after an update

1 /***************************************************************************
2 * *
3 * LinuxSampler - modular, streaming capable sampler *
4 * *
5 * 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 *
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_ENGINE_H__
25 #define __LS_GIG_ENGINE_H__
26
27 #include "../../common/global.h"
28
29 #include <map>
30 #include <gig.h>
31
32 #include "EngineGlobals.h"
33 #include "../../common/RingBuffer.h"
34 #include "../../common/Pool.h"
35 #include "../../common/ArrayList.h"
36 #include "../../common/ConditionServer.h"
37 #include "../Engine.h"
38 #include "../common/Event.h"
39 #include "../../network/lscp.h"
40 #include "EngineChannel.h"
41
42 // identifier of this sampler engine
43 #define LS_GIG_ENGINE_NAME "GIG"
44
45 namespace LinuxSampler { namespace gig {
46
47 // just symbol prototyping
48 class Voice;
49 class DiskThread;
50 class InstrumentResourceManager;
51 class midi_key_info_t;
52 class EngineChannel;
53
54 /**
55 * Sampler engine for the Gigasampler format.
56 */
57 class Engine : public LinuxSampler::Engine {
58 public:
59 // methods
60 Engine();
61 virtual ~Engine();
62 void Connect(AudioOutputDevice* pAudioOut);
63 ::gig::DimensionRegion** ChangeInstrument(EngineChannel* pEngineChannel, ::gig::Instrument* pInstrument);
64 void SuspendAll();
65 void ResumeAll();
66 void Suspend(::gig::Region* pRegion);
67 void Resume(::gig::Region* pRegion);
68
69 // implementation of abstract methods derived from class 'LinuxSampler::Engine'
70 virtual int RenderAudio(uint Samples);
71 virtual void SendSysex(void* pData, uint Size);
72 virtual void Reset();
73 virtual void Enable();
74 virtual void Disable();
75 virtual uint VoiceCount();
76 virtual uint VoiceCountMax();
77 virtual bool DiskStreamSupported();
78 virtual uint DiskStreamCount();
79 virtual uint DiskStreamCountMax();
80 virtual String DiskStreamBufferFillBytes();
81 virtual String DiskStreamBufferFillPercentage();
82 virtual String Description();
83 virtual String Version();
84 virtual String EngineName();
85 virtual InstrumentManager* GetInstrumentManager();
86
87 // Simple array wrapper just to make sure memory is freed
88 // when liblinuxsampler is unloaded
89 class FloatTable {
90 private:
91 const float* array;
92 public:
93 FloatTable(const float* array) : array(array) { }
94 ~FloatTable() { delete[] array; }
95 const float& operator[](int i) const { return array[i]; }
96 };
97
98 static const FloatTable VolumeCurve; ///< Table that maps volume control change values 0..127 to amplitude. Unity gain is at 90.
99 static const FloatTable PanCurve; ///< Table that maps pan control change values 0..128 to right channel amplitude. Unity gain is at 64 (center).
100 static const FloatTable CrossfadeCurve; ///< Table that maps crossfade control change values 0..127 to amplitude. Unity gain is at 127.
101
102 //protected:
103 static InstrumentResourceManager instruments;
104
105 AudioOutputDevice* pAudioOutputDevice;
106 uint SampleRate; ///< Sample rate of the engines output audio signal (in Hz)
107 uint MaxSamplesPerCycle; ///< Size of each audio output buffer
108 DiskThread* pDiskThread;
109 RingBuffer<Event,false>* pEventQueue; ///< Input event queue for engine global events (e.g. SysEx messages).
110 Pool<Voice>* pVoicePool; ///< Contains all voices that can be activated.
111 EventGenerator* pEventGenerator;
112 RTList<Event>* pVoiceStealingQueue; ///< All voice-launching events which had to be postponed due to free voice shortage.
113 RTList<Event>* pGlobalEvents; ///< All engine global events for the current audio fragment (usually only SysEx messages).
114 Pool<Event>* pEventPool; ///< Contains all Event objects that can be used.
115 RingBuffer<uint8_t,false>* pSysexBuffer; ///< Input buffer for MIDI system exclusive messages.
116 int ActiveVoiceCount; ///< number of currently active voices (this value will be returned for public calls)
117 int ActiveVoiceCountTemp; ///< number of currently active voices (for internal usage, will be used for incrementation)
118 int ActiveVoiceCountMax; ///< the maximum voice usage since application start
119 int VoiceSpawnsLeft; ///< We only allow CONFIG_MAX_VOICES voices to be spawned per audio fragment, we use this variable to ensure this limit.
120 RTList<Voice>::Iterator itLastStolenVoice; ///< Only for voice stealing: points to the last voice which was theft in current audio fragment, NULL otherwise.
121 RTList<uint>::Iterator iuiLastStolenKey; ///< Only for voice stealing: key number of last key on which the last voice was theft in current audio fragment, NULL otherwise.
122 EngineChannel* pLastStolenChannel; ///< Only for voice stealing: points to the engine channel on which the previous voice was stolen in this audio fragment.
123 RTList<Voice>::Iterator itLastStolenVoiceGlobally; ///< Same as itLastStolenVoice, but engine globally
124 RTList<uint>::Iterator iuiLastStolenKeyGlobally; ///< Same as iuiLastStolenKey, but engine globally
125 bool SuspensionRequested;
126 ConditionServer EngineDisabled;
127 int8_t ScaleTuning[12]; ///< contains optional detune factors (-64..+63 cents) for all 12 semitones of an octave
128 int MaxFadeOutPos; ///< The last position in an audio fragment to allow an instant fade out (e.g. for voice stealing) without leading to clicks.
129 uint32_t RandomSeed; ///< State of the random number generator used by the random dimension.
130 Mutex ResetInternalMutex; ///< Mutex to protect the ResetInternal function for concurrent usage (e.g. by the lscp and instrument loader threads).
131
132 void ProcessEvents(EngineChannel* pEngineChannel, uint Samples);
133 void RenderActiveVoices(EngineChannel* pEngineChannel, uint Samples);
134 void RenderStolenVoices(uint Samples);
135 void RouteAudio(EngineChannel* pEngineChannel, uint Samples);
136 void PostProcess(EngineChannel* pEngineChannel);
137 void ClearEventLists();
138 void ImportEvents(uint Samples);
139 void ProcessNoteOn(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent);
140 void ProcessNoteOff(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOffEvent);
141 void ProcessPitchbend(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itPitchbendEvent);
142 void ProcessControlChange(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itControlChangeEvent);
143 void ProcessSysex(Pool<Event>::Iterator& itSysexEvent);
144 void ProcessSuspensionsChanges();
145 void ProcessPendingStreamDeletions();
146 Pool<Voice>::Iterator LaunchVoice(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent, int iLayer, bool ReleaseTriggerVoice, bool VoiceStealing, bool HandleKeyGroupConflicts);
147 int StealVoice(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent);
148 void FreeVoice(EngineChannel* pEngineChannel, Pool<Voice>::Iterator& itVoice);
149 void FreeKey(EngineChannel* pEngineChannel, midi_key_info_t* pKey);
150 void ResetInternal();
151 void ResetScaleTuning();
152 void ResetSuspendedRegions();
153
154 static Engine* AcquireEngine(LinuxSampler::gig::EngineChannel* pChannel, AudioOutputDevice* pDevice);
155 static void FreeEngine(LinuxSampler::gig::EngineChannel* pChannel, AudioOutputDevice* pDevice);
156
157 void DisableAndLock(); // FIXME: should at least be protected
158
159 friend class Voice;
160 private:
161
162 /// Command used by the instrument loader thread to
163 /// request an instrument change on a channel.
164 struct instrument_change_command_t {
165 EngineChannel* pEngineChannel;
166 ::gig::Instrument* pInstrument;
167 };
168 struct instrument_change_reply_t {
169 int dummy;
170 };
171 RingBuffer<instrument_change_command_t,false>* InstrumentChangeQueue; ///< Contains the instrument change command
172 RingBuffer<instrument_change_reply_t,false>* InstrumentChangeReplyQueue; ///< Contains the acknowledge of an instrument change
173 ::gig::DimensionRegion** pDimRegionsInUse; ///< After an instrument change, this contains a list of dimension regions that are still in use by playing voices
174
175 Pool< ::gig::Region*> SuspendedRegions;
176 Mutex SuspendedRegionsMutex;
177 Condition SuspensionChangeOngoing;
178 ::gig::Region* pPendingRegionSuspension;
179 ::gig::Region* pPendingRegionResumption;
180 int iPendingStreamDeletions;
181
182 ArrayList<EngineChannel*> engineChannels; ///< All engine channels of a gig::Engine instance.
183
184 static std::map<AudioOutputDevice*,Engine*> engines; ///< All instances of gig::Engine.
185
186 int SostenutoKeys[128];
187 int SostenutoKeyCount;
188
189 uint8_t GSCheckSum(const RingBuffer<uint8_t,false>::NonVolatileReader AddrReader, uint DataSize);
190 void AdjustScale(int8_t ScaleTunes[12]);
191 void ReleaseAllVoices(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itReleaseEvent);
192 void KillAllVoices(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itKillEvent);
193 bool ShouldReleaseVoice(EngineChannel* pEngineChannel, int Key);
194 bool RegionSuspended(::gig::Region* pRegion);
195 static float* InitVolumeCurve();
196 static float* InitPanCurve();
197 static float* InitCrossfadeCurve();
198 static float* InitCurve(const float* segments, int size = 128);
199
200 unsigned long FrameTime; ///< Time in frames of the start of the current audio fragment
201 };
202
203 }} // namespace LinuxSampler::gig
204
205 #endif // __LS_GIG_ENGINE_H__

  ViewVC Help
Powered by ViewVC