private static bool OpenMidiIn() { int num_in = Midi.MidiInGetNumDevs(); int in_index = -1; for (int i = 0; i < num_in; i++) { string s = Midi.MidiInGetName(i); //if(s == "FLEX 5000" || s == "FLEX-5000 Midi" || s == "FlexRadio Flex-5000" || s == "FlexRadio Flex-5000 MIDI") if (s.IndexOf("FLEX") >= 0 || s.IndexOf("Flex") >= 0) { Debug.WriteLine("Midi In Device Name: " + s); in_index = i; break; } } if (in_index < 0) { //MessageBox.Show("Error opening Midi In device"); return(false); } callback = new Midi.MidiInCallback(InCallback); int result = Midi.MidiInOpen(ref midi_in_handle, in_index, callback, 0, Midi.CALLBACK_FUNCTION); if (result != 0) { StringBuilder error_text = new StringBuilder(64); Midi.MidiInGetErrorText(result, error_text, 64); Debug.WriteLine("MidiInOpen Error: " + error_text); //MessageBox.Show("Error opening Midi In device"); return(false); } for (int i = 0; i < 3; i++) { result = Midi.AddSysExBuffer(midi_in_handle); if (result != 0) { StringBuilder error_text = new StringBuilder(64); Midi.MidiInGetErrorText(result, error_text, 64); Debug.WriteLine("AddSysExBuffer Error: " + error_text); //MessageBox.Show("Error adding Midi In device SysEx Buffer"); return(false); } } result = Midi.MidiInStart(midi_in_handle); if (result != 0) { StringBuilder error_text = new StringBuilder(64); Midi.MidiInGetErrorText(result, error_text, 64); Debug.WriteLine("MidiInStart Error: " + error_text); //MessageBox.Show("Error starting Midi In device"); return(false); } return(true); }
private static int InCallback(int hMidiIn, int wMsg, int dwInstance, int dwParam1, int dwParam2) { //log.AddLine("Midi.InCallback("+hMidiIn.ToString("X")+", "+wMsg.ToString("X")+", "+dwInstance.ToString("X")+", "+dwParam1.ToString("X")+", "+dwParam2.ToString("X")+")"); #if (TIMING) lock (in_lock_obj) { if (wMsg == Midi.MIM_DATA && (Command)((byte)dwParam1) == Command.NoteOn && (Note)(byte)(dwParam1 >> 8) == Note.Dot) { FWCMidi.SendSetMessage(Opcode.RDAL_OP_WRITE_GPIO, 0x01, 0); FWCMidi.SendSetMessage(Opcode.RDAL_OP_WRITE_GPIO, 0x00, 0); } } #else lock (in_lock_obj) { switch (wMsg) { case Midi.MIM_DATA: Command cmd = (Command)((byte)dwParam1); byte byte1 = (byte)(dwParam1 >> 8); byte byte2 = (byte)(dwParam1 >> 16); switch (cmd) { case Command.NoteOn: switch ((Note)byte1) { case Note.Dot: //console.Keyer.FWCDot = true; CWSensorItem.InputType type = CWSensorItem.InputType.Dot; if (reverse_paddles) { type = CWSensorItem.InputType.Dash; } CWSensorItem item = new CWSensorItem(type, true); CWKeyer.SensorEnqueue(item); break; case Note.Dash: //console.Keyer.FWCDash = true; type = CWSensorItem.InputType.Dash; if (reverse_paddles) { type = CWSensorItem.InputType.Dot; } item = new CWSensorItem(type, true); CWKeyer.SensorEnqueue(item); break; case Note.MicDown: if (console == null) { return(1); } console.MicDown = true; break; case Note.MicUp: if (console == null) { return(1); } console.MicUp = true; break; case Note.MicFast: if (console == null) { return(1); } console.MicFast = !console.MicFast; break; } break; case Command.NoteOff: switch ((Note)byte1) { case Note.Dot: //console.Keyer.FWCDot = false; CWSensorItem.InputType type = CWSensorItem.InputType.Dot; if (reverse_paddles) { type = CWSensorItem.InputType.Dash; } CWSensorItem item = new CWSensorItem(type, false); CWKeyer.SensorEnqueue(item); break; case Note.Dash: //console.Keyer.FWCDash = false; type = CWSensorItem.InputType.Dash; if (reverse_paddles) { type = CWSensorItem.InputType.Dot; } item = new CWSensorItem(type, false); CWKeyer.SensorEnqueue(item); break; case Note.MicDown: if (console == null) { return(1); } console.MicDown = false; break; case Note.MicUp: if (console == null) { return(1); } console.MicUp = false; break; case Note.MicFast: break; } break; case Command.Controller: switch ((Controller)byte1) { case Controller.HoldPedal: if (console == null) { return(1); } console.FWCMicPTT = (byte2 > 63); break; case Controller.Hold2Pedal: if (console == null) { return(1); } console.FWCRCAPTT = (byte2 > 63); break; } break; case Command.Aftertouch0: case Command.Aftertouch1: int id = (ushort)((((byte)cmd - (byte)Command.Aftertouch0) << 2) + (byte1 >> 5)); int data = (int)(((byte1 & 0x1F) << 7) + byte2); if (midi_in_table.ContainsKey(id)) { midi_in_table.Remove(id); } midi_in_table.Add(id, data); break; } break; case Midi.MIM_LONGDATA: if (!resetting && midi_in_handle != 0) // in case device closes, don't send anymore buffers { int result = Midi.AddSysExBuffer(midi_in_handle); if (result != 0) { StringBuilder error_text = new StringBuilder(64); Midi.MidiInGetErrorText(result, error_text, 64); Debug.WriteLine("AddSysExBuffer Error: " + error_text); } } IntPtr headerPtr = new IntPtr(dwParam1); Midi.MidiHeader header = (Midi.MidiHeader)Marshal.PtrToStructure(headerPtr, typeof(Midi.MidiHeader)); byte[] temp = new byte[header.bytesRecorded]; for (int i = 0; i < header.bytesRecorded; i++) { temp[i] = Marshal.ReadByte(header.data, i); } if (temp.Length > 5) { byte[] temp2 = new byte[temp.Length - 5]; for (int i = 0; i < temp.Length - 5; i++) { temp2[i] = temp[i + 4]; } byte[] buf = new byte[temp2.Length / 2]; DecodeBytes(buf, temp2); if (midi_in_table.ContainsKey(BitConverter.ToUInt16(buf, 0))) { midi_in_table.Remove(BitConverter.ToUInt16(buf, 0)); } midi_in_table.Add(BitConverter.ToUInt16(buf, 0), buf); } /*for(int i=0; i<header.bytesRecorded; i++) * Debug.Write(buf[i].ToString("X")+" "); * Debug.WriteLine("");*/ if (midi_in_handle != 0) { Midi.ReleaseBuffer(midi_in_handle, headerPtr); } break; } } #endif return(0); }
private static int InCallback(int hMidiIn, int wMsg, int dwInstance, int dwParam1, int dwParam2) { lock (in_lock_obj) { switch (wMsg) { case Midi.MIM_DATA: Command cmd = (Command)((byte)dwParam1); byte byte1 = (byte)(dwParam1 >> 8); byte byte2 = (byte)(dwParam1 >> 16); switch (cmd) { case Command.NoteOn: switch ((Note)byte1) { case Note.Dot: console.Keyer.FWCDot = true; //FWC.SetMOX(true); break; case Note.Dash: console.Keyer.FWCDash = true; //FWC.SetMOX(true); break; case Note.MicDown: break; case Note.MicUp: break; case Note.MicFast: break; } break; case Command.NoteOff: switch ((Note)byte1) { case Note.Dot: console.Keyer.FWCDot = false; //FWC.SetMOX(false); break; case Note.Dash: console.Keyer.FWCDash = false; //FWC.SetMOX(false); break; case Note.MicDown: break; case Note.MicUp: break; case Note.MicFast: break; } break; case Command.Controller: switch ((Controller)byte1) { case Controller.HoldPedal: console.FWCMicPTT = (byte2 > 63); break; case Controller.Hold2Pedal: console.FWCRCAPTT = (byte2 > 63); break; } break; case Command.Aftertouch0: case Command.Aftertouch1: int id = (ushort)((((byte)cmd - (byte)Command.Aftertouch0) << 2) + (byte1 >> 5)); int data = (int)(((byte1 & 0x1F) << 7) + byte2); if (midi_in_table.ContainsKey(id)) { midi_in_table.Remove(id); } midi_in_table.Add(id, data); break; } break; case Midi.MIM_LONGDATA: if (!resetting && midi_in_handle != 0) // in case device closes, don't send anymore buffers { int result = Midi.AddSysExBuffer(midi_in_handle); if (result != 0) { StringBuilder error_text = new StringBuilder(64); Midi.MidiInGetErrorText(result, error_text, 64); Debug.WriteLine("AddSysExBuffer Error: " + error_text); } } IntPtr headerPtr = new IntPtr(dwParam1); Midi.MidiHeader header = (Midi.MidiHeader)Marshal.PtrToStructure(headerPtr, typeof(Midi.MidiHeader)); byte[] temp = new byte[header.bytesRecorded]; for (int i = 0; i < header.bytesRecorded; i++) { temp[i] = Marshal.ReadByte(header.data, i); } if (temp.Length > 5) { byte[] temp2 = new byte[temp.Length - 5]; for (int i = 0; i < temp.Length - 5; i++) { temp2[i] = temp[i + 4]; } byte[] buf = new byte[temp2.Length / 2]; DecodeBytes(buf, temp2); if (midi_in_table.ContainsKey(BitConverter.ToUInt16(buf, 0))) { midi_in_table.Remove(BitConverter.ToUInt16(buf, 0)); } midi_in_table.Add(BitConverter.ToUInt16(buf, 0), buf); } /*for(int i=0; i<header.bytesRecorded; i++) * Debug.Write(buf[i].ToString("X")+" "); * Debug.WriteLine("");*/ if (midi_in_handle != 0) { Midi.ReleaseBuffer(midi_in_handle, headerPtr); } break; } } return(0); }