CLAM-Development  1.4.0
NaiveFlowControl.cxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2004 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 
23 #include "NaiveFlowControl.hxx"
24 #include "Processing.hxx"
25 #include "OutPort.hxx"
26 #include "InPort.hxx"
27 #include "Network.hxx"
28 
29 namespace CLAM
30 {
31 
33 {
34 }
35 
37 {
39  std::string processingType = added.GetClassName();
40 
41  if ( processingType == "AudioSource" || processingType == "AudioBufferSource" )
42  {
43  mSources.push_back( &added );
44  return;
45  }
46  if (added.GetNInPorts()==0 && added.GetNOutPorts()==0) //isolated processings
47  {
48  mSources.push_back( &added);
49  return;
50  }
51  if (added.GetNInPorts()==0 && added.GetNOutPorts()!=0)
52  {
53  mGenerators.push_back( &added);
54  return;
55  }
56  if ( processingType == "AudioSink" || processingType == "AudioBufferSink" )
57  {
58  mSinks.push_back( &added );
59  return;
60  }
61  mNormalProcessings.push_back ( &added );
62 }
63 
65 {
67  std::string processingType = removed.GetClassName();
68  if ( processingType == "AudioSource" || processingType == "AudioBufferSource" )
69  {
70  mSources.remove( &removed );
71  return;
72  }
73  if (removed.GetNInPorts()==0 && removed.GetNOutPorts()==0) //isolated processings
74  {
75  mSources.remove( &removed );
76  return;
77  }
78  if (removed.GetNInPorts()==0 && removed.GetNOutPorts()!=0)
79  {
80  mGenerators.remove( &removed);
81  return;
82  }
83  if ( processingType == "AudioSink" || processingType == "AudioBufferSink" )
84  {
85  mSinks.remove( &removed );
86  return;
87  }
88  mNormalProcessings.remove ( &removed );
89 }
90 
92 {
93  // by now it have nothing of pulling
94  // a naive approach: do sources, do normal processings, do sinks
95  ProcessingList pendingSinks(mSinks);
96  for (ProcessingList::iterator it=mSources.begin(); it!=mSources.end(); it++ )
97  {
98  Processing* proc = *it;
99  if (proc->CanConsumeAndProduce())
100  {
101  //std::cerr << "Do: "<<proc->GetClassName() << std::endl;
102  proc->Do();
103  }
104  else
105  {
106  std::cerr << "Warning: some AudioSource was not able to consume incoming audio from the call-back.";
107  }
108  }
109  while (true)
110  {
111  bool noProcessingRun = true;
112  for (ProcessingList::iterator it=mNormalProcessings.begin(); it!=mNormalProcessings.end(); it++)
113  {
114  Processing* proc = *it;
115  if (!proc->CanConsumeAndProduce() )
116  {
117  //std::cerr << "could NOT Do: "<<proc->GetClassName() << std::endl;
118  continue;
119  }
120  //std::cerr << "Do: "<<proc->GetClassName() << std::endl;
121  noProcessingRun = false;
122  proc->Do();
123  }
124  for (ProcessingList::iterator it=pendingSinks.begin(); it!=pendingSinks.end(); )
125  {
126  Processing* proc = *it;
127  if (!proc->CanConsumeAndProduce())
128  {
129  it++;
130  continue;
131  }
132  //std::cerr << "Do: "<<proc->GetClassName() << std::endl;
133  proc->Do();
134  it = pendingSinks.erase(it);
135  noProcessingRun = false;
136  }
137  if (noProcessingRun && !pendingSinks.empty())
138  {
139  for (ProcessingList::iterator it=mGenerators.begin(); it!=mGenerators.end(); it++)
140  {
141  Processing* proc = *it;
142  if (!proc->CanConsumeAndProduce() )
143  {
144  // std::cerr << "could NOT Do: "<<proc->GetClassName() << std::endl;
145  continue;
146  }
147  //std::cerr << "Do: "<<proc->GetClassName() << std::endl;
148  noProcessingRun = false;
149  proc->Do();
150  }
151  }
152  if (noProcessingRun) break;
153  }
154  if (!pendingSinks.empty())
155  std::cerr << "Warning: " << pendingSinks.size() << " sinks were not fed, so could not send audio to the callback." << std::endl;
156 
157  //std::cerr << "<<< Network.Do() is Done" << std::endl;
158 
159 }
160 
161 
162 } // namespace CLAM
163 
164