Skip to content

Commit

Permalink
Allow MIDI instruments to be used
Browse files Browse the repository at this point in the history
  • Loading branch information
sabihoshi committed Apr 3, 2021
1 parent 374e564 commit 0f7898f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 21 deletions.
2 changes: 1 addition & 1 deletion GenshinLyreMidiPlayer/GenshinLyreMidiPlayer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<UseWPF>true</UseWPF>
<StartupObject>GenshinLyreMidiPlayer.App</StartupObject>
<ApplicationManifest>app.manifest</ApplicationManifest>
<Version>1.1.1</Version>
<Version>1.2.0</Version>
<ApplicationIcon>item_windsong_lyre.ico</ApplicationIcon>
</PropertyGroup>

Expand Down
60 changes: 45 additions & 15 deletions GenshinLyreMidiPlayer/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,26 @@ namespace GenshinLyreMidiPlayer.ViewModels
public class MainWindowViewModel : Screen, IHandle<MidiTrackModel>
{
private bool _ignoreSliderChange;
private InputDevice _inputDevice;
private int _keyOffset;
private MidiFile _midiFile;
private Playback _playback;
private ITimeSpan _playTime = new MidiTimeSpan();
private Timer _playTimer;
private bool _reloadPlayback;
private MidiInputModel _selectedMidiInput;
private double _songSlider;

public MainWindowViewModel()
{
SelectedSpeed = MidiSpeeds[3];
SelectedSpeed = MidiSpeeds[3];
SelectedMidiInput = MidiInputs[0];
}

public BindableCollection<MidiInputModel> MidiInputs { get; set; }
public BindableCollection<MidiInputModel> MidiInputs { get; set; } = new BindableCollection<MidiInputModel>
{
new MidiInputModel("None")
};

public bool TransposeNotes { get; set; } = true;

Expand Down Expand Up @@ -115,6 +121,8 @@ public double SongSlider
}
}

public IEnumerable<MidiTrackModel> MidiTracks { get; set; }

public int MinOffset => KeyOffsets.Keys.Min();

public int MaxOffset => KeyOffsets.Keys.Max();
Expand All @@ -137,9 +145,24 @@ public int KeyOffset
new MidiSpeedModel("2x", 2)
};

public List<MidiTrackModel> MidiTracks { get; set; } = new List<MidiTrackModel>();
public MidiInputModel SelectedMidiInput
{
get => _selectedMidiInput;
set
{
SetAndNotify(ref _selectedMidiInput, value);

_inputDevice?.Dispose();

public MidiInputModel SelectedMidiInput { get; set; }
if (_selectedMidiInput?.DeviceName != null && _selectedMidiInput.DeviceName != "None")
{
_inputDevice = InputDevice.GetByName(_selectedMidiInput.DeviceName);

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

public MidiSpeedModel SelectedSpeed { get; set; }

Expand Down Expand Up @@ -185,8 +208,7 @@ public void OpenFile()

MidiTracks = _midiFile
.GetTrackChunks()
.Select(t => new MidiTrackModel(t))
.ToList();
.Select(t => new MidiTrackModel(t));
MidiTracks.First().IsChecked = true;
}

Expand All @@ -200,8 +222,8 @@ public void CloseFile()
_playback = null;
}

_midiFile = null;
MidiTracks.Clear();
_midiFile = null;
MidiTracks = Enumerable.Empty<MidiTrackModel>();

PlayPauseIcon = PlayIcon;
SongName = string.Empty;
Expand Down Expand Up @@ -290,16 +312,24 @@ public void OnSongTick(object sender, PlaybackCurrentTimeChangedEventArgs e)
}
}

public void OnNoteEvent(object sender, MidiEventPlayedEventArgs e)
private void OnNoteEvent(object sender, MidiEventPlayedEventArgs e)
{
if (e.Event.EventType == MidiEventType.NoteOn)
{
var note = e.Event as NoteOnEvent;
if (note != null && note.Velocity <= 0) return;
PlayNote(e.Event as NoteOnEvent);
}

if (!LyrePlayer.PlayNote(note, TransposeNotes, KeyOffset))
PlayPause();
}
private void OnNoteEvent(object sender, MidiEventReceivedEventArgs e)
{
if (e.Event.EventType == MidiEventType.NoteOn)
PlayNote(e.Event as NoteOnEvent);
}

private void PlayNote(NoteOnEvent note)
{
if (note != null && note.Velocity <= 0) return;

if (!LyrePlayer.PlayNote(note, TransposeNotes, KeyOffset))
PlayPause();
}

private void PlayTimerElapsed(object sender, ElapsedEventArgs e)
Expand Down
23 changes: 18 additions & 5 deletions GenshinLyreMidiPlayer/Views/MainWindowView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
d:DataContext="{d:DesignInstance Type=viewModels:MainWindowViewModel}"

Title="Genshin Lyre MIDI Player" Height="750" Width="400"
Title="Genshin Lyre MIDI Player" Height="800" Width="400"
ui:WindowHelper.UseModernWindowStyle="True">

<Grid Margin="10">
Expand All @@ -23,6 +23,7 @@

<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>

<Grid Grid.Row="0">
Expand Down Expand Up @@ -77,9 +78,7 @@
Text="{Binding TotalTime}" />
</Grid>

<GroupBox
Grid.Row="3"
Header="MIDI Tracks">
<GroupBox Grid.Row="3" Header="MIDI Tracks">
<ui:SimpleStackPanel>
<Separator Margin="0,-20,0,0" />
<ScrollViewer>
Expand All @@ -97,7 +96,7 @@
</ui:SimpleStackPanel>
</GroupBox>

<GroupBox Grid.Row="4" Header="Settings">
<GroupBox Grid.Row="5" Header="Settings">
<ui:SimpleStackPanel>
<Separator Margin="0,-20,0,0" />
<StackPanel Orientation="Horizontal">
Expand All @@ -109,7 +108,21 @@
Header="{Binding Key}" SpinButtonPlacementMode="Compact"
Minimum="{Binding MinOffset}" Maximum="{Binding MaxOffset}"
Value="{Binding KeyOffset}" />
</StackPanel>

<StackPanel Orientation="Horizontal">
<GroupBox Header="MIDI Instrument">
<ComboBox
ItemsSource="{Binding MidiInputs}"
SelectedItem="{Binding SelectedMidiInput}"
DisplayMemberPath="DeviceName" />
</GroupBox>

<Button Command="{s:Action RefreshDevices}"
Background="Transparent"
Margin="10,35,0,0">
<ui:FontIcon Glyph="&#xE72C;"/>
</Button>
</StackPanel>

<GroupBox Header="Speed">
Expand Down

0 comments on commit 0f7898f

Please sign in to comment.