private static void Receive() { if (!NetworkAvailable) return; SocketAsyncEventArgs eventArgs = GetEventArgs(_outEndpoint, new Byte[3]); eventArgs.Completed += new EventHandler<SocketAsyncEventArgs>(delegate(object s, SocketAsyncEventArgs e) { if (e.SocketError == SocketError.Success && e.BytesTransferred == 3) { Debug.WriteLine("DSMI: Midi Message Received {0} {1} {2}", e.Buffer[0], e.Buffer[1], e.Buffer[2]); byte message = e.Buffer[0]; byte data1 = e.Buffer[1]; byte data2 = e.Buffer[2]; byte channel = (byte)(message & 0x0F); MidiMessage midiMessage; if ((message & MidiMessageType.MIDI_CC) == MidiMessageType.MIDI_CC) { midiMessage = new MidiControlChange(channel, data1, data2); if (ControlChangeReceivedHandler != null) ControlChangeReceivedHandler(new MidiMessageReceivedEventArgs(midiMessage)); } else if ((message & MidiMessageType.MIDI_PC) == MidiMessageType.MIDI_PC) { midiMessage = new MidiProgramChange(channel, data1); if (ProgramChangeReceivedHandler != null) ProgramChangeReceivedHandler(new MidiMessageReceivedEventArgs(midiMessage)); } else if ((message & MidiMessageType.NOTE_ON) == MidiMessageType.NOTE_ON) { midiMessage = new MidiNoteOn(channel, data1, data2); if (NoteOnReceivedHandler != null) NoteOnReceivedHandler(new MidiMessageReceivedEventArgs(midiMessage)); } else if ((message & MidiMessageType.NOTE_OFF) == MidiMessageType.NOTE_OFF) { midiMessage = new MidiNoteOff(channel, data1, data2); if (NoteOffReceivedHandler != null) NoteOffReceivedHandler(new MidiMessageReceivedEventArgs(midiMessage)); } else if ((message & MidiMessageType.MIDI_AT) == MidiMessageType.MIDI_AT) { midiMessage = new MidiAftertouch(channel, data1, data2); if (AftertouchReceivedHandler != null) AftertouchReceivedHandler(new MidiMessageReceivedEventArgs(midiMessage)); } else { midiMessage = new MidiMessage(message, data1, data2); } if (MessageReceivedHandler != null) MessageReceivedHandler(new MidiMessageReceivedEventArgs(midiMessage)); } Receive(); }); try { _inSocket.ReceiveFromAsync(eventArgs); } catch { _inSocket.Bind(_inEndpoint); Receive(); } }
private static void Receive() { if (!NetworkAvailable) { return; } SocketAsyncEventArgs eventArgs = GetEventArgs(_outEndpoint, new Byte[3]); eventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(delegate(object s, SocketAsyncEventArgs e) { if (e.SocketError == SocketError.Success && e.BytesTransferred == 3) { Debug.WriteLine("DSMI: Midi Message Received {0} {1} {2}", e.Buffer[0], e.Buffer[1], e.Buffer[2]); byte message = e.Buffer[0]; byte data1 = e.Buffer[1]; byte data2 = e.Buffer[2]; byte channel = (byte)(message & 0x0F); MidiMessage midiMessage; if ((message & MidiMessageType.MIDI_CC) == MidiMessageType.MIDI_CC) { midiMessage = new MidiControlChange(channel, data1, data2); if (ControlChangeReceivedHandler != null) { ControlChangeReceivedHandler(new MidiMessageReceivedEventArgs(midiMessage)); } } else if ((message & MidiMessageType.MIDI_PC) == MidiMessageType.MIDI_PC) { midiMessage = new MidiProgramChange(channel, data1); if (ProgramChangeReceivedHandler != null) { ProgramChangeReceivedHandler(new MidiMessageReceivedEventArgs(midiMessage)); } } else if ((message & MidiMessageType.NOTE_ON) == MidiMessageType.NOTE_ON) { midiMessage = new MidiNoteOn(channel, data1, data2); if (NoteOnReceivedHandler != null) { NoteOnReceivedHandler(new MidiMessageReceivedEventArgs(midiMessage)); } } else if ((message & MidiMessageType.NOTE_OFF) == MidiMessageType.NOTE_OFF) { midiMessage = new MidiNoteOff(channel, data1, data2); if (NoteOffReceivedHandler != null) { NoteOffReceivedHandler(new MidiMessageReceivedEventArgs(midiMessage)); } } else if ((message & MidiMessageType.MIDI_AT) == MidiMessageType.MIDI_AT) { midiMessage = new MidiAftertouch(channel, data1, data2); if (AftertouchReceivedHandler != null) { AftertouchReceivedHandler(new MidiMessageReceivedEventArgs(midiMessage)); } } else { midiMessage = new MidiMessage(message, data1, data2); } if (MessageReceivedHandler != null) { MessageReceivedHandler(new MidiMessageReceivedEventArgs(midiMessage)); } } Receive(); }); try { _inSocket.ReceiveFromAsync(eventArgs); } catch { _inSocket.Bind(_inEndpoint); Receive(); } }