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