CLAM-Development
1.1
|
00001 /* 00002 * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG) 00003 * UNIVERSITAT POMPEU FABRA 00004 * 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 2 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 * 00020 */ 00021 00022 00023 #include "Processing.hxx" 00024 #include "ProcessingComposite.hxx" 00025 #include "TopLevelProcessing.hxx" 00026 #include "InPort.hxx" 00027 #include "OutPort.hxx" 00028 #include "InControl.hxx" 00029 #include "OutControl.hxx" 00030 #include "Network.hxx" 00031 #include "TypedInControl.hxx" 00032 #include "TypedOutControl.hxx" 00033 00034 #include <cstring> 00035 #include <string> 00036 00037 00038 namespace CLAM 00039 { 00040 void ConnectPorts( 00041 Processing & sender, const std::string & outPortName, 00042 Processing & receiver, const std::string & inPortName ) 00043 { 00044 OutPortBase & out = sender.GetOutPort(outPortName); 00045 InPortBase & in = receiver.GetInPort(inPortName); 00046 out.ConnectToIn(in); 00047 } 00048 00049 void ConnectControls( 00050 Processing & sender, const std::string & outControlName, 00051 Processing & receiver, const std::string & inControlName ) 00052 { 00053 OutControl & out = sender.GetOutControls().Get(outControlName); 00054 InControl & in = receiver.GetInControls().Get(inControlName); 00055 out.AddLink(in); 00056 } 00057 00058 void ConnectTypedControls( 00059 Processing & sender, const std::string & typedOutControlName, 00060 Processing & receiver, const std::string & typedInControlName ) 00061 { 00062 BaseTypedOutControl & out = sender.GetTypedOutControls().Get(typedOutControlName); 00063 BaseTypedInControl & in = receiver.GetTypedInControls().Get(typedInControlName); 00064 out.AddLink(in); 00065 } 00066 00067 void ConnectPorts( 00068 Processing & sender, unsigned outPortNumber, 00069 Processing & receiver, unsigned inPortNumber ) 00070 { 00071 OutPortBase & out = sender.GetOutPorts().GetByNumber(outPortNumber); 00072 InPortBase & in = receiver.GetInPorts().GetByNumber(inPortNumber); 00073 out.ConnectToIn(in); 00074 } 00075 00076 void ConnectControls( 00077 Processing & sender, unsigned outControlNumber, 00078 Processing & receiver, unsigned inControlNumber ) 00079 { 00080 OutControl & out = sender.GetOutControls().GetByNumber(outControlNumber); 00081 InControl & in = receiver.GetInControls().GetByNumber(inControlNumber); 00082 out.AddLink(in); 00083 } 00084 00085 void ConnectTypedControls( 00086 Processing & sender, unsigned typedOutControlNumber, 00087 Processing & receiver, unsigned typedInControlNumber ) 00088 { 00089 BaseTypedOutControl & out = sender.GetTypedOutControls().GetByNumber(typedOutControlNumber); 00090 BaseTypedInControl & in = receiver.GetTypedInControls().GetByNumber(typedInControlNumber); 00091 out.AddLink(in); 00092 } 00093 00094 void ConnectPorts( 00095 Processing & sender, unsigned outPortNumber, 00096 InPortBase & in ) 00097 { 00098 OutPortBase & out = sender.GetOutPorts().GetByNumber(outPortNumber); 00099 out.ConnectToIn(in); 00100 } 00101 00102 void ConnectPorts( 00103 OutPortBase & out, 00104 Processing & receiver, unsigned inPortNumber ) 00105 { 00106 InPortBase & in = receiver.GetInPorts().GetByNumber(inPortNumber); 00107 out.ConnectToIn(in); 00108 } 00109 00110 void ConnectPorts( 00111 Processing & sender, std::string outPortName, 00112 InPortBase & in ) 00113 { 00114 OutPortBase & out = sender.GetOutPorts().Get(outPortName); 00115 out.ConnectToIn(in); 00116 } 00117 00118 void ConnectPorts( 00119 OutPortBase & out, 00120 Processing & receiver, std::string inPortName ) 00121 { 00122 InPortBase & in = receiver.GetInPorts().Get(inPortName); 00123 out.ConnectToIn(in); 00124 } 00125 00126 void SendFloatToInControl(Processing & receiver, const std::string & inControlName, float value){ 00127 OutControl controlSender("tmpOutControl"); 00128 controlSender.AddLink(receiver.GetInControls().Get(inControlName)); 00129 controlSender.SendControl(value); 00130 } 00131 00132 void SendFloatToInControl(Processing & receiver, int inControlIndex, float value){ 00133 OutControl controlSender("tmpOutControl"); 00134 controlSender.AddLink(receiver.GetInControls().GetByNumber(inControlIndex)); 00135 controlSender.SendControl(value); 00136 } 00137 00138 void SendFloatToOutControl(Processing & sender, const std::string & inControlName, float value){ 00139 OutControl& out = *(dynamic_cast<OutControl*>(&(sender.GetOutControls().Get(inControlName)))); 00140 out.SendControl(value); 00141 } 00142 00143 void SendFloatToOutControl(Processing & sender, int inControlIndex, float value){ 00144 OutControl& out = *(dynamic_cast<OutControl*>(&(sender.GetOutControls().GetByNumber(inControlIndex)))); 00145 out.SendControl(value); 00146 } 00147 00148 float GetFloatFromInControl(Processing & proc, const std::string & inControlName){ 00149 InControl& in = *(dynamic_cast<InControl*>(&(proc.GetInControl(inControlName)))); 00150 return in.GetLastValue(); 00151 } 00152 00153 float GetFloatFromInControl(Processing & proc, int inControlIndex){ 00154 InControl& in = *(dynamic_cast<InControl*>(&(proc.GetInControls().GetByNumber(inControlIndex)))); 00155 return in.GetLastValue(); 00156 } 00157 00158 Processing::Processing() 00159 : mpParent(0) 00160 , _network(0) 00161 , _execState(Unconfigured) 00162 { 00163 } 00164 00165 bool Processing::Configure(const ProcessingConfig &c) 00166 { 00167 CLAM_ASSERT(!IsRunning(), "Configuring an already running Processing."); 00168 _configErrorMessage = ""; 00169 // if (!mpParent) //TODO remove 00170 // TopLevelProcessing::GetInstance().Insert(*this); 00171 _execState = Unconfigured; 00172 try 00173 { 00174 if (!ConcreteConfigure(c)) 00175 { 00176 if (_configErrorMessage=="") 00177 _configErrorMessage = "Configuration failed."; 00178 return false; 00179 } 00180 } 00181 catch( ErrProcessingObj& error ) 00182 { 00183 _configErrorMessage += "Exception thrown during ConcreteConfigure:\n"; 00184 _configErrorMessage += error.what(); 00185 _configErrorMessage += "\n"; 00186 _configErrorMessage += "Configuration failed."; 00187 return false; 00188 } 00189 _execState = Ready; 00190 _configErrorMessage="Ready to be started"; 00191 return true; 00192 } 00193 00194 Processing::~Processing() 00195 { 00196 if ( mpParent ) 00197 mpParent->Remove(*this); 00198 } 00199 00200 void Processing::Start(void) 00201 { 00202 CLAM_ASSERT(!IsRunning(), "Starting an already started processing"); 00203 CLAM_ASSERT(IsConfigured(), "Starting an unconfigured processing"); 00204 try { 00205 if (ConcreteStart()) 00206 _execState = Running; 00207 } 00208 catch (ErrProcessingObj &e) { 00209 _configErrorMessage += "Exception thrown while starting.\n"; 00210 _configErrorMessage += e.what(); 00211 } 00212 } 00213 00214 void Processing::Stop(void) 00215 { 00216 CLAM_ASSERT(IsRunning(), "Stop(): Object not running." ); 00217 try { 00218 if(ConcreteStop()) 00219 _execState = Ready; 00220 } 00221 catch (ErrProcessingObj &e) { 00222 _configErrorMessage += "Exception thrown while stoping.\n"; 00223 _configErrorMessage += e.what(); 00224 } 00225 } 00226 unsigned Processing::BackendBufferSize() 00227 { 00228 return _network? _network->BackendBufferSize() : 1024; 00229 } 00230 unsigned Processing::BackendSampleRate() 00231 { 00232 return _network? _network->BackendBufferSize() : 44100; 00233 } 00234 00235 void Processing::RegisterOutPort(OutPortBase* out) 00236 { 00237 mOutPortRegistry.ProcessingInterface_Register(out); 00238 } 00239 void Processing::RegisterInPort(InPortBase* in) 00240 { 00241 mInPortRegistry.ProcessingInterface_Register(in); 00242 } 00243 00244 void Processing::RegisterOutControl(OutControl* out) 00245 { 00246 mOutControlRegistry.ProcessingInterface_Register(out); 00247 } 00248 void Processing::RegisterInControl(InControl* in) 00249 { 00250 mInControlRegistry.ProcessingInterface_Register(in); 00251 } 00252 void Processing::RegisterTypedOutControl(BaseTypedOutControl* out) 00253 { 00254 mTypedOutControlRegistry.ProcessingInterface_Register(out); 00255 } 00256 void Processing::RegisterTypedInControl(BaseTypedInControl* in) 00257 { 00258 mTypedInControlRegistry.ProcessingInterface_Register(in); 00259 } 00260 00261 void Processing::SetParent(Processing * parent) 00262 { 00263 ProcessingComposite * composite; 00264 if (!parent) 00265 composite = &(TopLevelProcessing::GetInstance()); 00266 else 00267 composite = dynamic_cast<ProcessingComposite*>(parent); 00268 CLAM_ASSERT(composite, "Setting a non ProcessingComposite as Parent"); 00269 00270 if (mpParent==composite) 00271 return; 00272 00273 if (mpParent) 00274 mpParent->Remove(*this); 00275 mpParent=composite; 00276 mpParent->Insert(*this); 00277 } 00278 00279 void Processing::SetNetworkBackLink(Network * network) 00280 { 00281 _network=network; 00282 } 00283 void Processing::AddConfigErrorMessage( const std::string& msg ) 00284 { 00285 _configErrorMessage += msg; 00286 } 00287 00288 bool Processing::CanConsumeAndProduce() 00289 { 00290 if(!IsRunning()) 00291 { 00292 std::cerr << "Cannot execute '" << GetClassName() << "' because not Running!" << std::endl; 00293 return false; 00294 } 00295 // std::cerr<< "inports ready? " << GetInPorts().AreReadyForReading() << std::endl; 00296 // std::cerr << "outports ready? " << GetOutPorts().AreReadyForWriting() << std::endl; 00297 return GetInPorts().AreReadyForReading() && GetOutPorts().AreReadyForWriting(); 00298 } 00299 const ProcessingConfig& Processing::GetConfig() const 00300 { 00301 static NullProcessingConfig nullConfig; 00302 return nullConfig; 00303 } 00304 std::string Processing::GetExecStateString() const 00305 { 00306 switch (_execState) 00307 { 00308 case Unconfigured: 00309 return "Unconfigured"; 00310 case Ready: 00311 return "Ready"; 00312 case Running: 00313 return "Running"; 00314 } 00315 CLAM_ASSERT(false, "Unknown processing exec state found"); 00316 return "INTERNAL ERROR"; 00317 } 00318 00319 00320 00321 };//namespace CLAM 00322