Skip to content

Commit

Permalink
Changed MIDI library to NuGet, added playback speed control, disabled…
Browse files Browse the repository at this point in the history
… "Play through speakers" temporarilly
  • Loading branch information
ianespana committed Sep 24, 2020
1 parent a7951a5 commit 7331c18
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 54 deletions.
4 changes: 3 additions & 1 deletion ShawzinBot/ActionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
using System.Windows;
using System.Windows.Forms;
using System.Windows.Shapes;
using Melanchall.DryWetMidi.Smf;
using Melanchall.DryWetMidi;
using Melanchall.DryWetMidi.Core;
using ShawzinBot.ViewModels;
using Keyboard = InputManager.Keyboard;
using Timer = System.Threading.Timer;

Expand Down
Binary file removed ShawzinBot/Libraries/InputManager.dll
Binary file not shown.
Binary file removed ShawzinBot/Libraries/Melanchall.DryWetMidi.dll
Binary file not shown.
14 changes: 14 additions & 0 deletions ShawzinBot/Models/MidiSpeedModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace ShawzinBot.Models
{
public class MidiSpeedModel
{
public string SpeedName { get; private set; }
public double Speed { get; private set; }

public MidiSpeedModel(string speedName, double speed)
{
SpeedName = speedName;
Speed = speed;
}
}
}
2 changes: 1 addition & 1 deletion ShawzinBot/Models/MidiTrackModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Melanchall.DryWetMidi.Smf;
using Melanchall.DryWetMidi.Core;
using System.Linq;

namespace ShawzinBot.Models
Expand Down
10 changes: 3 additions & 7 deletions ShawzinBot/ShawzinBot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,8 @@
<Reference Include="MaterialDesignThemes.Wpf, Version=3.2.0.1979, Culture=neutral, PublicKeyToken=df2a72020bd7962a, processorArchitecture=MSIL">
<HintPath>..\packages\MaterialDesignThemes.3.2.0\lib\net45\MaterialDesignThemes.Wpf.dll</HintPath>
</Reference>
<Reference Include="Melanchall.DryWetMidi, Version=4.1.1.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>Libraries\Melanchall.DryWetMidi.dll</HintPath>
<Reference Include="Melanchall.DryWetMidi, Version=5.1.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Melanchall.DryWetMidi.5.1.1\lib\net45\Melanchall.DryWetMidi.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
Expand Down Expand Up @@ -118,6 +117,7 @@
<DependentUpon>App.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Include="Models\MidiSpeedModel.cs" />
<Compile Include="Models\MidiTrackModel.cs" />
<Compile Include="Models\MidiInputModel.cs" />
<Compile Include="ViewModels\MainViewModel.cs" />
Expand Down Expand Up @@ -177,10 +177,6 @@
<Install>false</Install>
</BootstrapperPackage>
</ItemGroup>
<ItemGroup>
<Content Include="Libraries\InputManager.dll" />
<Content Include="Libraries\Melanchall.DryWetMidi.dll" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
Expand Down
120 changes: 79 additions & 41 deletions ShawzinBot/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
using System.Linq;
using System.Timers;
using Caliburn.Micro;
using Melanchall.DryWetMidi.Core;
using Melanchall.DryWetMidi.Devices;
using Melanchall.DryWetMidi.Smf;
using Melanchall.DryWetMidi.Smf.Interaction;
using Melanchall.DryWetMidi.Interaction;
using Microsoft.Win32;
using ShawzinBot.Models;
using InputDevice = Melanchall.DryWetMidi.Devices.InputDevice;
Expand All @@ -26,7 +26,9 @@ public class MainViewModel : Screen

private BindableCollection<MidiInputModel> _midiInputs = new BindableCollection<MidiInputModel>();
private BindableCollection<MidiTrackModel> _midiTracks = new BindableCollection<MidiTrackModel>();
private BindableCollection<MidiSpeedModel> _midiSpeeds = new BindableCollection<MidiSpeedModel>();
private MidiInputModel _selectedMidiInput;
private MidiSpeedModel _selectedMidiSpeed;

private bool _enableVibrato = true;
private bool _transposeNotes = true;
Expand All @@ -49,6 +51,8 @@ public class MainViewModel : Screen
private TrackChunk firstTrack;

private Timer playTimer;
private OutputDevice device;
private ITimeSpan playTime = new MidiTimeSpan();

#endregion

Expand Down Expand Up @@ -76,6 +80,17 @@ public MainViewModel()

SelectedMidiInput = MidiInputs[0];

