AudioSource.hxx

Go to the documentation of this file.
00001 #ifndef AudioSource_hxx
00002 #define AudioSource_hxx
00003 
00004 #include "Processing.hxx"
00005 #include "AudioOutPort.hxx"
00006 
00007 #include <sstream>
00008 
00009 namespace CLAM
00010 {
00011 
00012         class AudioSource : public Processing
00013         {
00014         public:
00015                 struct Port
00016                 {
00017                         const float* mFloatBuffer;
00018                         const double* mDoubleBuffer;
00019                         unsigned mBufferSize;
00020                         AudioOutPort* mPort;
00021 
00022                         Port()
00023                         : mFloatBuffer(0), mDoubleBuffer(0), mBufferSize(0), mPort(0)
00024                         {
00025                         }
00026 
00027                         explicit Port(AudioOutPort* p)
00028                         : mFloatBuffer(0), mDoubleBuffer(0), mBufferSize(0), mPort(p)
00029                         {
00030                         }
00031                 };
00032                 typedef std::vector<Port> Ports;
00033 
00034         private:
00035 
00036                 class Config : public ProcessingConfig
00037                 {
00038                         DYNAMIC_TYPE_USING_INTERFACE( Config, 1, ProcessingConfig );
00039                         DYN_ATTRIBUTE( 0, public, int, NSources);
00040                         ~Config();
00041                 protected:
00042                         void DefaultInit()
00043                         {
00044                                 AddAll();
00045                                 UpdateData();
00046                                 SetNSources(1);
00047                         };
00048                         void LoadFrom(Storage & storage)
00049                         {
00050                                 ProcessingConfig::LoadFrom(storage);
00051                                 if (not HasNSources())
00052                                 {
00053                                         AddNSources();
00054                                         UpdateData();
00055                                         SetNSources(1);
00056                                 }
00057                         }
00058                 };
00059 
00060                 private:
00061                         Config _config;
00062                         Ports  _ports;
00063 
00064                 public:
00065                         AudioSource(const ProcessingConfig & config=Config())
00066                         {
00067                                 //After being dropped it is ready to run as it does not need any configuration at all
00068                                 //SetExecState(Ready);
00069                                 Configure( config );
00070 
00071                                 // default constructed with 1 port
00072                                 ResizePorts(1);
00073                         }
00074 
00075                         ~AudioSource()
00076                         {
00077                                 for (unsigned port = 0; port < _ports.size(); ++port)
00078                                         delete _ports[port].mPort;
00079                         }
00080 
00081                         void SetFrameAndHopSize(const int val, unsigned index)
00082                         {
00083                                 CLAM_ASSERT(index < _ports.size(), "AudioOutPort index out of range");
00084                                 Port& port = _ports[index];
00085                                 port.mPort->SetSize(val);
00086                                 port.mPort->SetHop(val);
00087                         }
00088 
00089                         void SetExternalBuffer(const float* buf, unsigned nframes, unsigned index);
00090                         void SetExternalBuffer(const double* buf, unsigned nframes, unsigned index);
00091 
00092                         bool Do();
00093 
00094                         const char* GetClassName() const { return "AudioSource";}
00095 
00096                         const ProcessingConfig & GetConfig() const
00097                         {
00098                                 return _config;
00099                         }
00100 
00101                         bool ConcreteConfigure(const ProcessingConfig& config)
00102                         {
00103                                 CopyAsConcreteConfig(_config, config);
00104                                 unsigned sources = _config.GetNSources();
00105 
00106                                 ResizePorts(sources);
00107 
00108                                 return true;
00109                         }
00110 
00111                         Ports& GetPorts() { return _ports; }
00112                         const Ports & GetPorts() const { return _ports; }
00113 
00114                 private:
00115                         std::string const Portname(unsigned port) const
00116                         {
00117                                 std::ostringstream os;
00118                                 os << port + 1; //to make ports one based (when viewed in jack)
00119                                 return os.str();
00120                         }
00121 
00122                         void ResizePorts(unsigned sources)
00123                         {
00124                                 if (sources == _ports.size())
00125                                         return;
00126 
00127                                 for (unsigned port = sources; port < _ports.size(); ++port)
00128                                         delete _ports[port].mPort;
00129 
00130                                 unsigned oldSize = _ports.size();
00131                                 _ports.resize(sources);
00132 
00133                                 for (unsigned port = oldSize; port < sources; ++port)
00134                                         _ports[port] = Port(new AudioOutPort(Portname(port), this));
00135                         }
00136 
00137 
00138         };
00139 
00140 } //namespace CLAM
00141 
00142 #endif
Generated by  doxygen 1.6.3