CLAM-Development  1.4.0
Processing.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2005 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 
23 #ifndef _Processing_hxx_
24 #define _Processing_hxx_
25 
26 
27 #include "ErrProcessingObj.hxx"
28 #include "InControlRegistry.hxx"
29 #include "OutControlRegistry.hxx"
30 #include "InPortRegistry.hxx"
31 #include "OutPortRegistry.hxx"
32 #include "ProcessingConfig.hxx"
33 #include "NullProcessingConfig.hxx"
34 
35 #include <list>
36 #include <typeinfo>
37 #include <string>
38 
39 namespace CLAM
40 {
41 
42  class Processing;
43  class InPortBase;
44  class OutPortBase;
45  class ProcessingComposite;
46  class Network;
51  void ConnectPorts(
52  Processing & sender, const std::string & outPortName,
53  Processing & receiver, const std::string & inPortName );
58  void ConnectPorts(
59  Processing & sender, unsigned outPortNumber,
60  Processing & receiver, unsigned inPortNumber );
65  void ConnectControls(
66  Processing & sender, unsigned outControlNumber,
67  Processing & receiver, unsigned inControlNumber );
72  void ConnectControls(
73  Processing & sender, const std::string & outControlName,
74  Processing & receiver, const std::string & inControlName );
75 #if 0
76 
79  void ConnectTypedControls(
80  Processing & sender, unsigned typedOutControlNumber,
81  Processing & receiver, unsigned typedInControlNumber );
85  void ConnectTypedControls(
86  Processing & sender, const std::string & typedOutControlName,
87  Processing & receiver, const std::string & typedInControlName );
88 
89 #endif
90 
94  void ConnectPorts(
95  OutPortBase & sender,
96  Processing & receiver, unsigned inPortNumber );
101  void ConnectPorts(
102  Processing & sender, unsigned outPortNumber,
103  InPortBase & receiver );
109  void SendFloatToInControl(Processing & receiver, const std::string & inControlName, float value);
115  void SendFloatToInControl(Processing & receiver, int inControlIndex, float value);
121  void SendFloatToOutControl(Processing & sender, const std::string & inControlName, float value);
127  void SendFloatToOutControl(Processing & sender, int inControlIndex, float value);
133  float GetFloatFromInControl(Processing & proc, const std::string & inControlName);
139  float GetFloatFromInControl(Processing & proc, int inControlIndex);
140 
185  class Processing {
186  public:
188  typedef enum {
192  } ExecState;
193 
195 
196  // TODO: Temporary kludge to be able to access the connectors registry directly from connector classes
197  friend class InPortBase;
198  friend class OutPortBase;
199  friend class InControlBase;
200  friend class OutControlBase;
201 // Basic usage interface:
202 
203  public:
204 
218  bool Configure(const ProcessingConfig& config);
219 
224  void Start(void);
225 
230  virtual bool Do(void)=0;
231 
237  void Stop(void);
238 
239  Processing();
240  virtual ~Processing();
241 
242 // Overridable interface:
243  public:
244 
246  virtual const char * GetClassName() const = 0;
247 
249  virtual bool CanProcessInplace() { return true; }
250 
251  protected:
271  virtual bool ConcreteConfigure(const ProcessingConfig&) { return true; }
272 
278  virtual bool ConcreteStart() {return true;}
279 
285  virtual bool ConcreteStop() {return true;}
286 
288  unsigned BackendBufferSize();
290  unsigned BackendSampleRate();
291 // Public interface:
292  public:
294  bool CanConsumeAndProduce();
295 
301  void ConsumeAndProduce();
302 
311  virtual const ProcessingConfig &GetConfig() const;
312 
319  private:
320  ExecState GetExecState() const {return _execState;}
321  public:
322  std::string GetExecStateString() const;
323  bool IsConfigured() const { return _execState != Unconfigured; }
324  bool IsRunning() const { return _execState == Running; }
325 
326 
327  void RegisterOutPort(OutPortBase* out);
328  void RegisterInPort(InPortBase* in);
331 
332  void SetParent(Processing *p);
333  void SetNetworkBackLink(Network * network);
340  virtual bool ModifiesPortsAndControlsAtConfiguration() { return false; }
341 
342  bool HasInPort( const std::string & name )
343  {
344  return mInPortRegistry.Has(name);
345  }
346 
347  bool HasOutPort( const std::string & name )
348  {
349  return mOutPortRegistry.Has(name);
350  }
351 
352  bool HasInControl( const std::string & name )
353  {
354  return mInControlRegistry.Has(name);
355  }
356 
357  bool HasOutControl( const std::string & name )
358  {
359  return mOutControlRegistry.Has(name);
360  }
361 
362  InPortBase & GetInPort( const std::string & name )
363  {
364  return mInPortRegistry.Get(name);
365  }
366  OutPortBase & GetOutPort( const std::string & name )
367  {
368  return mOutPortRegistry.Get(name);
369  }
370  InControlBase & GetInControl( const std::string & name )
371  {
372  return mInControlRegistry.Get(name);
373  }
374  OutControlBase & GetOutControl( const std::string & name )
375  {
376  return mOutControlRegistry.Get(name);
377  }
378  InPortBase & GetInPort( unsigned index )
379  {
380  return mInPortRegistry.GetByNumber(index);
381  }
382  OutPortBase & GetOutPort( unsigned index )
383  {
384  return mOutPortRegistry.GetByNumber(index);
385  }
386  InControlBase & GetInControl( unsigned index )
387  {
388  return mInControlRegistry.GetByNumber(index);
389  }
390  OutControlBase & GetOutControl( unsigned index )
391  {
392  return mOutControlRegistry.GetByNumber(index);
393  }
394  unsigned GetNInPorts() const
395  {
396  return mInPortRegistry.Size();
397  }
398  unsigned GetNOutPorts() const
399  {
400  return mOutPortRegistry.Size();
401  }
402  unsigned GetNInControls() const
403  {
404  return mInControlRegistry.Size();
405  }
406  unsigned GetNOutControls() const
407  {
408  return mOutControlRegistry.Size();
409  }
410 
411  protected:
413  InControlRegistry& GetInControls() { return mInControlRegistry; }
414 
416  OutControlRegistry& GetOutControls() { return mOutControlRegistry; }
417 
419  InPortRegistry& GetInPorts() { return mInPortRegistry; }
420 
422  OutPortRegistry& GetOutPorts() { return mOutPortRegistry; }
423  public:
425  const std::string& GetConfigErrorMessage() const { return _configErrorMessage; }
426 
429  virtual bool IsSyncSource() const { return false; }
430 
432  virtual bool SupportsVariableAudioSize() const {return ((GetNInPorts()+GetNOutPorts())==0)?true:false;}
433 
434 // Helpers only for subclasses
435  protected:
443  bool AddConfigErrorMessage( const std::string& msg );
444 
447  bool AbleToExecute(void) const;
448 
456  template <typename ConcreteConfig>
457  void CopyAsConcreteConfig(ConcreteConfig & concrete, const ProcessingConfig & abstract) const;
458  protected:
460  {
461  _execState = state;
462  }
463 // Attributes:
469  private:
470 
472  ExecState _execState;
473 
475  std::string _configErrorMessage;
476 
477 
478  private:
479  InControlRegistry mInControlRegistry;
480  OutControlRegistry mOutControlRegistry;
481  InPortRegistry mInPortRegistry;
482  OutPortRegistry mOutPortRegistry;
483  };
484 
485 
486 
487 // -----------------------------------------------------------------------------------------------
488 // Inline implementations
489 
490 inline bool Processing::AbleToExecute(void) const
491 {
493  if (!IsRunning())
494  {
495  std::string err(GetClassName());
496  err += ": Do(): Not in execution mode - did you call Start on this "
497  "object, the composite it is in, or the ToplevelProcessing singleton?";
498  CLAM_DEBUG_ASSERT( false, err.c_str() );
499  }
501  return IsRunning();
502 }
503 
504 template <typename ConcreteConfig>
505 inline void Processing::CopyAsConcreteConfig(ConcreteConfig & concrete, const ProcessingConfig & abstract) const
506 {
507  CLAM_ASSERT(typeid(ConcreteConfig)==typeid(abstract),
508  (std::string("Configuring a processing with a configuration of type ") + typeid(abstract).name() +
509  " while it was expected a " + typeid(ConcreteConfig).name() + ".").c_str());
510  concrete = static_cast<const ConcreteConfig &>(abstract);
511 }
512 
513 };//namespace CLAM
514 
515 #endif
516