MidiSpeeds.Add(new MidiSpeedModel("0.25", 0.25));
MidiSpeeds.Add(new MidiSpeedModel("0.5", 0.5));
MidiSpeeds.Add(new MidiSpeedModel("0.75", 0.75));
MidiSpeeds.Add(new MidiSpeedModel("Normal", 1));
MidiSpeeds.Add(new MidiSpeedModel("1.25", 1.25));
MidiSpeeds.Add(new MidiSpeedModel("1.5", 1.5));
MidiSpeeds.Add(new MidiSpeedModel("1.75", 1.75));
MidiSpeeds.Add(new MidiSpeedModel("2", 2));

SelectedMidiSpeed = MidiSpeeds[3];

EnableVibrato = Properties.Settings.Default.EnableVibrato;
TransposeNotes = Properties.Settings.Default.TransposeNotes;
PlayThroughSpeakers = Properties.Settings.Default.PlayThroughSpeakers;
Expand Down Expand Up @@ -169,16 +184,6 @@ public BindableCollection<MidiInputModel> MidiInputs
}
}

public BindableCollection<MidiTrackModel> MidiTracks
{
get => _midiTracks;
set
{
_midiTracks = value;
NotifyOfPropertyChange(() => MidiTracks);
}
}

public MidiInputModel SelectedMidiInput
{
get => _selectedMidiInput;
Expand All @@ -199,6 +204,41 @@ public MidiInputModel SelectedMidiInput
}
}

public BindableCollection<MidiSpeedModel> MidiSpeeds
{
get => _midiSpeeds;
set
{
_midiSpeeds = value;
NotifyOfPropertyChange(() => MidiSpeeds);
}
}

public MidiSpeedModel SelectedMidiSpeed
{
get => _selectedMidiSpeed;
set
{
_selectedMidiSpeed = value;
NotifyOfPropertyChange(() => SelectedMidiSpeed);

if (value?.Speed != null && playback != null)
{
playback.Speed = value.Speed;
}
}
}

public BindableCollection<MidiTrackModel> MidiTracks
{
get => _midiTracks;
set
{
_midiTracks = value;
NotifyOfPropertyChange(() => MidiTracks);
}
}

public bool EnableVibrato
{
get => _enableVibrato;
Expand Down Expand Up @@ -228,27 +268,24 @@ public bool PlayThroughSpeakers
get => _playThroughSpeakers;
set
{
_playThroughSpeakers = value;
//_playThroughSpeakers = value;
_playThroughSpeakers = false;
Properties.Settings.Default.PlayThroughSpeakers = value;
Properties.Settings.Default.Save();
NotifyOfPropertyChange(() => PlayThroughSpeakers);

if (playback != null)
/*if (playback != null)
{
if (!_playThroughSpeakers && playback.OutputDevice != null)
if (!_playThroughSpeakers && device != null)
{
playback.OutputDevice.Dispose();
playback.OutputDevice = null;
device.Dispose();
}
else if (_playThroughSpeakers && (playback.OutputDevice == null))
else if (_playThroughSpeakers && (device == null))
{
var list = OutputDevice.GetAll();
if (list.Count() >= 1)
{
playback.OutputDevice = list.FirstOrDefault();
}
device = OutputDevice.GetByName("Microsoft GS Wavetable Synth");
playback.OutputDevice = device;
}
}
}*/
}
}

Expand Down Expand Up @@ -302,15 +339,13 @@ public void CloseFile()
if (playback != null)
{
playback.Stop();
playback.OutputDevice?.Dispose();

PlaybackCurrentTimeWatcher.Instance.RemovePlayback(playback);

playback?.Dispose();
playback.Dispose();
playback = null;
}

playback = null;
midiFile = null;
MidiTracks.Clear();

