CLAM-Development  1.4.0
LadspaProcessingExporter.cxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 #include <cstdlib>
23 #include <cstring>
24 
26 
27 // LADSPA call backs
28 extern "C"
29 {
30 
31 static LADSPA_Handle Instantiate(const struct _LADSPA_Descriptor * descriptor, unsigned long sampleRate)
32 {
33  std::string className = std::string(descriptor->Name).substr(5);
36  adapter->Instantiate();
37  return adapter;
38 }
39 
40 static void ConnectPort(LADSPA_Handle instance, unsigned long port, LADSPA_Data * data)
41 {
43  adapter->ConnectPort(port, data);
44 }
45 
46 static void Activate(LADSPA_Handle instance)
47 {
49  adapter->Activate();
50 }
51 
52 static void Run(LADSPA_Handle instance, unsigned long sampleCount)
53 {
55  adapter->Run(sampleCount);
56 }
57 
58 static void Deactivate(LADSPA_Handle instance)
59 {
61  adapter->Deactivate();
62 }
63 
64 static void CleanUp(LADSPA_Handle instance)
65 {
67  delete adapter;
68 }
69 }
70 
71 namespace CLAM
72 {
73 namespace Hidden
74 {
75 
76 LADSPA_Descriptor * ProcessingClass2LadspaBase::CreateDescriptor(unsigned long id,
77  const std::string & maker, const std::string & copyright)
78 {
79  LADSPA_Descriptor * descriptor = new LADSPA_Descriptor;
80  std::string className = _proc->GetClassName();
81  descriptor->UniqueID = id;
82  descriptor->Label = LadspaLibrary::dupstr(("CLAM_"+className).c_str());
83  descriptor->Name = LadspaLibrary::dupstr(("CLAM "+className).c_str());
84  descriptor->Maker = LadspaLibrary::dupstr(maker.c_str());
85  descriptor->Copyright = LadspaLibrary::dupstr(copyright.c_str());
86  descriptor->Properties = LADSPA_PROPERTY_HARD_RT_CAPABLE; //?
87  descriptor->PortCount = NPorts();
88 
89  descriptor->instantiate = ::Instantiate;
90  descriptor->connect_port = ::ConnectPort;
91  descriptor->activate = ::Activate;
92  descriptor->run = ::Run;
93  descriptor->run_adding = 0;
94  descriptor->set_run_adding_gain = 0;
95  descriptor->deactivate = ::Deactivate;
96  descriptor->cleanup = ::CleanUp;
97 
98  SetPortsAndControls(descriptor);
99 
100  return descriptor;
101 }
102 
103 void ProcessingClass2LadspaBase::SetPortsAndControls(LADSPA_Descriptor *& descriptor)
104 {
105  LADSPA_PortDescriptor *& portDescriptors = const_cast<LADSPA_PortDescriptor*&>(descriptor->PortDescriptors);
106  const char **& portNames = const_cast<const char **&>(descriptor->PortNames);
107  LADSPA_PortRangeHint *& portRangeHints = const_cast<LADSPA_PortRangeHint *&>(descriptor->PortRangeHints);
108 
109  typedef const char * ConstCharPtr;
110  portNames = new ConstCharPtr[NPorts()];
111  portDescriptors = new LADSPA_PortDescriptor[NPorts()];
112  portRangeHints = new LADSPA_PortRangeHint[NPorts()];
113 
114  unsigned i=0;
115  for(unsigned j=0; j<_nInControls; i++, j++)
116  {
117  portDescriptors[i] = LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
118  portNames[i] = LadspaLibrary::dupstr(GetInControlName(j));
119  portRangeHints[i].HintDescriptor = 0;
120  }
121 
122  for(unsigned j=0; j<_nOutControls; i++, j++)
123  {
124  portDescriptors[i] = LADSPA_PORT_OUTPUT | LADSPA_PORT_CONTROL;
125  portNames[i] = LadspaLibrary::dupstr(GetOutControlName(j));
126  portRangeHints[i].HintDescriptor = 0;
127  }
128  for(unsigned j=0; j<_nInPorts; i++, j++)
129  {
130  portDescriptors[i] = LADSPA_PORT_INPUT | LADSPA_PORT_AUDIO;
131  portNames[i] = LadspaLibrary::dupstr(GetInPortName(j));
132  portRangeHints[i].HintDescriptor = 0;
133  }
134  for(unsigned j=0; j<_nOutPorts; i++, j++)
135  {
136  portDescriptors[i] = LADSPA_PORT_OUTPUT | LADSPA_PORT_AUDIO;
137  portNames[i] = LadspaLibrary::dupstr(GetOutPortName(j));
138  portRangeHints[i].HintDescriptor = 0;
139  }
140 /*
141  // pitch
142  portRangeHints[0].HintDescriptor = (
143  LADSPA_HINT_BOUNDED_BELOW |
144  LADSPA_HINT_BOUNDED_ABOVE |
145  LADSPA_HINT_SAMPLE_RATE |
146  LADSPA_HINT_LOGARITHMIC |
147  LADSPA_HINT_DEFAULT_440);
148  portRangeHints[0].LowerBound = 0;
149  portRangeHints[0].UpperBound = 0.5;
150 
151  // amplitude
152  portRangeHints[1].HintDescriptor = (
153  LADSPA_HINT_BOUNDED_BELOW |
154  LADSPA_HINT_BOUNDED_ABOVE |
155  LADSPA_HINT_DEFAULT_1);
156  portRangeHints[1].LowerBound = 0;
157  portRangeHints[1].UpperBound = 1;
158 
159  // audio output
160  portRangeHints[2] = 0;
161 */
162 }
163 
164 void ProcessingClass2LadspaBase::DoControls()
165 {
166  for(unsigned i=0;i<_nInControls;i++)
167  SendFloatToInControl(*_proc, i,(CLAM::TData)*_incontrolBuffers[i]);
168  // TODO: No output controls!
169 }
170 
171 void ProcessingClass2LadspaBase::SetPortSizes(int size)
172 {
173  for(int i=0;i<_nInControls;i++)
174  {
175  if(_proc->GetInPort(i).GetSize() == size ) continue;
176  _proc->GetInPort(i).SetSize( size );
177  mWrappersList[i]->SetSize( size );
178  }
179 
180  for(int i=0;i<_nOutPorts;i++)
181  {
182  if(_proc->GetOutPort(i).GetSize() == size ) continue;
183  _proc->GetOutPort(i).SetSize( size );
184  }
185 }
186 
187 void ProcessingClass2LadspaBase::DoProc(unsigned long nSamples)
188 {
189  for(int i=0;i<_nInPorts;i++)
190  {
191  memcpy( &(mWrappersList[i]->GetData()), _inportBuffers[i], nSamples*sizeof(CLAM::TData) );
192  mWrappersList[i]->Produce();
193  }
194 
195  std::vector<CLAM::TData*> dataList(_nOutPorts);
196  for(int i=0;i<_nOutPorts;i++)
197  {
198  CLAM::OutPortBase & port = _proc->GetOutPort(i);
199  CLAM::AudioOutPort & audioPort = dynamic_cast<CLAM::AudioOutPort&>(port);
200  dataList[i] = &(audioPort.GetData());
201  }
202 
203  _proc->Do();
204 
205  for(int i=0; i<_nOutControls; i++)
206  memcpy(_outcontrolBuffers[i], dataList[i], nSamples*sizeof(CLAM::TData) );
207 }
208 
209 
210 
211 const char * ProcessingClass2LadspaBase::GetInControlName(int id) const
212 {
213  return _proc->GetInControl(id).GetName().c_str();
214 }
215 const char * ProcessingClass2LadspaBase::GetOutControlName(int id) const
216 {
217  return _proc->GetOutControl(id).GetName().c_str();
218 }
219 const char * ProcessingClass2LadspaBase::GetInPortName(int id) const
220 {
221  return _proc->GetInPort(id).GetName().c_str();
222 }
223 const char * ProcessingClass2LadspaBase::GetOutPortName(int id) const
224 {
225  return _proc->GetOutPort(id).GetName().c_str();
226 }
227 
228 }
229 }
230