/// <summary> /// Gets all MIDI resources needed to use the selected device. /// </summary> private void Acquire(MidiDevice device) { lock (_acquireLock) { if (device != null) { // setup the input device if (device.CanRead) { // grab the device id by name uint deviceId = 0; if (_deviceIdByName.TryGetValue("IN_" + device.ID, out deviceId) == false) { throw new Exception("Could not find MIDI input device " + device.ID); } // start streaming the input WinMM.CheckReturnCode(Win32API.midiInOpen(out _inputHandle, (UIntPtr)deviceId, _callback, (UIntPtr)0)); WinMM.CheckReturnCode(Win32API.midiInStart(_inputHandle)); _inputOpen = true; } else { _inputOpen = false; } // setup the output device if (device.CanWrite) { // grab the device id by name uint deviceId = 0; if (_deviceIdByName.TryGetValue("OUT_" + device.ID, out deviceId) == false) { throw new Exception("Could not find MIDI output device " + device.ID); } // setup the device WinMM.CheckReturnCode(Win32API.midiOutOpen(out _outputHandle, (UIntPtr)deviceId, null, (UIntPtr)0)); _outputOpen = true; } else { _outputOpen = false; } } } }
/// <summary> /// Releases all currently used MIDI resources. /// </summary> private void Release() { lock (_acquireLock) { if (_inputOpen) { WinMM.CheckReturnCode(Win32API.midiInStop(_inputHandle)); WinMM.CheckReturnCode(Win32API.midiInClose(_inputHandle)); _inputOpen = false; } if (_outputOpen) { WinMM.CheckReturnCode(Win32API.midiOutReset(_outputHandle));// TODO: not sure if we should WinMM.CheckReturnCode(Win32API.midiOutClose(_outputHandle)); _outputOpen = false; } } }