private int AddSong(int songId) { // Keep queue trimmed if (LookupMap.Count >= MAX_QUEUE_SIZE) { PlayQueueEntryModel head = null; if (PlaybackQueue.Count > 0) { head = PlaybackQueue.First(); } if (head != null) { if (CurrentPlaybackQueueEntryId == head.RowId) { // TODO: #6 raise alert about queue being full return(-1); } else { RemoveEntry(head.RowId); } } } PlayQueueEntryModel currentTail = null; if (PlaybackQueue.Count > 0) { currentTail = PlaybackQueue.Last(); } PlayQueueEntryTable newPlayQueueEntry; if (currentTail == null) { newPlayQueueEntry = new PlayQueueEntryTable(songId, 0, 0); DatabaseManager.Current.AddPlayQueueEntry(newPlayQueueEntry); } else { newPlayQueueEntry = new PlayQueueEntryTable(songId, 0, currentTail.RowId); DatabaseManager.Current.AddPlayQueueEntry(newPlayQueueEntry); currentTail.NextId = newPlayQueueEntry.RowId; } PlayQueueEntryModel newEntry = new PlayQueueEntryModel(newPlayQueueEntry); LookupMap.Add(newEntry.RowId, newEntry); PlaybackQueue.Add(newEntry); NotifyPropertyChanged(Properties.NextTrack); NotifyPropertyChanged(Properties.PrevTrack); return(newEntry.RowId); }
public void ResetPlayerToStart() { if (PlaybackQueue.Count > 0) { PlayQueueEntryModel playQueueEntry = PlaybackQueue.First(); CurrentPlaybackQueueEntryId = playQueueEntry.RowId; } }
public void ClearQueue() { Stop(); CurrentPlaybackQueueEntryId = 0; PlaybackQueue.Clear(); LookupMap.Clear(); ApplicationSettings.PutSettingsValue(ApplicationSettings.CURRENT_TRACK_PERCENTAGE, 0.0); DatabaseManager.Current.ClearPlaybackQueue(); }
public void Populate() { Logger.Current.Log(new CallerInfo(), LogLevel.Info, "Calling Popuplate"); List <PlayQueueEntryTable> allEntries = DatabaseManager.Current.FetchPlayQueueEntries(); PlayQueueEntryModel head = null; foreach (PlayQueueEntryTable playQueueEntry in allEntries) { PlayQueueEntryModel newEntry = new PlayQueueEntryModel(playQueueEntry); LookupMap.Add(newEntry.RowId, newEntry); if (newEntry.PrevId == 0) { DebugHelper.Assert(new CallerInfo(), head == null, "Second head found in play queue!!!"); head = newEntry; } } PlayQueueEntryModel currentLocation = head; while (currentLocation != null && PlaybackQueue.Count < LookupMap.Count) { PlaybackQueue.Add(currentLocation); if (LookupMap.ContainsKey(currentLocation.NextId)) { currentLocation = LookupMap[currentLocation.NextId]; } else { currentLocation = null; } } DebugHelper.Assert(new CallerInfo(), currentLocation == null, "Circular reference found in Play Queue"); DebugHelper.Assert(new CallerInfo(), PlaybackQueue.Count == LookupMap.Count, "Missing element found in Play Queue"); CurrentPlaybackQueueEntryId = ApplicationSettings.GetSettingsValue <int>(ApplicationSettings.CURRENT_PLAYQUEUE_POSITION, 0); if (CurrentPlaybackQueueEntryId == 0) { ResetPlayerToStart(); } }
public async Task DeleteFromQueueAsync(QueueSong songToRemove) { QueueSong previousModel; if (songToRemove == null) { return; } if (_lookupMap.TryGetValue(songToRemove.PrevId, out previousModel)) { previousModel.NextId = songToRemove.NextId; await _bgSqlService.UpdateItemAsync(previousModel); } if (_lookupMap.TryGetValue(songToRemove.ShufflePrevId, out previousModel)) { previousModel.ShuffleNextId = songToRemove.ShuffleNextId; await _bgSqlService.UpdateItemAsync(previousModel); } QueueSong nextModel; if (_lookupMap.TryGetValue(songToRemove.NextId, out nextModel)) { nextModel.PrevId = songToRemove.PrevId; await _bgSqlService.UpdateItemAsync(nextModel); } if (_lookupMap.TryGetValue(songToRemove.ShuffleNextId, out nextModel)) { nextModel.ShufflePrevId = songToRemove.ShufflePrevId; await _bgSqlService.UpdateItemAsync(nextModel); } await _dispatcher.RunAsync( () => { PlaybackQueue.Remove(songToRemove); ShufflePlaybackQueue.Remove(songToRemove); }); _lookupMap.TryRemove(songToRemove.Id, out songToRemove); // Delete from database await _bgSqlService.DeleteItemAsync(songToRemove); }
public async Task ClearQueueAsync() { if (PlaybackQueue.Count == 0) { return; } await _bgSqlService.DeleteTableAsync <QueueSong>(); _lookupMap.Clear(); await _dispatcher.RunAsync( () => { PlaybackQueue.Clear(); ShufflePlaybackQueue.Clear(); }); }
public void RemoveEntry(int entryId) { if (entryId == CurrentPlaybackQueueEntryId) { Stop(); CurrentPlaybackQueueEntryId = 0; } PlayQueueEntryModel songToRemove = null; if (!LookupMap.TryGetValue(entryId, out songToRemove)) { DebugHelper.Alert(new CallerInfo(), "Tried to remove play queue entry {0} but its not in our lookup", entryId); return; } PlayQueueEntryModel previousModel = null; if (LookupMap.TryGetValue(songToRemove.PrevId, out previousModel)) { previousModel.NextId = songToRemove.NextId; } PlayQueueEntryModel nextModel = null; if (LookupMap.TryGetValue(songToRemove.NextId, out nextModel)) { nextModel.PrevId = songToRemove.PrevId; } PlaybackQueue.Remove(songToRemove); LookupMap.Remove(songToRemove.RowId); DatabaseManager.Current.DeletePlayQueueEntry(songToRemove.RowId); NotifyPropertyChanged(Properties.NextTrack); NotifyPropertyChanged(Properties.PrevTrack); }
public static void Tick(Form1 form) { if (State == VoicePlayerState.Available) { VoicePlayerItem item = null; lock (padlock) if (PlaybackQueue.Count > 0) { item = PlaybackQueue[0]; PlaybackQueue.RemoveAt(0); } if (item != null) { String path = Path.Combine(Settings.VoicePath, item.FileName + ".wav"); int success = 0; mciSendString("open \"" + path + "\" type mpegvideo alias " + PLAYBACK_DEVICE_NAME, null, 0, IntPtr.Zero); success = mciSendString("set " + PLAYBACK_DEVICE_NAME + " output " + AudioHelpers.GetPlaybackIdent(), null, 0, IntPtr.Zero); success = mciSendString("play " + PLAYBACK_DEVICE_NAME + " notify", null, 0, form.Handle); if (success == 0) { State = VoicePlayerState.Busy; if (item.Auto) { Room room = RoomPool.Rooms.Find(x => x.EndPoint.Equals(item.EndPoint)); if (room != null) { room.Panel.ShowVoice(item.Sender, item.ShortCut); } } } } } }
public async Task ShuffleCurrentQueueAsync() { var newShuffle = await Task.Factory.StartNew(() => PlaybackQueue.ToList().Shuffle()).ConfigureAwait(false); if (newShuffle.Count > 0) { await _dispatcher.RunAsync(() => ShufflePlaybackQueue.SwitchTo(newShuffle)).ConfigureAwait(false); for (var i = 0; i < newShuffle.Count; i++) { var queueSong = newShuffle[i]; queueSong.ShufflePrevId = i == 0 ? 0 : newShuffle[i - 1].Id; if (i + 1 < newShuffle.Count) { queueSong.ShuffleNextId = newShuffle[i + 1].Id; } await _bgSqlService.UpdateItemAsync(queueSong).ConfigureAwait(false); } } }
public void MoveSong(int oldIndex, int newIndex) { if (oldIndex == newIndex) { return; } PlayQueueEntryModel songToMove = PlaybackQueue[oldIndex]; PlayQueueEntryModel target = null; if (newIndex > 0) { if (newIndex < oldIndex) { target = PlaybackQueue[newIndex - 1]; } else { target = PlaybackQueue[newIndex]; } } // Remove from old spot PlayQueueEntryModel previousModel = null; if (LookupMap.TryGetValue(songToMove.PrevId, out previousModel)) { previousModel.NextId = songToMove.NextId; } PlayQueueEntryModel nextModel = null; if (LookupMap.TryGetValue(songToMove.NextId, out nextModel)) { nextModel.PrevId = songToMove.PrevId; } // Insert after new spot if (target == null) { PlayQueueEntryModel head = null; if (PlaybackQueue.Count > 0) { head = PlaybackQueue.First(); } if (head != null) { songToMove.NextId = head.RowId; head.PrevId = songToMove.RowId; songToMove.PrevId = 0; } else { // Should be redundant songToMove.NextId = 0; songToMove.PrevId = 0; } } else { PlayQueueEntryModel newNextModel = null; if (LookupMap.TryGetValue(target.NextId, out newNextModel)) { newNextModel.PrevId = songToMove.RowId; } songToMove.NextId = target.NextId; target.NextId = songToMove.RowId; songToMove.PrevId = target.RowId; } PlaybackQueue.Move(oldIndex, newIndex); NotifyPropertyChanged(Properties.NextTrack); NotifyPropertyChanged(Properties.PrevTrack); }
private void LoadQueue(List <Song> songs) { var queue = _bgSqlService.SelectAll <QueueSong>(); QueueSong head = null; QueueSong shuffleHead = null; foreach (var queueSong in queue) { queueSong.Song = songs.FirstOrDefault(p => p.Id == queueSong.SongId); _lookupMap.TryAdd(queueSong.Id, queueSong); if (queueSong.ShufflePrevId == 0) { shuffleHead = queueSong; } if (queueSong.PrevId == 0) { head = queueSong; } } if (head != null) { for (var i = 0; i < queue.Count; i++) { if (_dispatcher != null) { _dispatcher.RunAsync(() => PlaybackQueue.Add(head)).Wait(); } else { PlaybackQueue.Add(head); } QueueSong value; if (head.NextId != 0 && _lookupMap.TryGetValue(head.NextId, out value)) { head = value; } else { break; } } } if (shuffleHead != null) { for (var i = 0; i < queue.Count; i++) { if (_dispatcher != null) { _dispatcher.RunAsync(() => ShufflePlaybackQueue.Add(shuffleHead)).Wait(); } else { ShufflePlaybackQueue.Add(shuffleHead); } QueueSong value; if (shuffleHead.ShuffleNextId != 0 && _lookupMap.TryGetValue(shuffleHead.ShuffleNextId, out value)) { shuffleHead = value; } else { break; } } } }
public async Task <QueueSong> AddToQueueAsync(Song song, QueueSong position = null, bool shuffleInsert = true) { if (song == null) { return(null); } var rnd = new Random(DateTime.Now.Millisecond); QueueSong prev = null; QueueSong shufflePrev = null; QueueSong next = null; QueueSong shuffleNext = null; var shuffleIndex = -1; var normalIndex = -1; if (position != null) { shuffleIndex = ShufflePlaybackQueue.IndexOf(position) + 1; normalIndex = PlaybackQueue.IndexOf(position) + 1; } var insert = normalIndex > -1 && normalIndex < PlaybackQueue.Count; var insertShuffle = shuffleIndex > -1 && shuffleInsert; var shuffleLastAdd = shuffleIndex == ShufflePlaybackQueue.Count; if (insert) { next = PlaybackQueue.ElementAtOrDefault(normalIndex); if (next != null) { _lookupMap.TryGetValue(next.PrevId, out prev); } } else { prev = PlaybackQueue.LastOrDefault(); } if (insertShuffle) { if (shuffleLastAdd) { shufflePrev = ShufflePlaybackQueue.ElementAtOrDefault(ShufflePlaybackQueue.Count - 1); } else { shuffleNext = ShufflePlaybackQueue.ElementAtOrDefault(shuffleIndex); if (shuffleNext != null) { _lookupMap.TryGetValue(shuffleNext.ShufflePrevId, out shufflePrev); } } } else { if (ShufflePlaybackQueue.Count > 1) { shuffleIndex = rnd.Next(1, ShufflePlaybackQueue.Count - 1); shuffleNext = ShufflePlaybackQueue.ElementAt(shuffleIndex); _lookupMap.TryGetValue(shuffleNext.ShufflePrevId, out shufflePrev); } else { shuffleLastAdd = true; shufflePrev = prev; } } // Create the new queue entry var newQueue = new QueueSong { SongId = song.Id, NextId = next == null ? 0 : next.Id, PrevId = prev == null ? 0 : prev.Id, ShuffleNextId = shuffleNext == null ? 0 : shuffleNext.Id, ShufflePrevId = shufflePrev == null ? 0 : shufflePrev.Id, Song = song }; // Add it to the database await _bgSqlService.InsertAsync(newQueue).ConfigureAwait(false); if (next != null) { // Update the prev id of the queue that was replaced next.PrevId = newQueue.Id; await _bgSqlService.UpdateItemAsync(next).ConfigureAwait(false); } if (prev != null) { // Update the next id of the previous tail prev.NextId = newQueue.Id; await _bgSqlService.UpdateItemAsync(prev).ConfigureAwait(false); } if (shuffleNext != null) { shuffleNext.ShufflePrevId = newQueue.Id; await _bgSqlService.UpdateItemAsync(shuffleNext).ConfigureAwait(false); } if (shufflePrev != null) { shufflePrev.ShuffleNextId = newQueue.Id; await _bgSqlService.UpdateItemAsync(shufflePrev).ConfigureAwait(false); } // Add the new queue entry to the collection and map await _dispatcher.RunAsync( () => { if (insert) { try { PlaybackQueue.Insert(normalIndex, newQueue); } catch (ArgumentOutOfRangeException) { PlaybackQueue.Add(newQueue); } } else { PlaybackQueue.Add(newQueue); } if (shuffleLastAdd || !shuffleInsert) { ShufflePlaybackQueue.Add(newQueue); } else { try { ShufflePlaybackQueue.Insert(shuffleIndex, newQueue); } catch (ArgumentOutOfRangeException) { ShufflePlaybackQueue.Add(newQueue); } } }).ConfigureAwait(false); _lookupMap.TryAdd(newQueue.Id, newQueue); return(newQueue); }
public async Task DeleteSongAsync(Song song) { var queueSong = PlaybackQueue.FirstOrDefault(p => p.SongId == song.Id); if (queueSong != null) { await DeleteFromQueueAsync(queueSong); } // remove it from artist, albums and playlists songs var playlists = Playlists.Where(p => p.Songs.Count(pp => pp.SongId == song.Id) > 0).ToList(); foreach (var playlist in playlists) { var songs = playlist.Songs.Where(p => p.SongId == song.Id).ToList(); foreach (var playlistSong in songs) { await DeleteFromPlaylistAsync(playlist, playlistSong); } if (playlist.Songs.Count == 0) { await DeletePlaylistAsync(playlist); } } if (song.Album != null) { song.Album.Songs.Remove(song); // If the album is empty, make sure that is not being used by any temp song if (song.Album.Songs.Count == 0 && Songs.Count(p => p.AlbumId == song.AlbumId) < 2) { await _sqlService.DeleteItemAsync(song.Album); await _dispatcher.RunAsync( () => { Albums.Remove(song.Album); if (song.Artist != null) { song.Artist.Albums.Remove(song.Album); } }); } } if (song.Artist != null) { song.Artist.Songs.Remove(song); if (song.Artist.Songs.Count == 0 && Songs.Count(p => p.ArtistId == song.ArtistId) < 2) { await _sqlService.DeleteItemAsync(song.Artist); await _dispatcher.RunAsync( () => { Artists.Remove(song.Artist); }); } } // good, now lets delete it from the db await _sqlService.DeleteItemAsync(song); await _dispatcher.RunAsync(() => Songs.Remove(song)); }
public static void QueueItem(VoicePlayerItem item) { lock (padlock) PlaybackQueue.Add(item); }