From 49b481ccf53a918853a6ca6fc2172b2cd5bdd449 Mon Sep 17 00:00:00 2001 From: Flone Date: Sat, 2 Nov 2019 15:59:06 +0500 Subject: [PATCH] Added history of played tracks (available only with Random Track enabled). --- TODO.txt | 3 +- src/Model/AudioService/audioservice.cpp | 72 +++++++++++++++++++++++-- src/Model/AudioService/audioservice.h | 4 +- src/globalparams.h | 2 + 4 files changed, 73 insertions(+), 8 deletions(-) diff --git a/TODO.txt b/TODO.txt index 5947794..44ccdfc 100644 --- a/TODO.txt +++ b/TODO.txt @@ -1,5 +1,4 @@ -- Add a history of played tracks so the 'prev' button will get back to actually previous track even if 'random track' function is selected. -- Speed up the process of adding the tracks. +- Speed up the process of adding the tracks. - When adding music, we need to add it in the same order in which it was in the folder. - Do something with color banding. - Seems like with 'speed (by pitch)' effect we also need to increase mid-high frequencies on like 3-4 dB and maybe do the same thing with the low and the mid-low. diff --git a/src/Model/AudioService/audioservice.cpp b/src/Model/AudioService/audioservice.cpp index 0b7e3f3..dba0153 100644 --- a/src/Model/AudioService/audioservice.cpp +++ b/src/Model/AudioService/audioservice.cpp @@ -452,12 +452,23 @@ void AudioService::playTrack(size_t iTrackIndex, bool bDontLockMutex) pMainWindow->removePlayingOnTrack(iCurrentlyPlayingTrackIndex); } + // Play track if ( tracks[iTrackIndex]->playTrack(fCurrentVolume) ) { bIsSomeTrackPlaying = true; bCurrentTrackPaused = false; iCurrentlyPlayingTrackIndex = iTrackIndex; + + if (bRandomNextTrack) + { + vTracksHistory.push_back(tracks[iTrackIndex]); + + if (vTracksHistory.size() > MAX_HISTORY_SIZE) + { + vTracksHistory.erase( vTracksHistory.begin() ); + } + } } else { @@ -614,17 +625,52 @@ void AudioService::prevTrack() if ( tracks.size() > 0 ) { - if (iCurrentlyPlayingTrackIndex == 0) + if (bRandomNextTrack) { - mtxTracksVec.unlock(); + if (vTracksHistory.size() > 1) + { + size_t iTrackIndex = 0; + + Track* pPrevTrack = vTracksHistory[ vTracksHistory.size() - 2 ]; - playTrack( tracks.size() - 1 ); + for (size_t i = 0; i < tracks.size(); i++) + { + if (pPrevTrack == tracks[i]) + { + iTrackIndex = i; + break; + } + } + + + mtxTracksVec.unlock(); + + vTracksHistory.pop_back(); + vTracksHistory.pop_back(); + + playTrack ( iTrackIndex ); + } + else + { + mtxTracksVec.unlock(); + + playTrack( iCurrentlyPlayingTrackIndex ); + } } else { - mtxTracksVec.unlock(); + if (iCurrentlyPlayingTrackIndex == 0) + { + mtxTracksVec.unlock(); + + playTrack( tracks.size() - 1 ); + } + else + { + mtxTracksVec.unlock(); - playTrack(iCurrentlyPlayingTrackIndex - 1); + playTrack(iCurrentlyPlayingTrackIndex - 1); + } } } else @@ -678,6 +724,17 @@ void AudioService::removeTrack(size_t iTrackIndex) } } + + // Erase from vTracksHistory + for (size_t i = 0; i < vTracksHistory.size(); i++) + { + if (vTracksHistory[i] == tracks[iTrackIndex]) + { + vTracksHistory.erase ( vTracksHistory.begin() + i ); + break; + } + } + delete tracks[iTrackIndex]; tracks.erase( tracks.begin() + iTrackIndex ); @@ -719,6 +776,9 @@ void AudioService::clearPlaylist() fCurrentVolume = DEFAULT_VOLUME; + + vTracksHistory.clear(); + mtxTracksVec.unlock(); } @@ -1216,6 +1276,8 @@ void AudioService::randomNextTrack() bRepeatTrack = false; pMainWindow->uncheckRepeatTrackButton(); } + + vTracksHistory.clear(); } void AudioService::setVolume(float fNewVolume) diff --git a/src/Model/AudioService/audioservice.h b/src/Model/AudioService/audioservice.h index 9145ec5..3ce0b05 100644 --- a/src/Model/AudioService/audioservice.h +++ b/src/Model/AudioService/audioservice.h @@ -178,11 +178,13 @@ class AudioService bool bMonitorTracks; - // Searcy + // Search std::vector vSearchResult; size_t iCurrentPosInSearchVec; bool bFirstSearchAfterKeyChange; + // Tracks std::vector tracks; + std::vector vTracksHistory; }; diff --git a/src/globalparams.h b/src/globalparams.h index e9a7590..ca5b55e 100644 --- a/src/globalparams.h +++ b/src/globalparams.h @@ -5,5 +5,7 @@ #define MONITOR_TRACK_INTERVAL_MS 200 #define MAX_TIME_ERROR_MS 50 +#define MAX_HISTORY_SIZE 50 + // graph #define MAX_X_AXIS_VALUE 1000