CLAM-Development  1.4.0
MIDIManager.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 #include "MIDIManager.hxx"
23 #include "MIDIDevice.hxx"
24 #include "MIDIDeviceList.hxx"
25 #include "MIDIIn.hxx"
26 #include "MIDIOut.hxx"
27 #include "MIDIClocker.hxx"
28 #include <algorithm>
29 using std::find ;
30 
31 
32 namespace CLAM
33 {
34 
36 :mStarted(false)
37 {
38  _Current(true,this);
39 }
40 
42 {
43  std::vector<MIDIDevice*>::iterator it;
44 
45  it = mDevices.begin();
46 
47  while (it!=mDevices.end())
48  {
49  MIDIDevice* d = *it;
50 
51  it = mDevices.erase(it);
52  delete d;
53  }
54 
55  _Current(true,0);
56 }
57 
58 MIDIDevice* MIDIManager::FindDevice(const std::string& name)
59 {
60  /* Find a created device */
61  std::vector<MIDIDevice*>::iterator it;
62 
63  for (it = mDevices.begin(); it!=mDevices.end(); it++)
64  {
65  if ((*it)->mName == name ) return *it;
66  }
67  return 0;
68 }
69 
70 void MIDIManager::Start(void) throw(Err)
71 {
72  std::vector<MIDIDevice*>::iterator it;
73 
74  it = mDevices.begin();
75 
78  while (it!=mDevices.end())
79  {
80  MIDIDevice* d = *it;
81 
82  if (d->mInputs.size()==0 &&
83  d->mOutputs.size()==0)
84  {
85  it = mDevices.erase(it);
86  delete d;
87  }else{
88  it++;
89  }
90  }
91 
94  for (it = mDevices.begin(); it!=mDevices.end(); it++)
95  {
96  (*it)->Start();
97  }
98 
99  mStarted = true;
100 }
101 
102 void MIDIManager::Stop(void) throw(Err)
103 {
104  std::vector<MIDIDevice*>::iterator it;
105 
108  for (it = mDevices.begin(); it!=mDevices.end(); it++)
109  {
110  (*it)->Stop();
111  }
112 
113  mStarted = false;
114 }
115 
116 
118 {
119  CLAM_DEBUG_ASSERT(mStarted,"MIDIManager not started before calling MIDIManager::Check()\n");
120 
121  std::vector<MIDIDevice*>::iterator it;
122 
125  for (it = mDevices.begin(); it!=mDevices.end(); it++)
126  {
127  (*it)->Read();
128  }
129 }
130 
131 
133 {
134  std::string arch = name.substr(0,name.find(":",0));
135  std::string device = name.substr(name.find(":",0)+1,name.size());
136 
137  if (arch == "" || device == "")
138  {
139  std::string msg = "MIDIManager::FindOrCreateDevice(...): Invalid device name: ";
140  msg += name;
141  throw Err(msg.c_str());
142  }
143 
144  if (arch == "default")
145  {
146  arch = DEFAULT_MIDI_ARCH;
147  }
148 
151  MIDIDeviceList* list = FindList(arch);
152 
153  if (list==0)
154  {
155  std::string errstr;
156  errstr = "MIDIManager::FindOrCreateDevice(): "
157  "Don't have a list of \""+arch+"\" devices";
158  throw Err((char*) errstr.c_str());
159  }
160 
161  if (list->AvailableDevices().size()==0)
162  {
163  std::string errstr;
164  errstr = "MIDIManager::FindOrCreateDevice(): "
165  "Don't have any \""+arch+"\" devices available";
166  throw Err((char*) errstr.c_str());
167  }
168 
169  if (device == "default")
170  {
171  device = list->DefaultDevice();
172  }
173 
177  std::vector<std::string>::const_iterator it;
178 
179  for (
180  it = list->AvailableDevices().begin();
181  it != list->AvailableDevices().end();
182  it++)
183  {
184  if (*it == device) break;
185  break;
186  }
187 
188 /* if (find(list->AvailableDevices().begin(),
189  list->AvailableDevices().end(),
190  device) ==
191  list->AvailableDevices().end())
192 */
193  if (it == list->AvailableDevices().end())
194  {
195  std::string errstr;
196  errstr = "MIDIManager::FindOrCreateDevice(): "
197  "No device \""+device+"\" available in architecture \""+arch+"\".\n";
198  throw Err((char*) errstr.c_str());
199  }
200 
201  std::string real_name = arch+":"+device;
202 
205  MIDIDevice* mididevice = FindDevice(real_name);
206 
207  if (mididevice==0) {
208 
211  mididevice = list->Create(real_name,device);
212 
213  if (mididevice==0)
214  {
215  std::string errstr;
216  errstr = "MIDIManager::FindOrCreateDevice(): Don't know how to make device "+real_name;
217  throw Err((char*) errstr.c_str());
218  }
219  else
220  {
221  mDevices.push_back(mididevice);
222  }
223  }
224  return mididevice;
225 }
226 
227 
229 {
233  MIDIDevice* device = FindOrCreateDevice(in.mConfig.GetDevice());
234  return device->Register(this,in);
235 }
236 
238 {
242  MIDIDevice* device = FindOrCreateDevice(cl.mConfig.GetDevice());
243  return device->Register(this,cl);
244 }
245 
247 {
248  MIDIDevice* device = FindOrCreateDevice(out.mConfig.GetDevice());
249  return device->Register(this,out);
250 }
251 
252 MIDIDeviceList* MIDIManager::FindList(const std::string& arch)
253 {
254  unsigned int i;
255  std::string tmp = arch;
256 
257  if (tmp == "default")
258  tmp = DEFAULT_MIDI_ARCH;
259 
262  for (i=0;i<DeviceLists().size();i++)
263  {
264  if (DeviceLists()[i]->ArchName() == tmp)
265  {
266  return DeviceLists()[i];
267  }
268  }
269 
270  return 0;
271 }
272 
273 
274 }
275