Skip to content

Commit

Permalink
Slider fixed
Browse files Browse the repository at this point in the history
Made the slider fully functional. You can now change the current time of the song
  • Loading branch information
ianespana committed Sep 22, 2020
1 parent cb3e829 commit 5dea1c1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
12 changes: 11 additions & 1 deletion ShawzinBot/Models/MidiTrackModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
49 changes: 41 additions & 8 deletions ShawzinBot/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ public class MainViewModel : Screen
private string _scale = "Scale: Chromatic";

private BindableCollection<MidiInputModel> _midiInputs = new BindableCollection<MidiInputModel>();
private BindableCollection<MidiTrackModel> _midiChannels = new BindableCollection<MidiTrackModel>();
private BindableCollection<MidiTrackModel> _midiTracks = new BindableCollection<MidiTrackModel>();
private MidiInputModel _selectedMidiInput;

private bool _enableVibrato = true;
private bool _transposeNotes = true;
private bool _playThroughSpeakers;
private bool _ignoreSliderChange;

private string[] ScaleArray = {
"Chromatic",
Expand All @@ -63,6 +64,8 @@ public class MainViewModel : Screen
public Playback playback;
public InputDevice inputDevice;

public static bool reloadPlayback;

#endregion

#region Constructor
Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -160,10 +176,10 @@ public BindableCollection<MidiInputModel> MidiInputs

public BindableCollection<MidiTrackModel> MidiTracks
{
get => _midiChannels;
get => _midiTracks;
set
{
_midiChannels = value;
_midiTracks = value;
NotifyOfPropertyChange(() => MidiTracks);
}
}
Expand Down Expand Up @@ -255,7 +271,7 @@ public void OpenFile()
TimeSpan midiFileDuration = midiFile.GetDuration<MetricTimeSpan>();
TotalTime = midiFileDuration.ToString("m\\:ss");
MaximumTime = midiFileDuration.TotalSeconds;
SongSlider = 0;
UpdateSlider(0);
CurrentTime = "0:00";
metaTrack = midiFile.GetTrackChunks().FirstOrDefault();
midiFile.Chunks.Remove(metaTrack);
Expand Down Expand Up @@ -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);

Expand All @@ -308,7 +334,7 @@ public void PlayPause()
}

playback = midiFile.GetPlayback();

playback.MoveToTime(playTime);
playback.Finished += (s, e) =>
{
CloseFile();
Expand All @@ -319,6 +345,7 @@ public void PlayPause()
PlaybackCurrentTimeWatcher.Instance.Start();

playback.EventPlayed += OnNoteEvent;
reloadPlayback = false;
}

if (playback.IsRunning)
Expand Down Expand Up @@ -354,7 +381,7 @@ public void Previous()
if (playback != null)
{
playback.MoveToStart();
SongSlider = 0;
UpdateSlider(0);
CurrentTime = "0:00";
}
}
Expand Down Expand Up @@ -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");
}
}
Expand Down Expand Up @@ -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
}
}

0 comments on commit 5dea1c1

Please sign in to comment.