NetworkPlayer.hxx

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2001-2007 MUSIC TECHNOLOGY GROUP (MTG)
00003  *                         UNIVERSITAT POMPEU FABRA
00004  *
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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; // { if (not IsPlaying()) BePlaying(); }
00071 
00073         virtual void Stop()=0; // { if (not IsStopped()) BeStopped(); }
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 GetNSinks() const { return _exportedSinks.size(); }                   
00110         unsigned GetNSources() const { return _exportedSources.size(); }               
00111         void CacheSourcesAndSinks()
00112         {
00113                 _exportedSources.clear();
00114                 Network::Processings sources = GetSources();
00115                 for (Network::Processings::const_iterator it = sources.begin(); it != sources.end(); ++it)
00116                 {
00117                         Processing * processing = *it;
00118                         std::string processingName = _network->GetNetworkId(processing);
00119                         unsigned nPorts = processing->GetNOutPorts();
00120                         for (unsigned i=0; i<nPorts; i++)
00121                         {
00122                                 std::ostringstream portName;
00123                                 portName << processingName;
00124                                 if (nPorts > 1)
00125                                         portName << "_" << processing->GetOutPort(i).GetName();
00126                                 _exportedSources.push_back(ExportedPort(processing,i, portName.str()));
00127                         }
00128                 }
00129                 _exportedSinks.clear();
00130                 Network::Processings sinks = GetSinks();
00131                 for (Network::Processings::const_iterator it = sinks.begin(); it != sinks.end(); ++it)
00132                 {
00133                         Processing * processing = *it;
00134                         std::string processingName = _network->GetNetworkId(processing);
00135                         unsigned nPorts = processing->GetNInPorts();
00136                         for (unsigned i=0; i<nPorts; i++)
00137                         {
00138                                 std::ostringstream portName;
00139                                 portName << processingName;
00140                                 if (nPorts > 1)
00141                                         portName << "_" << processing->GetInPort(i).GetName();
00142                                 _exportedSinks.push_back(ExportedPort(processing,i, portName.str()));
00143                         }
00144                 }
00145         }
00146         const std::string & SourceName(unsigned source) const
00147         {
00148                 return _exportedSources[source].name;
00149         }
00150         const std::string & SinkName(unsigned sink) const
00151         {
00152                 return _exportedSinks[sink].name;
00153         }
00154         void SetSourceBuffer(unsigned source, const float * data, unsigned nframes);
00155         void SetSinkBuffer(unsigned sink, float * data, unsigned nframes);
00156         void SetSinkFrameSize(unsigned sink, unsigned frameSize);
00157         void SetSourceFrameSize(unsigned source, unsigned frameSize);
00158 private:
00159         Network::Processings GetSources()
00160         {
00161                 return GetNetwork().getOrderedProcessingsByAttribute("port_source_type");
00162         }
00163 
00164         Network::Processings GetSinks()
00165         {
00166                 return GetNetwork().getOrderedProcessingsByAttribute("port_sink_type");
00167         }
00168         template <typename ProcessingType>
00169         void SetFrameAndHopSizeIf(Processing * proc, unsigned bufferSize, unsigned port)
00170         {
00171                 if(typeid(*proc)!=typeid(ProcessingType)) return;
00172                 ((ProcessingType*)proc)->SetFrameAndHopSize(bufferSize, port);
00173                 
00174         }
00175         template <typename ProcessingType>
00176         void SetExternalBuffer(Processing * proc, const float * data, unsigned nframes, unsigned port)
00177         {
00178                 if(typeid(*proc)!=typeid(ProcessingType)) return;
00179                 ((ProcessingType*)proc)->SetExternalBuffer(data, nframes, port);
00180         }
00181         template <typename ProcessingType>
00182         void SetExternalBuffer(Processing * proc, float * data, unsigned nframes, unsigned port)
00183         {
00184                 if(typeid(*proc)!=typeid(ProcessingType)) return;
00185                 ((ProcessingType*)proc)->SetExternalBuffer(data, nframes, port);
00186         }
00187 
00188 private:
00189         struct ExportedPort
00190         {
00191                 ExportedPort(Processing * aProcesing, unsigned aPort, const std::string & aName)
00192                         : processing(aProcesing)
00193                         , port(aPort)
00194                         , name(aName)
00195                 {
00196                 }
00197                 Processing * processing;
00198                 unsigned port;
00199                 std::string name;
00200         };
00201         typedef std::vector <ExportedPort> ExportedPorts;
00202         ExportedPorts _exportedSources;
00203         ExportedPorts _exportedSinks;
00204         Network *_network;
00205         volatile Status _status;
00206 };
00207 
00208 } //namespace CLAM
00209 
00210 #endif // NetworkPlayer_hxx
00211 
Generated by  doxygen 1.6.3