#include #include #include #include /* * 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 deviceNames; closeDevice(); if(deviceName=="MIDIMAPPER") { mMIDIDeviceID=MIDIMAPPER; return openDevice(); } if(!getDeviceNames(deviceNames))return false; for(int index=0;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 & that will be populated with the device names * @return bool true if success. */ bool MIDIOutputDevice::getDeviceNames(Block &deviceNames) { UINT numDevs; MIDIOutCaps midiOutCaps; deviceNames.remove(); numDevs=getNumDevs(); for(int index=0;index=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; }