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

Contents of /linuxsampler/trunk/src/engines/AbstractEngine.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2594 - (show annotations) (download) (as text)
Thu Jun 5 00:16:25 2014 UTC (9 years, 10 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 10668 byte(s)
* ScriptVM (WIP): started to integrate real-time instrument script
  support into the sampler engine implementations. The code is
  shared among all sampler engines, however currently only the gig
  file format supports storing instrument scripts (as LinuxSampler
  extension to the original GigaStudio 4 file format).
* gig engine: Added support for loading instrument scripts from .gig
  files.
* ScriptVM (WIP): Implemented built-in script variables %CC, $CC_NUM,
  $EVENT_NOTE, $EVENT_VELOCITY, $VCC_MONO_AT, $VCC_PITCH_BEND.
* ScriptVM (WIP): Implemented execution of script event handler "init".
* ScriptVM (WIP): Implemented execution of script event handler
  "controller".
* Bumped version (1.0.0.svn42).

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 * Copyright (C) 2009-2013 Christian Schoenebeck and Grigor Iliev *
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 * This program is distributed in the hope that it will be useful, *
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17 * GNU General Public License for more details. *
18 * *
19 * You should have received a copy of the GNU General Public License *
20 * along with this program; if not, write to the Free Software *
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
22 * MA 02111-1307 USA *
23 ***************************************************************************/
24
25 #ifndef __LS_ABSTRACTENGINE_H__
26 #define __LS_ABSTRACTENGINE_H__
27
28 #include "Engine.h"
29 #include "../common/ArrayList.h"
30 #include "../common/atomic.h"
31 #include "../common/ConditionServer.h"
32 #include "../common/Pool.h"
33 #include "../common/RingBuffer.h"
34 #include "../common/ChangeFlagRelaxed.h"
35 #include "../common/ResourceManager.h"
36 #include "../drivers/audio/AudioOutputDevice.h"
37 #include "common/Event.h"
38 #include "common/SignalUnitRack.h"
39 #include "common/InstrumentScriptVM.h"
40
41 namespace LinuxSampler {
42
43 class AbstractEngineChannel;
44
45 class AbstractEngine: public Engine {
46
47 public:
48 enum Format { GIG = 1, SF2, SFZ };
49 static String GetFormatString(Format f);
50 static AbstractEngine* AcquireEngine(AbstractEngineChannel* pChannel, AudioOutputDevice* pDevice);
51 static void FreeEngine(AbstractEngineChannel* pChannel, AudioOutputDevice* pDevice);
52
53 AbstractEngine();
54 virtual ~AbstractEngine();
55
56 // implementation of abstract methods derived from class 'LinuxSampler::Engine'
57 virtual void SendSysex(void* pData, uint Size, MidiInputPort* pSender) OVERRIDE;
58 virtual void Reset() OVERRIDE;
59 virtual void Enable() OVERRIDE;
60 virtual void Disable() OVERRIDE;
61 virtual uint VoiceCount() OVERRIDE;
62 virtual uint VoiceCountMax() OVERRIDE;
63 virtual String EngineName() OVERRIDE;
64 virtual void AdjustScaleTuning(const int8_t ScaleTunes[12]) OVERRIDE;
65 virtual void GetScaleTuning(int8_t* pScaleTunes) OVERRIDE;
66 virtual void ResetScaleTuning() OVERRIDE;
67
68 virtual Format GetEngineFormat() = 0;
69 virtual void Connect(AudioOutputDevice* pAudioOut) = 0;
70 virtual void DisableAndLock();
71
72 void SetVoiceCount(uint Count);
73
74 float Random() {
75 RandomSeed = RandomSeed * 1103515245 + 12345; // classic pseudo random number generator
76 return RandomSeed / 4294967296.0f;
77 }
78
79 // Simple array wrapper just to make sure memory is freed
80 // when liblinuxsampler is unloaded
81 class FloatTable {
82 private:
83 const float* array;
84 public:
85 FloatTable(const float* array) : array(array) { }
86 ~FloatTable() { delete[] array; }
87 const float& operator[](int i) const { return array[i]; }
88 };
89
90 static const FloatTable VolumeCurve; ///< Table that maps volume control change values 0..127 to amplitude. Unity gain is at 90.
91 static const FloatTable PanCurve; ///< Table that maps pan control change values 0..128 to right channel amplitude. Unity gain is at 64 (center).
92 static const FloatTable CrossfadeCurve; ///< Table that maps crossfade control change values 0..127 to amplitude. Unity gain is at 127.
93
94 AudioOutputDevice* pAudioOutputDevice;
95
96 //TODO: should be protected
97 AudioChannel* pDedicatedVoiceChannelLeft; ///< encapsulates a special audio rendering buffer (left) for rendering and routing audio on a per voice basis (this is a very special case and only used for voices which lie on a note which was set with individual, dedicated FX send level)
98 AudioChannel* pDedicatedVoiceChannelRight; ///< encapsulates a special audio rendering buffer (right) for rendering and routing audio on a per voice basis (this is a very special case and only used for voices which lie on a note which was set with individual, dedicated FX send level)
99
100 typedef ResourceConsumer<VMParserContext> ScriptConsumer;
101
102 class ScriptResourceManager : public ResourceManager<String, VMParserContext> {
103 protected:
104 // implementation of derived abstract methods from 'ResourceManager'
105 virtual VMParserContext* Create(String Key, ScriptConsumer* pConsumer, void*& pArg);
106 virtual void Destroy(VMParserContext* pResource, void* pArg);
107 virtual void OnBorrow(VMParserContext* pResource, ScriptConsumer* pConsumer, void*& pArg) {} // ignore
108 public:
109 ScriptResourceManager(AbstractEngine* parent) : parent(parent) {}
110 virtual ~ScriptResourceManager() {}
111 private:
112 AbstractEngine* parent;
113 } scripts;
114
115 friend class AbstractVoice;
116 friend class AbstractEngineChannel;
117 template<class V, class R, class I> friend class EngineChannelBase;
118 template<class EC, class R, class S, class D> friend class VoiceBase;
119
120 protected:
121 ArrayList<EngineChannel*> engineChannels; ///< All engine channels of a Engine instance.
122 ConditionServer EngineDisabled;
123 int8_t ScaleTuning[12]; ///< contains optional detune factors (-64..+63 cents) for all 12 semitones of an octave
124 ChangeFlagRelaxed ScaleTuningChanged; ///< Boolean flag indicating whenever ScaleTuning has been modified by a foreign thread (i.e. by API).
125 RingBuffer<Event,false>* pEventQueue; ///< Input event queue for engine global events (e.g. SysEx messages).
126 EventGenerator* pEventGenerator;
127 RTList<Event>* pGlobalEvents; ///< All engine global events for the current audio fragment (usually only SysEx messages).
128 Pool<Event>* pEventPool; ///< Contains all Event objects that can be used.
129 RingBuffer<uint8_t,false>* pSysexBuffer; ///< Input buffer for MIDI system exclusive messages.
130 uint SampleRate; ///< Sample rate of the engines output audio signal (in Hz)
131 uint MaxSamplesPerCycle; ///< Size of each audio output buffer
132 unsigned long FrameTime; ///< Time in frames of the start of the current audio fragment
133 int ActiveVoiceCountMax; ///< the maximum voice usage since application start
134 atomic_t ActiveVoiceCount; ///< number of currently active voices
135 int VoiceSpawnsLeft; ///< We only allow CONFIG_MAX_VOICES voices to be spawned per audio fragment, we use this variable to ensure this limit.
136 InstrumentScriptVM* pScriptVM; ///< Real-time instrument script virtual machine runner for this engine.
137
138 void RouteAudio(EngineChannel* pEngineChannel, uint Samples);
139 void RouteDedicatedVoiceChannels(EngineChannel* pEngineChannel, optional<float> FxSendLevels[2], uint Samples);
140 void ClearEventLists();
141 void ImportEvents(uint Samples);
142 void ProcessSysex(Pool<Event>::Iterator& itSysexEvent);
143 void ProcessPitchbend(AbstractEngineChannel* pEngineChannel, Pool<Event>::Iterator& itPitchbendEvent);
144
145 void ProcessFxSendControllers (
146 AbstractEngineChannel* pEngineChannel,
147 Pool<Event>::Iterator& itControlChangeEvent
148 );
149
150 uint8_t GSCheckSum(const RingBuffer<uint8_t,false>::NonVolatileReader AddrReader, uint DataSize);
151
152 virtual void ResetInternal() = 0;
153 virtual void KillAllVoices(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itKillEvent) = 0;
154 virtual void ProcessNoteOn(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent) = 0;
155 virtual void ProcessNoteOff(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOffEvent) = 0;
156 virtual void ProcessControlChange(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itControlChangeEvent) = 0;
157 virtual void ProcessChannelPressure(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itChannelPressureEvent) = 0;
158 virtual void ProcessPolyphonicKeyPressure(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNotePressureEvent) = 0;
159 virtual int GetMinFadeOutSamples() = 0;
160
161 private:
162 static std::map<Format, std::map<AudioOutputDevice*,AbstractEngine*> > engines;
163 uint32_t RandomSeed; ///< State of the random number generator used by the random dimension.
164
165 static float* InitVolumeCurve();
166 static float* InitPanCurve();
167 static float* InitCrossfadeCurve();
168 static float* InitCurve(const float* segments, int size = 128);
169
170 bool RouteFxSend(FxSend* pFxSend, AudioChannel* ppSource[2], float FxSendLevel, uint Samples);
171 };
172
173 } // namespace LinuxSampler
174
175 #endif /* __LS_ABSTRACTENGINE_H__ */
176

  ViewVC Help
Powered by ViewVC