Esempio n. 1
0
        public void Start()
        {
            if (IsDisposed)
            {
                throw new ObjectDisposedException("InputDevice");
            }

            if (!_recording)
            {
                lock (_lockObject)
                {
                    var       result          = 0;
                    const int bufcount        = 4;
                    var       currentBufCount = 0;
                    while (result == (int)EDeviceException.MmsyserrNoerror && currentBufCount < bufcount)
                    {
                        result = AddSysExBuffer();
                        currentBufCount++;
                    }
                    if (result == (int)EDeviceException.MmsyserrNoerror)
                    {
                        result = WindowsMultimediaDevice.midiInStart(Handle);
                    }
                    if (result != (int)EDeviceException.MmsyserrNoerror)
                    {
                        throw new InputDeviceException(result);
                    }
                    _recording = true;
                }
            }
        }
Esempio n. 2
0
 private void Send(byte[] message)
 {
     _runningStatus = 0;
     if (IsDisposed)
     {
         throw new ObjectDisposedException(GetType().Name);
     }
     lock (_lockObject)
     {
         _headerBuilder.InitializeBuffer(message);
         _headerBuilder.Build();
         int result = WindowsMultimediaDevice.midiOutPrepareHeader(Handle, _headerBuilder.Result, SizeOfMidiHeader);
         if (result == (int)EDeviceException.MmsyserrNoerror)
         {
             _bufferCount++;
             result = WindowsMultimediaDevice.midiOutLongMsg(Handle, _headerBuilder.Result, SizeOfMidiHeader);
             if (result != (int)EDeviceException.MmsyserrNoerror)
             {
                 WindowsMultimediaDevice.midiOutUnprepareHeader(Handle, _headerBuilder.Result, SizeOfMidiHeader);
                 _bufferCount--;
                 _headerBuilder.Destroy();
                 OnError(new ErrorEventArgs(new OutputDeviceException(result)));
             }
         }
         else
         {
             _headerBuilder.Destroy();
             OnError(new ErrorEventArgs(new OutputDeviceException(result)));
         }
     }
 }
Esempio n. 3
0
 private void Send(int message)
 {
     lock (_lockObject)
     {
         var result = WindowsMultimediaDevice.midiOutShortMsg(Handle, message);
         if (result != (int)EDeviceException.MmsyserrNoerror)
         {
             OnError(new ErrorEventArgs(new OutputDeviceException(result)));
         }
     }
 }
Esempio n. 4
0
        private void ReleaseBuffer(IntPtr headerPtr)
        {
            int result = WindowsMultimediaDevice.midiInUnprepareHeader(Handle, headerPtr, SizeOfMidiHeader);

            if (result != (int)EDeviceException.MmsyserrNoerror)
            {
                OnError(new ErrorEventArgs(new InputDeviceException(result)));
            }
            _headerBuilder.Destroy(headerPtr);
            _bufferCount--;
        }
Esempio n. 5
0
        public OutputDevice(int deviceId) : base(deviceId)
        {
            _lockObject           = new object();
            _bufferCount          = 0;
            _headerBuilder        = new MidiHeaderBuilder();
            Handle                = 0;
            _runningStatusEnabled = false;
            _runningStatus        = 0;
            _midiOutProcIstance   = HandleMessage;
            var result = WindowsMultimediaDevice.midiOutOpen(ref Handle, deviceId, _midiOutProcIstance, 0, 0x30000);

            if (result != (int)EDeviceException.MmsyserrNoerror)
            {
                OnError(new ErrorEventArgs(new OutputDeviceException(result)));
            }
        }
Esempio n. 6
0
        public InputDevice(int deviceId) : base(deviceId)
        {
            _bufferCount     = 0;
            _lockObject      = new object();
            _recording       = false;
            _headerBuilder   = new MidiHeaderBuilder();
            Handle           = 0;
            _resetting       = false;
            _sysExBufferSize = 0x1000;
            _midiInProc      = HandleMessage;
            var result = WindowsMultimediaDevice.midiInOpen(ref Handle, deviceId, _midiInProc, 0, 0x30000);

            if (result != (int)EDeviceException.MmsyserrNoerror)
            {
                throw new InputDeviceException(result);
            }
        }
Esempio n. 7
0
 public void Stop()
 {
     if (IsDisposed)
     {
         throw new ObjectDisposedException("InputDevice");
     }
     if (_recording)
     {
         lock (_lockObject)
         {
             int result = WindowsMultimediaDevice.midiInStop(Handle);
             if (result != 0)
             {
                 throw new InputDeviceException(result);
             }
             _recording = false;
         }
     }
 }
Esempio n. 8
0
 public override void Reset()
 {
     if (IsDisposed)
     {
         throw new ObjectDisposedException(GetType().Name);
     }
     _runningStatus = 0;
     lock (_lockObject)
     {
         int result = WindowsMultimediaDevice.midiOutReset(Handle);
         if (result != (int)EDeviceException.MmsyserrNoerror)
         {
             OnError(new ErrorEventArgs(new OutputDeviceException(result)));
         }
         while (_bufferCount > 0)
         {
             Monitor.Wait(_lockObject);
         }
     }
 }
Esempio n. 9
0
 protected override void Dispose(bool disposing)
 {
     if (disposing)
     {
         lock (_lockObject)
         {
             Reset();
             int result = WindowsMultimediaDevice.midiOutClose(Handle);
             if (result != (int)EDeviceException.MmsyserrNoerror)
             {
                 OnError(new ErrorEventArgs(new OutputDeviceException(result)));
             }
         }
     }
     else
     {
         WindowsMultimediaDevice.midiOutReset(Handle);
         WindowsMultimediaDevice.midiOutClose(Handle);
     }
     base.Dispose(disposing);
 }
Esempio n. 10
0
 protected override void Dispose(bool disposing)
 {
     if (disposing)
     {
         lock (_lockObject)
         {
             Reset();
             var result = WindowsMultimediaDevice.midiInClose(Handle);
             if (result != 0)
             {
                 throw new InputDeviceException(result);
             }
         }
     }
     else
     {
         WindowsMultimediaDevice.midiInReset(Handle);
         WindowsMultimediaDevice.midiInClose(Handle);
     }
     base.Dispose(disposing);
 }
Esempio n. 11
0
        public int AddSysExBuffer()
        {
            _headerBuilder.BufferLength = _sysExBufferSize;
            _headerBuilder.Build();
            var headerPtr = _headerBuilder.Result;
            var result    = WindowsMultimediaDevice.midiInPrepareHeader(Handle, headerPtr, SizeOfMidiHeader);

            if (result == 0)
            {
                _bufferCount++;
                result = WindowsMultimediaDevice.midiInAddBuffer(Handle, headerPtr, SizeOfMidiHeader);
                if (result != 0)
                {
                    WindowsMultimediaDevice.midiInUnprepareHeader(Handle, headerPtr, SizeOfMidiHeader);
                    _bufferCount--;
                    _headerBuilder.Destroy();
                }
                return(result);
            }
            _headerBuilder.Destroy();
            return(result);
        }
Esempio n. 12
0
 public override void Reset()
 {
     if (IsDisposed)
     {
         throw new ObjectDisposedException("InputDevice");
     }
     lock (_lockObject)
     {
         _resetting = true;
         var result = WindowsMultimediaDevice.midiInReset(Handle);
         if (result == (int)EDeviceException.MmsyserrNoerror)
         {
             _recording = false;
             _resetting = false;
         }
         else
         {
             _resetting = false;
             throw new InputDeviceException(result);
         }
     }
 }