public async void PlaySingleNote()
        {
            try
            {
                if (!IsPlaying)
                {
                    IsPlaying = true;
                    FreePlayViewModel viewModel = (FreePlayViewModel)PageController.GetViewModel().CurrentPage.DataContext;
                    if (CurrentNote == NOTES.Length - 1)
                    {
                        CurrentNote = 0;
                        viewModel.CurrentNoteNumber = CurrentNote;
                    }
                    else
                    {
                        CurrentNote += 1;
                        viewModel.CurrentNoteNumber = CurrentNote;
                    }
                    viewModel.CurrentNoteNumber = CurrentNote;
                    SetPressedButtonColor();
                    MidiPlayer.Play(new NoteOn(0, 0, NOTES[CurrentNote], volume));
                    await Task.Factory.StartNew(() =>
                    {
                        Thread.Sleep(150);
                    });

                    SetOriginalButtonColor();
                    MidiPlayer.Play(new NoteOff(0, 0, NOTES[CurrentNote], volume));
                    IsPlaying = false;
                }
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }
        public async void PlayNOTES()
        {
            try
            {
                IsPlaying = true;
                FreePlayViewModel viewModel = (FreePlayViewModel)PageController.GetViewModel().CurrentPage.DataContext;
                while (IsPlaying)
                {
                    volume = PageController.GetPianoRollModel().volume; //получаем текущее значение громкости
                    SetPressedButtonColor();                            //меняем цвет кнопки
                    MidiPlayer.Play(new NoteOn(0, 0, NOTES[CurrentNote], volume));
                    await Task.Factory.StartNew(() =>
                    {
                        try { Thread.Sleep(Convert.ToInt32(DURATIONS[CurrentNote])); }
                        catch { Thread.Sleep(250); }
                    });

                    SetOriginalButtonColor();//возвращаем цвет
                    MidiPlayer.Play(new NoteOff(0, 0, NOTES[CurrentNote], volume));
                    CurrentNote = CurrentNote == NOTES.Length - 1 ? 0 : CurrentNote += 1;
                    viewModel.CurrentNoteNumber = CurrentNote;
                }
            }
            catch (Exception ex) { IsPlaying = false; MessageBox.Show(ex.Message); }
        }
 void SetPressedButtonColor()
 {
     try
     {
         List <FrameworkElement> lstElement;
         FreePlayPage            page      = (FreePlayPage)PageController.GetViewModel().CurrentPage;
         FreePlayViewModel       viewModel = (FreePlayViewModel)page.DataContext;
         Panel pianoRoll             = (Panel)page.pianoRoll.Content;
         UIElementCollection element = pianoRoll.Children;
         lstElement = element.Cast <FrameworkElement>().ToList();
         Dictionary <string, string> _notesByKeys = PageController.GetPianoRollModel()._notesByKeys;
         List <string> pressedNotes = new List <string>();
         var           lstControl   = lstElement.OfType <Button>();
         foreach (var control in lstControl)
         {
             if (control.Tag.ToString() == _notesByKeys.FirstOrDefault(x => x.Value == NOTES[CurrentNote]).Key)
             {
                 if (NOTES[CurrentNote].Contains("#"))
                 {
                     control.SetResourceReference(Button.StyleProperty, "BlackPressedPianoKeyStyle");
                 }
                 else
                 {
                     control.SetResourceReference(Button.StyleProperty, "WhitePressedPianoKeyStyle");
                 }
             }
         }
     }
     catch (Exception) { IsPlaying = false; }
 }
        public void SaveSong(string NewNotes, string NewDurations, string SongTitle)
        {
            FreePlayViewModel viewModel = (FreePlayViewModel)PageController.GetViewModel().CurrentPage.DataContext;

            using (SqlConnection dbConn = new SqlConnection())
            {
                try
                {
                    int pos = NewDurations.IndexOf(' ');
                    NewDurations            = NewDurations.Substring(pos + 1);
                    dbConn.ConnectionString = ConfigurationManager.ConnectionStrings["YourPianoConnection"].ConnectionString;
                    string querytString = $"insert NoteSequence(notes, durations) values ('{NewNotes}', '{NewDurations}')";
                    dbConn.Open();
                    SqlCommand command = new SqlCommand(querytString, dbConn);
                    command.ExecuteNonQuery();
                    dbConn.Close();

                    querytString = $"select ID from NoteSequence where notes = '{NewNotes}'";
                    dbConn.Open();
                    SqlDataAdapter dbAdapter = new SqlDataAdapter(querytString, dbConn);
                    DataTable      id        = new DataTable();
                    dbAdapter = new SqlDataAdapter(querytString, dbConn);
                    dbAdapter.Fill(id);
                    dbConn.Close();

                    dbConn.ConnectionString = ConfigurationManager.ConnectionStrings["YourPianoConnection"].ConnectionString;
                    querytString            = $"insert into ActionInfo(userID, songName, date, noteSecuence)" +
                                              $"values ('{CurrentUser.userID}', '{SongTitle}', '{DateTime.Now.ToString()}', '{id.Rows[0][0]}')";
                    dbConn.Open();
                    command = new SqlCommand(querytString, dbConn);
                    command.ExecuteNonQuery();
                    MessageBox.Show("Произведение сохранено!");
                    NOTES                       = NewNotes.Split(new char[] { ' ' });
                    DURATIONS                   = NewDurations.Split(new char[] { ' ' });
                    viewModel.SongNotes         = NOTES;
                    CurrentNote                 = 0;
                    viewModel.CurrentNoteNumber = 0;
                    dbConn.Close();
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message);
                }
                catch (Exception ex) { MessageBox.Show(ex.Message); }
                finally
                {
                    dbConn.Close();
                }
            }
            CurrentNote = 0;
            viewModel.CurrentNoteNumber = 0;
        }
Example #5
0
 //comboBox filling
 public FreePlayPage()
 {
     try
     {
         InitializeComponent();
         viewModel                   = new FreePlayViewModel();
         DataContext                 = viewModel;
         viewModel.view              = this;
         instrumentsBox.ItemsSource  = Enum.GetValues(typeof(GeneralMidiInstruments));
         instrumentsBox.SelectedItem = instrumentsBox.Items[0];
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
     finally
     {
         pianoRoll.buttonB2.Focus();
     }
 }