MIDIManager.cxx
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 #include "MIDIManager.hxx"
00023 #include "MIDIDevice.hxx"
00024 #include "MIDIDeviceList.hxx"
00025 #include "MIDIIn.hxx"
00026 #include "MIDIOut.hxx"
00027 #include "MIDIClocker.hxx"
00028 #include <algorithm>
00029 using std::find ;
00030
00031
00032 namespace CLAM
00033 {
00034
00035 MIDIManager::MIDIManager() throw(Err)
00036 :mStarted(false)
00037 {
00038 _Current(true,this);
00039 }
00040
00041 MIDIManager::~MIDIManager()
00042 {
00043 std::vector<MIDIDevice*>::iterator it;
00044
00045 it = mDevices.begin();
00046
00047 while (it!=mDevices.end())
00048 {
00049 MIDIDevice* d = *it;
00050
00051 it = mDevices.erase(it);
00052 delete d;
00053 }
00054
00055 _Current(true,0);
00056 }
00057
00058 MIDIDevice* MIDIManager::FindDevice(const std::string& name)
00059 {
00060
00061 std::vector<MIDIDevice*>::iterator it;
00062
00063 for (it = mDevices.begin(); it!=mDevices.end(); it++)
00064 {
00065 if ((*it)->mName == name ) return *it;
00066 }
00067 return 0;
00068 }
00069
00070 void MIDIManager::Start(void) throw(Err)
00071 {
00072 std::vector<MIDIDevice*>::iterator it;
00073
00074 it = mDevices.begin();
00075
00078 while (it!=mDevices.end())
00079 {
00080 MIDIDevice* d = *it;
00081
00082 if (d->mInputs.size()==0 &&
00083 d->mOutputs.size()==0)
00084 {
00085 it = mDevices.erase(it);
00086 delete d;
00087 }else{
00088 it++;
00089 }
00090 }
00091
00094 for (it = mDevices.begin(); it!=mDevices.end(); it++)
00095 {
00096 (*it)->Start();
00097 }
00098
00099 mStarted = true;
00100 }
00101
00102 void MIDIManager::Stop(void) throw(Err)
00103 {
00104 std::vector<MIDIDevice*>::iterator it;
00105
00108 for (it = mDevices.begin(); it!=mDevices.end(); it++)
00109 {
00110 (*it)->Stop();
00111 }
00112
00113 mStarted = false;
00114 }
00115
00116
00117 void MIDIManager::Check(void)
00118 {
00119 CLAM_DEBUG_ASSERT(mStarted,"MIDIManager not started before calling MIDIManager::Check()\n");
00120
00121 std::vector<MIDIDevice*>::iterator it;
00122
00125 for (it = mDevices.begin(); it!=mDevices.end(); it++)
00126 {
00127 (*it)->Read();
00128 }
00129 }
00130
00131
00132 MIDIDevice* MIDIManager::FindOrCreateDevice(const std::string& name)
00133 {
00134 std::string arch = name.substr(0,name.find(":",0));
00135 std::string device = name.substr(name.find(":",0)+1,name.size());
00136
00137 if (arch == "" || device == "")
00138 {
00139 std::string msg = "MIDIManager::FindOrCreateDevice(...): Invalid device name: ";
00140 msg += name;
00141 throw Err(msg.c_str());
00142 }
00143
00144 if (arch == "default")
00145 {
00146 arch = DEFAULT_MIDI_ARCH;
00147 }
00148
00151 MIDIDeviceList* list = FindList(arch);
00152
00153 if (list==0)
00154 {
00155 std::string errstr;
00156 errstr = "MIDIManager::FindOrCreateDevice(): "
00157 "Don't have a list of \""+arch+"\" devices";
00158 throw Err((char*) errstr.c_str());
00159 }
00160
00161 if (list->AvailableDevices().size()==0)
00162 {
00163 std::string errstr;
00164 errstr = "MIDIManager::FindOrCreateDevice(): "
00165 "Don't have any \""+arch+"\" devices available";
00166 throw Err((char*) errstr.c_str());
00167 }
00168
00169 if (device == "default")
00170 {
00171 device = list->DefaultDevice();
00172 }
00173
00177 std::vector<std::string>::const_iterator it;
00178
00179 for (
00180 it = list->AvailableDevices().begin();
00181 it != list->AvailableDevices().end();
00182 it++)
00183 {
00184 if (*it == device) break;
00185 break;
00186 }
00187
00188
00189
00190
00191
00192
00193 if (it == list->AvailableDevices().end())
00194 {
00195 std::string errstr;
00196 errstr = "MIDIManager::FindOrCreateDevice(): "
00197 "No device \""+device+"\" available in architecture \""+arch+"\".\n";
00198 throw Err((char*) errstr.c_str());
00199 }
00200
00201 std::string real_name = arch+":"+device;
00202
00205 MIDIDevice* mididevice = FindDevice(real_name);
00206
00207 if (mididevice==0) {
00208
00211 mididevice = list->Create(real_name,device);
00212
00213 if (mididevice==0)
00214 {
00215 std::string errstr;
00216 errstr = "MIDIManager::FindOrCreateDevice(): Don't know how to make device "+real_name;
00217 throw Err((char*) errstr.c_str());
00218 }
00219 else
00220 {
00221 mDevices.push_back(mididevice);
00222 }
00223 }
00224 return mididevice;
00225 }
00226
00227
00228 bool MIDIManager::Register(MIDIIn& in)
00229 {
00233 MIDIDevice* device = FindOrCreateDevice(in.mConfig.GetDevice());
00234 return device->Register(this,in);
00235 }
00236
00237 bool MIDIManager::Register(MIDIClocker& cl)
00238 {
00242 MIDIDevice* device = FindOrCreateDevice(cl.mConfig.GetDevice());
00243 return device->Register(this,cl);
00244 }
00245
00246 bool MIDIManager::Register(MIDIOut& out)
00247 {
00248 MIDIDevice* device = FindOrCreateDevice(out.mConfig.GetDevice());
00249 return device->Register(this,out);
00250 }
00251
00252 MIDIDeviceList* MIDIManager::FindList(const std::string& arch)
00253 {
00254 unsigned int i;
00255 std::string tmp = arch;
00256
00257 if (tmp == "default")
00258 tmp = DEFAULT_MIDI_ARCH;
00259
00262 for (i=0;i<DeviceLists().size();i++)
00263 {
00264 if (DeviceLists()[i]->ArchName() == tmp)
00265 {
00266 return DeviceLists()[i];
00267 }
00268 }
00269
00270 return 0;
00271 }
00272
00273
00274 }
00275