//================================================================= // midi.cs //================================================================= // PowerSDR is a C# implementation of a Software Defined Radio. // Copyright (C) 2004-2008 FlexRadio Systems // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // // You may contact us via email at: sales@flex-radio.com. // Paper mail may be sent to: // FlexRadio Systems // 8900 Marybank Dr. // Austin, TX 78750 // USA //================================================================= using System; using System.Diagnostics; using System.Text; using System.Runtime.InteropServices; using PowerSDR; namespace PowerSDR { /// /// Summary description for midi. /// unsafe public class Midi { public const int CALLBACK_FUNCTION = 0x00030000; public const int MIM_OPEN = 0x3C1; public const int MIM_CLOSE = 0x3C2; public const int MIM_DATA = 0x3C3; public const int MIM_LONGDATA = 0x3C4; public static int AddSysExBuffer(int handle) { int result; IntPtr headerPtr; int size = Marshal.SizeOf(typeof(MidiHeader)); MidiHeader header = new MidiHeader(); header.bufferLength = 64; header.bytesRecorded = 0; header.data = Marshal.AllocHGlobal(64); header.flags = 0; try { headerPtr = Marshal.AllocHGlobal(size); } catch(Exception) { Marshal.FreeHGlobal(header.data); throw; } try { Marshal.StructureToPtr(header, headerPtr, false); } catch(Exception) { Marshal.FreeHGlobal(header.data); Marshal.FreeHGlobal(headerPtr); throw; } result = Midi.MidiInPrepareHeader(handle, headerPtr, size); if(result != 0) return result; result = Midi.MidiInAddBuffer(handle, headerPtr, size); if(result != 0) return result; return result; } public static void ReleaseBuffer(int handle, IntPtr headerPtr) { int result = MidiInUnprepareHeader(handle, headerPtr, Marshal.SizeOf(typeof(MidiHeader))); if(result != 0) { StringBuilder error_text = new StringBuilder(64); Midi.MidiInGetErrorText(result, error_text, 64); Debug.WriteLine("MidiInUnprepareHeader Error: "+error_text); return; } MidiHeader header = (MidiHeader)Marshal.PtrToStructure(headerPtr, typeof(MidiHeader)); Marshal.FreeHGlobal(header.data); Marshal.FreeHGlobal(headerPtr); } private static byte[] SwapBytes(byte[] b) { byte temp; for(int i=0; i