1 /* cmt.cpp
2 
3    Computer Music Toolkit - a library of LADSPA plugins. Copyright (C)
4    2000-2002 Richard W.E. Furse. The author may be contacted at
5    richard@muse.demon.co.uk.
6 
7    This library is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public Licence as
9    published by the Free Software Foundation; either version 2 of the
10    Licence, or (at your option) any later version.
11 
12    This library is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this library; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20    02111-1307, USA. */
21 
22 /*****************************************************************************/
23 
24 #include <string.h>
25 
26 /*****************************************************************************/
27 
28 #include "cmt.h"
29 
30 /*****************************************************************************/
31 
32 inline char *
localStrdup(const char * input)33 localStrdup(const char * input) {
34   char * output = new char[strlen(input) + 1];
35   strcpy(output, input);
36   return output;
37 }
38 
39 /*****************************************************************************/
40 
41 CMT_Descriptor::
~CMT_Descriptor()42 ~CMT_Descriptor() {
43   if (Label)
44     delete [] Label;
45   if (Name)
46     delete [] Name;
47   if (Maker)
48     delete [] Maker;
49   if (Copyright)
50     delete [] Copyright;
51   if (ImplementationData)
52     delete (CMT_ImplementationData *)ImplementationData;
53   if (PortDescriptors)
54     delete [] PortDescriptors;
55   if (PortNames) {
56     for (unsigned long lIndex = 0; lIndex < PortCount; lIndex++)
57       if (PortNames[lIndex])
58 	delete [] PortNames[lIndex];
59     delete [] PortNames;
60   }
61   if (PortRangeHints)
62     delete [] PortRangeHints;
63 }
64 
65 /*****************************************************************************/
66 
67 void
CMT_ConnectPort(LADSPA_Handle Instance,unsigned long Port,LADSPA_Data * DataLocation)68 CMT_ConnectPort(LADSPA_Handle Instance,
69 		unsigned long Port,
70 		LADSPA_Data * DataLocation) {
71   CMT_PluginInstance * poInstance = (CMT_PluginInstance *)Instance;
72   poInstance->m_ppfPorts[Port] = DataLocation;
73 }
74 
75 /*****************************************************************************/
76 
77 void
CMT_Cleanup(LADSPA_Handle Instance)78 CMT_Cleanup(LADSPA_Handle Instance) {
79   CMT_PluginInstance * poInstance = (CMT_PluginInstance *)Instance;
80   delete poInstance;
81 }
82 
83 /*****************************************************************************/
84 
85 CMT_Descriptor::
CMT_Descriptor(unsigned long lUniqueID,const char * pcLabel,LADSPA_Properties iProperties,const char * pcName,const char * pcMaker,const char * pcCopyright,CMT_ImplementationData * poImplementationData,LADSPA_Instantiate_Function fInstantiate,LADSPA_Activate_Function fActivate,LADSPA_Run_Function fRun,LADSPA_Run_Adding_Function fRunAdding,LADSPA_Set_Run_Adding_Gain_Function fSetRunAddingGain,LADSPA_Deactivate_Function fDeactivate)86 CMT_Descriptor(unsigned long                       lUniqueID,
87 	       const char *                        pcLabel,
88 	       LADSPA_Properties                   iProperties,
89 	       const char *                        pcName,
90 	       const char *                        pcMaker,
91 	       const char *                        pcCopyright,
92 	       CMT_ImplementationData *            poImplementationData,
93 	       LADSPA_Instantiate_Function         fInstantiate,
94 	       LADSPA_Activate_Function            fActivate,
95 	       LADSPA_Run_Function                 fRun,
96 	       LADSPA_Run_Adding_Function          fRunAdding,
97 	       LADSPA_Set_Run_Adding_Gain_Function fSetRunAddingGain,
98 	       LADSPA_Deactivate_Function          fDeactivate) {
99 
100   UniqueID = lUniqueID;
101   Label = localStrdup(pcLabel);
102   Properties = iProperties;
103   Name = localStrdup(pcName);
104   Maker = localStrdup(pcMaker);
105   Copyright = localStrdup(pcCopyright);
106   PortCount = 0;
107   ImplementationData = poImplementationData;
108 
109   instantiate = fInstantiate;
110   connect_port = CMT_ConnectPort;
111   activate = fActivate;
112   run = fRun;
113   run_adding = fRunAdding;
114   set_run_adding_gain = fSetRunAddingGain;
115   deactivate = fDeactivate;
116   cleanup = CMT_Cleanup;
117 
118 };
119 
120 /*****************************************************************************/
121 
122 typedef char * char_ptr;
123 
124 void CMT_Descriptor::
addPort(LADSPA_PortDescriptor iPortDescriptor,const char * pcPortName,LADSPA_PortRangeHintDescriptor iHintDescriptor,LADSPA_Data fLowerBound,LADSPA_Data fUpperBound)125 addPort(LADSPA_PortDescriptor          iPortDescriptor,
126 	const char *                   pcPortName,
127 	LADSPA_PortRangeHintDescriptor iHintDescriptor,
128 	LADSPA_Data                    fLowerBound,
129 	LADSPA_Data                    fUpperBound) {
130 
131   unsigned long lOldPortCount = PortCount;
132   unsigned long lNewPortCount = PortCount + 1;
133 
134   LADSPA_PortDescriptor * piOldPortDescriptors
135     = (LADSPA_PortDescriptor *)PortDescriptors;
136   char ** ppcOldPortNames
137     = (char **)PortNames;
138   LADSPA_PortRangeHint * psOldPortRangeHints
139     = (LADSPA_PortRangeHint *)PortRangeHints;
140 
141   LADSPA_PortDescriptor * piNewPortDescriptors
142     = new LADSPA_PortDescriptor[lNewPortCount];
143   char ** ppcNewPortNames
144     = new char_ptr[lNewPortCount];
145   LADSPA_PortRangeHint * psNewPortRangeHints
146     = new LADSPA_PortRangeHint[lNewPortCount];
147 
148   if (piNewPortDescriptors == NULL
149       || ppcNewPortNames == NULL
150       || psNewPortRangeHints == NULL) {
151     /* Memory allocation failure that we cannot handle. Best option is
152        probably just to get out while the going is reasonably good. */
153     return;
154   }
155 
156   for (unsigned long lPortIndex = 0;
157        lPortIndex < lOldPortCount;
158        lPortIndex++) {
159     piNewPortDescriptors[lPortIndex] = piOldPortDescriptors[lPortIndex];
160     ppcNewPortNames[lPortIndex] = ppcOldPortNames[lPortIndex];
161     psNewPortRangeHints[lPortIndex] = psOldPortRangeHints[lPortIndex];
162   }
163   if (lOldPortCount > 0) {
164     delete [] piOldPortDescriptors;
165     delete [] ppcOldPortNames;
166     delete [] psOldPortRangeHints;
167   }
168 
169   piNewPortDescriptors[lOldPortCount] = iPortDescriptor;
170   ppcNewPortNames[lOldPortCount] = localStrdup(pcPortName);
171   psNewPortRangeHints[lOldPortCount].HintDescriptor = iHintDescriptor;
172   psNewPortRangeHints[lOldPortCount].LowerBound = fLowerBound;
173   psNewPortRangeHints[lOldPortCount].UpperBound = fUpperBound;
174 
175   PortDescriptors = piNewPortDescriptors;
176   PortNames = ppcNewPortNames;
177   PortRangeHints = psNewPortRangeHints;
178 
179   PortCount++;
180 }
181 
182 /*****************************************************************************/
183 
184 /* EOF */
185