Exemple #1
0
        public static void Main(string [] args)
        {
            int inId = -1, outId = -1, bufsize = -1;
            string filename = null;
            TimeSpan interval = TimeSpan.Zero;
            byte [] ex = null;
            foreach (var arg in args) {
                if (arg.StartsWith ("--in:")) {
                    if (!int.TryParse (arg.Substring (5), out inId))
                        inId = -1;
                }
                if (arg.StartsWith ("--out:")) {
                    if (!int.TryParse (arg.Substring (6), out outId))
                        outId = -1;
                }
                if (arg.StartsWith ("--buffer:")) {
                    if (!int.TryParse (arg.Substring (9), out bufsize))
                        bufsize = -1;
                }
                if (arg.StartsWith ("--file:"))
                    filename = arg.Substring (7);
                if (arg.StartsWith ("--interval:")) {
                    int intervalint;
                    if (int.TryParse (arg.Substring (11), out intervalint))
                        interval = TimeSpan.FromMilliseconds (intervalint);
                }
                if (arg.StartsWith ("--sysex:")) {
                    string [] l = arg.Substring (8).Split (',');
                    ex = new byte [l.Length];
                    byte v;
                    for (int i = 0; i < ex.Length; i++) {
                        if (byte.TryParse (l [i], NumberStyles.HexNumber, NumberFormatInfo.InvariantInfo, out v))
                            ex [i] = v;
                        else {
                            ex = null;
                            break;
                        }
                    }
                    if (ex == null) {
                        Console.WriteLine ("invalid sysex: " + arg);
                        return;
                    }
                }
            }
            var a = new List<MidiDeviceInfo> (MidiDeviceManager.AllDevices);
            if (inId < 0) {
                foreach (var dev in a)
                    if (dev.IsInput)
                        Console.WriteLine ("ID {0}: {1}", dev.ID, dev.Name);
                Console.WriteLine ("Type number to select MIDI In Device to use (type anything else to quit)");
                if (!int.TryParse (Console.ReadLine (), out inId))
                    return;
            }
            if (outId < 0) {
                foreach (var dev in a)
                    if (dev.IsOutput)
                        Console.WriteLine ("ID {0}: {1}", dev.ID, dev.Name);
                Console.WriteLine ("Type number to select MIDI Out Device to use (type anything else to quit)");
                if (!int.TryParse (Console.ReadLine (), out outId))
                    return;
            }

            var dump = new BulkDump ();
            if (interval != TimeSpan.Zero)
                dump.Interval = interval;
            if (bufsize > 0)
                dump.BufferSize = bufsize;
            if (ex != null)
                dump.SetSysEx (ex);
            dump.Start (inId, outId);
            Console.WriteLine ("Type [CR] to stop receiving");
            Console.ReadLine ();
            dump.Stop ();

            if (String.IsNullOrEmpty (filename)) {
                Console.Write ("Type filename to save if you want: ");
                filename = Console.ReadLine ();
            }
            if (filename.Length > 0) {
                var music = new SmfMusic ();
                var track = new SmfTrack ();
                foreach (var e in dump.Results) {
                    if (e.SysEx != null)
                        track.Messages.Add (new SmfMessage (e.Timestamp, new SmfEvent (0xF0, 0, 0, e.SysEx)));
                    else
                        track.Messages.Add (new SmfMessage (e.Timestamp, new SmfEvent (e.Message.Value)));
                }
                music.Tracks.Add (track);
                using (var f = File.Create (filename))
                    new SmfWriter (f).WriteMusic (music);
            }
        }
Exemple #2
0
        public static void Main(string [] args)
        {
            int      inId = -1, outId = -1, bufsize = -1;
            string   filename = null;
            TimeSpan interval = TimeSpan.Zero;

            byte [] ex = null;
            foreach (var arg in args)
            {
                if (arg.StartsWith("--in:"))
                {
                    if (!int.TryParse(arg.Substring(5), out inId))
                    {
                        inId = -1;
                    }
                }
                if (arg.StartsWith("--out:"))
                {
                    if (!int.TryParse(arg.Substring(6), out outId))
                    {
                        outId = -1;
                    }
                }
                if (arg.StartsWith("--buffer:"))
                {
                    if (!int.TryParse(arg.Substring(9), out bufsize))
                    {
                        bufsize = -1;
                    }
                }
                if (arg.StartsWith("--file:"))
                {
                    filename = arg.Substring(7);
                }
                if (arg.StartsWith("--interval:"))
                {
                    int intervalint;
                    if (int.TryParse(arg.Substring(11), out intervalint))
                    {
                        interval = TimeSpan.FromMilliseconds(intervalint);
                    }
                }
                if (arg.StartsWith("--sysex:"))
                {
                    string [] l = arg.Substring(8).Split(',');
                    ex = new byte [l.Length];
                    byte v;
                    for (int i = 0; i < ex.Length; i++)
                    {
                        if (byte.TryParse(l [i], NumberStyles.HexNumber, NumberFormatInfo.InvariantInfo, out v))
                        {
                            ex [i] = v;
                        }
                        else
                        {
                            ex = null;
                            break;
                        }
                    }
                    if (ex == null)
                    {
                        Console.WriteLine("invalid sysex: " + arg);
                        return;
                    }
                }
            }
            var a = new List <MidiDeviceInfo> (MidiDeviceManager.AllDevices);

            if (inId < 0)
            {
                foreach (var dev in a)
                {
                    if (dev.IsInput)
                    {
                        Console.WriteLine("ID {0}: {1}", dev.ID, dev.Name);
                    }
                }
                Console.WriteLine("Type number to select MIDI In Device to use (type anything else to quit)");
                if (!int.TryParse(Console.ReadLine(), out inId))
                {
                    return;
                }
            }
            if (outId < 0)
            {
                foreach (var dev in a)
                {
                    if (dev.IsOutput)
                    {
                        Console.WriteLine("ID {0}: {1}", dev.ID, dev.Name);
                    }
                }
                Console.WriteLine("Type number to select MIDI Out Device to use (type anything else to quit)");
                if (!int.TryParse(Console.ReadLine(), out outId))
                {
                    return;
                }
            }

            var dump = new BulkDump();

            if (interval != TimeSpan.Zero)
            {
                dump.Interval = interval;
            }
            if (bufsize > 0)
            {
                dump.BufferSize = bufsize;
            }
            if (ex != null)
            {
                dump.SetSysEx(ex);
            }
            dump.Start(inId, outId);
            Console.WriteLine("Type [CR] to stop receiving");
            Console.ReadLine();
            dump.Stop();

            if (String.IsNullOrEmpty(filename))
            {
                Console.Write("Type filename to save if you want: ");
                filename = Console.ReadLine();
            }
            if (filename.Length > 0)
            {
                var music = new SmfMusic();
                var track = new SmfTrack();
                foreach (var e in dump.Results)
                {
                    if (e.SysEx != null)
                    {
                        track.Messages.Add(new SmfMessage(e.Timestamp, new SmfEvent(0xF0, 0, 0, e.SysEx)));
                    }
                    else
                    {
                        track.Messages.Add(new SmfMessage(e.Timestamp, new SmfEvent(e.Message.Value)));
                    }
                }
                music.Tracks.Add(track);
                using (var f = File.Create(filename))
                    new SmfWriter(f).WriteMusic(music);
            }
        }