// Closes the MIDI input device void CMIDIInDevice::Close() { // If the device is recording, stop recording before closing the // device if(m_State == RECORDING) { StopRecording(); } // If the device is opened... if(m_State == OPENED) { // Close the device MMRESULT Result = ::midiInClose(m_DevHandle); // If a failure occurred, throw exception if(Result != MMSYSERR_NOERROR) { throw CMIDIInException(Result); } // Change state m_State = CLOSED; } }
// Add system exclusive buffer to queue void CMIDIInDevice::CMIDIInHeader::AddSysExBuffer() { MMRESULT Result = ::midiInAddBuffer(m_DevHandle, &m_MIDIHdr, sizeof m_MIDIHdr); // If an error occurred, throw exception if(Result != MMSYSERR_NOERROR) { throw CMIDIInException(Result); } }
// Gets the capabilities of a particular MIDI input device void CMIDIInDevice::GetDevCaps(UINT DeviceId, MIDIINCAPS &Caps) { MMRESULT Result = ::midiInGetDevCaps(DeviceId, &Caps, sizeof Caps); // If we are not able to retrieve device capabilities, throw // exception if(Result != MMSYSERR_NOERROR) { throw CMIDIInException(Result); } }
// Gets Id for this device UINT CMIDIInDevice::GetDevID() const { UINT DeviceID; MMRESULT Result = ::midiInGetID(m_DevHandle, &DeviceID); if(Result != MMSYSERR_NOERROR) { throw CMIDIInException(Result); } return DeviceID; }
// Starts the recording process void CMIDIInDevice::StartRecording(ThreadPriority RecordingThreadPriority/* = MIDI_THREAD_PRIORITY_NORMAL*/) { static const int WinThreadPriorities[5] = { THREAD_PRIORITY_LOWEST, THREAD_PRIORITY_BELOW_NORMAL, THREAD_PRIORITY_NORMAL, THREAD_PRIORITY_ABOVE_NORMAL, THREAD_PRIORITY_HIGHEST }; // Only begin recording if the MIDI input device has been opened if(m_State == OPENED) { // Change state m_State = RECORDING; //m_Thread = ::AfxBeginThread((AFX_THREADPROC)HeaderProc, this); if (!m_hThread) { m_hThread = CreateThread(NULL, 0, &HeaderProc, (void*)this, 0, &m_threadID); if (RecordingThreadPriority >= 0 && RecordingThreadPriority < (signed)sizeof(WinThreadPriorities) / (signed)sizeof(int)) SetThreadPriority(m_hThread, WinThreadPriorities[RecordingThreadPriority]); } // Start recording MMRESULT Result = ::midiInStart(m_DevHandle); // If recording attempt failed... if(Result != MMSYSERR_NOERROR) { // Revert back to opened state m_State = OPENED; // Signal the worker thread to finish ::SetEvent(m_Event); ::WaitForSingleObject(m_hThread, INFINITE); // Throw exception throw CMIDIInException(Result); } } }
// Constructor CMIDIInDevice::CMIDIInHeader::CMIDIInHeader(HMIDIIN DevHandle, LPSTR Buffer, DWORD BufferLength) : m_DevHandle(DevHandle) { // Initialize header m_MIDIHdr.lpData = Buffer; m_MIDIHdr.dwBufferLength = BufferLength; m_MIDIHdr.dwFlags = 0; // Prepare header MMRESULT Result = ::midiInPrepareHeader(DevHandle, &m_MIDIHdr, sizeof m_MIDIHdr); // If an error occurred, throw exception if(Result != MMSYSERR_NOERROR) { throw CMIDIInException(Result); } }
// Opens the MIDI input device void CMIDIInDevice::Open(UINT DeviceId) { // Makes sure the previous device, if any, is closed before // opening another one Close(); // Open MIDI input device MMRESULT Result = ::midiInOpen(&m_DevHandle, DeviceId, reinterpret_cast<DWORD>(MidiInProc), reinterpret_cast<DWORD>(this), CALLBACK_FUNCTION); // If we are able to open the device, change state if(Result == MMSYSERR_NOERROR) { m_State = OPENED; } // Else opening failed, throw exception else { throw CMIDIInException(Result); } }