CLAM-Development  1.4.0
NetworkPlayer.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2007 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 #ifndef NetworkPlayer_hxx
23 #define NetworkPlayer_hxx
24 
25 #include "Network.hxx"
26 #include "AudioSource.hxx"
27 #include "AudioSink.hxx"
28 
29 namespace CLAM
30 {
31 
41 {
42 protected:
43  enum Status { Playing=0, Stopped=1, Paused=2 };
44 
45 public:
47  : _network(NULL)
48  , _status(Stopped)
49  {
50  }
51 
52  virtual ~NetworkPlayer()
53  {
54  }
55 
57  virtual bool IsWorking() = 0;
58 
60  virtual std::string NonWorkingReason() = 0;
61 
64  virtual void Init() {}
65 
70  virtual void Start()=0; // { if (not IsPlaying()) BePlaying(); }
71 
73  virtual void Stop()=0; // { if (not IsStopped()) BeStopped(); }
74 
75  virtual void Pause() { if (IsPlaying()) BePaused(); }
76 
78  {
79  _network=&net;
80  }
81 
82  void BePaused() { _status=Paused; }
83  void BeStopped() { _status=Stopped; }
84  void BePlaying() { _status=Playing; }
85  bool IsPaused() const { return _status==Paused; }
86  bool IsStopped() const { return _status==Stopped; }
87  bool IsPlaying() const { return _status==Playing; }
88  virtual bool IsRealTime() const = 0;
89 
90  virtual unsigned BackendBufferSize()
91  {
92  return 512;
93  }
94 
95  virtual unsigned BackendSampleRate()
96  {
97  return 44100;
98  }
99 
100  std::string SourcesAndSinksToString();
101 
102 protected:
104  {
105  CLAM_ASSERT( (_network!=NULL), "NetworkPlayer::GetNetwork() : NetworkPlayer does not have any Network");
106  return *_network;
107  }
108 protected:
109  unsigned GetNSinks() const { return _exportedSinks.size(); }
110  unsigned GetNSources() const { return _exportedSources.size(); }
112  {
113  _exportedSources.clear();
114  Network::Processings sources = GetSources();
115  for (Network::Processings::const_iterator it = sources.begin(); it != sources.end(); ++it)
116  {
117  Processing * processing = *it;
118  std::string processingName = _network->GetNetworkId(processing);
119  unsigned nPorts = processing->GetNOutPorts();
120  for (unsigned i=0; i<nPorts; i++)
121  {
122  std::ostringstream portName;
123  portName << processingName;
124  if (nPorts > 1)
125  portName << "_" << processing->GetOutPort(i).GetName();
126  _exportedSources.push_back(ExportedPort(processing,i, portName.str()));
127  }
128  }
129  _exportedSinks.clear();
130  Network::Processings sinks = GetSinks();
131  for (Network::Processings::const_iterator it = sinks.begin(); it != sinks.end(); ++it)
132  {
133  Processing * processing = *it;
134  std::string processingName = _network->GetNetworkId(processing);
135  unsigned nPorts = processing->GetNInPorts();
136  for (unsigned i=0; i<nPorts; i++)
137  {
138  std::ostringstream portName;
139  portName << processingName;
140  if (nPorts > 1)
141  portName << "_" << processing->GetInPort(i).GetName();
142  _exportedSinks.push_back(ExportedPort(processing,i, portName.str()));
143  }
144  }
145  }
146  const std::string & SourceName(unsigned source) const
147  {
148  return _exportedSources[source].name;
149  }
150  const std::string & SinkName(unsigned sink) const
151  {
152  return _exportedSinks[sink].name;
153  }
154  void SetSourceBuffer(unsigned source, const float * data, unsigned nframes);
155  void SetSinkBuffer(unsigned sink, float * data, unsigned nframes);
156  void SetSinkFrameSize(unsigned sink, unsigned frameSize);
157  void SetSourceFrameSize(unsigned source, unsigned frameSize);
158 private:
159  Network::Processings GetSources()
160  {
161  return GetNetwork().getOrderedProcessingsByAttribute("port_source_type");
162  }
163 
164  Network::Processings GetSinks()
165  {
166  return GetNetwork().getOrderedProcessingsByAttribute("port_sink_type");
167  }
168  template <typename ProcessingType>
169  void SetFrameAndHopSizeIf(Processing * proc, unsigned bufferSize, unsigned port)
170  {
171  if(typeid(*proc)!=typeid(ProcessingType)) return;
172  ((ProcessingType*)proc)->SetFrameAndHopSize(bufferSize, port);
173 
174  }
175  template <typename ProcessingType>
176  void SetExternalBuffer(Processing * proc, const float * data, unsigned nframes, unsigned port)
177  {
178  if(typeid(*proc)!=typeid(ProcessingType)) return;
179  ((ProcessingType*)proc)->SetExternalBuffer(data, nframes, port);
180  }
181  template <typename ProcessingType>
182  void SetExternalBuffer(Processing * proc, float * data, unsigned nframes, unsigned port)
183  {
184  if(typeid(*proc)!=typeid(ProcessingType)) return;
185  ((ProcessingType*)proc)->SetExternalBuffer(data, nframes, port);
186  }
187 
188 private:
189  struct ExportedPort
190  {
191  ExportedPort(Processing * aProcesing, unsigned aPort, const std::string & aName)
192  : processing(aProcesing)
193  , port(aPort)
194  , name(aName)
195  {
196  }
197  Processing * processing;
198  unsigned port;
199  std::string name;
200  };
201  typedef std::vector <ExportedPort> ExportedPorts;
202  ExportedPorts _exportedSources;
203  ExportedPorts _exportedSinks;
204  Network *_network;
205  volatile Status _status;
206 };
207 
208 } //namespace CLAM
209 
210 #endif // NetworkPlayer_hxx
211