NetworkPlayer.hxx
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef NetworkPlayer_hxx
00023 #define NetworkPlayer_hxx
00024
00025 #include "Network.hxx"
00026 #include "AudioSource.hxx"
00027 #include "AudioSink.hxx"
00028
00029 namespace CLAM
00030 {
00031
00040 class NetworkPlayer
00041 {
00042 protected:
00043 enum Status { Playing=0, Stopped=1, Paused=2 };
00044
00045 public:
00046 NetworkPlayer()
00047 : _network(NULL)
00048 , _status(Stopped)
00049 {
00050 }
00051
00052 virtual ~NetworkPlayer()
00053 {
00054 }
00055
00057 virtual bool IsWorking() = 0;
00058
00060 virtual std::string NonWorkingReason() = 0;
00061
00064 virtual void Init() {}
00065
00070 virtual void Start()=0;
00071
00073 virtual void Stop()=0;
00074
00075 virtual void Pause() { if (IsPlaying()) BePaused(); }
00076
00077 void SetNetworkBackLink( Network& net )
00078 {
00079 _network=&net;
00080 }
00081
00082 void BePaused() { _status=Paused; }
00083 void BeStopped() { _status=Stopped; }
00084 void BePlaying() { _status=Playing; }
00085 bool IsPaused() const { return _status==Paused; }
00086 bool IsStopped() const { return _status==Stopped; }
00087 bool IsPlaying() const { return _status==Playing; }
00088 virtual bool IsRealTime() const = 0;
00089
00090 virtual unsigned BackendBufferSize()
00091 {
00092 return 512;
00093 }
00094
00095 virtual unsigned BackendSampleRate()
00096 {
00097 return 44100;
00098 }
00099
00100 std::string SourcesAndSinksToString();
00101
00102 protected:
00103 Network& GetNetwork()
00104 {
00105 CLAM_ASSERT( (_network!=NULL), "NetworkPlayer::GetNetwork() : NetworkPlayer does not have any Network");
00106 return *_network;
00107 }
00108 protected:
00109 unsigned GetNControlSinks() const { return _controlSinks.size(); }
00110 unsigned GetNControlSources() const { return _controlSources.size(); }
00111 unsigned GetNSinks() const { return _audioSinks.size(); }
00112 unsigned GetNSources() const { return _audioSources.size(); }
00113 void CacheSourcesAndSinks()
00114 {
00115 _audioSources.clear();
00116 Network::Processings sources = GetSources();
00117 for (Network::Processings::const_iterator it = sources.begin(); it != sources.end(); ++it)
00118 {
00119 Processing * processing = *it;
00120 std::string processingName = _network->GetNetworkId(processing);
00121 unsigned nPorts = processing->GetNOutPorts();
00122 for (unsigned i=0; i<nPorts; i++)
00123 {
00124 std::string portName = processingName;
00125 if (nPorts > 1)
00126 portName += "_"+processing->GetOutPort(i).GetName();
00127 _audioSources.push_back(ExportedPort(processing,i, portName));
00128 }
00129 }
00130 _audioSinks.clear();
00131 Network::Processings sinks = GetSinks();
00132 for (Network::Processings::const_iterator it = sinks.begin(); it != sinks.end(); ++it)
00133 {
00134 Processing * processing = *it;
00135 std::string processingName = _network->GetNetworkId(processing);
00136 unsigned nPorts = processing->GetNInPorts();
00137 for (unsigned i=0; i<nPorts; i++)
00138 {
00139 std::string portName = processingName;
00140 if (nPorts > 1)
00141 portName +="_"+processing->GetInPort(i).GetName();
00142 _audioSinks.push_back(ExportedPort(processing,i, portName));
00143 }
00144 }
00145 _controlSources.clear();
00146 Network::ControlSources controlSources = _network->getOrderedControlSources();
00147 for (Network::ControlSources::iterator it=controlSources.begin(); it!=controlSources.end(); it++)
00148 {
00149 Processing * processing = (Processing*) *it;
00150 std::string portName = _network->GetNetworkId(processing);
00151 _controlSources.push_back(ExportedPort(processing,0,portName));
00152 }
00153 _controlSinks.clear();
00154 Network::ControlSinks controlSinks = _network->getOrderedControlSinks();
00155 for (Network::ControlSinks::iterator it=controlSinks.begin(); it!=controlSinks.end(); it++)
00156 {
00157 Processing * processing = (Processing*) *it;
00158 std::string portName = _network->GetNetworkId(processing);
00159 _controlSinks.push_back(ExportedPort(processing,0,portName));
00160 }
00161 }
00162 const std::string & ControlSourceName(unsigned source) const
00163 {
00164 return _controlSources[source].name;
00165 }
00166 const std::string & ControlSinkName(unsigned sink) const
00167 {
00168 return _controlSinks[sink].name;
00169 }
00170 const std::string & SourceName(unsigned source) const
00171 {
00172 return _audioSources[source].name;
00173 }
00174 const std::string & SinkName(unsigned sink) const
00175 {
00176 return _audioSinks[sink].name;
00177 }
00178 void SetSourceBuffer(unsigned source, const float * data, unsigned nframes);
00179 void SetSinkBuffer(unsigned sink, float * data, unsigned nframes);
00180 void ReadControlSource(unsigned source, const float * data);
00181 void FeedControlSink(unsigned sink, float * data);
00182 void SetSinkFrameSize(unsigned sink, unsigned frameSize);
00183 void SetSourceFrameSize(unsigned source, unsigned frameSize);
00184 private:
00185 Network::Processings GetSources()
00186 {
00187 return GetNetwork().getOrderedProcessingsByAttribute("port_source_type");
00188 }
00189
00190 Network::Processings GetSinks()
00191 {
00192 return GetNetwork().getOrderedProcessingsByAttribute("port_sink_type");
00193 }
00194 template <typename ProcessingType>
00195 void SetFrameAndHopSizeIf(Processing * proc, unsigned bufferSize, unsigned port)
00196 {
00197 if(typeid(*proc)!=typeid(ProcessingType)) return;
00198 ((ProcessingType*)proc)->SetFrameAndHopSize(bufferSize, port);
00199
00200 }
00201 template <typename ProcessingType>
00202 void SetExternalBuffer(Processing * proc, const float * data, unsigned nframes, unsigned port)
00203 {
00204 if(typeid(*proc)!=typeid(ProcessingType)) return;
00205 ((ProcessingType*)proc)->SetExternalBuffer(data, nframes, port);
00206 }
00207 template <typename ProcessingType>
00208 void SetExternalBuffer(Processing * proc, float * data, unsigned nframes, unsigned port)
00209 {
00210 if(typeid(*proc)!=typeid(ProcessingType)) return;
00211 ((ProcessingType*)proc)->SetExternalBuffer(data, nframes, port);
00212 }
00213
00214 private:
00215 struct ExportedPort
00216 {
00217 ExportedPort(Processing * aProcesing, unsigned aPort, const std::string & aName)
00218 : processing(aProcesing)
00219 , port(aPort)
00220 , name(aName)
00221 {
00222 }
00223 Processing * processing;
00224 unsigned port;
00225 std::string name;
00226 };
00227 typedef std::vector <ExportedPort> ExportedPorts;
00228 ExportedPorts _audioSources;
00229 ExportedPorts _audioSinks;
00230 protected:
00231 ExportedPorts _controlSources;
00232 ExportedPorts _controlSinks;
00233 private:
00234 Network *_network;
00235 volatile Status _status;
00236 };
00237
00238 }
00239
00240 #endif // NetworkPlayer_hxx
00241