From 0097334902854e3289c4dae71df3a7509456d5fa Mon Sep 17 00:00:00 2001 From: Don Archer Date: Sun, 7 Aug 2016 16:33:27 -0700 Subject: [PATCH] moved player node outside of songs for speed --- FirebaseKaraoke/FirebaseController.cs | 21 +++--- Herse.Models/PlayerState.cs | 6 +- KaraokePlayer/App.config | 2 +- KaraokePlayer/MainForm.cs | 10 +-- SongCrawler/Program.cs | 100 ++++++++++++++++---------- 5 files changed, 85 insertions(+), 54 deletions(-) diff --git a/FirebaseKaraoke/FirebaseController.cs b/FirebaseKaraoke/FirebaseController.cs index 5bdcbb9..1be1b72 100644 --- a/FirebaseKaraoke/FirebaseController.cs +++ b/FirebaseKaraoke/FirebaseController.cs @@ -17,18 +17,22 @@ namespace KaraokePlayer.Classes AuthSecret = ConfigurationManager.AppSettings["Firebase.Secret"], BasePath = ConfigurationManager.AppSettings["Firebase.Path"] }; - private PlayerState _state = PlayerState.playing; + private PlayerState _state = PlayerState.Playing; private IFirebaseClient _client; private ControllerStateChangedEventHandler _stateChanged; private ControllerSongChangedEventHandler _songChanged; + private string QueuePath + { + get { return string.Format("controllers/{0}/player/queue", this.Id); } + } private string StatePath { - get { return string.Format("controllers/{0}/state/", this.Id); } + get { return string.Format("controllers/{0}/player/state/", this.Id); } } private string CurrentSongPath { - get { return string.Format("controllers/{0}/queue/0/", this.Id); } + get { return string.Format("controllers/{0}/player/queue/0/", this.Id); } } public string Id { get; set; } @@ -36,17 +40,16 @@ namespace KaraokePlayer.Classes public void EndSong() { - //_client.DeleteAsync(CurrentSongPath.TrimEnd('/')); - var response = _client.Get(string.Format("controllers/{0}/queue", this.Id)); + var response = _client.Get(QueuePath); List queue = response.ResultAs>(); queue.RemoveAt(0); - _client.Set(string.Format("controllers/{0}/queue", this.Id), queue); + _client.Set(QueuePath, queue); } public void SetState(PlayerState state) { _state = state; - _client.SetAsync(StatePath, "playing"); + _client.SetAsync(StatePath, "Playing"); } public FirebaseController(string clientId, ControllerStateChangedEventHandler stateChanged, ControllerSongChangedEventHandler songChanged) @@ -80,7 +83,7 @@ namespace KaraokePlayer.Classes changed: (s, args, obj) => { Console.WriteLine("state changed " + args.Data); - PlayerState newstate = (PlayerState)Enum.Parse(typeof(PlayerState), args.Data); + PlayerState newstate = (PlayerState)Enum.Parse(typeof(PlayerState), args.Data, true); StateDidChange(newstate); }, removed: null @@ -89,7 +92,7 @@ namespace KaraokePlayer.Classes private void CurrentSongDidChange() { - var response = _client.Get(CurrentSongPath); + var response = _client.Get(CurrentSongPath); var item = response.ResultAs(); if (item == null) return; if (CurrentSong == null || CurrentSong.Path != item.Song.Path) diff --git a/Herse.Models/PlayerState.cs b/Herse.Models/PlayerState.cs index 07a72a1..05c6fd8 100644 --- a/Herse.Models/PlayerState.cs +++ b/Herse.Models/PlayerState.cs @@ -8,8 +8,8 @@ namespace Herse.Models { public enum PlayerState { - playing, - stopped, - paused + Playing, + Stopped, + Paused } } diff --git a/KaraokePlayer/App.config b/KaraokePlayer/App.config index 4fc68df..8520856 100644 --- a/KaraokePlayer/App.config +++ b/KaraokePlayer/App.config @@ -8,7 +8,7 @@ - + diff --git a/KaraokePlayer/MainForm.cs b/KaraokePlayer/MainForm.cs index 10e1d8c..3653bb6 100644 --- a/KaraokePlayer/MainForm.cs +++ b/KaraokePlayer/MainForm.cs @@ -33,7 +33,7 @@ namespace KaraokePlayer player.OnSongEnded += SongEnded; this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; - //this.WindowState = FormWindowState.Maximized; + this.WindowState = FormWindowState.Maximized; this.ShowInTaskbar = true; @@ -43,13 +43,13 @@ namespace KaraokePlayer { switch (args.State) { - case PlayerState.playing: + case PlayerState.Playing: this.Invoke(new Action(() => { player.play(); })); break; - case PlayerState.paused: + case PlayerState.Paused: this.Invoke(new Action(() => { player.pause(); })); break; - case PlayerState.stopped: + case PlayerState.Stopped: this.Invoke(new Action(() => { player.stop(); })); break; } @@ -69,7 +69,7 @@ namespace KaraokePlayer songInfoForm.Show(); await Task.Delay(TimeSpan.FromSeconds(5)); player.play(); - controller.SetState(PlayerState.playing); + controller.SetState(PlayerState.Playing); songInfoForm.Hide(); } } diff --git a/SongCrawler/Program.cs b/SongCrawler/Program.cs index 1999f61..4fb9fa1 100644 --- a/SongCrawler/Program.cs +++ b/SongCrawler/Program.cs @@ -27,32 +27,58 @@ namespace SongCrawler string firepath = string.Format("controllers/{0}/songs", controller); List files = new List(); - files.AddRange(Directory.GetFiles(songpath, "*.mp3", SearchOption.AllDirectories)); - files.AddRange(Directory.GetFiles(songpath, "*.zip", SearchOption.AllDirectories)); + files.AddRange(FindFiles("mp3", songpath)); + files.AddRange(FindFiles("zip", songpath)); + files.AddRange(FindFiles("mp4", songpath)); + List songs = new List(); - string id3path = null; - Song song; + Song song = null; + int i = 0; foreach (string filepath in files) { - Console.WriteLine(filepath); - var ext = Path.GetExtension(filepath).ToLower(); - switch (ext) + i++; + try { - case ".mp3": - id3path = filepath; - break; - case ".zip": - id3path = UnzipMp3(filepath); - break; + song = MakeSong(filepath); + Console.WriteLine(string.Format("{0:000000}/{1} - {2}", i, files.Count, song.Title)); + songs.Add(song); + } + catch(Exception ex) + { + Console.WriteLine(ex.Message); } - song = ReadId3(id3path); - CheckTitle(song); - song.Path = filepath; - songs.Add(song); } SetResponse response = client.Set(firepath, songs); } + private static Song MakeSong(string filepath) + { + + Song song = null; + var ext = Path.GetExtension(filepath).ToLower(); + switch (ext) + { + case ".mp3": + case ".mp4": + song = ReadId3(filepath); + break; + case ".zip": + song = ReadId3FromZip(filepath); + break; + } + CheckTitle(song); + song.Path = filepath; + return song; + } + + private static string[] FindFiles(string ext, string path) + { + Console.Write(string.Format("\rscanning {0} for {1} - ", path, ext)); + string[] files = Directory.GetFiles(path, "*." + ext, SearchOption.AllDirectories); + Console.WriteLine(string.Format("{0} found", files.Length)); + return files; + } + private static void CheckTitle(Song song) { if(string.IsNullOrEmpty(song.Title)) @@ -71,31 +97,33 @@ namespace SongCrawler } } - private static string UnzipMp3(string filepath) + private static Song ReadId3FromZip(string zippath) { - string file = Path.GetFileNameWithoutExtension(filepath); - try - { - ZipFile.ExtractToDirectory(filepath, "c:\\temp"); - } - catch - { - // do nothing fancy - } - return "c:\\temp\\" + file + ".mp3"; + ZipFile.ExtractToDirectory(zippath, "c:\\temp"); + string filepath = Directory.GetFiles("c:\\temp", "*.mp3")[0]; + Song song = ReadId3(filepath); + foreach (string file in Directory.GetFiles("c:\\temp")) File.Delete(file); + return song; } static Song ReadId3(string path) { - TagLib.File tagFile = TagLib.File.Create(path); - return new Song() + Song song = new Song(); + TagLib.File tagFile; + try { - Title = tagFile.Tag.Title, - Artist = tagFile.Tag.FirstPerformer, - Path = path, - Genre = tagFile.Tag.FirstGenre, - Year = (int)tagFile.Tag.Year - }; + tagFile = TagLib.File.Create(path); + song.Title = tagFile.Tag.Title; + song.Artist = tagFile.Tag.FirstPerformer; + song.Genre = tagFile.Tag.FirstGenre; + song.Year = (int)tagFile.Tag.Year; + } + catch + { + // do nothing; + } + song.Path = path; + return song; } } }