DSSIPluginInstance.h
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6  Centre for Digital Music, Queen Mary, University of London.
7 
8  This program is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2 of the
11  License, or (at your option) any later version. See the file
12  COPYING included with this distribution for more information.
13 */
14 
15 /*
16  This is a modified version of a source file from the
17  Rosegarden MIDI and audio sequencer and notation editor.
18  This file copyright 2000-2006 Chris Cannam.
19 */
20 
21 #ifndef SV_DSSIPLUGININSTANCE_H
22 #define SV_DSSIPLUGININSTANCE_H
23 
24 #define DSSI_API_LEVEL 2
25 
26 #include <vector>
27 #include <set>
28 #include <map>
29 #include <QString>
30 #include <QMutex>
31 
32 #include "api/dssi.h"
33 
34 #include "base/RingBuffer.h"
35 #include "base/Thread.h"
36 #include "RealTimePluginInstance.h"
37 #include "base/Scavenger.h"
38 
40 {
41 public:
42  virtual ~DSSIPluginInstance();
43 
44  bool isOK() const override { return m_instanceHandle != 0; }
45 
46  int getClientId() const { return m_client; }
47  QString getPluginIdentifier() const override { return m_identifier; }
48  int getPosition() const { return m_position; }
49 
50  std::string getIdentifier() const override;
51  std::string getName() const override;
52  std::string getDescription() const override;
53  std::string getMaker() const override;
54  int getPluginVersion() const override;
55  std::string getCopyright() const override;
56 
57  void run(const RealTime &, int count = 0) override;
58 
59  int getParameterCount() const override;
60  void setParameterValue(int parameter, float value) override;
61  float getParameterValue(int parameter) const override;
62  float getParameterDefault(int parameter) const override;
63  int getParameterDisplayHint(int parameter) const override;
64 
65  ParameterList getParameterDescriptors() const override;
66  float getParameter(std::string) const override;
67  void setParameter(std::string, float) override;
68 
69  std::string configure(std::string key, std::string value) override;
70  void sendEvent(const RealTime &eventTime,
71  const void *event) override;
72  void clearEvents() override;
73 
74  int getBufferSize() const override { return m_blockSize; }
75  int getAudioInputCount() const override { return (int)m_audioPortsIn.size(); }
76  int getAudioOutputCount() const override { return m_idealChannelCount; }
77  sample_t **getAudioInputBuffers() override { return m_inputBuffers; }
79 
80  int getControlOutputCount() const override { return (int)m_controlPortsOut.size(); }
81  float getControlOutputValue(int n) const override;
82 
83  ProgramList getPrograms() const override;
84  std::string getCurrentProgram() const override;
85  std::string getProgram(int bank, int program) const override;
86  virtual int getProgram(std::string name) const;
87  void selectProgram(std::string program) override;
88 
89  bool isBypassed() const override { return m_bypassed; }
90  void setBypassed(bool bypassed) override { m_bypassed = bypassed; }
91 
92  sv_frame_t getLatency() override;
93 
94  void silence() override;
95  void discardEvents() override;
96  void setIdealChannelCount(int channels) override; // may re-instantiate
97 
98  virtual bool isInGroup() const { return m_grouped; }
99  virtual void detachFromGroup();
100 
101  std::string getType() const override { return "DSSI Real-Time Plugin"; }
102 
103 protected:
104  // To be constructed only by DSSIPluginFactory
105  friend class DSSIPluginFactory;
106 
107  // Constructor that creates the buffers internally
108  //
110  int client,
111  QString identifier,
112  int position,
113  sv_samplerate_t sampleRate,
114  int blockSize,
115  int idealChannelCount,
116  const DSSI_Descriptor* descriptor);
117 
118  void init();
119  void instantiate(sv_samplerate_t sampleRate);
120  void cleanup();
121  void activate();
122  void deactivate();
123  void connectPorts();
124 
125  bool handleController(snd_seq_event_t *ev);
126  void setPortValueFromController(int portNumber, int controlValue);
127  void selectProgramAux(std::string program, bool backupPortValues);
128  void checkProgramCache() const;
129 
131  void runGrouped(const RealTime &);
132 
133  // For use in DSSIPluginFactory (set in the DSSI_Host_Descriptor):
134  static int requestMidiSend(LADSPA_Handle instance,
135  unsigned char ports,
136  unsigned char channels);
137  static void midiSend(LADSPA_Handle instance,
138  snd_seq_event_t *events,
139  unsigned long eventCount);
140  static int requestNonRTThread(LADSPA_Handle instance,
141  void (*runFunction)(LADSPA_Handle));
142 
143  int m_client;
145  LADSPA_Handle m_instanceHandle;
146  const DSSI_Descriptor *m_descriptor;
147 
148  std::vector<std::pair<int, LADSPA_Data*> > m_controlPortsIn;
149  std::vector<std::pair<int, LADSPA_Data*> > m_controlPortsOut;
150 
151  std::vector<LADSPA_Data> m_backupControlPortsIn;
152 
153  std::map<int, int> m_controllerMap;
154 
155  std::vector<int> m_audioPortsIn;
156  std::vector<int> m_audioPortsOut;
157 
158  struct ProgramControl {
159  int msb;
160  int lsb;
161  int program;
162  };
164 
166  int bank;
167  int program;
168  std::string name;
169  };
170  mutable std::vector<ProgramDescriptor> m_cachedPrograms;
171  mutable bool m_programCacheValid;
172 
174 
183  bool m_run;
184 
186  std::string m_program;
187  bool m_grouped;
189 
192 
194 
195  typedef std::set<DSSIPluginInstance *> PluginSet;
196  typedef std::map<QString, PluginSet> GroupMap;
197  static GroupMap m_groupMap;
198  static snd_seq_event_t **m_groupLocalEventBuffers;
200 
202 
203  class NonRTPluginThread : public Thread
204  {
205  public:
206  NonRTPluginThread(LADSPA_Handle handle,
207  void (*runFunction)(LADSPA_Handle)) :
208  m_handle(handle),
209  m_runFunction(runFunction),
210  m_exiting(false) { }
211 
212  void run() override;
213  void setExiting() { m_exiting = true; }
214 
215  protected:
216  LADSPA_Handle m_handle;
217  void (*m_runFunction)(LADSPA_Handle);
218  bool m_exiting;
219  };
220  static std::map<LADSPA_Handle, std::set<NonRTPluginThread *> > m_threads;
221 };
222 
223 #endif // _DSSIPLUGININSTANCE_H_
224 
double sv_samplerate_t
Sample rate.
Definition: BaseTypes.h:51
int getClientId() const
std::vector< LADSPA_Data > m_backupControlPortsIn
std::vector< std::pair< int, LADSPA_Data * > > m_controlPortsOut
static int requestMidiSend(LADSPA_Handle instance, unsigned char ports, unsigned char channels)
std::string configure(std::string key, std::string value) override
static std::map< LADSPA_Handle, std::set< NonRTPluginThread * > > m_threads
int64_t sv_frame_t
Frame index, the unit of our time axis.
Definition: BaseTypes.h:31
float getControlOutputValue(int n) const override
int getAudioOutputCount() const override
static void midiSend(LADSPA_Handle instance, snd_seq_event_t *events, unsigned long eventCount)
std::map< int, int > m_controllerMap
int getPluginVersion() const override
std::string getType() const override
bool handleController(snd_seq_event_t *ev)
A very simple class that facilitates running things like plugins without locking, by collecting unwan...
Definition: Scavenger.h:44
NonRTPluginThread(LADSPA_Handle handle, void(*runFunction)(LADSPA_Handle))
void setParameterValue(int parameter, float value) override
static snd_seq_event_t ** m_groupLocalEventBuffers
int getPosition() const
std::string getName() const override
sample_t ** getAudioOutputBuffers() override
RingBuffer< snd_seq_event_t > m_eventBuffer
int getParameterDisplayHint(int parameter) const override
std::string getMaker() const override
void selectProgram(std::string program) override
int getControlOutputCount() const override
void selectProgramAux(std::string program, bool backupPortValues)
void run(const RealTime &, int count=0) override
Run for one block, starting at the given time.
std::vector< ProgramDescriptor > m_cachedPrograms
int getParameterCount() const override
std::vector< int > m_audioPortsOut
QString getPluginIdentifier() const override
void setIdealChannelCount(int channels) override
std::string getProgram(int bank, int program) const override
const DSSI_Descriptor * m_descriptor
float getParameter(std::string) const override
sv_frame_t getLatency() override
static Scavenger< ScavengerArrayWrapper< snd_seq_event_t * > > m_bufferScavenger
ParameterList getParameterDescriptors() const override
sample_t ** getAudioInputBuffers() override
void setParameter(std::string, float) override
int getBufferSize() const override
bool isOK() const override
static int requestNonRTThread(LADSPA_Handle instance, void(*runFunction)(LADSPA_Handle))
std::string getDescription() const override
ProgramList getPrograms() const override
std::string getCurrentProgram() const override
virtual bool isInGroup() const
void checkProgramCache() const
void instantiate(sv_samplerate_t sampleRate)
Definition: Thread.h:24
static RealTimePluginFactory * instance(QString pluginType)
bool isBypassed() const override
sv_samplerate_t m_sampleRate
void setBypassed(bool bypassed) override
std::vector< std::pair< int, LADSPA_Data * > > m_controlPortsIn
float getParameterDefault(int parameter) const override
void setPortValueFromController(int portNumber, int controlValue)
void runGrouped(const RealTime &)
float getParameterValue(int parameter) const override
std::string getCopyright() const override
std::string getIdentifier() const override
static size_t m_groupLocalEventBufferCount
ProgramControl m_pending
static GroupMap m_groupMap
void discardEvents() override
void sendEvent(const RealTime &eventTime, const void *event) override
virtual void detachFromGroup()
std::vector< int > m_audioPortsIn
std::map< QString, PluginSet > GroupMap
int getAudioInputCount() const override
void clearEvents() override
std::set< DSSIPluginInstance * > PluginSet
LADSPA_Handle m_instanceHandle
DSSIPluginInstance(RealTimePluginFactory *factory, int client, QString identifier, int position, sv_samplerate_t sampleRate, int blockSize, int idealChannelCount, const DSSI_Descriptor *descriptor)
RealTime represents time values to nanosecond precision with accurate arithmetic and frame-rate conve...
Definition: RealTime.h:42