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