Initial
This commit is contained in:
251
midiseq/midiout.cpp
Normal file
251
midiseq/midiout.cpp
Normal file
@@ -0,0 +1,251 @@
|
||||
#include <midiseq/midiout.hpp>
|
||||
#include <common/mmsystem.hpp>
|
||||
#include <common/string.hpp>
|
||||
#include <common/block.hpp>
|
||||
|
||||
/*
|
||||
* Constructor
|
||||
* @param deviceName - name of device to open, if null then opens default device
|
||||
* @return return
|
||||
*/
|
||||
MIDIOutputDevice::MIDIOutputDevice(const String &deviceName)
|
||||
: mHasDevice(false), mMIDIDeviceID(MIDIMAPPER)
|
||||
{
|
||||
if(deviceName.isNull())openDevice();
|
||||
else openDevice(deviceName);
|
||||
}
|
||||
|
||||
/*
|
||||
* openDevice - opens the default MIDI output device
|
||||
* @param none
|
||||
* @return bool true - if device was opened, false otherwise
|
||||
*/
|
||||
bool MIDIOutputDevice::openDevice(void)
|
||||
{
|
||||
closeDevice();
|
||||
if(!::midiOutGetNumDevs())return false;
|
||||
if(::midiOutOpen(&mhMIDIOutput,mMIDIDeviceID,0,0,0))return false;
|
||||
hasDevice(TRUE);
|
||||
getDeviceCapabilities();
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* openDevice - opens the named MIDI output device
|
||||
* @param deviceName - Name of the device to open
|
||||
* @return bool true - if device was opened, false otherwise
|
||||
*/
|
||||
bool MIDIOutputDevice::openDevice(const String &deviceName)
|
||||
{
|
||||
Block<String> deviceNames;
|
||||
|
||||
closeDevice();
|
||||
if(deviceName=="MIDIMAPPER")
|
||||
{
|
||||
mMIDIDeviceID=MIDIMAPPER;
|
||||
return openDevice();
|
||||
}
|
||||
if(!getDeviceNames(deviceNames))return false;
|
||||
for(int index=0;index<deviceNames.size();index++)
|
||||
{
|
||||
if(deviceNames[index]==deviceName)break;
|
||||
}
|
||||
if(index>=deviceNames.size())return false;
|
||||
return openDevice(index);
|
||||
}
|
||||
|
||||
/*
|
||||
* openDevice - opens the device with the given id
|
||||
* @param deviceID - The ID of the device to open
|
||||
* @return bool true - if device was opened, false otherwise
|
||||
*/
|
||||
bool MIDIOutputDevice::openDevice(int deviceID)
|
||||
{
|
||||
UINT numDevs;
|
||||
|
||||
closeDevice();
|
||||
if(-1==deviceID)
|
||||
{
|
||||
mMIDIDeviceID=MIDIMAPPER;
|
||||
return openDevice();
|
||||
}
|
||||
if(!(numDevs=::midiOutGetNumDevs()))return false;
|
||||
if(deviceID>=numDevs)return false;
|
||||
if(::midiOutOpen(&mhMIDIOutput,deviceID,0,0,0))return false;
|
||||
mMIDIDeviceID=deviceID;
|
||||
hasDevice(true);
|
||||
getDeviceCapabilities();
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* closeDevice - closes the device
|
||||
* @param none
|
||||
* @return none
|
||||
*/
|
||||
void MIDIOutputDevice::closeDevice(void)
|
||||
{
|
||||
if(!hasDevice())return;
|
||||
::midiOutReset(mhMIDIOutput);
|
||||
::midiOutClose(mhMIDIOutput);
|
||||
hasDevice(false);
|
||||
mMIDIDeviceID=MIDIMAPPER;
|
||||
}
|
||||
|
||||
/*
|
||||
* getDeviceCapabilities - Gets Caps for current device
|
||||
* @param none
|
||||
* @return bool true=success, false otherwise
|
||||
*/
|
||||
bool MIDIOutputDevice::getDeviceCapabilities(void)
|
||||
{
|
||||
if(!hasDevice())return false;
|
||||
::memset(&mMIDIOutDevCaps,0,sizeof(MIDIOutCaps));
|
||||
if(::midiOutGetDevCaps(mMIDIDeviceID,mMIDIOutDevCaps,sizeof(MIDIOutCaps)))return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* getNumDevs - Get number of devices in system
|
||||
* @param none
|
||||
* @return UINT number of devices in system (this is related to device ID)
|
||||
*/
|
||||
UINT MIDIOutputDevice::getNumDevs(void)
|
||||
{
|
||||
return midiOutGetNumDevs();
|
||||
}
|
||||
|
||||
/*
|
||||
* getDeviceNames - Get names of devices currently in system
|
||||
* @param Block<String> & that will be populated with the device names
|
||||
* @return bool true if success.
|
||||
*/
|
||||
bool MIDIOutputDevice::getDeviceNames(Block<String> &deviceNames)
|
||||
{
|
||||
UINT numDevs;
|
||||
MIDIOutCaps midiOutCaps;
|
||||
|
||||
deviceNames.remove();
|
||||
numDevs=getNumDevs();
|
||||
for(int index=0;index<numDevs;index++)
|
||||
{
|
||||
::memset(&midiOutCaps,0,sizeof(midiOutCaps));
|
||||
if(::midiOutGetDevCaps(index,&midiOutCaps.getMIDIOUTCAPS(),sizeof(midiOutCaps)))break;
|
||||
deviceNames.insert(&midiOutCaps.productName());
|
||||
}
|
||||
return deviceNames.size()?true:false;
|
||||
}
|
||||
|
||||
/*
|
||||
* midiEvent - Put a MIDI event to the current device
|
||||
* @param PureEvent - an event
|
||||
* @return bool true if success.
|
||||
*/
|
||||
bool MIDIOutputDevice::midiEvent(const PureEvent &somePureEvent)
|
||||
{
|
||||
union{DWORD dwData;BYTE bData[4];}unionData;
|
||||
|
||||
// String str=(String)((PureEvent&)somePureEvent)+String("\n");;
|
||||
// ::OutputDebugString(str.str());
|
||||
if(!hasDevice())return false;
|
||||
unionData.bData[0]=somePureEvent.eventType();
|
||||
unionData.bData[1]=somePureEvent.firstData();
|
||||
unionData.bData[2]=somePureEvent.secondData();
|
||||
unionData.bData[3]=0;
|
||||
if(::midiOutShortMsg(mhMIDIOutput,unionData.dwData))return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* getLeftVolume - Get left volume level of current device
|
||||
* @param none
|
||||
* @return unsigned short - volume level
|
||||
*/
|
||||
|
||||
WORD MIDIOutputDevice::getLeftVolume(void)const
|
||||
{
|
||||
DWORD volumeLevel;
|
||||
|
||||
if(!hasDevice())return FALSE;
|
||||
if(!mMIDIOutDevCaps.hasVolume())return FALSE;
|
||||
#if WINVER>=0x0400
|
||||
if(::midiOutGetVolume(mhMIDIOutput,&volumeLevel))return FALSE;
|
||||
#else
|
||||
if(::midiOutGetVolume(mMIDIDeviceID,&volumeLevel))return FALSE;
|
||||
#endif
|
||||
return LOWORD(volumeLevel);
|
||||
}
|
||||
|
||||
/*
|
||||
* getRightVolume - Get right volume level of current device
|
||||
* @param none
|
||||
* @return unsigned short - volume level
|
||||
*/
|
||||
|
||||
WORD MIDIOutputDevice::getRightVolume(void)const
|
||||
{
|
||||
DWORD volumeLevel;
|
||||
|
||||
if(!hasDevice())return FALSE;
|
||||
if(!mMIDIOutDevCaps.hasVolume())return FALSE;
|
||||
#if WINVER>=0x0400
|
||||
if(::midiOutGetVolume(mhMIDIOutput,&volumeLevel))return FALSE;
|
||||
#else
|
||||
if(::midiOutGetVolume(mMIDIDeviceID,&volumeLevel))return FALSE;
|
||||
#endif
|
||||
if(!mMIDIOutDevCaps.hasLeftRightVolume())return LOWORD(volumeLevel);
|
||||
return HIWORD(volumeLevel);
|
||||
}
|
||||
|
||||
/*
|
||||
* setRightVolume - Set right volume level of current device
|
||||
* @param unsigned short volume level
|
||||
* @return bool true is success
|
||||
*/
|
||||
|
||||
bool MIDIOutputDevice::setRightVolume(WORD volumeRight)
|
||||
{
|
||||
DWORD volumeLevel;
|
||||
|
||||
if(!hasDevice())return false;
|
||||
if(!mMIDIOutDevCaps.hasVolume())return false;
|
||||
volumeLevel=volumeRight;
|
||||
if(mMIDIOutDevCaps.hasLeftRightVolume())
|
||||
{
|
||||
volumeLevel<<=8;
|
||||
volumeLevel+=getLeftVolume();
|
||||
}
|
||||
#if WINVER>=0x0400
|
||||
if(::midiOutSetVolume(mhMIDIOutput,volumeLevel))return false;
|
||||
#else
|
||||
if(::midiOutSetVolume(mMIDIDeviceID,volumeLevel))return false;
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* setLeftVolume - Set left volume level of current device
|
||||
* @param unsigned short volume level
|
||||
* @return bool true is success
|
||||
*/
|
||||
|
||||
bool MIDIOutputDevice::setLeftVolume(WORD volumeLeft)
|
||||
{
|
||||
DWORD volumeLevel(0L);
|
||||
|
||||
if(!hasDevice())return false;
|
||||
if(!mMIDIOutDevCaps.hasVolume())return false;
|
||||
if(mMIDIOutDevCaps.hasLeftRightVolume())
|
||||
{
|
||||
volumeLevel=getRightVolume();
|
||||
volumeLevel<<=8;
|
||||
}
|
||||
volumeLevel+=volumeLeft;
|
||||
#if WINVER>=0x0400
|
||||
if(::midiOutSetVolume(mhMIDIOutput,volumeLevel))return false;
|
||||
#else
|
||||
if(::midiOutSetVolume(mMIDIDeviceID,volumeLevel))return false;
|
||||
#endif
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user