public void ProcessBeginPlay(MidiPlayer player, int totalMilliseconds)
 {
     timer = new DispatcherTimer ();
     timer.Interval = TimeSpan.FromMilliseconds (100);
     timer.Tick += delegate {
         tick_count.Text = player.PlayDeltaTime.ToString ("D08");
         TimeSpan now = GetTimerOffsetWithTempoRatio () + timer_offset;
         passed_time.Text = String.Format ("{0:D02}:{1:D02}", (int) now.TotalMinutes, now.Seconds);
     };
     timer_offset = TimeSpan.Zero;
     last_tempo_changed = timer_resumed = DateTime.Now;
     tick_offset = 0;
     timer.Start ();
 }
        public static int Main(string [] args)
        {
            var apiProviderSpec = args.FirstOrDefault (a => a.StartsWith ("--provider:", StringComparison.Ordinal));
            Type apiType = null;
            if (apiProviderSpec != null) {
                apiType = Type.GetType (apiProviderSpec.Substring ("--provider:".Length));
                if (apiType == null) {
                    ShowHelp ();
                    Console.Error.WriteLine ();
                    Console.Error.WriteLine (apiProviderSpec + " didn't work.");
                    Console.Error.WriteLine ();
                    return -1;
                }
                Console.Error.WriteLine ("Using MidiAccess '{0}'", apiType.AssemblyQualifiedName);
            }
            var api = apiProviderSpec != null ?
                (IMidiAccess) Activator.CreateInstance (apiType) :
                MidiAccessManager.Default;
            var output = api.Outputs.LastOrDefault ();
            var files = new List<string> ();
            bool diagnostic = false;
            foreach (var arg in args) {
                if (arg == apiProviderSpec)
                    continue;
                if (arg == "--help") {
                    ShowHelp ();
                    return 0;
                }
                else if (arg == "--verbose")
                    diagnostic = true;
                else if (arg.StartsWith ("--device:", StringComparison.Ordinal)) {
                    output = api.Outputs.FirstOrDefault (o => o.Id == arg.Substring (9));
                    if (output == null) {
                        ShowHelp ();
                        Console.WriteLine ();
                        Console.WriteLine ("Invalid MIDI output device ID.");
                        Console.Error.WriteLine ();
                        return -2;
                    }
                }
                else
                    files.Add (arg);
            }
            if (!files.Any ()) {
                ShowHelp ();
                return 0;
            }

            var wh = new ManualResetEvent (false);
            bool loop = true;

            foreach (var arg in files) {
                var parser = new SmfReader ();
                parser.Read (File.OpenRead (arg));
                var player = new MidiPlayer (parser.Music, api.OpenOutputAsync (output.Id).Result);
                DateTimeOffset start = DateTimeOffset.Now;
                if (diagnostic)
                    player.EventReceived += e => {
                        string type = null;
                        switch (e.EventType) {
                        case SmfEvent.NoteOn: type = "NOn"; break;
                        case SmfEvent.NoteOff: type = "NOff"; break;
                        case SmfEvent.PAf: type = "PAf"; break;
                        case SmfEvent.CC: type = "CC"; break;
                        case SmfEvent.Program: type = "@"; break;
                        case SmfEvent.CAf: type = "CAf"; break;
                        case SmfEvent.Pitch: type = "P"; break;
                        case SmfEvent.SysEx1: type = "SysEX"; break;
                        case SmfEvent.SysEx2: type = "SysEX2"; break;
                        case SmfEvent.Meta: type = "META"; break;
                        }
                        Console.WriteLine ("{0:06} {1:D02} {2} {3}", (DateTimeOffset.Now - start).TotalMilliseconds, e.Channel, type, e);
                    };
                player.Finished += delegate {
                    loop = false;
                    wh.Set ();
                };

                new Task (() => {
                    Console.WriteLine ("empty line to quit, P to pause and resume");
                    while (loop) {
                        string line = Console.ReadLine ();
                        if (line == "P") {
                            if (player.State == PlayerState.Playing)
                                player.PauseAsync ();
                            else
                                player.PlayAsync ();
                        }
                        else if (line == "") {
                            loop = false;
                            wh.Set ();
                            player.Dispose ();
                            break;
                        }
                        else
                            Console.WriteLine ("what do you mean by '{0}' ?", line);
                    }
                }).Start ();

                //player.StartLoop ();
                player.PlayAsync ();
                while (loop) {
                    wh.WaitOne ();
                }
                player.PauseAsync ();
            }
            return 0;
        }
 public void ProcessBeginPlay(MidiPlayer player, int totalMilliseconds)
 {
     progress_story.Stop ();
     var a = (DoubleAnimation) progress_story.Children [0];
     a.Duration = new Duration (TimeSpan.FromMilliseconds (totalMilliseconds));
     a.From = 0;
     progress_story.Begin ();
     circle_timer.Start ();
     State = PlayerState.Playing;
 }