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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2326 by persson, Thu Mar 8 19:40:14 2012 UTC revision 3706 by schoenebeck, Wed Jan 8 20:39:59 2020 UTC
# Line 3  Line 3 
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-2008 Christian Schoenebeck                         *   *   Copyright (C) 2005-2020 Christian Schoenebeck                         *
7   *   Copyright (C) 2009-2012 Christian Schoenebeck and Grigor Iliev        *   *   Copyright (C) 2009-2012 Grigor Iliev                                  *
8     *   Copyright (C) 2012-2017 Andreas Persson                               *
9   *                                                                         *   *                                                                         *
10   *   This program is free software; you can redistribute it and/or modify  *   *   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  *   *   it under the terms of the GNU General Public License as published by  *
# Line 30  Line 31 
31    
32  #include "../common/Pool.h"  #include "../common/Pool.h"
33  #include "../common/RingBuffer.h"  #include "../common/RingBuffer.h"
34    #include "../common/ResourceManager.h"
35    #include "common/AbstractInstrumentManager.h"
36    #include "common/InstrumentScriptVM.h"
37    
38    #define CTRL_TABLE_IDX_AFTERTOUCH   128
39    #define CTRL_TABLE_IDX_PITCHBEND    129
40    #define CTRL_TABLE_SIZE             130
41    
42  namespace LinuxSampler {  namespace LinuxSampler {
43    
44      class AbstractEngineChannel: public EngineChannel {      class MidiKeyboardManagerBase;
45    
46        class AbstractEngineChannel: public EngineChannel, public InstrumentScriptConsumer {
47          public:          public:
48              // implementation of abstract methods derived from interface class 'LinuxSampler::EngineChannel'              // implementation of abstract methods derived from interface class 'LinuxSampler::EngineChannel'
49              virtual void    PrepareLoadInstrument(const char* FileName, uint Instrument);              virtual void    PrepareLoadInstrument(const char* FileName, uint Instrument) OVERRIDE;
50              virtual void    Reset();              virtual void    Reset() OVERRIDE;
51              virtual void    SendNoteOn(uint8_t Key, uint8_t Velocity, uint8_t MidiChannel);              virtual void    SendNoteOn(uint8_t Key, uint8_t Velocity, uint8_t MidiChannel) OVERRIDE;
52              virtual void    SendNoteOn(uint8_t Key, uint8_t Velocity, uint8_t MidiChannel, int32_t FragmentPos);              virtual void    SendNoteOn(uint8_t Key, uint8_t Velocity, uint8_t MidiChannel, int32_t FragmentPos) OVERRIDE;
53              virtual void    SendNoteOff(uint8_t Key, uint8_t Velocity, uint8_t MidiChannel);              virtual void    SendNoteOff(uint8_t Key, uint8_t Velocity, uint8_t MidiChannel) OVERRIDE;
54              virtual void    SendNoteOff(uint8_t Key, uint8_t Velocity, uint8_t MidiChannel, int32_t FragmentPos);              virtual void    SendNoteOff(uint8_t Key, uint8_t Velocity, uint8_t MidiChannel, int32_t FragmentPos) OVERRIDE;
55              virtual void    SendPitchbend(int Pitch, uint8_t MidiChannel);              virtual void    SendPitchbend(int Pitch, uint8_t MidiChannel) OVERRIDE;
56              virtual void    SendPitchbend(int Pitch, uint8_t MidiChannel, int32_t FragmentPos);              virtual void    SendPitchbend(int Pitch, uint8_t MidiChannel, int32_t FragmentPos) OVERRIDE;
57              virtual void    SendControlChange(uint8_t Controller, uint8_t Value, uint8_t MidiChannel);              virtual void    SendControlChange(uint8_t Controller, uint8_t Value, uint8_t MidiChannel) OVERRIDE;
58              virtual void    SendControlChange(uint8_t Controller, uint8_t Value, uint8_t MidiChannel, int32_t FragmentPos);              virtual void    SendControlChange(uint8_t Controller, uint8_t Value, uint8_t MidiChannel, int32_t FragmentPos) OVERRIDE;            
59              virtual bool    StatusChanged(bool bNewStatus = false);              virtual void    SendChannelPressure(uint8_t Value, uint8_t MidiChannel) OVERRIDE;
60              virtual float   Volume();              virtual void    SendChannelPressure(uint8_t Value, uint8_t MidiChannel, int32_t FragmentPos) OVERRIDE;
61              virtual void    Volume(float f);              virtual void    SendPolyphonicKeyPressure(uint8_t Key, uint8_t Value, uint8_t MidiChannel) OVERRIDE;
62              virtual float   Pan();              virtual void    SendPolyphonicKeyPressure(uint8_t Key, uint8_t Value, uint8_t MidiChannel, int32_t FragmentPos) OVERRIDE;
63              virtual void    Pan(float f);              virtual bool    StatusChanged(bool bNewStatus = false) OVERRIDE;
64              virtual uint    Channels();              virtual float   Volume() OVERRIDE;
65              virtual AudioOutputDevice* GetAudioOutputDevice();              virtual void    Volume(float f) OVERRIDE;
66              virtual void    SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel);              virtual float   Pan() OVERRIDE;
67              virtual int     OutputChannel(uint EngineAudioChannel);              virtual void    Pan(float f) OVERRIDE;
68              virtual void    Connect(MidiInputPort* pMidiPort, midi_chan_t MidiChannel);              virtual uint    Channels() OVERRIDE;
69              virtual void    DisconnectMidiInputPort();              virtual AudioOutputDevice* GetAudioOutputDevice() OVERRIDE;
70              virtual MidiInputPort* GetMidiInputPort();              virtual void    SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) OVERRIDE;
71              virtual midi_chan_t MidiChannel();              virtual int     OutputChannel(uint EngineAudioChannel) OVERRIDE;
72              virtual String  InstrumentFileName();              virtual void    Connect(MidiInputPort* pMidiPort) OVERRIDE;
73              virtual String  InstrumentName();              virtual void    Disconnect(MidiInputPort* pMidiPort) OVERRIDE;
74              virtual int     InstrumentIndex();              virtual void    DisconnectAllMidiInputPorts() OVERRIDE;
75              virtual int     InstrumentStatus();              virtual uint    GetMidiInputPortCount() OVERRIDE;
76              virtual Engine* GetEngine();              virtual MidiInputPort* GetMidiInputPort(uint index) OVERRIDE;
77              virtual String  EngineName();              virtual midi_chan_t MidiChannel() OVERRIDE;
78              virtual FxSend* AddFxSend(uint8_t MidiCtrl, String Name = "") throw (Exception);              virtual void    SetMidiChannel(midi_chan_t MidiChannel) OVERRIDE;
79              virtual FxSend* GetFxSend(uint FxSendIndex);              virtual void    Connect(MidiInputPort* pMidiPort, midi_chan_t MidiChannel) OVERRIDE; // deprecated, may be removed
80              virtual uint    GetFxSendCount();              virtual void    DisconnectMidiInputPort() OVERRIDE; // deprecated, may be removed
81              virtual void    RemoveFxSend(FxSend* pFxSend);              virtual MidiInputPort* GetMidiInputPort() OVERRIDE; // deprecated, may be removed
82              virtual void    Connect(VirtualMidiDevice* pDevice);              virtual String  InstrumentFileName() OVERRIDE;
83              virtual void    Disconnect(VirtualMidiDevice* pDevice);              virtual String  InstrumentName() OVERRIDE;
84                virtual int     InstrumentIndex() OVERRIDE;
85                virtual int     InstrumentStatus() OVERRIDE;
86                virtual Engine* GetEngine() OVERRIDE;
87                virtual String  EngineName() OVERRIDE;
88                virtual FxSend* AddFxSend(uint8_t MidiCtrl, String Name = "") throw (Exception) OVERRIDE;
89                virtual FxSend* GetFxSend(uint FxSendIndex) OVERRIDE;
90                virtual uint    GetFxSendCount() OVERRIDE;
91                virtual void    RemoveFxSend(FxSend* pFxSend) OVERRIDE;
92                virtual void    Connect(VirtualMidiDevice* pDevice) OVERRIDE;
93                virtual void    Disconnect(VirtualMidiDevice* pDevice) OVERRIDE;
94    
95                // implementation of abstract methods derived from AbstractEngine::ScriptConsumer
96                virtual void ResourceToBeUpdated(VMParserContext* pResource, void*& pUpdateArg) OVERRIDE {}
97                virtual void ResourceUpdated(VMParserContext* pOldResource, VMParserContext* pNewResource, void* pUpdateArg) OVERRIDE {}
98                virtual void OnResourceProgress(float fProgress) OVERRIDE {}
99    
100              virtual AbstractEngine::Format GetEngineFormat() = 0;              virtual AbstractEngine::Format GetEngineFormat() = 0;
101                virtual MidiKeyboardManagerBase* GetMidiKeyboardManager() = 0;
102    
103              AudioOutputDevice* GetAudioOutputDeviceSafe();              AudioOutputDevice* GetAudioOutputDeviceSafe();
104    
105                script_callback_id_t GetScriptCallbackID(const ScriptEvent* e) const {
106                    return pScript->pEvents->getID(e);
107                }
108    
109                RTList<ScriptEvent>::Iterator ScriptCallbackByID(script_callback_id_t id) const {
110                    return pScript->pEvents->fromID(id);
111                }
112    
113                void ScheduleResumeOfScriptCallback(RTList<ScriptEvent>::Iterator& itCallback, sched_time_t now, bool forever);
114    
115                bool hasFreeScriptCallbacks(vmint n) const {
116                    return pScript->pEvents->poolHasFreeElements(n);
117                }
118    
119                RTList<ScriptEvent>::Iterator forkScriptCallback(ScriptEvent* parent, bool bAutoAbort);
120    
121              friend class AbstractVoice;              friend class AbstractVoice;
122              friend class AbstractEngine;              friend class AbstractEngine;
123              template<class TV, class TRR, class TR, class TD, class TIM, class TI> friend class EngineBase;              template<class TV, class TRR, class TR, class TD, class TIM, class TI> friend class EngineBase;
124              template<class EC, class R, class S, class D> friend class VoiceBase;              template<class EC, class R, class S, class D> friend class VoiceBase;
125    
126          protected:          //protected:
127              AbstractEngineChannel();              AbstractEngineChannel();
128              virtual ~AbstractEngineChannel();              virtual ~AbstractEngineChannel();
129    
130              AbstractEngine*           pEngine;              AbstractEngine*           pEngine;
131              Mutex                     EngineMutex; ///< protects the Engine from access by the instrument loader thread when lscp is disconnecting              Mutex                     EngineMutex; ///< protects the Engine from access by the instrument loader thread when lscp is disconnecting
132                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.
133    
134          public: // TODO: should be protected          //protected:
135              AudioChannel*             pChannelLeft;             ///< encapsulates the audio rendering buffer (left)              AudioChannel*             pChannelLeft;             ///< encapsulates the audio rendering buffer (left)
136              AudioChannel*             pChannelRight;            ///< encapsulates the audio rendering buffer (right)              AudioChannel*             pChannelRight;            ///< encapsulates the audio rendering buffer (right)
         protected:  
137              int                       AudioDeviceChannelLeft;   ///< audio device channel number to which the left channel is connected to              int                       AudioDeviceChannelLeft;   ///< audio device channel number to which the left channel is connected to
138              int                       AudioDeviceChannelRight;  ///< audio device channel number to which the right channel is connected to              int                       AudioDeviceChannelRight;  ///< audio device channel number to which the right channel is connected to
139              MidiInputPort*            pMidiInputPort;           ///< Points to the connected MIDI input port or NULL if none assigned.              DoubleBuffer< ArrayList<MidiInputPort*> > midiInputs; ///< MIDI input ports on which this sampler engine channel shall listen to.
140              midi_chan_t               midiChannel;              ///< MIDI channel(s) on which this engine channel listens to.              midi_chan_t               midiChannel;              ///< MIDI channel(s) on which this engine channel listens to (on all MIDI input ports).
141              RingBuffer<Event,false>*  pEventQueue;              ///< Input event queue.              RingBuffer<Event,false>*  pEventQueue;              ///< Input event queue.
142              RTList<Event>*            pEvents;                  ///< All engine channel specific events for the current audio fragment.              RTList<Event>*            pEvents;                  ///< All engine channel specific events for the current audio fragment.
143              uint8_t                   ControllerTable[129];     ///< Reflects the current values (0-127) of all MIDI controllers for this engine / sampler channel. Number 128 is for channel pressure (mono aftertouch).              struct _DelayedEvents {
144                    RTList<Event>*            pList; ///< Unsorted list where all delayed events are moved to and remain here until they're finally processed.
145                    Pool<ScheduledEvent>      schedulerNodes; ///< Nodes used to sort the delayed events (stored on pList) with time sorted queue.
146                    RTAVLTree<ScheduledEvent> queue; ///< Used to access the delayed events (from pList) in time sorted manner.
147    
148                    _DelayedEvents() : pList(NULL), schedulerNodes(CONFIG_MAX_EVENTS_PER_FRAGMENT) {}
149    
150                    inline void clear() {
151                        if (pList) pList->clear();
152                        schedulerNodes.clear();
153                        queue.clear();
154                    }
155                } delayedEvents;
156                uint8_t                   ControllerTable[CTRL_TABLE_SIZE];     ///< 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.
157              String                    InstrumentFile;              String                    InstrumentFile;
158              int                       InstrumentIdx;              int                       InstrumentIdx;
159              String                    InstrumentIdxName;              String                    InstrumentIdxName;
160              int                       InstrumentStat;              int                       InstrumentStat;
161              double                    GlobalVolume;             ///< Master volume factor set through the C++ API / LSCP (a value < 1.0 means attenuation, a value > 1.0 means amplification)              double                    GlobalVolume;             ///< Master volume factor set through the C++ API / LSCP (a value < 1.0 means attenuation, a value > 1.0 means amplification)
162              double                    MidiVolume;               ///< Volume factor altered by MIDI CC#7 (a value < 1.0 means attenuation, a value > 1.0 means amplification)              double                    MidiVolume;               ///< Volume factor altered by MIDI CC#7 (a value < 1.0 means attenuation, a value > 1.0 means amplification)
             float                     GlobalPanLeft;  
             float                     GlobalPanRight;  
163              int                       Pitch;                    ///< Current (absolute) MIDI pitch value.              int                       Pitch;                    ///< Current (absolute) MIDI pitch value.
164              float                     CurrentKeyDimension;      ///< Current value (0-1.0) for the keyboard dimension, altered by pressing a keyswitching key.              float                     CurrentKeyDimension;      ///< Current value (0-1.0) for the keyboard dimension, altered by pressing a keyswitching key.
165              bool                      PortamentoMode;           ///< in Portamento Mode we slide the pitch from the last note to the current note.              bool                      PortamentoMode;           ///< in Portamento Mode we slide the pitch from the last note to the current note.
# Line 116  namespace LinuxSampler { Line 168  namespace LinuxSampler {
168              std::vector<FxSend*>      fxSends;              std::vector<FxSend*>      fxSends;
169              int                       GlobalTranspose;          ///< amount of semi tones all notes should be transposed              int                       GlobalTranspose;          ///< amount of semi tones all notes should be transposed
170              int                       iLastPanRequest;          ///< just for the return value of Pan(), so we don't have to make an injective function              int                       iLastPanRequest;          ///< just for the return value of Pan(), so we don't have to make an injective function
171              int                       iEngineIndexSelf;         ///< Reflects the index of this EngineChannel in the Engine's ArrayList.              ssize_t                   iEngineIndexSelf;         ///< Reflects the index of this EngineChannel in the Engine's ArrayList.
172              bool                      bStatusChanged;           ///< true in case an engine parameter has changed (e.g. new instrument, another volumet)              bool                      bStatusChanged;           ///< true in case an engine parameter has changed (e.g. new instrument, another volumet)
173              uint32_t                  RoundRobinIndex;          ///< counter for round robin sample selection, incremented for each note on              uint32_t                  RoundRobinIndex;          ///< counter for round robin sample selection, incremented for each note on
174                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.
175    
176              SynchronizedConfig< ArrayList<VirtualMidiDevice*> > virtualMidiDevices;              SynchronizedConfig< ArrayList<VirtualMidiDevice*> > virtualMidiDevices;
177              SynchronizedConfig< ArrayList<VirtualMidiDevice*> >::Reader virtualMidiDevicesReader_AudioThread;              SynchronizedConfig< ArrayList<VirtualMidiDevice*> >::Reader virtualMidiDevicesReader_AudioThread;
# Line 143  namespace LinuxSampler { Line 196  namespace LinuxSampler {
196              ActiveKeyGroupMap ActiveKeyGroups;      ///< Contains event queues for key groups, ordered by key group ID.              ActiveKeyGroupMap ActiveKeyGroups;      ///< Contains event queues for key groups, ordered by key group ID.
197    
198              virtual void ResetControllers();              virtual void ResetControllers();
199              virtual void ResetInternal();              virtual void ResetInternal(bool bResetEngine);
200              virtual void RemoveAllFxSends();              virtual void RemoveAllFxSends();
201    
202              void ImportEvents(uint Samples);              void ImportEvents(uint Samples);
203                virtual note_id_t ScheduleNoteMicroSec(const Event* pEvent, int64_t delay) = 0;
204                event_id_t ScheduleEventMicroSec(const Event* pEvent, int64_t delay);
205                void IgnoreEvent(event_id_t id);
206                virtual void IgnoreNote(note_id_t id) = 0;
207                void IgnoreEventByScriptID(const ScriptID& id);
208                virtual uint AllNoteIDs(note_id_t* dstBuf, uint bufSize) = 0;
209    
210              void AddGroup(uint group);              void AddGroup(uint group);
211              void HandleKeyGroupConflicts(uint KeyGroup, Pool<Event>::Iterator& itNoteOnEvent);              void HandleKeyGroupConflicts(uint KeyGroup, Pool<Event>::Iterator& itNoteOnEvent);
212              void ClearGroupEventLists();              void ClearGroupEventLists();
213              void DeleteGroupEventLists();              void DeleteGroupEventLists();
214    
215            private:
216                /**
217                 * Returns @c true if there are 2 ore more MidiInputPorts connected
218                 * to this engine channel.
219                 *
220                 * This method is currently only used to prevent unnecessary
221                 * MidiInputMutex.Lock() if there is not more than 1 MIDI input on
222                 * this engine channel.
223                 */
224                inline bool hasMultipleMIDIInputs() const {
225                    //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
226                    return midiInputs.unsafeBack().size() > 1;
227                }
228    
229                inline bool applyTranspose(Event* event);
230      };      };
231    
232  } // namespace LinuxSampler  } // namespace LinuxSampler

Legend:
Removed from v.2326  
changed lines
  Added in v.3706

  ViewVC Help
Powered by ViewVC