Processing.hxx

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2001-2005 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 #ifndef _Processing_hxx_
00024 #define _Processing_hxx_
00025 
00026 
00027 #include "ErrProcessingObj.hxx"
00028 #include "InControlRegistry.hxx"
00029 #include "OutControlRegistry.hxx"
00030 #include "InPortRegistry.hxx"
00031 #include "OutPortRegistry.hxx"
00032 #include "ProcessingConfig.hxx"
00033 #include "NullProcessingConfig.hxx"
00034 
00035 #include <list>
00036 #include <typeinfo>
00037 #include <string>
00038 
00039 namespace CLAM
00040 {
00041 
00042         class Processing;
00043         class InPortBase;
00044         class OutPortBase;
00045         class ProcessingComposite;
00046         class Network;
00051         void ConnectPorts(
00052                         Processing & sender, const std::string & outPortName,
00053                         Processing & receiver, const std::string & inPortName );
00058         void ConnectPorts(
00059                         Processing & sender, unsigned outPortNumber,
00060                         Processing & receiver, unsigned inPortNumber );
00065         void ConnectControls(
00066                         Processing & sender, unsigned outControlNumber,
00067                         Processing & receiver, unsigned inControlNumber );
00072         void ConnectControls(
00073                         Processing & sender, const std::string & outControlName,
00074                         Processing & receiver, const std::string & inControlName );
00075 #if 0
00076 
00079         void ConnectTypedControls(
00080                         Processing & sender, unsigned typedOutControlNumber,
00081                         Processing & receiver, unsigned typedInControlNumber );
00085         void ConnectTypedControls(
00086                         Processing & sender, const std::string & typedOutControlName,
00087                         Processing & receiver, const std::string & typedInControlName );
00088 
00089 #endif
00090 
00094         void ConnectPorts(
00095                         OutPortBase & sender,
00096                         Processing & receiver, unsigned inPortNumber );
00101         void ConnectPorts(
00102                         Processing & sender, unsigned outPortNumber,
00103                         InPortBase & receiver );
00109         void SendFloatToInControl(Processing & receiver, const std::string & inControlName, float value);
00115         void SendFloatToInControl(Processing & receiver, int inControlIndex, float value);
00121         void SendFloatToOutControl(Processing & sender, const std::string & inControlName, float value);
00127         void SendFloatToOutControl(Processing & sender, int inControlIndex, float value);
00133         float GetFloatFromInControl(Processing & proc, const std::string & inControlName);
00139         float GetFloatFromInControl(Processing & proc, int inControlIndex);
00140 
00185         class Processing {
00186         public:
00188                 typedef enum {
00189                         Unconfigured=0,
00190                         Ready,
00191                         Running
00192                 } ExecState;
00193 
00194                 typedef NullProcessingConfig Config;
00195 
00196                 // TODO: Temporary kludge to be able to access the connectors registry directly from connector classes
00197                 friend class InPortBase;
00198                 friend class OutPortBase;
00199                 friend class InControlBase;
00200                 friend class OutControlBase;
00201 // Basic usage interface:
00202 
00203         public:
00204 
00218                 bool Configure(const ProcessingConfig& config);
00219 
00224                 void Start(void);
00225 
00230                 virtual bool Do(void)=0;
00231 
00237                 void Stop(void);
00238 
00239                 Processing();
00240                 virtual ~Processing();
00241 
00242 // Overridable interface:
00243         public:
00244 
00246                 virtual const char * GetClassName() const = 0;
00247 
00249                 virtual bool CanProcessInplace() { return true; }
00250 
00251         protected:
00271                 virtual bool ConcreteConfigure(const ProcessingConfig&) { return true; }
00272 
00278                 virtual bool ConcreteStart() {return true;}
00279 
00285                 virtual bool ConcreteStop() {return true;}
00286 
00288                 unsigned BackendBufferSize();
00290                 unsigned BackendSampleRate();
00291 // Public interface:
00292         public:
00294                 bool CanConsumeAndProduce();
00295 
00301                 void ConsumeAndProduce();
00302 
00311                 virtual const ProcessingConfig &GetConfig() const;
00312 
00319         private:
00320                 ExecState GetExecState() const {return _execState;}
00321         public:
00322                 std::string GetExecStateString() const;
00323                 bool IsConfigured() const { return _execState != Unconfigured; }
00324                 bool IsRunning() const { return _execState == Running; }
00325 
00326 
00327                 void RegisterOutPort(OutPortBase* out);
00328                 void RegisterInPort(InPortBase* in);
00329                 void RegisterOutControl(OutControlBase* out);
00330                 void RegisterInControl(InControlBase* in);
00331 
00332                 void SetParent(Processing *p);
00333                 void SetNetworkBackLink(Network * network);
00340                 virtual bool ModifiesPortsAndControlsAtConfiguration()  { return false; }
00341 
00342                 bool HasInPort( const std::string & name )
00343                 {
00344                         return mInPortRegistry.Has(name);
00345                 }
00346 
00347                 bool HasOutPort( const std::string & name )
00348                 {
00349                         return mOutPortRegistry.Has(name);
00350                 }
00351 
00352                 bool HasInControl( const std::string & name )
00353                 {
00354                         return mInControlRegistry.Has(name);
00355                 }
00356 
00357                 bool HasOutControl( const std::string & name )
00358                 {
00359                         return mOutControlRegistry.Has(name);
00360                 }
00361 
00362                 InPortBase & GetInPort( const std::string & name )
00363                 {
00364                         return mInPortRegistry.Get(name);
00365                 }
00366                 OutPortBase & GetOutPort( const std::string & name )
00367                 {
00368                         return mOutPortRegistry.Get(name);
00369                 }
00370                 InControlBase & GetInControl( const std::string & name )
00371                 {
00372                         return mInControlRegistry.Get(name);
00373                 }
00374                 OutControlBase & GetOutControl( const std::string & name )
00375                 {
00376                         return mOutControlRegistry.Get(name);
00377                 }
00378                 InPortBase & GetInPort( unsigned index )
00379                 {
00380                         return mInPortRegistry.GetByNumber(index);
00381                 }
00382                 OutPortBase & GetOutPort( unsigned index )
00383                 {
00384                         return mOutPortRegistry.GetByNumber(index);
00385                 }
00386                 InControlBase & GetInControl( unsigned index )
00387                 {
00388                         return mInControlRegistry.GetByNumber(index);
00389                 }
00390                 OutControlBase & GetOutControl( unsigned index )
00391                 {
00392                         return mOutControlRegistry.GetByNumber(index);
00393                 }
00394                 unsigned GetNInPorts() const
00395                 {
00396                         return mInPortRegistry.Size();
00397                 }
00398                 unsigned GetNOutPorts() const
00399                 {
00400                         return mOutPortRegistry.Size();
00401                 }
00402                 unsigned GetNInControls() const
00403                 {
00404                         return mInControlRegistry.Size();
00405                 }
00406                 unsigned GetNOutControls() const
00407                 {
00408                         return mOutControlRegistry.Size();
00409                 }
00410 
00411         protected:
00413                 InControlRegistry& GetInControls() { return mInControlRegistry; }
00414 
00416                 OutControlRegistry& GetOutControls() { return mOutControlRegistry; }
00417 
00419                 InPortRegistry& GetInPorts() { return mInPortRegistry; }
00420 
00422                 OutPortRegistry& GetOutPorts() { return mOutPortRegistry; }
00423         public:
00425                 const std::string& GetConfigErrorMessage() const { return _configErrorMessage; }
00426 
00429                 virtual bool IsSyncSource() const { return false; }
00430 
00432                 virtual bool SupportsVariableAudioSize() const {return ((GetNInPorts()+GetNOutPorts())==0)?true:false;}
00433 
00434 // Helpers only for subclasses
00435         protected:
00443                 bool AddConfigErrorMessage( const std::string& msg );
00444 
00447                 bool AbleToExecute(void) const;
00448 
00456                 template <typename ConcreteConfig>
00457                 void CopyAsConcreteConfig(ConcreteConfig & concrete, const ProcessingConfig & abstract) const;
00458         protected:
00459                 void SetExecState(ExecState state)
00460                 {
00461                         _execState = state;
00462                 }
00463 // Attributes:
00465                 ProcessingComposite *mpParent;
00468                 Network * _network;
00469         private:
00470 
00472                 ExecState _execState;
00473 
00475                 std::string _configErrorMessage;
00476 
00477 
00478         private:
00479                 InControlRegistry mInControlRegistry;
00480                 OutControlRegistry mOutControlRegistry;
00481                 InPortRegistry mInPortRegistry;
00482                 OutPortRegistry mOutPortRegistry;
00483         };
00484 
00485 
00486 
00487 // -----------------------------------------------------------------------------------------------
00488 // Inline implementations
00489 
00490 inline bool Processing::AbleToExecute(void) const
00491 {
00492         CLAM_BEGIN_DEBUG_CHECK
00493                 if (!IsRunning())
00494                 {
00495                         std::string err(GetClassName());
00496                         err += ": Do(): Not in execution mode - did you call Start on this "
00497                                         "object, the composite it is in, or the ToplevelProcessing singleton?";
00498                                 CLAM_DEBUG_ASSERT( false, err.c_str() );
00499                 }
00500         CLAM_END_DEBUG_CHECK
00501         return IsRunning();
00502 }
00503 
00504 template <typename ConcreteConfig>
00505 inline void Processing::CopyAsConcreteConfig(ConcreteConfig & concrete, const ProcessingConfig & abstract) const
00506 {
00507         CLAM_ASSERT(typeid(ConcreteConfig)==typeid(abstract),
00508                 (std::string("Configuring a processing with a configuration of type ") + typeid(abstract).name() +
00509                 " while it was expected a " + typeid(ConcreteConfig).name() + ".").c_str());
00510         concrete = static_cast<const ConcreteConfig &>(abstract);
00511 }
00512 
00513 };//namespace CLAM
00514 
00515 #endif
00516 
Generated by  doxygen 1.6.3