SeeScore.PlayData.IPlayData GetMIDIPlayData()
 {
     if (midiPlayData_cache == null)
     {
         midiPlayData_cache = CreateMidiPlayData();
     }
     return(midiPlayData_cache);
 }
 private String GetMIDIFile()
 {
     if (midiPlayData_cache == null)
     {
         midiPlayData_cache = CreateMidiPlayData();
         midiPlayData_cache.CreateMidiFile(midiFileName);
     }
     return(midiFileName);
 }
        private void StartPlayAt(int barIndex, bool countIn)
        {
            axWindowsMediaPlayer1.URL = GetMIDIFile();
            axWindowsMediaPlayer1.Ctlcontrols.stop();
            SeeScore.PlayData.IPlayData pd = GetMIDIPlayData();
            int bar_ms = pd.durationUpToBar(barIndex);

            axWindowsMediaPlayer1.Ctlcontrols.currentPosition = (double)bar_ms / 1000.0F;
            DelayedStarter starter = new DelayedStarter(this, barIndex, countIn);

            testingDelay = true;
            mediaPlayerStartDelayTestStartTime = DateTime.Now;
            postDelayTestStartHandler          = starter.postDelayTestStartHandler;
            axWindowsMediaPlayer1.Ctlcontrols.play();
        }
        SeeScore.PlayData.IPlayData CreateMidiPlayData()
        {
            SeeScore.PlayData.IPlayData pd = SeeScore.PlayData.PD.Create(score, new UTempo(this));
            double rate = pd.HasDefinedTempo() ? pd.TempoAtStart().bpm : kDefaultTempoBPM; // use default 80 if no tempo defined in file

            bpmLabel.Text = "" + rate.ToString("F0");

            notifier = new SeeScore.Notifier(pd);
            if (kUsingNoteCursor)
            {
                notifier.SetNoteHandler(new NoteHandler(this));      // note cursor
            }
            notifier.SetBarChangeHandler(new BarEventHandler(this)); // bar cursor
            notifier.SetBeatHandler(new BeatHandler(this));
            notifier.SetEndHandler(new EndHandler(this));
            return(pd);
        }
        int PositionToBarIndex(double position_s)
        {
            int ms       = 0;
            int seqIndex = 0;
            int pos_ms   = (int)(position_s * 1000);

            SeeScore.PlayData.IPlayData playData = GetMIDIPlayData();
            foreach (SeeScore.PlayData.IBar bar in playData)
            {
                ms += bar.Duration();
                if (ms >= pos_ms)
                {
                    return(seqIndex);
                }
                ++seqIndex;
            }
            return(seqIndex);
        }
        private void LoadData(byte[] data)
        {
            try
            {
                seeScoreView.ClearAwaitLayoutCompletion();// careful to await termination of layout thread if mid-layout
                if (score != null)
                {
                    score.Dispose();
                    score = null;
                }
                if (notifier != null)
                {
                    notifier.Stop();
                }
                axWindowsMediaPlayer1.Ctlcontrols.stop();
                axWindowsMediaPlayer1.URL = null;
                SeeScore.LoadOptions loadOptions = new SeeScore.LoadOptions(SeeScore.K.Key, false, true);
                score = SeeScore.SS.LoadXMLData(data, loadOptions);
                SeeScore.LoadWarning[] warnings = score.GetLoadWarnings();
                foreach (SeeScore.LoadWarning w in warnings)
                {
                    System.Console.WriteLine(" error " + w.warning + " in " + w.element + " in part:" + w.partIndex + " bar:" + w.barIndex);
                }
                tempoSlider.Value = 100;
                seeScoreView.SetScore(score, new AppNotifierImpl(this));
                midiPlayData_cache    = null;
                this.zoomSlider.Value = 100;

                // Create MIDI file for playing
                midiFileName = loadedFile.Substring(0, openFileDialog1.FileName.Length - 4) + ".mid";
                GetMIDIFile();

                // Initialise the score follower
                InitialiseScoreFollower(midiFileName);
            }
            catch (SeeScore.LoadException ex)
            {
                Console.WriteLine("failed to load file: " + ex.Message);
            }
            catch (SeeScore.ScoreException ex)
            {
                Console.WriteLine("Failed to create midi file " + ex.Message);
            }
        }
 private void UpdateTempo(float tempoScaling)
 {
     if (score != null)
     {
         bool isPlaying = axWindowsMediaPlayer1.playState == WMPPlayState.wmppsPlaying;
         if (isPlaying)
         {
             axWindowsMediaPlayer1.Ctlcontrols.stop();
             notifier.Stop();
         }
         axWindowsMediaPlayer1.URL = null;
         SeeScore.PlayData.IPlayData playData = GetMIDIPlayData();
         double rate = (playData.HasDefinedTempo() ? playData.TempoAtStart().bpm : kDefaultTempoBPM) * tempoScaling;
         bpmLabel.Text = "" + rate.ToString("F0");
         playData.UpdateTempo();
         playData.ScaleMidiFileTempo(GetMIDIFile(), tempoScaling);
         if (isPlaying)
         {
             int barIndex = seeScoreView.CursorBarIndex();
             StartPlayAt(barIndex, false);
         }
     }
 }
        // called after testing mediaplayer start delay. We start the notifier and the mediaplayer with compensation for its start delay
        private void StartPlayerWithDelay(TimeSpan startDelay, int barIndex, bool countIn)
        {
            SeeScore.PlayData.IPlayData      pd      = GetMIDIPlayData();
            SeeScore.PlayData.IBarEnumerator barIter = pd.GetBarEnumerator();
            barIter.ChangeToBar(barIndex);
            SeeScore.PlayData.IBar bar    = barIter.CurrentBar();
            DateTime mediaPlayerStartTime = DateTime.Now.AddMilliseconds(1000);
            DateTime notifierStartTime    = mediaPlayerStartTime;

            if (countIn)
            {
                bar = bar.CreateCountIn();
                mediaPlayerStartTime = notifierStartTime.AddMilliseconds(bar.Duration() - startDelay.TotalMilliseconds);
            }
            else
            {
                mediaPlayerStartTime = notifierStartTime.AddMilliseconds(-startDelay.TotalMilliseconds);
            }
            int bar_ms = pd.durationUpToBar(barIndex);

            axWindowsMediaPlayer1.Ctlcontrols.currentPosition = (double)bar_ms / 1000.0F;
            new PlayerStartDispatch(mediaPlayerStartTime, this);
            notifier.StartAt(notifierStartTime, barIndex, countIn);// start count-in. MediaPlayer will be started after this
        }
 private void DisposeMIDI()
 {
     midiPlayData_cache = null;
 }