From 3b04429533969897231845bb1f922863c06e3fca Mon Sep 17 00:00:00 2001 From: std66 Date: Thu, 1 Nov 2018 12:30:00 +0100 Subject: [PATCH] Initial implementation of MIDI playback. --- TomiSoft.LyricsEditor/MainWindow.xaml | 2 +- .../TomiSoft.LyricsEditor.csproj | 3 + .../Playback/BASS/LocalAudioFilePlayback.cs | 3 - .../Playback/BASS/LocalMidiFilePlayback.cs | 25 ++++++++ .../Playback/PlaybackFactory.cs | 63 ++++++++++--------- TomiSoft.MP3Player/TomiSoft.MP3Player.csproj | 1 + .../Controls/PlaybackControl.xaml | 53 ++++++++-------- 7 files changed, 91 insertions(+), 59 deletions(-) create mode 100644 TomiSoft.MP3Player/Playback/BASS/LocalMidiFilePlayback.cs diff --git a/TomiSoft.LyricsEditor/MainWindow.xaml b/TomiSoft.LyricsEditor/MainWindow.xaml index 981ff10..d27eb45 100644 --- a/TomiSoft.LyricsEditor/MainWindow.xaml +++ b/TomiSoft.LyricsEditor/MainWindow.xaml @@ -107,5 +107,5 @@ - + diff --git a/TomiSoft.LyricsEditor/TomiSoft.LyricsEditor.csproj b/TomiSoft.LyricsEditor/TomiSoft.LyricsEditor.csproj index 3d2a815..3f62bcb 100644 --- a/TomiSoft.LyricsEditor/TomiSoft.LyricsEditor.csproj +++ b/TomiSoft.LyricsEditor/TomiSoft.LyricsEditor.csproj @@ -41,6 +41,7 @@ + @@ -50,6 +51,8 @@ 4.0 + + diff --git a/TomiSoft.MP3Player/Playback/BASS/LocalAudioFilePlayback.cs b/TomiSoft.MP3Player/Playback/BASS/LocalAudioFilePlayback.cs index 52e7701..de115e5 100644 --- a/TomiSoft.MP3Player/Playback/BASS/LocalAudioFilePlayback.cs +++ b/TomiSoft.MP3Player/Playback/BASS/LocalAudioFilePlayback.cs @@ -1,13 +1,10 @@ using System; using System.IO; -using System.Runtime.InteropServices; using System.Threading.Tasks; -using TomiSoft.MP3Player.Encoder.Lame; using TomiSoft.MP3Player.MediaInformation; using TomiSoft.MP3Player.Utils; using TomiSoft.MP3Player.Utils.Extensions; using Un4seen.Bass; -using Un4seen.Bass.AddOn.Enc; namespace TomiSoft.MP3Player.Playback.BASS { /// diff --git a/TomiSoft.MP3Player/Playback/BASS/LocalMidiFilePlayback.cs b/TomiSoft.MP3Player/Playback/BASS/LocalMidiFilePlayback.cs new file mode 100644 index 0000000..671639d --- /dev/null +++ b/TomiSoft.MP3Player/Playback/BASS/LocalMidiFilePlayback.cs @@ -0,0 +1,25 @@ +using TomiSoft.MP3Player.MediaInformation; +using TomiSoft.MP3Player.Utils; +using Un4seen.Bass.AddOn.Midi; + +namespace TomiSoft.MP3Player.Playback.BASS { + class LocalMidiFilePlayback : LocalAudioFilePlayback { + public LocalMidiFilePlayback(string Filename, string SoundfontFilename) : base(Filename) { + this.ApplySoundfontToStream(SoundfontFilename); + } + + public LocalMidiFilePlayback(UnmanagedStream MediaStream, ISongInfo SongInfo, string SoundfontFilename) + : base(MediaStream, SongInfo) { + this.ApplySoundfontToStream(SoundfontFilename); + } + + private void ApplySoundfontToStream(string SoundfontFilename) { + int FontHandle = BassMidi.BASS_MIDI_FontInit(SoundfontFilename); + if (FontHandle != 0) { + BASS_MIDI_FONT Font = new BASS_MIDI_FONT(FontHandle, -1, 0); + + BassMidi.BASS_MIDI_StreamSetFonts(this.ChannelID, new BASS_MIDI_FONT[] { Font }, 1); // apply it to the stream + } + } + } +} diff --git a/TomiSoft.MP3Player/Playback/PlaybackFactory.cs b/TomiSoft.MP3Player/Playback/PlaybackFactory.cs index b0e4cac..5fd5e93 100644 --- a/TomiSoft.MP3Player/Playback/PlaybackFactory.cs +++ b/TomiSoft.MP3Player/Playback/PlaybackFactory.cs @@ -13,11 +13,6 @@ namespace TomiSoft.MP3Player.Playback { /// Provides a simple way of loading media to play. /// public class PlaybackFactory { - /// - /// Stores the last IPlaybackManager instance. - /// - private static IPlaybackManager lastInstance; - public static event Action MediaOpenProgressChanged; /// @@ -31,8 +26,8 @@ public async static Task LoadMedia(ISongInfo SongInfo) { #region Error checking if (SongInfo == null) throw new ArgumentNullException(nameof(SongInfo)); - #endregion - + #endregion + //If the Source is file: if (File.Exists(SongInfo.Source)) { string Extension = PlayerUtils.GetFileExtension(SongInfo.Source); @@ -42,21 +37,32 @@ public async static Task LoadMedia(ISongInfo SongInfo) { //In case of any file supported by BASS: if (BassManager.GetSupportedExtensions().Contains(Extension)) { - //If Audio CD track: - if (Extension == "cda") { - lastInstance = new AudioCdPlayback(SongInfo.Source); - return lastInstance; - } - - //Any other stuff... - using (Stream SourceStream = File.OpenRead(SongInfo.Source)) { - lastInstance = new LocalAudioFilePlayback( - await UnmanagedStream.CreateFromStream(SourceStream, FileOpenProgress), - SongInfo - ); - } - - return lastInstance; + //If Audio CD track: + if (Extension == "cda") { + return new AudioCdPlayback(SongInfo.Source); + } + else { + using (Stream SourceStream = File.OpenRead(SongInfo.Source)) { + var unmanagedStream = await UnmanagedStream.CreateFromStream(SourceStream, FileOpenProgress); + + //If Midi file: + if (new string[] { "mid", "midi", "kar", "rmi" }.Contains(Extension)) { + return new LocalMidiFilePlayback( + unmanagedStream, + SongInfo, + @"C:\SGM-V2.01.sf2" + ); + } + + //Any other stuff... + else { + return new LocalAudioFilePlayback( + unmanagedStream, + SongInfo + ); + } + } + } } } @@ -67,10 +73,11 @@ await UnmanagedStream.CreateFromStream(SourceStream, FileOpenProgress), Progress Progress = new Progress(); Progress.ProgressChanged += OpenFileProgressChanged; - lastInstance = await YoutubePlayback.DownloadVideoAsync(SongInfo, Progress); + IPlaybackManager Result = await YoutubePlayback.DownloadVideoAsync(SongInfo, Progress); Progress.ProgressChanged -= OpenFileProgressChanged; - return lastInstance; + + return Result; } } @@ -95,11 +102,9 @@ private static void OpenFileProgressChanged(object sender, LongOperationProgress /// The volume to initialize with. /// An that represents the Null-Playback manager instance public static IPlaybackManager NullPlayback(int Volume) { - lastInstance = new NullPlayback() { - Volume = Volume - }; - - return lastInstance; + return new NullPlayback() { + Volume = Volume + }; } /// diff --git a/TomiSoft.MP3Player/TomiSoft.MP3Player.csproj b/TomiSoft.MP3Player/TomiSoft.MP3Player.csproj index f55ee04..fa4fc78 100644 --- a/TomiSoft.MP3Player/TomiSoft.MP3Player.csproj +++ b/TomiSoft.MP3Player/TomiSoft.MP3Player.csproj @@ -109,6 +109,7 @@ + diff --git a/TomiSoft.MP3Player/UserInterface/Controls/PlaybackControl.xaml b/TomiSoft.MP3Player/UserInterface/Controls/PlaybackControl.xaml index 266120d..e7045c3 100644 --- a/TomiSoft.MP3Player/UserInterface/Controls/PlaybackControl.xaml +++ b/TomiSoft.MP3Player/UserInterface/Controls/PlaybackControl.xaml @@ -7,55 +7,56 @@ xmlns:local="clr-namespace:TomiSoft_MP3_Player" x:Class="TomiSoft_MP3_Player.PlaybackControl" mc:Ignorable="d" d:DesignHeight="93.999" d:DesignWidth="267"> - - - - + + + + - - - + + + - - + + - + -