/// <summary> /// Closes the MIDI input device /// </summary> public override void Close() { if (MidiInputDeviceState.Closed != _state) { if (MidiInputDeviceState.Started == _state) { Stop(); } // flush any pending events Reset(); var sz = Marshal.SizeOf(typeof(MIDIHDR)); var ptr = _inHeader.lpData; // in case the header is being used: if (0 != _inHeader.dwFlags && MHDR_PREPARED != (_inHeader.dwFlags & MHDR_PREPARED)) { while ((_inHeader.dwFlags & MHDR_DONE) != MHDR_DONE) { Thread.Sleep(1); } } _CheckInResult(midiInUnprepareHeader(_handle, ref _inHeader, sz)); _CheckInResult(midiInClose(_handle)); Marshal.FreeHGlobal(ptr); _state = MidiInputDeviceState.Closed; Interlocked.Exchange(ref _timeBase, 24); Interlocked.Exchange(ref _microTempo, 500000); } }
/// <summary> /// Stops the MIDI input device /// </summary> public void Stop() { if (IntPtr.Zero == _handle) { throw new InvalidOperationException("The device is closed."); } _CheckInResult(midiInStop(_handle)); _state = MidiInputDeviceState.Stopped; }
/// <summary> /// Opens the MIDI input device /// </summary> public override void Open() { Close(); _CheckInResult(midiInOpen(out _handle, _index, _callback, 0, CALLBACK_FUNCTION)); var sz = Marshal.SizeOf(typeof(MIDIHDR)); _inHeader.dwBufferLength = _inHeader.dwBytesRecorded = 65536u; _inHeader.lpData = _buffer = Marshal.AllocHGlobal(65536); _CheckInResult(midiInPrepareHeader(_handle, ref _inHeader, sz)); _CheckInResult(midiInAddBuffer(_handle, ref _inHeader, sz)); _state = MidiInputDeviceState.Stopped; }
/// <summary> /// Closes the MIDI input device /// </summary> public override void Close() { if (MidiInputDeviceState.Closed != _state) { if (MidiInputDeviceState.Started == _state) { Stop(); } // flush any pending events Reset(); var sz = Marshal.SizeOf(typeof(MIDIHDR)); var ptr = _inHeader.lpData; _CheckInResult(midiInUnprepareHeader(_handle, ref _inHeader, sz)); _CheckInResult(midiInClose(_handle)); Marshal.FreeHGlobal(ptr); _state = MidiInputDeviceState.Closed; _timeBase = 96; _microTempo = 500000; } }
internal MidiInputDevice(int deviceIndex) { if (0 > deviceIndex) { throw new ArgumentOutOfRangeException("deviceIndex"); } _handle = IntPtr.Zero; _callback = new MidiInProc(_MidiInProc); _index = deviceIndex; _state = MidiInputDeviceState.Closed; _recordingLastTimestamp = 0L; _recordingPos = 0; _recordingTrack0 = null; _recordingSequence = null; _microTempo = 500000; // 120BPM _timeBase = 96; _timingTimestamp = 0L; _tempoSynchMininumTempo = 50d; _tempoSynchEnabled = 0; // false _tempoSyncFrequency = 0L; _tempoSyncTimestamp = 0L; _CheckInResult(midiInGetDevCaps(deviceIndex, ref _caps, Marshal.SizeOf(typeof(MIDIINCAPS)))); }