Skip to content

Commit

Permalink
Add single play loop mode
Browse files Browse the repository at this point in the history
  • Loading branch information
sabihoshi committed Aug 3, 2021
1 parent acf8bdc commit 71e5076
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 35 deletions.
4 changes: 2 additions & 2 deletions GenshinLyreMidiPlayer.WPF/GenshinLyreMidiPlayer.WPF.csproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net5.0-windows10.0.19041.0</TargetFramework>
<UseWPF>true</UseWPF>
<StartupObject>GenshinLyreMidiPlayer.WPF.App</StartupObject>
<ApplicationManifest>app.manifest</ApplicationManifest>
<Version>2.3.2</Version>
<Version>2.3.3</Version>
<ApplicationIcon>item_windsong_lyre.ico</ApplicationIcon>
<Nullable>enable</Nullable>
<RepositoryUrl>https://github.com/sabihoshi/GenshinLyreMidiPlayer</RepositoryUrl>
Expand Down
35 changes: 23 additions & 12 deletions GenshinLyreMidiPlayer.WPF/ViewModels/LyrePlayerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
using ModernWpf.Controls;
using Stylet;
using StyletIoC;
using static Windows.Media.MediaPlaybackAutoRepeatMode;
using static GenshinLyreMidiPlayer.WPF.Core.LyrePlayer.Transpose;
using MidiFile = GenshinLyreMidiPlayer.Data.Midi.MidiFile;

Expand Down Expand Up @@ -91,7 +90,7 @@ public bool CanHitNext
{
get
{
if (Playlist.Loop is List or Track)
if (Playlist.Loop is not PlaylistViewModel.LoopMode.Playlist)
return true;

var last = Playlist.GetPlaylist().LastOrDefault();
Expand Down Expand Up @@ -175,12 +174,17 @@ public async void Handle(PlayTimerNotification message)

public void OnSongPositionChanged()
{
if (!_ignoreSliderChange && Playback is { IsRunning: true })
NotifyOfPropertyChange(() => CurrentTime);

if (Playback is null) Next();

if (!_ignoreSliderChange && Playback is not null)
{
var isRunning = Playback.IsRunning;
Playback.Stop();
Playback.MoveToTime(new MetricTimeSpan(CurrentTime));
Playback.MoveToTime(new MetricTimeSpan(_songPosition));

if (Settings.UseSpeakers)
if (Settings.UseSpeakers && isRunning)
Playback.Start();
}

Expand All @@ -191,12 +195,13 @@ public void OnSelectedMidiInputChanged()
{
_inputDevice?.Dispose();

if (SelectedMidiInput?.DeviceName != null && SelectedMidiInput.DeviceName != "None")
if (SelectedMidiInput?.DeviceName is not null
&& SelectedMidiInput.DeviceName != "None")
{
_inputDevice = InputDevice.GetByName(SelectedMidiInput.DeviceName);

_inputDevice.EventReceived += OnNoteEvent;
_inputDevice.StartEventsListening();
_inputDevice!.EventReceived += OnNoteEvent;
_inputDevice!.StartEventsListening();
}
}

Expand Down Expand Up @@ -375,12 +380,18 @@ public async void Next()
{
var next = Playlist.Next();
if (next is null)
{
if (Playback is not null)
{
Playback.PlaybackStart = null;
Playback.MoveToStart();
}

MoveSlider(TimeSpan.Zero);
return;
}

if (next == Playlist.OpenedFile && Playlist.Loop == Track)
Handle(next);
else if (next != Playlist.OpenedFile)
Handle(next);
Handle(next);

if (Playback is not null)
await PlayPause();
Expand Down
47 changes: 26 additions & 21 deletions GenshinLyreMidiPlayer.WPF/ViewModels/PlaylistViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Media;
using Windows.Media;
using GenshinLyreMidiPlayer.Data;
using GenshinLyreMidiPlayer.Data.Entities;
using GenshinLyreMidiPlayer.WPF.ModernWPF.Errors;
Expand All @@ -13,13 +12,20 @@
using ModernWpf.Controls;
using Stylet;
using StyletIoC;
using static Windows.Media.MediaPlaybackAutoRepeatMode;
using MidiFile = GenshinLyreMidiPlayer.Data.Midi.MidiFile;

namespace GenshinLyreMidiPlayer.WPF.ViewModels
{
public class PlaylistViewModel : Screen
{
public enum LoopMode
{
Once,
Track,
Playlist,
All
}

private readonly IEventAggregator _events;
private readonly IContainer _ioc;

Expand All @@ -39,7 +45,7 @@ public PlaylistViewModel(IContainer ioc, IEventAggregator events)

public bool Shuffle { get; set; }

public MediaPlaybackAutoRepeatMode Loop { get; set; }
public LoopMode Loop { get; set; }

public MidiFile? OpenedFile { get; set; }

Expand All @@ -56,9 +62,10 @@ public PlaylistViewModel(IContainer ioc, IEventAggregator events)
public string LoopStateString =>
Loop switch
{
None => "\xF5E7",
Track => "\xE8ED",
List => "\xE8EE"
LoopMode.Once => "\xF5E7",
LoopMode.Track => "\xE8ED",
LoopMode.Playlist => "\xEBE7",
LoopMode.All => "\xE8EE"
};

public void OnFilterTextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs e)
Expand All @@ -79,32 +86,30 @@ public void ToggleShuffle()
public void ToggleLoop()
{
var loopState = (int) Loop;
var loopStates = Enum.GetValues(typeof(MediaPlaybackAutoRepeatMode)).Length;
var loopStates = Enum.GetValues(typeof(LoopMode)).Length;

var newState = (loopState + 1) % loopStates;
Loop = (MediaPlaybackAutoRepeatMode) newState;
Loop = (LoopMode) newState;
}

public MidiFile? Next()
{
var playlist = GetPlaylist().ToList();
if (OpenedFile is null) return playlist.FirstOrDefault();

if (Loop == Track)
return OpenedFile ?? playlist.FirstOrDefault();

var next = playlist.FirstOrDefault();

if (OpenedFile is not null)
switch (Loop)
{
var current = playlist.IndexOf(OpenedFile) + 1;

if (Loop is List)
current %= playlist.Count;

next = playlist.ElementAtOrDefault(current);
case LoopMode.Once:
return null;
case LoopMode.Track:
return OpenedFile;
}

return next;
var next = playlist.IndexOf(OpenedFile) + 1;
if (Loop is LoopMode.All)
next %= playlist.Count;

return playlist.ElementAtOrDefault(next);
}

public BindableCollection<MidiFile> GetPlaylist() => Shuffle ? ShuffledTracks : Tracks;
Expand Down

0 comments on commit 71e5076

Please sign in to comment.