Skip to content

Commit

Permalink
Make songs have their individual settings
Browse files Browse the repository at this point in the history
  • Loading branch information
sabihoshi committed Jan 20, 2022
1 parent ef78f74 commit 9ddb784
Show file tree
Hide file tree
Showing 13 changed files with 187 additions and 97 deletions.
12 changes: 10 additions & 2 deletions GenshinLyreMidiPlayer.Data/Entities/History.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@ namespace GenshinLyreMidiPlayer.Data.Entities;

public class History
{
public History() { }
protected History() { }

public History(string path) { Path = path; }
public History(string path, int key)
{
Key = key;
Path = path;
}

public Guid Id { get; set; }

public int Key { get; set; }

public string Path { get; set; } = null!;

public Transpose? Transpose { get; set; }
}
10 changes: 10 additions & 0 deletions GenshinLyreMidiPlayer.Data/Entities/Transpose.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.ComponentModel;

namespace GenshinLyreMidiPlayer.Data.Entities;

public enum Transpose
{
[Description("Ignore missing notes")] Ignore,
[Description("Transpose up")] Up,
[Description("Transpose down")] Down
}
9 changes: 6 additions & 3 deletions GenshinLyreMidiPlayer.Data/Midi/MidiFile.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using GenshinLyreMidiPlayer.Data.Entities;
using Melanchall.DryWetMidi.Core;
using Melanchall.DryWetMidi.Interaction;
using Melanchall.DryWetMidi.Tools;
Expand All @@ -13,14 +14,16 @@ public class MidiFile : Screen
private readonly ReadingSettings? _settings;
private int _position;

public MidiFile(string path, ReadingSettings? settings = null)
public MidiFile(History history, ReadingSettings? settings = null)
{
_settings = settings;

Path = path;
History = history;
InitializeMidi();
}

public History History { get; }

