diff --git a/ShawzinBot/Models/MidiTrackModel.cs b/ShawzinBot/Models/MidiTrackModel.cs index ce26690..6bd69fb 100644 --- a/ShawzinBot/Models/MidiTrackModel.cs +++ b/ShawzinBot/Models/MidiTrackModel.cs @@ -5,14 +5,24 @@ using System.Text; using System.Threading.Tasks; using System.Windows; +using System.Windows.Navigation; namespace ShawzinBot.Models { public class MidiTrackModel { + private bool _isChecked; + public string TrackName { get; private set; } public TrackChunk Track { get; private set; } - public bool IsChecked { get; set; } + public bool IsChecked { + get => _isChecked; + set + { + _isChecked = value; + ViewModels.MainViewModel.reloadPlayback = true; + } + } public MidiTrackModel(TrackChunk track) { diff --git a/ShawzinBot/ViewModels/MainViewModel.cs b/ShawzinBot/ViewModels/MainViewModel.cs index e572c33..4eee531 100644 --- a/ShawzinBot/ViewModels/MainViewModel.cs +++ b/ShawzinBot/ViewModels/MainViewModel.cs @@ -32,12 +32,13 @@ public class MainViewModel : Screen private string _scale = "Scale: Chromatic"; private BindableCollection _midiInputs = new BindableCollection(); - private BindableCollection _midiChannels = new BindableCollection(); + private BindableCollection _midiTracks = new BindableCollection(); private MidiInputModel _selectedMidiInput; private bool _enableVibrato = true; private bool _transposeNotes = true; private bool _playThroughSpeakers; + private bool _ignoreSliderChange; private string[] ScaleArray = { "Chromatic", @@ -63,6 +64,8 @@ public class MainViewModel : Screen public Playback playback; public InputDevice inputDevice; + public static bool reloadPlayback; + #endregion #region Constructor @@ -125,6 +128,19 @@ public double SongSlider { _songSlider = value; NotifyOfPropertyChange(() => SongSlider); + if (!_ignoreSliderChange && playback != null) + { + if (playback.IsRunning) + { + playback.Stop(); + PlayPauseIcon = "Play"; + } + TimeSpan time = TimeSpan.FromSeconds(_songSlider); + + CurrentTime = time.ToString("m\\:ss"); + playback.MoveToTime((MetricTimeSpan) time); + } + _ignoreSliderChange = false; } } @@ -160,10 +176,10 @@ public BindableCollection MidiInputs public BindableCollection MidiTracks { - get => _midiChannels; + get => _midiTracks; set { - _midiChannels = value; + _midiTracks = value; NotifyOfPropertyChange(() => MidiTracks); } } @@ -255,7 +271,7 @@ public void OpenFile() TimeSpan midiFileDuration = midiFile.GetDuration(); TotalTime = midiFileDuration.ToString("m\\:ss"); MaximumTime = midiFileDuration.TotalSeconds; - SongSlider = 0; + UpdateSlider(0); CurrentTime = "0:00"; metaTrack = midiFile.GetTrackChunks().FirstOrDefault(); midiFile.Chunks.Remove(metaTrack); @@ -294,8 +310,18 @@ public void CloseFile() public void PlayPause() { if (midiFile == null || MaximumTime == 0d) return; - if (playback == null) + if (playback == null || reloadPlayback) { + ITimeSpan playTime = new MidiTimeSpan(); + if (playback != null) + { + playback.Stop(); + playTime = playback.GetCurrentTime(TimeSpanType.Midi); + playback.OutputDevice?.Dispose(); + playback = null; + PlayPauseIcon = "Play"; + } + midiFile.Chunks.Clear(); midiFile.Chunks.Add(metaTrack); @@ -308,7 +334,7 @@ public void PlayPause() } playback = midiFile.GetPlayback(); - + playback.MoveToTime(playTime); playback.Finished += (s, e) => { CloseFile(); @@ -319,6 +345,7 @@ public void PlayPause() PlaybackCurrentTimeWatcher.Instance.Start(); playback.EventPlayed += OnNoteEvent; + reloadPlayback = false; } if (playback.IsRunning) @@ -354,7 +381,7 @@ public void Previous() if (playback != null) { playback.MoveToStart(); - SongSlider = 0; + UpdateSlider(0); CurrentTime = "0:00"; } } @@ -392,7 +419,7 @@ public void OnTick(object sender, PlaybackCurrentTimeChangedEventArgs e) { TimeSpan time = (MetricTimeSpan) playbackTime.Time; - SongSlider = time.TotalSeconds; + UpdateSlider(time.TotalSeconds); CurrentTime = time.ToString("m\\:ss"); } } @@ -427,6 +454,12 @@ public void OnNoteEvent(object sender, MidiEventReceivedEventArgs e) ActionManager.PlayNote(note, EnableVibrato, TransposeNotes); } + private void UpdateSlider(double value) + { + _ignoreSliderChange = true; + SongSlider = value; + } + #endregion } }