Exemplo n.º 1
0
        static void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            if (midiIn != null) return;

            while (UtilitySerial.BytesToRead > 0)
            {
                byte Start = (byte)UtilitySerial.ReadByte();

                if (Start == 0xF0)//SysEx
                {
                    byte ID = (byte)UtilitySerial.ReadByte();
                    byte Cmd = (byte)UtilitySerial.ReadByte();

                    MIDIDataReceivedEventArgs eMIDI = new MIDIDataReceivedEventArgs();
                    eMIDI.Cmd = Cmd;

                    if (Cmd == 0x6E)//LOG
                    {
                        byte Size = (byte)UtilitySerial.ReadByte();
                        byte[] Message = new byte[Size];
                        for (int i = 0; i < Size; i++)
                            Message[i] = (byte)UtilitySerial.ReadByte();
                        if (Message.Length < 14) return;
                        Log log = new Log();
                        log.Time = (uint)(Message[0] | (Message[1] << 8) | (Message[2] << 16) | (Message[3] << 24));
                        log.Sensor = Message[4];
                        log.N = Message[5] | (Message[6] << 8);
                        log.Reading = Message[7] | (Message[8] << 8);
                        log.Y0 = Message[9] | (Message[10] << 8);
                        log.MaxReading = Message[11] | (Message[12] << 8);
                        log.State = Message[13];
                        eMIDI.Log = log;
                    }
                    else if (Cmd == 0x6D)//PROFILING
                    {
                        byte Size = (byte)UtilitySerial.ReadByte();
                        byte[] Message = new byte[Size];
                        for (int i = 0; i < Size; i++)
                            Message[i] = (byte)UtilitySerial.ReadByte();
                        uint Time = (uint)(Message[0] | (Message[1] << 8) | (Message[2] << 16) | (Message[3] << 24));
                        uint N = (uint)(Message[4] | (Message[5] << 8) | (Message[6] << 16) | (Message[7] << 24));

                        System.Windows.Forms.MessageBox.Show("Time:" + Time.ToString() + " / N:" + N.ToString() + " = "+ (Time/N).ToString(),"Profiling");
                    }
                    else
                    {
                        eMIDI.Data1 = (byte)UtilitySerial.ReadByte();
                        eMIDI.Data2 = (byte)UtilitySerial.ReadByte();
                        eMIDI.Data3 = (byte)UtilitySerial.ReadByte();

                    }
                    byte End = (byte)UtilitySerial.ReadByte();

            #if DEBUG
                    if (Cmd < 128 && eMIDI.Data1 < 128 && eMIDI.Data2 < 128 && eMIDI.Data3 < 128)
                        System.Diagnostics.Debug.WriteLine("<- 0x" + Cmd.ToString("X2") + " 0x" + eMIDI.Data1.ToString("X2") + " 0x" + eMIDI.Data2.ToString("X2") + " 0x" + eMIDI.Data3.ToString("X2"));
                    else
                        System.Diagnostics.Debug.WriteLine("<- ATTENZIONE:0x" + Cmd.ToString("X2") + " 0x" + eMIDI.Data1.ToString("X2") + " 0x" + eMIDI.Data2.ToString("X2") + " 0x" + eMIDI.Data3.ToString("X2"));
            #endif
                    if (MIDIReceived_SysEx != null && ID == 0x77 && End == 0xF7)
                        MIDIReceived_SysEx(eMIDI);
                }
                else if ((Start & 0xF0)== 0x90)//NoteOn
                {
                    MIDIDataReceivedEventArgs eMIDI = new MIDIDataReceivedEventArgs();
                    eMIDI.Cmd = Start;
                    eMIDI.Data1 = (byte)UtilitySerial.ReadByte();
                    eMIDI.Data2 = (byte)UtilitySerial.ReadByte();

                    if (MIDIReceived_NoteOn != null)
                        MIDIReceived_NoteOn(eMIDI);

                    UtilityAudio.MIDI_NoteOn(eMIDI.Data1, eMIDI.Data2);
                }
                else if (Start == 0xB9)//CC
                {
                    MIDIDataReceivedEventArgs eMIDI = new MIDIDataReceivedEventArgs();
                    eMIDI.Cmd = 0xB9;
                    eMIDI.Data1 = (byte)UtilitySerial.ReadByte();
                    eMIDI.Data2 = (byte)UtilitySerial.ReadByte();

                    if (MIDIReceived_CC != null)
                        MIDIReceived_CC(eMIDI);

                    UtilityAudio.MIDI_CC(eMIDI.Data1, eMIDI.Data2);
                }
                //else continue;
            }
        }
Exemplo n.º 2
0
 static void midiIn_MessageReceived(object sender, MidiInMessageEventArgs e)
 {
     if (e.MidiEvent is NAudio.Midi.NoteOnEvent && MIDIReceived_NoteOn != null)
     {
         MIDIDataReceivedEventArgs eMIDI = new MIDIDataReceivedEventArgs();
         eMIDI.Cmd = 0x99;
         eMIDI.Data1 = (byte)((NAudio.Midi.NoteOnEvent)e.MidiEvent).NoteNumber;
         eMIDI.Data2 = (byte)((NAudio.Midi.NoteOnEvent)e.MidiEvent).Velocity;
         MIDIReceived_NoteOn(eMIDI);
         UtilityAudio.MIDI_NoteOn(eMIDI.Data1, eMIDI.Data2);
     }
     else if (e.MidiEvent is NAudio.Midi.ControlChangeEvent && MIDIReceived_CC != null)
     {
         MIDIDataReceivedEventArgs eMIDI = new MIDIDataReceivedEventArgs();
         eMIDI.Cmd = 0xB9;
         eMIDI.Data1 = (byte)((NAudio.Midi.ControlChangeEvent)e.MidiEvent).Controller;
         eMIDI.Data2 = (byte)((NAudio.Midi.ControlChangeEvent)e.MidiEvent).ControllerValue;
         MIDIReceived_CC(eMIDI);
         UtilityAudio.MIDI_CC(eMIDI.Data1, eMIDI.Data2);
     }
     else if (e.MidiEvent is NAudio.Midi.SysexEvent && MIDIReceived_SysEx != null)
     {
         MIDIDataReceivedEventArgs eMIDI = new MIDIDataReceivedEventArgs();
         //e.RawMessage;//Meglio leggere direttamente questo...
         /*if(...)
         {
             eMIDI.Cmd = 0xB9;
             eMIDI.Data1 = (byte)((NAudio.Midi.SysexEvent)e.MidiEvent).;
             eMIDI.Data2 = (byte)((NAudio.Midi.SysexEvent)e.MidiEvent).ControllerValue;
             MIDIReceived_SysEx(eMIDI);
         }*/
     }
 }