public int Position
{
get => _position + 1;
Expand All @@ -29,7 +32,7 @@ public int Position

public Melanchall.DryWetMidi.Core.MidiFile Midi { get; private set; } = null!;

public string Path { get; }
public string Path => History.Path;

public string Title => GetFileNameWithoutExtension(Path);

Expand Down
18 changes: 3 additions & 15 deletions GenshinLyreMidiPlayer.Data/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions GenshinLyreMidiPlayer.Data/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@
<Setting Name="UseSpeakers" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">False</Value>
</Setting>
<Setting Name="KeyOffset" Type="System.Int32" Scope="User">
<Value Profile="(Default)">0</Value>
</Setting>
<Setting Name="AppTheme" Type="System.Int32" Scope="User">
<Value Profile="(Default)">-1</Value>
</Setting>
Expand Down
8 changes: 1 addition & 7 deletions GenshinLyreMidiPlayer.WPF/Core/LyrePlayer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using GenshinLyreMidiPlayer.Data.Entities;
using WindowsInput;
using WindowsInput.Native;
using static GenshinLyreMidiPlayer.WPF.Core.Keyboard;
Expand All @@ -9,13 +10,6 @@ namespace GenshinLyreMidiPlayer.WPF.Core;

public static class LyrePlayer
{
public enum Transpose
{
Ignore,
Up,
Down
}

private static readonly IInputSimulator Input = new InputSimulator();

private static readonly List<int> LyreNotes = new()
Expand Down
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>net6.0-windows10.0.22000.0</TargetFramework>
<UseWPF>true</UseWPF>
<StartupObject>GenshinLyreMidiPlayer.WPF.App</StartupObject>
<ApplicationManifest>app.manifest</ApplicationManifest>
<Version>3.0.0</Version>
<Version>3.1.0</Version>
<ApplicationIcon>item_windsong_lyre.ico</ApplicationIcon>
<Nullable>enable</Nullable>
<RepositoryUrl>https://github.com/sabihoshi/GenshinLyreMidiPlayer</RepositoryUrl>
Expand Down
42 changes: 21 additions & 21 deletions GenshinLyreMidiPlayer.WPF/ViewModels/LyrePlayerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Windows;
using Windows.Media;
using Windows.Media.Playback;
using GenshinLyreMidiPlayer.Data.Entities;
using GenshinLyreMidiPlayer.Data.Midi;
using GenshinLyreMidiPlayer.Data.Notification;
using GenshinLyreMidiPlayer.Data.Properties;
Expand All @@ -16,7 +17,7 @@
using ModernWpf.Controls;
using Stylet;
using StyletIoC;
using static GenshinLyreMidiPlayer.WPF.Core.LyrePlayer.Transpose;
using static GenshinLyreMidiPlayer.WPF.ViewModels.SettingsPageViewModel;
using MidiFile = GenshinLyreMidiPlayer.Data.Midi.MidiFile;

namespace GenshinLyreMidiPlayer.WPF.ViewModels;
Expand All @@ -29,25 +30,22 @@ public class LyrePlayerViewModel : Screen,
{
private static readonly Settings Settings = Settings.Default;
private readonly IEventAggregator _events;
private readonly MainWindowViewModel _main;
private readonly MediaPlayer? _player;
private readonly OutputDevice? _speakers;
private readonly PlaybackCurrentTimeWatcher _timeWatcher;
private readonly SettingsPageViewModel _settings;
private bool _ignoreSliderChange;
private InputDevice? _inputDevice;
private TimeSpan _songPosition;

public LyrePlayerViewModel(IContainer ioc,
SettingsPageViewModel settings, PlaylistViewModel playlist)
public LyrePlayerViewModel(IContainer ioc, MainWindowViewModel main)
{
_main = main;
_timeWatcher = PlaybackCurrentTimeWatcher.Instance;

_events = ioc.Get<IEventAggregator>();
_events.Subscribe(this);

_settings = settings;
Playlist = playlist;

SelectedMidiInput = MidiInputs[0];

_timeWatcher.CurrentTimeChanged += OnSongTick;
Expand All @@ -74,8 +72,8 @@ public LyrePlayerViewModel(IContainer ioc,
{
new ErrorContentDialog(e, closeText: "Ignore").ShowAsync();

_settings.CanUseSpeakers = false;
Settings.UseSpeakers = false;
SettingsView.CanUseSpeakers = false;
Settings.UseSpeakers = false;
}
}

Expand Down Expand Up @@ -124,7 +122,7 @@ public double SongPosition

public Playback? Playback { get; private set; }

public PlaylistViewModel Playlist { get; }
public PlaylistViewModel Playlist => _main.PlaylistView;

public string PlayPauseIcon => Playback?.IsRunning ?? false ? PauseIcon : PlayIcon;

Expand All @@ -135,6 +133,8 @@ public double SongPosition
private MusicDisplayProperties? Display =>
_player?.SystemMediaTransportControls.DisplayUpdater.MusicProperties;

private SettingsPageViewModel SettingsView => _main.SettingsView;

private static string PauseIcon => "\xEDB4";

private static string PlayIcon => "\xF5B0";
Expand Down Expand Up @@ -355,10 +355,10 @@ public void UpdateButtons()

private int ApplyNoteSettings(int noteId)
{
noteId -= Settings.KeyOffset;
noteId -= Playlist.OpenedFile?.History.Key ?? 0;

if (Settings.TransposeNotes)
noteId = LyrePlayer.TransposeNote(noteId, _settings.Transpose ?? Ignore);
noteId = LyrePlayer.TransposeNote(noteId, SettingsView.Transpose.Key);

return noteId;
}
Expand Down Expand Up @@ -388,13 +388,13 @@ private async Task InitializePlayback()

// Check for notes that cannot be played even after transposing.
var outOfRange = midi.GetNotes().Where(note =>
!_settings.SelectedLayout.Key.TryGetKey(ApplyNoteSettings(note.NoteNumber), out _));
!SettingsView.SelectedLayout.Key.TryGetKey(ApplyNoteSettings(note.NoteNumber), out _));

if (_settings.Transpose is null && outOfRange.Any())
if (Playlist.OpenedFile.History.Transpose is null && outOfRange.Any())
{
await Application.Current.Dispatcher.Invoke(async () =>
{
var options = new Enum[] { Up, Down };
var options = new Enum[] { Transpose.Up, Transpose.Down };
var exceptionDialog = new ErrorContentDialog(
new IndexOutOfRangeException(
"Some notes cannot be played by the Lyre because it is missing Sharps & Flats. " +
Expand All @@ -403,17 +403,17 @@ await Application.Current.Dispatcher.Invoke(async () =>
var result = await exceptionDialog.ShowAsync();
_settings.Transpose = result switch
SettingsView.Transpose = result switch
{
ContentDialogResult.None => Ignore,

This comment has been minimized.

Copy link
@nguyennro123

nguyennro123 Jul 22, 2022

write_seting=0

ContentDialogResult.Primary => Up,
ContentDialogResult.Secondary => Down
ContentDialogResult.None => TransposeNames.ElementAt(0),
ContentDialogResult.Primary => TransposeNames.ElementAt(1),
ContentDialogResult.Secondary => TransposeNames.ElementAt(2)
};
});
}

Playback = midi.GetPlayback();
Playback.Speed = _settings.SelectedSpeed.Speed;
Playback.Speed = SettingsView.SelectedSpeed.Speed;

Playback.InterruptNotesOnStop = true;

Expand Down Expand Up @@ -486,7 +486,7 @@ private void PlayNote(NoteEvent noteEvent)
return;
}

var layout = _settings.SelectedLayout.Key;
var layout = SettingsView.SelectedLayout.Key;
var note = ApplyNoteSettings(noteEvent.NoteNumber);

switch (noteEvent.EventType)
Expand Down
12 changes: 6 additions & 6 deletions GenshinLyreMidiPlayer.WPF/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ public class MainWindowViewModel : Conductor<IScreen>.StackNavigation
{
private readonly IContainer _ioc;
private readonly Stack<NavigationViewItem> _history = new();
private NavigationView _navView;
private NavigationView _navView = null!;

public MainWindowViewModel(IContainer ioc, IEventAggregator events)
{
_ioc = ioc;

SettingsView = ioc.Get<SettingsPageViewModel>();
PlaylistView = ioc.Get<PlaylistViewModel>();
PlayerView = new(ioc, SettingsView, PlaylistView);
PianoSheetView = new(SettingsView, PlaylistView);
PlaylistView = new(ioc, this);
SettingsView = new(ioc, this);
PlayerView = new(ioc, this);
PianoSheetView = new(this);
}

public bool ShowUpdate => SettingsView.NeedsUpdate && ActiveItem != SettingsView;
Expand Down Expand Up @@ -58,7 +58,7 @@ protected override async void OnViewLoaded()
}

await using var db = _ioc.Get<LyreContext>();
await PlaylistView.AddFiles(db.History.Select(midi => midi.Path));
await PlaylistView.AddFiles(db.History);
}

private void AutoSuggestBoxOnTextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs e)
Expand Down
15 changes: 5 additions & 10 deletions GenshinLyreMidiPlayer.WPF/ViewModels/PianoSheetViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,17 @@
using Melanchall.DryWetMidi.Interaction;
using PropertyChanged;
using Stylet;
using static GenshinLyreMidiPlayer.WPF.Core.LyrePlayer.Transpose;

namespace GenshinLyreMidiPlayer.WPF.ViewModels;

public class PianoSheetViewModel : Screen
{
private readonly MainWindowViewModel _main;
private uint _bars = 1;
private uint _beats;
private uint _shorten = 1;

public PianoSheetViewModel(SettingsPageViewModel settingsPage,
PlaylistViewModel playlistView)
{
SettingsPage = settingsPage;
PlaylistView = playlistView;
}
public PianoSheetViewModel(MainWindowViewModel main) { _main = main; }

[OnChangedMethod(nameof(Update))] public char Delimiter { get; set; } = '.';

Expand All @@ -32,9 +27,9 @@ public PianoSheetViewModel(SettingsPageViewModel settingsPage,
set => SettingsPage.SelectedLayout = value;
}

public PlaylistViewModel PlaylistView { get; }
public PlaylistViewModel PlaylistView => _main.PlaylistView;

public SettingsPageViewModel SettingsPage { get; }
public SettingsPageViewModel SettingsPage => _main.SettingsView;

public string Result { get; private set; } = string.Empty;

Expand Down Expand Up @@ -84,7 +79,7 @@ public void Update()
foreach (var note in notes)
{
var offset = note.NoteNumber - SettingsPage.KeyOffset;
var transpose = SettingsPage.Transpose ?? Ignore;
var transpose = SettingsPage.Transpose.Key;
var id = LyrePlayer.TransposeNote(offset, transpose);

if (!layout.TryGetKey(id, out var key)) continue;
Expand Down
Loading

0 comments on commit 9ddb784

Please sign in to comment.