Processing.cxx

Go to the documentation of this file.
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 
00032 #include <cstring>
00033 #include <string>
00034 
00035 
00036 namespace CLAM
00037 {
00038         void ConnectPorts(
00039                         Processing & sender, const std::string & outPortName, 
00040                         Processing & receiver, const std::string & inPortName )
00041         {
00042                 OutPortBase & out = sender.GetOutPort(outPortName);
00043                 InPortBase & in = receiver.GetInPort(inPortName);
00044                 out.ConnectToIn(in);
00045         }
00046 
00047         void ConnectControls(
00048                         Processing & sender, const std::string & outControlName, 
00049                         Processing & receiver, const std::string & inControlName )
00050         {
00051                 OutControlBase & out = sender.GetOutControl(outControlName);
00052                 InControlBase & in = receiver.GetInControl(inControlName);
00053                 out.AddLink(in);
00054         }
00055         
00056         void ConnectPorts(
00057                         Processing & sender, unsigned outPortNumber, 
00058                         Processing & receiver, unsigned inPortNumber )
00059         {
00060                 OutPortBase & out = sender.GetOutPort(outPortNumber);
00061                 InPortBase & in = receiver.GetInPort(inPortNumber);
00062                 out.ConnectToIn(in);
00063         }
00064         
00065         void ConnectControls(
00066                         Processing & sender, unsigned outControlNumber, 
00067                         Processing & receiver, unsigned inControlNumber )
00068         {
00069                 OutControlBase & out = sender.GetOutControl(outControlNumber);
00070                 InControlBase & in = receiver.GetInControl(inControlNumber);
00071                 out.AddLink(in);
00072         }
00073 
00074         void ConnectPorts(
00075                         Processing & sender, unsigned outPortNumber, 
00076                         InPortBase & in )
00077         {
00078                 OutPortBase & out = sender.GetOutPort(outPortNumber);
00079                 out.ConnectToIn(in);
00080         }
00081         
00082         void ConnectPorts(
00083                         OutPortBase & out,
00084                         Processing & receiver, unsigned inPortNumber )
00085         {
00086                 InPortBase & in = receiver.GetInPort(inPortNumber);
00087                 out.ConnectToIn(in);
00088         }
00089         
00090         void ConnectPorts(
00091                         Processing & sender, std::string outPortName, 
00092                         InPortBase & in )
00093         {
00094                 OutPortBase & out = sender.GetOutPort(outPortName);
00095                 out.ConnectToIn(in);
00096         }
00097         
00098         void ConnectPorts(
00099                         OutPortBase & out,
00100                         Processing & receiver, std::string inPortName )
00101         {
00102                 InPortBase & in = receiver.GetInPort(inPortName);
00103                 out.ConnectToIn(in);
00104         }
00105         
00106         void SendFloatToInControl(Processing & receiver, const std::string & inControlName, float value){
00107                 InControlBase & in = receiver.GetInControl(inControlName);
00108                 FloatOutControl controlSender("tmpOutControl");
00109                 CLAM_ASSERT(controlSender.IsLinkable(in), "GetFloatFromInControl: the control was not a Float control");
00110                 controlSender.AddLink(in);
00111                 controlSender.SendControl(value);
00112         }
00113 
00114         void SendFloatToInControl(Processing & receiver, int inControlIndex, float value){
00115                 InControlBase & in = receiver.GetInControl(inControlIndex);
00116                 FloatOutControl controlSender("tmpOutControl");
00117                 CLAM_ASSERT(controlSender.IsLinkable(in), "GetFloatFromInControl: the control was not a Float control");
00118                 controlSender.AddLink(in);
00119                 controlSender.SendControl(value);
00120         }
00121         
00122         void SendFloatToOutControl(Processing & sender, const std::string & inControlName, float value){
00123                 FloatOutControl * out = dynamic_cast<FloatOutControl*>(&(sender.GetOutControl(inControlName)));
00124                 CLAM_ASSERT(out, "SendFloatToOutControl: the control was not a Float control");
00125                 out->SendControl(value);
00126         }
00127         
00128         void SendFloatToOutControl(Processing & sender, int outControlIndex, float value){
00129                 FloatOutControl * out = dynamic_cast<FloatOutControl*>(&(sender.GetOutControl(outControlIndex)));
00130                 CLAM_ASSERT(out, "SendFloatToOutControl: the control was not a Float control");
00131                 out->SendControl(value);
00132         }
00133         
00134         float GetFloatFromInControl(Processing & proc, const std::string & inControlName){
00135                 FloatInControl * in = dynamic_cast<FloatInControl*>(&(proc.GetInControl(inControlName)));
00136                 CLAM_ASSERT(in, "GetFloatFromInControl: the control was not a Float control");
00137                 return in->GetLastValue();
00138         }
00139         
00140         float GetFloatFromInControl(Processing & proc, int inControlIndex){
00141                 FloatInControl * in = dynamic_cast<FloatInControl*>(&(proc.GetInControl(inControlIndex)));
00142                 CLAM_ASSERT(in, "GetFloatFromInControl: the control was not a Float control");
00143                 return in->GetLastValue();
00144         }
00145         
00146         Processing::Processing() 
00147                 : mpParent(0)
00148                 , _network(0)
00149                 , _execState(Unconfigured)
00150         {
00151         }
00152 
00153         bool Processing::Configure(const ProcessingConfig &c)
00154         {
00155                 CLAM_ASSERT(!IsRunning(), "Configuring an already running Processing.");
00156                 _configErrorMessage = "";
00157 //              if (!mpParent) //TODO remove
00158 //                      TopLevelProcessing::GetInstance().Insert(*this);
00159                 _execState = Unconfigured;
00160                 try
00161                 {
00162                         if (!ConcreteConfigure(c)) 
00163                         {
00164                                 if (_configErrorMessage=="")
00165                                         _configErrorMessage = "Configuration failed.";
00166                                 return false;
00167                         }
00168                 }
00169                 catch( ErrProcessingObj& error ) 
00170                 {
00171                         _configErrorMessage += "Exception thrown during ConcreteConfigure:\n";
00172                         _configErrorMessage += error.what();
00173                         _configErrorMessage += "\n";
00174                         _configErrorMessage += "Configuration failed.";
00175                         return false;
00176                 }
00177                 _execState = Ready;
00178                 _configErrorMessage="Ready to be started";
00179                 return true;
00180         }
00181 
00182         Processing::~Processing()
00183         {
00184                 if ( mpParent )
00185                         mpParent->Remove(*this);
00186         }
00187 
00188         void Processing::Start(void) 
00189         {
00190                 CLAM_ASSERT(!IsRunning(), "Starting an already started processing");
00191                 CLAM_ASSERT(IsConfigured(), "Starting an unconfigured processing");
00192                 try {
00193                         if (ConcreteStart())
00194                                 _execState = Running;
00195                 }
00196                 catch (ErrProcessingObj &e) {
00197                         _configErrorMessage += "Exception thrown while starting.\n";
00198                         _configErrorMessage += e.what();
00199                 }
00200         }
00201         
00202         void Processing::Stop(void)
00203         {
00204                 CLAM_ASSERT(IsRunning(), "Stop(): Object not running." );
00205                 try {
00206                         if(ConcreteStop())
00207                                 _execState = Ready; 
00208                 }
00209                 catch (ErrProcessingObj &e) {
00210                         _configErrorMessage += "Exception thrown while stoping.\n";
00211                         _configErrorMessage += e.what();
00212                 }
00213         }
00214         unsigned Processing::BackendBufferSize()
00215         {
00216                 if (_network)
00217                         return _network->BackendBufferSize();
00218                 //TODO: 1- inherit the buffer size on embeded processings without linked network
00219                 //TODO: 2- resolve the multiple configuration instances, that makes the first ConcreteConfigure (on XML loading) print this message
00220                 //std::cout<<"Warning: no linked network, using hardcoded backend buffer size (1024) on processing "<<GetClassName()<<std::endl;
00221                 return 1024;
00222         }
00223         unsigned Processing::BackendSampleRate()
00224         {
00225                 return _network? _network->BackendSampleRate() : 44100;
00226         }
00227 
00228         void Processing::RegisterOutPort(OutPortBase* out) 
00229         {
00230                 mOutPortRegistry.ProcessingInterface_Register(out);
00231         }
00232         void Processing::RegisterInPort(InPortBase* in)
00233         {
00234                 mInPortRegistry.ProcessingInterface_Register(in);
00235         }
00236 
00237         void Processing::RegisterOutControl(OutControlBase* out) 
00238         {
00239                 mOutControlRegistry.ProcessingInterface_Register(out);
00240         }
00241         void Processing::RegisterInControl(InControlBase* in)
00242         {
00243                 mInControlRegistry.ProcessingInterface_Register(in);
00244         }
00245         void Processing::SetParent(Processing * parent)
00246         {
00247                 ProcessingComposite * composite;
00248                 if (!parent)
00249                         composite = &(TopLevelProcessing::GetInstance());
00250                 else
00251                         composite = dynamic_cast<ProcessingComposite*>(parent);
00252                 CLAM_ASSERT(composite, "Setting a non ProcessingComposite as Parent");
00253 
00254                 if (mpParent==composite)
00255                         return;
00256 
00257                 if (mpParent)
00258                         mpParent->Remove(*this);
00259                 mpParent=composite;
00260                 mpParent->Insert(*this);
00261         }
00262 
00263         void Processing::SetNetworkBackLink(Network * network)
00264         {
00265                 _network=network;
00266         }
00267         bool Processing::AddConfigErrorMessage( const std::string& msg )
00268         {
00269                 _configErrorMessage += msg;
00270                 // For convenience, so you can report and exit in one line from ConcreteConfigure
00271                 // return AddConfigErrorMessage("My error");
00272                 return false;
00273         }
00274         
00275         bool Processing::CanConsumeAndProduce()
00276         {
00277                 if(!IsRunning())
00278                 {
00279                         std::cerr << "Cannot execute '" << GetClassName() << "' because not Running!" << std::endl;
00280                         return false;
00281                 }
00282 //              std::cerr<< "inports ready? " << GetInPorts().AreReadyForReading() << std::endl;
00283 //              std::cerr << "outports ready? " << GetOutPorts().AreReadyForWriting() << std::endl;
00284                 return GetInPorts().AreReadyForReading() && GetOutPorts().AreReadyForWriting();
00285         }
00286         void Processing::ConsumeAndProduce()
00287         {
00288                 // TODO: Not yet implemented
00289                 /*
00290                 for (unsigned i=0; i<GetNOutPorts(); i++)
00291                         GetOutPort(i).Produce();
00292                 for (unsigned i=0; i<GetNInPorts(); i++)
00293                         GetInPort(i).Consume();
00294                 */
00295                 
00296         }
00297 
00298         const ProcessingConfig& Processing::GetConfig() const
00299         {
00300                 static NullProcessingConfig nullConfig;
00301                 return nullConfig;
00302         }
00303         std::string Processing::GetExecStateString() const 
00304         {
00305                 switch (_execState)
00306                 {
00307                         case Unconfigured:
00308                                 return "Unconfigured";
00309                         case Ready:
00310                                 return "Ready";
00311                         case Running:
00312                                 return "Running";
00313                 }
00314                 CLAM_ASSERT(false, "Unknown processing exec state found");
00315                 return "INTERNAL ERROR";
00316         }
00317                 
00318 
00319 
00320 };//namespace CLAM
00321 
Generated by  doxygen 1.6.3