private void Button_Click_1(object sender, RoutedEventArgs e) { BeatMachineDataContext context = new BeatMachineDataContext( BeatMachineDataContext.DBConnectionString); context.AnalyzedSongs.DeleteAllOnSubmit( context.AnalyzedSongs.ToList()); context.SubmitChanges(); }
public SettingsViewModel(IMessenger messenger) : base(messenger) { ////if (IsInDesignMode) ////{ //// // Code runs in Blend --> create design time data. ////} ////else ////{ //// // Code runs "for real" ////} MessengerInstance.Register<NewSongsAddedMessage>( this, m => { DispatcherHelper.CheckBeginInvokeOnUI(() => { SongsAnalyzed = true; }); }); ReanalyzeSongsCommand = new RelayCommand(() => { MessengerInstance.Send<MediaPlayerStopMessage>( new MediaPlayerStopMessage()); using (BeatMachineDataContext context = new BeatMachineDataContext( BeatMachineDataContext.DBConnectionString)) { context.AnalyzedSongs.DeleteAllOnSubmit( context.AnalyzedSongs.ToList()); context.Summary.DeleteAllOnSubmit( context.Summary.ToList()); context.SubmitChanges(); } }); SendErrorLogsCommand = new RelayCommand(() => { MemoryTarget target = LogManager.Configuration.FindTargetByName("memory") as MemoryTarget; StringBuilder sb = new StringBuilder(); foreach (string entry in target.Logs) { sb.AppendLine(entry); } EmailComposeTask emailComposeTask = new EmailComposeTask(); emailComposeTask.Subject = "mu:ru error log"; emailComposeTask.Body = sb.ToString(); emailComposeTask.To = "*****@*****.**"; emailComposeTask.Show(); }); }
private void LoadSongs() { BeatMachineDataContext context = new BeatMachineDataContext( BeatMachineDataContext.DBConnectionString); var songs = context.AnalyzedSongs.ToList(); songsHeader.Header = String.Format("songs ({0})", songs.Count); if (songs.Count > 0) { result.ItemsSource = songs; } else { result.ItemsSource = new List<string> { "No analyzed songs available" }; } }
/// <summary> /// Constructor for the Application object. /// </summary> public App() { // Global handler for uncaught exceptions. UnhandledException += Application_UnhandledException; // Standard Silverlight initialization InitializeComponent(); // Phone-specific initialization InitializePhoneApplication(); // Show graphics profiling information while debugging. if (System.Diagnostics.Debugger.IsAttached) { // Display the current frame rate counters. Application.Current.Host.Settings.EnableFrameRateCounter = true; // Show the areas of the app that are being redrawn in each frame. //Application.Current.Host.Settings.EnableRedrawRegions = true; // Enable non-production analysis visualization mode, // which shows areas of a page that are handed off to GPU with a colored overlay. //Application.Current.Host.Settings.EnableCacheVisualization = true; // Disable the application idle detection by setting the UserIdleDetectionMode property of the // application's PhoneApplicationService object to Disabled. // Caution:- Use this under debug mode only. Application that disables user idle detection will continue to run // and consume battery power when the user is not using the phone. PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled; } // Create the database if it does not exist. using (BeatMachineDataContext db = new BeatMachineDataContext(BeatMachineDataContext.DBConnectionString)) { if (db.DatabaseExists() == false) { db.CreateDatabase(); } } }
private void StoreDownloadedSongs(Catalog cat) { logger.Debug("Downloaded {0} songs", cat.Items.Count); var analyzedSongs = cat.Items. Select<Song, AnalyzedSong>(s => new AnalyzedSong { ItemId = s.Request.ItemId, ArtistName = s.ArtistName ?? s.Request.ArtistName, SongName = s.SongName ?? s.Request.SongName, AudioSummary = s.AudioSummary != null ? new AnalyzedSong.Summary { Tempo = s.AudioSummary.Tempo, ItemId = s.Request.ItemId } : null }). Where<AnalyzedSong>(s => { bool matches = SongsToAnalyzeIds.Contains(s.ItemId); if (matches) { logger.Trace("Song '{0}' matches a song we're looking for", s); } return matches; }); int analyzedCount = analyzedSongs.Count(); logger.Debug("Matched {0} songs", analyzedCount); if (analyzedCount > 0) { using (BeatMachineDataContext context = new BeatMachineDataContext( BeatMachineDataContext.DBConnectionString)) { context.AnalyzedSongs.InsertAllOnSubmit(analyzedSongs); context.SubmitChanges(); } logger.Debug("Stored all matches to database"); foreach (AnalyzedSong s in analyzedSongs) { SongsToAnalyze.Remove( SongsToAnalyze.Where(x => String.Equals(x.ItemId, s.ItemId)).First()); SongsToAnalyzeIds.Remove(s.ItemId); } logger.Debug("Removed matches from list of songs to analyze"); NewSongsAdded = true; } }
public void GetAnalyzedSongs(object state) { using (BeatMachineDataContext context = new BeatMachineDataContext( BeatMachineDataContext.DBConnectionString)) { context.DeferredLoadingEnabled = false; var loadedSongs = from AnalyzedSong song in context.AnalyzedSongs select song; AnalyzedSongs.AddRange(loadedSongs); AnalyzedSongIds.AddRange( AnalyzedSongs.Select<AnalyzedSong, string>((x) => x.ItemId)); AnalyzedSongsLoaded = true; } logger.Info("Completed GetAnalyzedSongs"); }
public PlayViewModel() { ////if (IsInDesignMode) ////{ //// // Code runs in Blend --> create design time data. ////} ////else ////{ //// // Code runs "for real" ////} PropertyChanged += (sender, e) => { if (String.Equals(e.PropertyName, BPMPropertyName)) { ThreadPool.QueueUserWorkItem(new WaitCallback(o => { List<AnalyzedSong> songs; using (BeatMachineDataContext context = new BeatMachineDataContext( BeatMachineDataContext.DBConnectionString)) { DataLoadOptions dlo = new DataLoadOptions(); dlo.LoadWith<AnalyzedSong>(p => p.AudioSummary); context.LoadOptions = dlo; context.ObjectTrackingEnabled = false; songs = context.AnalyzedSongs .Where(s => s.AudioSummary != null && Math.Abs((int)s.AudioSummary.Tempo - BPM) >= BPMTolerance) .Shuffle() .ToList(); } // Need to look up the actual MediaLibrary instances corresponding // to the songs we have in the database List<Song> mediaLibrarySongs = songs .Select<AnalyzedSong, Song>(song => song.ToMediaLibrarySong()) .Where(song => song != null) .ToList(); Messenger.Default.Send<NotificationMessage<List<Song>>>( new NotificationMessage<List<Song>>(mediaLibrarySongs, null)); })); } }; Messenger.Default.Register<PropertyChangedMessage<bool>>(this, m => { if (String.Equals(m.PropertyName, SongsAnalyzedPropertyName) && m.NewValue) { SongsAnalyzed = true; } }); Messenger.Default.Register<NotificationMessage<List<Song>>>( this, m => { BetterMediaPlayer p = new BetterMediaPlayer(m.Content); DispatcherHelper.InvokeAsync(() => Player = p); }); PlayCommand = new RelayCommand( () => { if (Player.State == MediaState.Paused) { Player.Resume(); } else if (Player.State == MediaState.Playing) { Player.Pause(); } else { Player.Play(); } }, () => true); // TODO Remember user setting BPM = 120; }
static void Api_CatalogReadCompleted(object sender, EchoNestApiEventArgs e) { if (e.Error == null) { App thisApp = App.Current as App; BeatMachineDataContext context = new BeatMachineDataContext( BeatMachineDataContext.DBConnectionString); Catalog cat = (Catalog)e.GetResultData(); // TODO This check doesn't work well, it won't terminate // especially in the case where the catalog has more items that // the client doesn't know about if (!(cat.Items.Count == 0 && context.AnalyzedSongs.Count() == thisApp.Model.SongsToAnalyzeBatchSize)) { context.AnalyzedSongs.InsertAllOnSubmit( cat.Items.Select<Song, AnalyzedSong>( s => new AnalyzedSong { ItemId = s.Request.ItemId, ArtistName = s.ArtistName ?? s.Request.ArtistName, SongName = s.SongName ?? s.Request.SongName, AudioSummary = s.AudioSummary != null ? new AnalyzedSong.Summary { Tempo = s.AudioSummary.Tempo } : null } )); context.SubmitChanges(); downloadSkip = context.AnalyzedSongs.Count(); DownloadAnalyzedSongsNeedsToRunAgain(); } else { thisApp.Model.SongsToAnalyzeBatchDownloadReady = true; } } else { DownloadAnalyzedSongsNeedsToRunAgain(); } }
public static void GetAnalyzedSongs(object state) { App thisApp = App.Current as App; List<AnalyzedSong> songs = thisApp.Model.AnalyzedSongs; BeatMachineDataContext context = new BeatMachineDataContext( BeatMachineDataContext.DBConnectionString); lock (songs) { var loadedSongs = from AnalyzedSong song in context.AnalyzedSongs select song; songs.AddRange(loadedSongs.ToArray<AnalyzedSong>()); thisApp.Model.AnalyzedSongsLoaded = true; } }
/// <summary> /// Constructor for the Application object. /// </summary> public App() { // Global handler for uncaught exceptions. UnhandledException += Application_UnhandledException; // Standard Silverlight initialization InitializeComponent(); // Phone-specific initialization InitializePhoneApplication(); // Show graphics profiling information while debugging. if (System.Diagnostics.Debugger.IsAttached) { // Display the current frame rate counters. Application.Current.Host.Settings.EnableFrameRateCounter = true; // Show the areas of the app that are being redrawn in each frame. //Application.Current.Host.Settings.EnableRedrawRegions = true; // Enable non-production analysis visualization mode, // which shows areas of a page that are handed off to GPU with a colored overlay. //Application.Current.Host.Settings.EnableCacheVisualization = true; // Disable the application idle detection by setting the UserIdleDetectionMode property of the // application's PhoneApplicationService object to Disabled. // Caution:- Use this under debug mode only. Application that disables user idle detection will continue to run // and consume battery power when the user is not using the phone. //PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled; } DispatcherHelper.Initialize(); logger = LogManager.GetCurrentClassLogger(); Model = new DataModel(); Songs = new ObservableCollection<Group<AnalyzedSong>>(); Cache = new Dictionary<Song,AnalyzedSong>(); // Create the database if it does not exist. using (BeatMachineDataContext db = new BeatMachineDataContext(BeatMachineDataContext.DBConnectionString)) { if (!db.DatabaseExists()) { logger.Info("Creating new database"); db.CreateDatabase(); } else { logger.Info("Database exists already"); } } MessengerInstance.Register<NewSongsAddedMessage>( this, m => { ThreadPool.QueueUserWorkItem(new WaitCallback(o => { List<IGrouping<int, AnalyzedSong>> rawSongs; using (BeatMachineDataContext context = new BeatMachineDataContext( BeatMachineDataContext.DBConnectionString)) { DataLoadOptions dlo = new DataLoadOptions(); dlo.LoadWith<AnalyzedSong>(p => p.AudioSummary); context.LoadOptions = dlo; context.ObjectTrackingEnabled = false; rawSongs = context.AnalyzedSongs .Where(s => s.AudioSummary != null) .GroupBy(s => (int)Math.Floor((double)s.AudioSummary.Tempo / 10) * 10) .OrderBy(g => g.Key) .ToList(); } foreach (IGrouping<int, AnalyzedSong> g in rawSongs) { List<AnalyzedSong> existingSongs = new List<AnalyzedSong>(); foreach (AnalyzedSong s in g) { Song mls = s.MediaLibrarySong; if (mls != null) { existingSongs.Add(s); Cache[mls] = s; } } if (existingSongs.Count > 0) { Songs.Add(new Group<AnalyzedSong>(g.Key, g.Shuffle())); SongGroupMessage message = new SongGroupMessage(); message.Songs = Songs; message.Cache = Cache; MessengerInstance.Send<SongGroupMessage>(message); } } })); }); // Clean up and restore MediaPlayer settings this.ApplicationLifetimeObjects.Add(new RunMediaPlayer(TimeSpan.FromMilliseconds(50))); }