PlayPauseIcon = "Play";
SongName = "";
Expand All @@ -323,13 +358,12 @@ public void PlayPause()
{
if (midiFile == null || MaximumTime == 0d) return;
if (playback == null || reloadPlayback)
{
ITimeSpan playTime = new MidiTimeSpan();
{
if (playback != null)
{
playback.Stop();
playTime = playback.GetCurrentTime(TimeSpanType.Midi);
playback.OutputDevice?.Dispose();
playback.Dispose();
playback = null;
PlayPauseIcon = "Play";
}
Expand All @@ -346,6 +380,12 @@ public void PlayPause()
}

playback = midiFile.GetPlayback();
playback.Speed = SelectedMidiSpeed.Speed;
if (PlayThroughSpeakers)
{
device = OutputDevice.GetByName("Microsoft GS Wavetable Synth");
playback.OutputDevice = device;
}
playback.MoveToTime(playTime);
playback.Finished += (s, e) =>
{
Expand All @@ -365,6 +405,10 @@ public void PlayPause()
PlayPauseIcon = "Play";
playback.Stop();
}
else if (PlayPauseIcon == "Pause") {
PlayPauseIcon = "Play";
playTimer.Dispose();
}
else
{
PlayPauseIcon = "Pause";
Expand All @@ -373,23 +417,16 @@ public void PlayPause()
playTimer = new Timer();
playTimer.Interval = 100;
playTimer.Elapsed += new ElapsedEventHandler(PlayTimerElapsed);
playTimer.AutoReset = false;
playTimer.Start();
}
UpdateScale(ActionManager.activeScale);
}

private void PlayTimerElapsed(object sender, ElapsedEventArgs e)
{
if (ActionManager.IsWindowFocused("Warframe") || PlayThroughSpeakers)
{
playback.Start();
playTimer.Stop();
}
else
{
playTimer.Stop();
playTimer.Start();
playTimer.Dispose();
}
}

Expand Down Expand Up @@ -447,14 +484,15 @@ public void OnNoteEvent(object sender, MidiEventPlayedEventArgs e)
{
case MidiEventType.SetTempo:
var tempo = e.Event as SetTempoEvent;
playback.Speed = tempo.MicrosecondsPerQuarterNote;
//playback.Speed = tempo.MicrosecondsPerQuarterNote;
return;
case MidiEventType.NoteOn:
var note = e.Event as NoteOnEvent;
if (note != null && note.Velocity <= 0) return;

//Check if the user has tabbed out of warframe, and stop playback to avoid Scale issues
if (!(ActionManager.PlayNote(note, EnableVibrato, TransposeNotes) || PlayThroughSpeakers)) PlayPause();
UpdateScale(ActionManager.activeScale);
return;
default:
return;
Expand Down
13 changes: 9 additions & 4 deletions ShawzinBot/Views/MainView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
xmlns:local="clr-namespace:ShawzinBot.Views"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:cal="http://www.caliburnproject.org"
Expand All @@ -15,7 +16,7 @@
ResizeMode="NoResize"
WindowStartupLocation="CenterScreen"
WindowStyle="None"
Title="MainView" Height="500" Width="300">
Title="MainView" Height="510" Width="300">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20"/>
Expand Down Expand Up @@ -78,7 +79,7 @@
<RowDefinition Height="40"/>
<RowDefinition Height="70"/>
<RowDefinition Height="120"/>
<RowDefinition Height="120"/>
<RowDefinition Height="130"/>
</Grid.RowDefinitions>

<Ellipse Grid.Row="0" Width="150" Height="150" HorizontalAlignment="Center" VerticalAlignment="Center" StrokeThickness="1">
Expand Down Expand Up @@ -191,12 +192,16 @@
</ItemsControl>
</ScrollViewer>
</Grid>
<Grid Grid.Row="5" VerticalAlignment="Center" Background="#44444444" Height="110">
<Grid Grid.Row="5" VerticalAlignment="Center" Background="#44444444" Height="120">
<TextBlock Text="Settings" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="10,5"/>
<TextBlock x:Name="Scale" Text="Scale: Chromatic" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="10,25"/>
<CheckBox x:Name="EnableVibrato" Content="Enable vibrato" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="10,45,0,0" ClickMode="Press"/>
<CheckBox x:Name="TransposeNotes" Content="Transpose notes" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="10,65,0,0" ClickMode="Press"/>
<CheckBox x:Name="PlayThroughSpeakers" Content="Play MIDI through speakers" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="10,85,0,0" ClickMode="Press"/>
<TextBlock Text="Playback speed:" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="10,91,0,0"/>
<ComboBox x:Name="MidiSpeeds" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="115,85,0,0" Width="65" Height="30" Background="#FF444444"
SelectedItem="{Binding Path=SelectedMidiSpeed, Mode=TwoWay}"
DisplayMemberPath="SpeedName"/>
<CheckBox x:Name="PlayThroughSpeakers" Content="Play MIDI through speakers" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="10,85,0,0" ClickMode="Press" Visibility="Hidden"/>
</Grid>
</Grid>
</Grid>
Expand Down
1 change: 1 addition & 0 deletions ShawzinBot/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
<package id="InputManager" version="1.0.0" targetFramework="net472" />
<package id="MaterialDesignColors" version="1.2.7" targetFramework="net472" />
<package id="MaterialDesignThemes" version="3.2.0" targetFramework="net472" />
<package id="Melanchall.DryWetMidi" version="5.1.1" targetFramework="net472" />
<package id="System.ValueTuple" version="4.5.0" targetFramework="net472" />
</packages>

0 comments on commit 7331c18

Please sign in to comment.