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

Annotation of /linuxsampler/trunk/src/engines/AbstractEngineChannel.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2871 - (hide annotations) (download) (as text)
Sun Apr 10 18:22:23 2016 UTC (8 years ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 14721 byte(s)
* All engines: Implemented scheduler for delayed MIDI events and for
  suspended real-time instrument scripts.
* Real-Time instrument scripts: Implemented support for built-in "wait()"
  function's "duration-us" argument, thus scripts using this function are
  now correctly resumed after the requested amount of microseconds.
* Real-Time instrument scripts: Implemented support for built-in
  "play_note()" function's "duration-us" argument, thus notes triggered
  with this argument are now correctly released after the requested amount
  of microseconds.
* Real-Time instrument scripts: Fixed crash which happened when trying to
  reference an undeclared script variable.
* Real-Time instrument scripts: Script events were not cleared when
  engine channel was reset, potentially causing undefined behavior.
* All engines: Attempt to partly fix resetting engine channels vs.
  resetting engine, an overall cleanup of the Reset*(),
  ConnectAudioDevice(), DisconnectAudioDevice() API methods would still be
  desirable though, because the current situation is still inconsistent
  and error prone.
* Bumped version (2.0.0.svn2).

1 iliev 2012 /***************************************************************************
2     * *
3     * LinuxSampler - modular, streaming capable sampler *
4     * *
5     * Copyright (C) 2003,2004 by Benno Senoner and Christian Schoenebeck *
6 persson 2114 * Copyright (C) 2005-2008 Christian Schoenebeck *
7 persson 2317 * Copyright (C) 2009-2012 Christian Schoenebeck and Grigor Iliev *
8 schoenebeck 2871 * Copyright (C) 2012-2016 Christian Schoenebeck and Andreas Persson *
9 iliev 2012 * *
10     * This program is free software; you can redistribute it and/or modify *
11     * it under the terms of the GNU General Public License as published by *
12     * the Free Software Foundation; either version 2 of the License, or *
13     * (at your option) any later version. *
14     * *
15     * This program is distributed in the hope that it will be useful, *
16     * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18     * GNU General Public License for more details. *
19     * *
20     * You should have received a copy of the GNU General Public License *
21     * along with this program; if not, write to the Free Software *
22     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
23     * MA 02111-1307 USA *
24     ***************************************************************************/
25    
26     #ifndef __LS_ABSTRACTENGINECHANNEL_H__
27 persson 2114 #define __LS_ABSTRACTENGINECHANNEL_H__
28 iliev 2012
29     #include "EngineChannel.h"
30     #include "AbstractEngine.h"
31    
32     #include "../common/Pool.h"
33     #include "../common/RingBuffer.h"
34 schoenebeck 2611 #include "../common/ResourceManager.h"
35     #include "common/AbstractInstrumentManager.h"
36     #include "common/InstrumentScriptVM.h"
37 iliev 2012
38 schoenebeck 2594 #define CTRL_TABLE_IDX_AFTERTOUCH 128
39     #define CTRL_TABLE_IDX_PITCHBEND 129
40    
41 iliev 2012 namespace LinuxSampler {
42    
43 schoenebeck 2613 class MidiKeyboardManagerBase;
44    
45 schoenebeck 2611 class AbstractEngineChannel: public EngineChannel, public InstrumentScriptConsumer {
46 iliev 2012 public:
47     // implementation of abstract methods derived from interface class 'LinuxSampler::EngineChannel'
48 schoenebeck 2434 virtual void PrepareLoadInstrument(const char* FileName, uint Instrument) OVERRIDE;
49     virtual void Reset() OVERRIDE;
50     virtual void SendNoteOn(uint8_t Key, uint8_t Velocity, uint8_t MidiChannel) OVERRIDE;
51     virtual void SendNoteOn(uint8_t Key, uint8_t Velocity, uint8_t MidiChannel, int32_t FragmentPos) OVERRIDE;
52     virtual void SendNoteOff(uint8_t Key, uint8_t Velocity, uint8_t MidiChannel) OVERRIDE;
53     virtual void SendNoteOff(uint8_t Key, uint8_t Velocity, uint8_t MidiChannel, int32_t FragmentPos) OVERRIDE;
54     virtual void SendPitchbend(int Pitch, uint8_t MidiChannel) OVERRIDE;
55     virtual void SendPitchbend(int Pitch, uint8_t MidiChannel, int32_t FragmentPos) OVERRIDE;
56     virtual void SendControlChange(uint8_t Controller, uint8_t Value, uint8_t MidiChannel) OVERRIDE;
57 schoenebeck 2559 virtual void SendControlChange(uint8_t Controller, uint8_t Value, uint8_t MidiChannel, int32_t FragmentPos) OVERRIDE;
58     virtual void SendChannelPressure(uint8_t Value, uint8_t MidiChannel) OVERRIDE;
59     virtual void SendChannelPressure(uint8_t Value, uint8_t MidiChannel, int32_t FragmentPos) OVERRIDE;
60     virtual void SendPolyphonicKeyPressure(uint8_t Key, uint8_t Value, uint8_t MidiChannel) OVERRIDE;
61     virtual void SendPolyphonicKeyPressure(uint8_t Key, uint8_t Value, uint8_t MidiChannel, int32_t FragmentPos) OVERRIDE;
62 schoenebeck 2434 virtual bool StatusChanged(bool bNewStatus = false) OVERRIDE;
63     virtual float Volume() OVERRIDE;
64     virtual void Volume(float f) OVERRIDE;
65     virtual float Pan() OVERRIDE;
66     virtual void Pan(float f) OVERRIDE;
67     virtual uint Channels() OVERRIDE;
68     virtual AudioOutputDevice* GetAudioOutputDevice() OVERRIDE;
69     virtual void SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) OVERRIDE;
70     virtual int OutputChannel(uint EngineAudioChannel) OVERRIDE;
71 schoenebeck 2500 virtual void Connect(MidiInputPort* pMidiPort) OVERRIDE;
72     virtual void Disconnect(MidiInputPort* pMidiPort) OVERRIDE;
73     virtual void DisconnectAllMidiInputPorts() OVERRIDE;
74     virtual uint GetMidiInputPortCount() OVERRIDE;
75     virtual MidiInputPort* GetMidiInputPort(uint index) OVERRIDE;
76 schoenebeck 2434 virtual midi_chan_t MidiChannel() OVERRIDE;
77 schoenebeck 2500 virtual void SetMidiChannel(midi_chan_t MidiChannel) OVERRIDE;
78     virtual void Connect(MidiInputPort* pMidiPort, midi_chan_t MidiChannel) OVERRIDE; // deprecated, may be removed
79     virtual void DisconnectMidiInputPort() OVERRIDE; // deprecated, may be removed
80     virtual MidiInputPort* GetMidiInputPort() OVERRIDE; // deprecated, may be removed
81 schoenebeck 2434 virtual String InstrumentFileName() OVERRIDE;
82     virtual String InstrumentName() OVERRIDE;
83     virtual int InstrumentIndex() OVERRIDE;
84     virtual int InstrumentStatus() OVERRIDE;
85     virtual Engine* GetEngine() OVERRIDE;
86     virtual String EngineName() OVERRIDE;
87     virtual FxSend* AddFxSend(uint8_t MidiCtrl, String Name = "") throw (Exception) OVERRIDE;
88     virtual FxSend* GetFxSend(uint FxSendIndex) OVERRIDE;
89     virtual uint GetFxSendCount() OVERRIDE;
90     virtual void RemoveFxSend(FxSend* pFxSend) OVERRIDE;
91     virtual void Connect(VirtualMidiDevice* pDevice) OVERRIDE;
92     virtual void Disconnect(VirtualMidiDevice* pDevice) OVERRIDE;
93 iliev 2012
94 schoenebeck 2594 // implementation of abstract methods derived from AbstractEngine::ScriptConsumer
95     virtual void ResourceToBeUpdated(VMParserContext* pResource, void*& pUpdateArg) OVERRIDE {}
96     virtual void ResourceUpdated(VMParserContext* pOldResource, VMParserContext* pNewResource, void* pUpdateArg) OVERRIDE {}
97     virtual void OnResourceProgress(float fProgress) OVERRIDE {}
98 iliev 2012
99     virtual AbstractEngine::Format GetEngineFormat() = 0;
100 schoenebeck 2613 virtual MidiKeyboardManagerBase* GetMidiKeyboardManager() = 0;
101 iliev 2012
102 persson 2326 AudioOutputDevice* GetAudioOutputDeviceSafe();
103    
104 iliev 2015 friend class AbstractVoice;
105 iliev 2012 friend class AbstractEngine;
106     template<class TV, class TRR, class TR, class TD, class TIM, class TI> friend class EngineBase;
107 iliev 2015 template<class EC, class R, class S, class D> friend class VoiceBase;
108 iliev 2012
109 schoenebeck 2594 //protected:
110 iliev 2012 AbstractEngineChannel();
111     virtual ~AbstractEngineChannel();
112    
113     AbstractEngine* pEngine;
114 persson 2326 Mutex EngineMutex; ///< protects the Engine from access by the instrument loader thread when lscp is disconnecting
115 schoenebeck 2500 Mutex MidiInputMutex; ///< Introduced when support for multiple MIDI inputs per engine channel was added: protects the MIDI event input ringbuffer on this engine channel to be accessed concurrently by multiple midi input threads. As alternative one might also move the ringbuffer from this engine channel to the individual MIDI ports/devices and let the sampler engine read the events from there instead of receiving them here.
116 persson 2326
117 schoenebeck 2594 //protected:
118 iliev 2012 AudioChannel* pChannelLeft; ///< encapsulates the audio rendering buffer (left)
119     AudioChannel* pChannelRight; ///< encapsulates the audio rendering buffer (right)
120     int AudioDeviceChannelLeft; ///< audio device channel number to which the left channel is connected to
121     int AudioDeviceChannelRight; ///< audio device channel number to which the right channel is connected to
122 schoenebeck 2500 DoubleBuffer< ArrayList<MidiInputPort*> > midiInputs; ///< MIDI input ports on which this sampler engine channel shall listen to.
123     midi_chan_t midiChannel; ///< MIDI channel(s) on which this engine channel listens to (on all MIDI input ports).
124 iliev 2012 RingBuffer<Event,false>* pEventQueue; ///< Input event queue.
125     RTList<Event>* pEvents; ///< All engine channel specific events for the current audio fragment.
126 schoenebeck 2871 struct _DelayedEvents {
127     RTList<Event>* pList; ///< Unsorted list where all delayed events are moved to and remain here until they're finally processed.
128     Pool<ScheduledEvent> schedulerNodes; ///< Nodes used to sort the delayed events (stored on pList) with time sorted queue.
129     RTAVLTree<ScheduledEvent> queue; ///< Used to access the delayed events (from pList) in time sorted manner.
130    
131     _DelayedEvents() : pList(NULL), schedulerNodes(CONFIG_MAX_EVENTS_PER_FRAGMENT) {}
132    
133     inline void clear() {
134     if (pList) pList->clear();
135     schedulerNodes.clear();
136     queue.clear();
137     }
138     } delayedEvents;
139 schoenebeck 2594 uint8_t ControllerTable[130]; ///< Reflects the current values (0-127) of all MIDI controllers for this engine / sampler channel. Number 128 is for channel pressure (mono aftertouch), 129 for pitch bend.
140 iliev 2012 String InstrumentFile;
141     int InstrumentIdx;
142     String InstrumentIdxName;
143     int InstrumentStat;
144     double GlobalVolume; ///< Master volume factor set through the C++ API / LSCP (a value < 1.0 means attenuation, a value > 1.0 means amplification)
145     double MidiVolume; ///< Volume factor altered by MIDI CC#7 (a value < 1.0 means attenuation, a value > 1.0 means amplification)
146     int Pitch; ///< Current (absolute) MIDI pitch value.
147     float CurrentKeyDimension; ///< Current value (0-1.0) for the keyboard dimension, altered by pressing a keyswitching key.
148     bool PortamentoMode; ///< in Portamento Mode we slide the pitch from the last note to the current note.
149     float PortamentoTime; ///< How long it will take to glide from the previous note to the current (in seconds)
150     float PortamentoPos; ///< Current position on the keyboard, that is integer and fractional part (only used if PortamentoMode is on)
151     std::vector<FxSend*> fxSends;
152     int GlobalTranspose; ///< amount of semi tones all notes should be transposed
153     int iLastPanRequest; ///< just for the return value of Pan(), so we don't have to make an injective function
154     int iEngineIndexSelf; ///< Reflects the index of this EngineChannel in the Engine's ArrayList.
155     bool bStatusChanged; ///< true in case an engine parameter has changed (e.g. new instrument, another volumet)
156 persson 2043 uint32_t RoundRobinIndex; ///< counter for round robin sample selection, incremented for each note on
157 schoenebeck 2630 InstrumentScript* pScript; ///< Points to the real-time instrument script(s) to be executed, NULL if current instrument does not have an instrument script. Even though the underlying VM representation of the script is shared among multiple sampler channels, the InstrumentScript object here is not shared though, it exists for each sampler channel separately.
158 iliev 2012
159     SynchronizedConfig< ArrayList<VirtualMidiDevice*> > virtualMidiDevices;
160     SynchronizedConfig< ArrayList<VirtualMidiDevice*> >::Reader virtualMidiDevicesReader_AudioThread;
161     SynchronizedConfig< ArrayList<VirtualMidiDevice*> >::Reader virtualMidiDevicesReader_MidiThread;
162    
163 persson 2127 // specialization of RTList that doesn't require the pool
164     // to be provided at construction time
165     template<typename T>
166     class LazyList : public RTList<T> {
167     public:
168     using RTList<T>::allocAppend;
169     using RTList<T>::pPool;
170 persson 2114
171 persson 2127 LazyList() : RTList<T>(0) { }
172     typename RTList<T>::Iterator allocAppend(Pool<T>* pool) {
173     pPool = pool;
174     return allocAppend();
175     }
176     };
177    
178     typedef std::map<uint, LazyList<Event>*> ActiveKeyGroupMap;
179     ActiveKeyGroupMap ActiveKeyGroups; ///< Contains event queues for key groups, ordered by key group ID.
180    
181 iliev 2012 virtual void ResetControllers();
182 schoenebeck 2871 virtual void ResetInternal(bool bResetEngine);
183 iliev 2012 virtual void RemoveAllFxSends();
184    
185     void ImportEvents(uint Samples);
186 schoenebeck 2871 int ScheduleEventMicroSec(const Event* pEvent, int delay);
187 schoenebeck 2598 void IgnoreEvent(int id);
188 persson 2114
189     void AddGroup(uint group);
190     void HandleKeyGroupConflicts(uint KeyGroup, Pool<Event>::Iterator& itNoteOnEvent);
191     void ClearGroupEventLists();
192     void DeleteGroupEventLists();
193 schoenebeck 2500
194     private:
195     /**
196     * Returns @c true if there are 2 ore more MidiInputPorts connected
197     * to this engine channel.
198     *
199     * This method is currently only used to prevent unnecessary
200     * MidiInputMutex.Lock() if there is not more than 1 MIDI input on
201     * this engine channel.
202     */
203     inline bool hasMultipleMIDIInputs() const {
204     //FIXME: leaves tiny time frames open (shortly after 1->2 devices connected or 2->1 disconnected) which could lead to concurrency issue for the purpose described above, however in practice it "should" be acceptable
205     return midiInputs.unsafeBack().size() > 1;
206     }
207 iliev 2012 };
208    
209     } // namespace LinuxSampler
210    
211 persson 2114 #endif /* __LS_ABSTRACTENGINECHANNEL_H__ */
212 iliev 2012

  ViewVC Help
Powered by ViewVC