private List<Song> fetchRandomTracks(offlineMode randomMode_) { int addedSongs = 0; MusicDatabase dbs = MusicDatabase.Instance; List<Song> randomSongList = new List<Song>(_limitRandomListCount); Song randomSong = new Song(); Song lookupSong = new Song(); int loops = 0; // fetch more than needed since there could be double entries while (addedSongs < _limitRandomListCount * 3) { loops++; lookupSong.Clear(); dbs.GetRandomSong(ref lookupSong); randomSong = lookupSong.Clone(); bool found = false; for (int i = 0; i < randomSongList.Count; i++) { if (randomSongList[i].Artist == randomSong.Artist) { found = true; break; } } if (!found) { switch (randomMode_) { case offlineMode.timesplayed: if (randomSong.TimesPlayed == 0) { randomSongList.Add(randomSong); addedSongs++; } break; case offlineMode.favorites: if (randomSong.Favorite) { randomSongList.Add(randomSong); addedSongs++; } break; case offlineMode.random: randomSongList.Add(randomSong); addedSongs++; break; } } // quick check; 3x rlimit times because every pass could try different artists in dbs.GetRandomSong(ref lookupSong); if (loops > 20) { if (randomMode_ == offlineMode.timesplayed) { Log.Debug("AudioScrobblerUtils: Not enough unique unheard tracks for random mode"); } break; } } return randomSongList; }
private bool GetSongByTitle(string aTitle, ref Song aSong) { try { aSong.Clear(); string strTitle = aTitle; DatabaseUtility.RemoveInvalidChars(ref strTitle); string strSQL = String.Format("SELECT * FROM tracks WHERE strTitle LIKE '{0}'", strTitle); SQLiteResultSet results = DirectExecute(strSQL); if (results.Rows.Count == 0) { return false; } if (results.Rows.Count > 1) { Log.Debug("MusicDatabase: Lookups: GetSongByTitle found multiple results ({0}) for {1}", Convert.ToString(results.Rows.Count), strTitle); } if (AssignAllSongFieldsFromResultSet(ref aSong, results, 0)) { return true; } } catch (Exception ex) { Log.Error("musicdatabase exception err:{0} stack:{1}", ex.Message, ex.StackTrace); Open(); } return false; }
public bool GetSongByFileName(string aFileName, ref Song aSong) { string strFileName = aFileName; try { aSong.Clear(); DatabaseUtility.RemoveInvalidChars(ref strFileName); string strSQL = String.Format("SELECT * FROM tracks WHERE strPath = '{0}'", strFileName); SQLiteResultSet results = DirectExecute(strSQL); if (results.Rows.Count == 0) { return false; } if (AssignAllSongFieldsFromResultSet(ref aSong, results, 0)) { return true; } } catch (Exception ex) { Log.Error("MusicDatabase: Lookups: GetSongByFileName failed - strFileName: {2} - err:{0} stack:{1}", ex.Message, ex.StackTrace, strFileName); Open(); } return false; }
public bool GetSongByMusicTagInfo(string aArtist, string aAlbum, string aTitle, bool inexactFallback, ref Song aSong) { //Log.Debug("MusicDatabase: GetSongByMusicTagInfo - Artist: {0}, Album: {1}, Title: {2}, Nearest match: {3}", aArtist, aAlbum, aTitle, Convert.ToString(inexactFallback)); aSong.Clear(); // we have all info - try exact if (!string.IsNullOrEmpty(aArtist) && !string.IsNullOrEmpty(aAlbum) && !string.IsNullOrEmpty(aTitle)) { if (GetSongByArtistAlbumTitle(aArtist, aAlbum, aTitle, ref aSong)) { return true; } else if (!inexactFallback) { return false; } } // An artist may have the same title in different versions (e.g. live, EP) on different discs - therefore this is the 2nd best option if (!string.IsNullOrEmpty(aAlbum) && !string.IsNullOrEmpty(aTitle)) { if (GetSongByAlbumTitle(aAlbum, aTitle, ref aSong)) { return true; } else if (!inexactFallback) { return false; } } // Maybe the album was spelled different on last.fm or is mistagged in the local collection if (!string.IsNullOrEmpty(aArtist) && !string.IsNullOrEmpty(aTitle)) { if (GetSongByArtistTitle(aArtist, aTitle, ref aSong)) { return true; } else if (!inexactFallback) { return false; } } // Make sure we get at least one / some usable results if (!string.IsNullOrEmpty(aTitle)) { if (GetSongByTitle(aTitle, ref aSong)) { return true; } else { return false; } } Log.Debug( "MusicDatabase: GetSongByMusicTagInfo did not get usable params! Artist: {0}, Album: {1}, Title: {2}, Nearest Match: {3}", aArtist, aAlbum, aTitle, Convert.ToString(inexactFallback)); return false; }
public bool GetRandomSong(ref Song aSong) { try { aSong.Clear(); PseudoRandomNumberGenerator rand = new PseudoRandomNumberGenerator(); int maxIDSong, rndIDSong; string strSQL = String.Format("SELECT max(idTrack) FROM tracks"); SQLiteResultSet results = DirectExecute(strSQL); maxIDSong = DatabaseUtility.GetAsInt(results, 0, 0); rndIDSong = rand.Next(0, maxIDSong); strSQL = String.Format("SELECT * FROM tracks WHERE idTrack={0}", rndIDSong); results = DirectExecute(strSQL); if (results.Rows.Count > 0) { if (AssignAllSongFieldsFromResultSet(ref aSong, results, 0)) { return true; } } else { GetRandomSong(ref aSong); return true; } } catch (Exception ex) { Log.Error("musicdatabase exception err:{0} stack:{1}", ex.Message, ex.StackTrace); Open(); } return false; }