public MidiDevice() { mCDMusic = new CDirectMusic(); mCDMusic.Initialize(); // Initialize ports given the DirectMusic manager object try { // Initialize in ports mCInPort = new CInputPort(); ((CMidiPort)mCInPort).Initialize(mCDMusic); CMidiPort midiIn = (CMidiPort)mCInPort; uint num = midiIn.GetNumPorts(); for (uint i = 1; i <= num; i++) { INFOPORT info; midiIn.GetPortInfo(i, out info); mMidiInPorts.Add(new MidiPort((int)i, info.szPortDescription + " (DirectSound)", true)); } // Initialize out ports mCOutPort = new COutputPort(); ((CMidiPort)mCOutPort).Initialize(mCDMusic); CMidiPort midiOut = (CMidiPort)mCOutPort; num = midiOut.GetNumPorts(); for (uint i = 1; i <= num; i++) { INFOPORT info; midiOut.GetPortInfo(i, out info); mMidiOutPorts.Add(new MidiPort((int)i, info.szPortDescription + " (DirectSound)", true)); } } catch (CDMusicException DMEx) { MessageBox.Show(DMEx.GetErrorDescription() + DMEx.ToString()); } // get Kernel32 midi in devices for (int i = 0; i < Kernel32Midi.InputDevice.InstalledDevices.Count; i++) { mMidiInPorts.Add(new MidiPort((int)i, Kernel32Midi.InputDevice.InstalledDevices[i].Name + " (Kernel32)", false)); } // get Kernel32 midi out device for (int i = 0; i < Kernel32Midi.OutputDevice.InstalledDevices.Count; i++) { mMidiOutPorts.Add(new MidiPort((int)i, Kernel32Midi.OutputDevice.InstalledDevices[i].Name + " (Kernel32)", false)); } }
public void OpenInPort(int portNr) { if (!mOpenInPorts.Contains(portNr)) { if (portNr < mMidiInPorts.Count) { mOpenInPorts.Add(portNr); MidiPort midiPort = mMidiInPorts[portNr]; if (midiPort.IsDirectMidi) { try { INFOPORT info; CMidiPort midiIn = (CMidiPort)mCInPort; midiIn.GetPortInfo((uint)midiPort.Index, out info); bool hasInPort = (HR_DMUS)midiIn.GetPortInfo((uint)midiPort.Index, out info) == HR_DMUS.HR_S_OK; if (!hasInPort) { throw new Exception("Midi-in port apparently does not support midi-in. Right..."); } else { mCInPort.ActivatePort(info, SYSTEM_EXCLUSIVE_MEM); mCInPort.SetReceiver(this); // Activates input MIDI message handling mCInPort.ActivateNotification(); } } catch (CDMusicException DMEx) { MessageBox.Show(DMEx.GetErrorDescription() + DMEx.ToString()); } } else { mMidiInputDevice = Kernel32Midi.InputDevice.InstalledDevices[midiPort.Index]; if (mMidiInputDevice != null) { mMidiInputDevice.Open(); mMidiInputDevice.StartReceiving(null); mMidiInputDevice.RawMessage += mMidiInputDevice_RawMessage; } } } } }
public void OpenOutPort(int portNr) { if (!mOpenOutPorts.Contains(portNr)) { if (portNr < mMidiOutPorts.Count) { mOpenOutPorts.Add(portNr); MidiPort midiPort = mMidiOutPorts[portNr]; if (midiPort.IsDirectMidi) { try { INFOPORT info; CMidiPort midiOut = (CMidiPort)mCOutPort; midiOut.GetPortInfo((uint)midiPort.Index, out info); bool hasOutPort = (HR_DMUS)midiOut.GetPortInfo((uint)midiPort.Index, out info) == HR_DMUS.HR_S_OK; if (!hasOutPort) { throw new Exception("Midi-out port apparently does not support midi-out. Right..."); } else { mCOutPort.ActivatePort(info, SYSTEM_EXCLUSIVE_MEM); } } catch (CDMusicException DMEx) { MessageBox.Show(DMEx.GetErrorDescription() + DMEx.ToString()); } } else { mMidiOutputDevice = Kernel32Midi.OutputDevice.InstalledDevices[midiPort.Index]; if (mMidiOutputDevice != null) { mMidiOutputDevice.Open(); } } } } }