comparison plugin/DSSIPluginInstance.h @ 0:da6937383da8

initial import
author Chris Cannam
date Tue, 10 Jan 2006 16:33:16 +0000
parents
children d86891498eef
comparison
equal deleted inserted replaced
-1:000000000000 0:da6937383da8
1 /* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 A waveform viewer and audio annotation editor.
5 Chris Cannam, Queen Mary University of London, 2005
6
7 This is experimental software. Not for distribution.
8 */
9
10 /*
11 This is a modified version of a source file from the
12 Rosegarden MIDI and audio sequencer and notation editor.
13 This file copyright 2000-2005 Chris Cannam.
14 */
15
16 #ifndef _DSSIPLUGININSTANCE_H_
17 #define _DSSIPLUGININSTANCE_H_
18
19 #define DSSI_API_LEVEL 2
20
21 #include <vector>
22 #include <set>
23 #include <map>
24 #include <QString>
25 #include <QMutex>
26 #include <QThread>
27
28 #include "api/dssi.h"
29
30 #include "base/RingBuffer.h"
31 #include "RealTimePluginInstance.h"
32 #include "base/Scavenger.h"
33
34 class DSSIPluginInstance : public RealTimePluginInstance
35 {
36 public:
37 virtual ~DSSIPluginInstance();
38
39 virtual bool isOK() const { return m_instanceHandle != 0; }
40
41 int getClientId() const { return m_client; }
42 virtual QString getIdentifier() const { return m_identifier; }
43 int getPosition() const { return m_position; }
44
45 virtual void run(const RealTime &);
46
47 virtual unsigned int getParameterCount() const;
48 virtual void setParameterValue(unsigned int parameter, float value);
49 virtual float getParameterValue(unsigned int parameter) const;
50 virtual float getParameterDefault(unsigned int parameter) const;
51 virtual QString configure(QString key, QString value);
52 virtual void sendEvent(const RealTime &eventTime,
53 const void *event);
54
55 virtual size_t getBufferSize() const { return m_blockSize; }
56 virtual size_t getAudioInputCount() const { return m_audioPortsIn.size(); }
57 virtual size_t getAudioOutputCount() const { return m_idealChannelCount; }
58 virtual sample_t **getAudioInputBuffers() { return m_inputBuffers; }
59 virtual sample_t **getAudioOutputBuffers() { return m_outputBuffers; }
60
61 virtual QStringList getPrograms() const;
62 virtual QString getCurrentProgram() const;
63 virtual QString getProgram(int bank, int program) const;
64 virtual unsigned long getProgram(QString name) const;
65 virtual void selectProgram(QString program);
66
67 virtual bool isBypassed() const { return m_bypassed; }
68 virtual void setBypassed(bool bypassed) { m_bypassed = bypassed; }
69
70 virtual size_t getLatency();
71
72 virtual void silence();
73 virtual void discardEvents();
74 virtual void setIdealChannelCount(size_t channels); // may re-instantiate
75
76 virtual bool isInGroup() const { return m_grouped; }
77 virtual void detachFromGroup();
78
79 protected:
80 // To be constructed only by DSSIPluginFactory
81 friend class DSSIPluginFactory;
82
83 // Constructor that creates the buffers internally
84 //
85 DSSIPluginInstance(RealTimePluginFactory *factory,
86 int client,
87 QString identifier,
88 int position,
89 unsigned long sampleRate,
90 size_t blockSize,
91 int idealChannelCount,
92 const DSSI_Descriptor* descriptor);
93
94 // Constructor that uses shared buffers
95 //
96 DSSIPluginInstance(RealTimePluginFactory *factory,
97 int client,
98 QString identifier,
99 int position,
100 unsigned long sampleRate,
101 size_t blockSize,
102 sample_t **inputBuffers,
103 sample_t **outputBuffers,
104 const DSSI_Descriptor* descriptor);
105
106 void init();
107 void instantiate(unsigned long sampleRate);
108 void cleanup();
109 void activate();
110 void deactivate();
111 void connectPorts();
112
113 bool handleController(snd_seq_event_t *ev);
114 void setPortValueFromController(unsigned int portNumber, int controlValue);
115 void selectProgramAux(QString program, bool backupPortValues);
116 void checkProgramCache() const;
117
118 void initialiseGroupMembership();
119 void runGrouped(const RealTime &);
120
121 // For use in DSSIPluginFactory (set in the DSSI_Host_Descriptor):
122 static int requestMidiSend(LADSPA_Handle instance,
123 unsigned char ports,
124 unsigned char channels);
125 static void midiSend(LADSPA_Handle instance,
126 snd_seq_event_t *events,
127 unsigned long eventCount);
128 static int requestNonRTThread(LADSPA_Handle instance,
129 void (*runFunction)(LADSPA_Handle));
130
131 int m_client;
132 int m_position;
133 LADSPA_Handle m_instanceHandle;
134 const DSSI_Descriptor *m_descriptor;
135
136 std::vector<std::pair<unsigned long, LADSPA_Data*> > m_controlPortsIn;
137 std::vector<std::pair<unsigned long, LADSPA_Data*> > m_controlPortsOut;
138
139 std::vector<LADSPA_Data> m_backupControlPortsIn;
140
141 std::map<int, int> m_controllerMap;
142
143 std::vector<int> m_audioPortsIn;
144 std::vector<int> m_audioPortsOut;
145
146 struct ProgramControl {
147 int msb;
148 int lsb;
149 int program;
150 };
151 ProgramControl m_pending;
152
153 struct ProgramDescriptor {
154 int bank;
155 int program;
156 QString name;
157 };
158 mutable std::vector<ProgramDescriptor> m_cachedPrograms;
159 mutable bool m_programCacheValid;
160
161 RingBuffer<snd_seq_event_t> m_eventBuffer;
162
163 size_t m_blockSize;
164 sample_t **m_inputBuffers;
165 sample_t **m_outputBuffers;
166 bool m_ownBuffers;
167 size_t m_idealChannelCount;
168 size_t m_outputBufferCount;
169 size_t m_sampleRate;
170 float *m_latencyPort;
171 bool m_run;
172
173 bool m_bypassed;
174 QString m_program;
175 bool m_grouped;
176 RealTime m_lastRunTime;
177
178 QMutex m_processLock;
179
180 typedef std::set<DSSIPluginInstance *> PluginSet;
181 typedef std::map<QString, PluginSet> GroupMap;
182 static GroupMap m_groupMap;
183 static snd_seq_event_t **m_groupLocalEventBuffers;
184 static size_t m_groupLocalEventBufferCount;
185
186 static Scavenger<ScavengerArrayWrapper<snd_seq_event_t *> > m_bufferScavenger;
187
188 class NonRTPluginThread : public QThread
189 {
190 public:
191 NonRTPluginThread(LADSPA_Handle handle,
192 void (*runFunction)(LADSPA_Handle)) :
193 m_handle(handle),
194 m_runFunction(runFunction),
195 m_exiting(false) { }
196
197 virtual void run();
198 void setExiting() { m_exiting = true; }
199
200 protected:
201 LADSPA_Handle m_handle;
202 void (*m_runFunction)(LADSPA_Handle);
203 bool m_exiting;
204 };
205 static std::map<LADSPA_Handle, std::set<NonRTPluginThread *> > m_threads;
206 };
207
208 #endif // _DSSIPLUGININSTANCE_H_
209