21 #ifndef __PortMIDIDevice__
22 #define __PortMIDIDevice__
31 std::ostream&
operator<<( std::ostream& out_str,
const PmDeviceInfo* dev_info )
34 out_str <<
"Device Name: " << dev_info->name << std::endl;
35 out_str <<
"Interface Name: " << dev_info->interf << std::endl;
36 if ( dev_info->output == 1 ) out_str <<
"Output device" << std::endl ;
37 else out_str <<
"Input device" << std::endl ;
42 class PortMIDIDevice:
public MIDIDevice
51 PortMIDIDevice(
const std::string& name,
const std::string& device);
55 void ConcreteStart(
void) throw(Err);
56 void ConcreteStop(
void) throw(Err);
57 void Read(
void) throw(Err);
58 void Write(
unsigned char* msg,
int size) throw(Err);
59 void OpenInput(
int dev) throw(Err);
60 void OpenOutput(
int dev) throw(Err);
64 PortMIDIDevice::PortMIDIDevice(const std::
string& name,const std::
string& device):
73 void PortMIDIDevice::OpenInput(
int dev)
throw(Err)
75 PmError err = Pm_OpenInput( &mHandleIn,
84 std::string str(
"Could not open PortMIDIDevice ");
86 throw Err(str.c_str());
90 void PortMIDIDevice::OpenOutput(
int dev)
throw(Err)
92 PmError err = Pm_OpenOutput(&mHandleOut,
101 std::string str(
"Could not open PortMIDIDevice ");
103 throw Err(str.c_str());
107 void PortMIDIDevice::ConcreteStart(
void) throw(Err)
111 if (mDevice ==
"default")
115 OpenInput(Pm_GetDefaultInputDeviceID());
119 OpenOutput(Pm_GetDefaultOutputDeviceID());
124 std::string type = mDevice.substr(0,mDevice.find(
":",0));
125 std::string name = mDevice.substr(mDevice.find(
":",0)+1,mDevice.size());
131 for ( dev = 0; dev < Pm_CountDevices(); dev++)
133 const PmDeviceInfo *info = Pm_GetDeviceInfo(dev);
134 if (info->input && name == info->name)
141 std::string str(
"Could not find PortMIDIDevice ");
143 throw Err(str.c_str());
148 if (type ==
"output")
150 for ( dev = 0; dev < Pm_CountDevices(); dev++)
152 const PmDeviceInfo *info = Pm_GetDeviceInfo(dev);
153 if (info->output && name == info->name)
160 std::string str(
"Could not find PortMIDIDevice ");
162 throw Err(str.c_str());
166 void PortMIDIDevice::ConcreteStop(
void) throw(Err)
177 Pm_Close(mHandleOut);
183 void PortMIDIDevice::Write(
unsigned char* msg,
int size)
throw(Err)
190 Pm_WriteSysEx(mHandleOut,0,(
char*) msg);
192 Pm_WriteSysEx(mHandleOut,0,msg);
200 Pm_WriteShort(mHandleOut,0,Pm_Message(msg[0],msg[1],msg[2]));
204 Pm_WriteShort(mHandleOut,0,Pm_Message(msg[0],msg[1],0));
209 void PortMIDIDevice::Read(
void) throw(Err)
212 unsigned char status, data1, data2;
215 while (Pm_Poll(mHandleIn) == TRUE)
218 if (Pm_Read(mHandleIn, &buffer, 1) == pmBufferOverflow)
continue;
221 status = (
unsigned char ) Pm_MessageStatus(buffer.message);
225 data1 = (
unsigned char ) Pm_MessageData1(buffer.message);
226 data2 = (
unsigned char ) Pm_MessageData2(buffer.message);
234 data1 = (
unsigned char ) Pm_MessageData1(buffer.message);
244 PortMIDIDevice::~PortMIDIDevice()
249 class PortMIDIDeviceList:
public MIDIDeviceList
251 static PortMIDIDeviceList sDevices;
254 :MIDIDeviceList(std::string(
"portmidi"))
262 for ( dev = 0; dev < Pm_CountDevices(); dev++)
264 const PmDeviceInfo *info = Pm_GetDeviceInfo(dev);
266 std::string name(info->name);
269 mAvailableDevices.push_back(std::string(
"input:")+name);
272 mAvailableDevices.push_back(std::string(
"output:")+name);
279 ~PortMIDIDeviceList()
284 std::string DefaultDevice(
void)
286 return std::string(
"default");
290 const std::string& name,
const std::string& device)
292 return new PortMIDIDevice(name,device);
296 PortMIDIDeviceList PortMIDIDeviceList::sDevices;