AudioBufferSink.hxx

Go to the documentation of this file.
00001 #ifndef AudioBufferSink_hxx
00002 #define AudioBufferSink_hxx
00003 
00004 #include "Processing.hxx"
00005 #include "InPort.hxx"
00006 #include "Audio.hxx"
00007 
00008 #include <sstream>
00009 
00010 namespace CLAM
00011 {
00012         class AudioBufferSink : public Processing
00013         {
00014         public:
00015                 struct Port
00016                 {
00017                         float* mFloatBuffer;
00018                         double* mDoubleBuffer;
00019                         unsigned mBufferSize;
00020                         //AudioInPort* mPort;
00021                         InPort<Audio>* mPort;
00022 
00023                         //resize needs a default constructor
00024                         Port()
00025                         : mFloatBuffer(0), mDoubleBuffer(0), mBufferSize(0), mPort(0)
00026                         {
00027                         }
00028 
00029                         explicit Port(InPort<Audio>* p)
00030                         : mFloatBuffer(0), mDoubleBuffer(0), mBufferSize(0), mPort(p)
00031                         {
00032                         }
00033                 };
00034                 typedef std::vector<Port> Ports;
00035 
00036         private:
00037                 class Config : public ProcessingConfig
00038                 {
00039                         DYNAMIC_TYPE_USING_INTERFACE( Config, 1, ProcessingConfig );
00040                         DYN_ATTRIBUTE( 0, public, int, NSinks);
00041                 protected:
00042                         void DefaultInit()
00043                         {
00044                                 AddAll();
00045                                 UpdateData();
00046                                 SetNSinks(1);
00047                         };
00048                         void LoadFrom(Storage & storage)
00049                         {
00050                                 ProcessingConfig::LoadFrom(storage);
00051                                 if (not HasNSinks())
00052                                 {
00053                                         AddNSinks();
00054                                         UpdateData();
00055                                         SetNSinks(1);
00056                                 }
00057                                 }
00058                         };
00059 
00060         private:
00061                 Config _config;
00062                 Ports _ports;
00063 
00064         public:
00065                 AudioBufferSink(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                         ResizePorts(1);
00071                 }
00072 
00073                 ~AudioBufferSink()
00074                 {
00075                         for (unsigned port = 0; port < _ports.size(); ++port)
00076                                 delete _ports[port].mPort;
00077                 }
00078 
00080                 void SetFrameAndHopSize(const int val, unsigned index)
00081                 {
00082                         CLAM_ASSERT(index < _ports.size(), "AudioInPort index out of range");
00083                         Port& port = _ports[index];
00084                         port.mPort->SetSize(1);
00085                         port.mPort->SetHop(1);
00086                 }
00087 
00088                 void SetExternalBuffer(float* buf, unsigned nframes, unsigned index);
00089                 void SetExternalBuffer(double* buf, unsigned nframes, unsigned index);
00090 
00091                 bool Do();
00092 
00093         virtual bool SupportsVariableAudioSize() const {return true;}
00094 
00095                 const char* GetClassName() const { return "AudioBufferSink";}
00096 
00097                 const ProcessingConfig & GetConfig() const
00098                 {
00099                         return _config;
00100                 }
00101 
00102                 bool ConcreteConfigure(const ProcessingConfig& config)
00103                 {
00104                         CopyAsConcreteConfig(_config, config);
00105                         unsigned sinks = _config.GetNSinks();
00106 
00107                         ResizePorts(sinks);
00108 
00109                                 return true;
00110                 }
00111 
00112                 Ports& GetPorts() { 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 sinks)
00123                 {
00124                         if (sinks == _ports.size())
00125                                 return;
00126 
00127                         for (unsigned port = sinks; port < _ports.size(); ++port)
00128                                 delete _ports[port].mPort;
00129 
00130                         unsigned oldSize = _ports.size();
00131                         _ports.resize(sinks);
00132 
00133                         for (unsigned port = oldSize; port < sinks; ++port)
00134                                 _ports[port] = Port(new InPort<Audio>(Portname(port), this));
00135                 }
00136         };
00137 
00138 } //namespace CLAM
00139 
00140 #endif
00141 
Generated by  doxygen 1.6.3