1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4     Rosegarden
5     A sequencer and musical notation editor.
6     Copyright 2000-2021 the Rosegarden development team.
7     See the AUTHORS file for more details.
8 
9     This program is free software; you can redistribute it and/or
10     modify it under the terms of the GNU General Public License as
11     published by the Free Software Foundation; either version 2 of the
12     License, or (at your option) any later version.  See the file
13     COPYING included with this distribution for more information.
14 */
15 
16 #include <vector>
17 #include <set>
18 #include <QString>
19 #include "base/Instrument.h"
20 
21 #ifndef RG_LADSPAPLUGININSTANCE_H
22 #define RG_LADSPAPLUGININSTANCE_H
23 
24 #include <ladspa.h>
25 #include "RunnablePluginInstance.h"
26 
27 namespace Rosegarden
28 {
29 
30 // LADSPA plugin instance.  LADSPA is a variable block size API, but
31 // for one reason and another it's more convenient to use a fixed
32 // block size in this wrapper.
33 //
34 class LADSPAPluginInstance : public RunnablePluginInstance
35 {
36 public:
37     ~LADSPAPluginInstance() override;
38 
isOK()39     bool isOK() const override { return !m_instanceHandles.empty(); }
40 
getInstrument()41     InstrumentId getInstrument() const { return m_instrument; }
getIdentifier()42     QString getIdentifier() const override { return m_identifier; }
getPosition()43     int getPosition() const { return m_position; }
44 
45     void run(const RealTime &rt) override;
46 
47     void setPortValue(unsigned int portNumber, float value) override;
48     float getPortValue(unsigned int portNumber) override;
49 
getBufferSize()50     size_t getBufferSize() override { return m_blockSize; }
getAudioInputCount()51     size_t getAudioInputCount() override { return m_instanceCount * m_audioPortsIn.size(); }
getAudioOutputCount()52     size_t getAudioOutputCount() override { return m_instanceCount * m_audioPortsOut.size(); }
getAudioInputBuffers()53     sample_t **getAudioInputBuffers() override { return m_inputBuffers; }
getAudioOutputBuffers()54     sample_t **getAudioOutputBuffers() override { return m_outputBuffers; }
55 
isBypassed()56     bool isBypassed() const override { return m_bypassed; }
setBypassed(bool bypassed)57     void setBypassed(bool bypassed) override { m_bypassed = bypassed; }
58 
59     size_t getLatency() override;
60 
61     void silence() override;
62     void setIdealChannelCount(size_t channels) override; // may re-instantiate
63 
64 protected:
65     // To be constructed only by LADSPAPluginFactory
66     friend class LADSPAPluginFactory;
67 
68     // Constructor that creates the buffers internally
69     //
70     LADSPAPluginInstance(PluginFactory *factory,
71                          InstrumentId instrument,
72                          QString identifier,
73                          int position,
74                          unsigned long sampleRate,
75                          size_t blockSize,
76                          int idealChannelCount,
77                          const LADSPA_Descriptor* descriptor);
78 
79     // Constructor that uses shared buffers
80     //
81     LADSPAPluginInstance(PluginFactory *factory,
82                          InstrumentId instrument,
83                          QString identifier,
84                          int position,
85                          unsigned long sampleRate,
86                          size_t blockSize,
87                          sample_t **inputBuffers,
88                          sample_t **outputBuffers,
89                          const LADSPA_Descriptor* descriptor);
90 
91     void init(int idealChannelCount = 0);
92     void instantiate(unsigned long sampleRate);
93     void cleanup();
94     void activate();
95     void deactivate();
96 
97     // Connection of data (and behind the scenes control) ports
98     //
99     void connectPorts();
100 
101     InstrumentId   m_instrument;
102     int                        m_position;
103     std::vector<LADSPA_Handle> m_instanceHandles;
104     size_t                     m_instanceCount;
105     const LADSPA_Descriptor   *m_descriptor;
106 
107     std::vector<std::pair<unsigned long, LADSPA_Data*> > m_controlPortsIn;
108     std::vector<std::pair<unsigned long, LADSPA_Data*> > m_controlPortsOut;
109 
110     std::vector<int>          m_audioPortsIn;
111     std::vector<int>          m_audioPortsOut;
112 
113     size_t                    m_blockSize;
114     sample_t                **m_inputBuffers;
115     sample_t                **m_outputBuffers;
116     bool                      m_ownBuffers;
117     size_t                    m_sampleRate;
118     float                    *m_latencyPort;
119     bool                      m_run;
120 
121     bool                      m_bypassed;
122 };
123 
124 }
125 
126 #endif // RG_LADSPAPLUGININSTANCE_H
127 
128