Newer
Older
Kevin Whitaker
committed
/*
* Copyright (C) 2017 Kevin Whitaker <eyecreate@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include "GroovePlayer.h"
#include "db/UserAction.h"
#include <Wt/WLogger>
#include <Wt/WServer>
#include <Wt/WApplication>
#include "WebInterface.h"
Kevin Whitaker
committed
#include <locale>
#include <Wt/WLogger>
#include <Wt/Dbo/Transaction>
#include <groovefingerprinter/fingerprinter.h>
Kevin Whitaker
committed
#include <grooveplayer/player.h>
Kevin Whitaker
committed
Kevin Whitaker
committed
GroovePlayerMgr::GroovePlayerMgr (std::string dbFile) : sqliteConnection(dbFile), connectionPool(&sqliteConnection, 3)
Kevin Whitaker
committed
{
Kevin Whitaker
committed
musicScanDir = std::filesystem::current_path()/"music";
Kevin Whitaker
committed
Wt::Dbo::Session sqlSession;
sqlSession.setConnectionPool(connectionPool);
sqlSession.mapClass<User>("user");
sqlSession.mapClass<AudioTrack>("tracks");
sqlSession.mapClass<UserAction>("actions");
try
{
sqlSession.createTables();
} catch(Wt::Dbo::Exception e)
{
Wt::log("info") << "Using Existing DB.";
}
groove_init();
Kevin Whitaker
committed
/*bool emptyTrackListing = false;
{
Wt::Dbo::Transaction transaction(sqlSession);
int trackCount = sqlSession.query<int>("select count(fingerprint) from tracks");
emptyTrackListing = trackCount==0;
}
if(emptyTrackListing)
{
grooveAudioScanner = new std::thread([this](){grooveAudioScannerLoop();});
grooveEvents = new std::thread([this](){while(!this->finishedLaunchScan){std::this_thread::sleep_for(std::chrono::milliseconds(200));}[this](){grooveEventLoop();};});
}
else
{*/
grooveAudioScanner = new std::thread([this](){grooveAudioScannerLoop();});
grooveEvents = new std::thread([this](){grooveEventLoop();});
//}
Kevin Whitaker
committed
}
Kevin Whitaker
committed
std::list<const AudioTrack*> GroovePlayerMgr::getNextVoteBatch(Wt::Dbo::Session* session)
Kevin Whitaker
committed
/**
* This method will attempt to pick 3 tracks that will be up for selection next.
*
* The first track should be from the same album or artist.
* The second track should be from the same genre.
* The third track should be from something in a different genre.
*
* What is picked inside those requirements is a mix of random and TODO:based on past user actions(33% of time).
*
* The third track will be replaced by a recent request item if one exists.
*/
std::list<const AudioTrack*> selectedTracks;
Kevin Whitaker
committed
Wt::Dbo::Transaction transaction(*session);
Kevin Whitaker
committed
//First make sure there are at least 3 tracks to suggest.
Kevin Whitaker
committed
int trackCount = session->query<int>("select count(fingerprint) from tracks");
Kevin Whitaker
committed
if(trackCount < 3)
{
//Return empty list to show there are not enough tracks to allow voting.
return selectedTracks;
}
//Determine first track
Kevin Whitaker
committed
int trackAlbumCount = session->query<int>("select count(fingerprint) from tracks").where("album = ?").bind(getCurrentTrack(session)->trackAlbumName);
int trackArtistCount = session->query<int>("select count(fingerprint) from tracks").where("artist = ?").bind(getCurrentTrack(session)->trackArtistName);
Kevin Whitaker
committed
int computerSlightOfHand = rand() % 10 + 1;
if(trackAlbumCount > 0 && computerSlightOfHand > 3)
{
//Pick item from album
Kevin Whitaker
committed
Wt::Dbo::ptr<AudioTrack> eligibleTrack = session->find<AudioTrack>().where("album = ?").limit(1).offset(rand() % trackAlbumCount).bind(getCurrentTrack(session)->trackAlbumName);
Kevin Whitaker
committed
selectedTracks.push_back(eligibleTrack.get());
}
else if(trackArtistCount > 0)
{
//Pick item from artist
Kevin Whitaker
committed
Wt::Dbo::ptr<AudioTrack> eligibleTrack = session->find<AudioTrack>().where("artist = ?").limit(1).offset(rand() % trackArtistCount).bind(getCurrentTrack(session)->trackArtistName);
Kevin Whitaker
committed
selectedTracks.push_back(eligibleTrack.get());
}
//Determine second track
Kevin Whitaker
committed
int trackGenreCount = session->query<int>("select count(fingerprint) from tracks").where("genre = ?").bind(getCurrentTrack(session)->trackGenre);
Kevin Whitaker
committed
if(trackGenreCount > 0)
{
//Pick item from genre
Kevin Whitaker
committed
Wt::Dbo::ptr<AudioTrack> eligibleTrack = session->find<AudioTrack>().where("genre = ?").limit(1).offset(rand() % trackGenreCount).bind(getCurrentTrack(session)->trackGenre);
Kevin Whitaker
committed
selectedTracks.push_back(eligibleTrack.get());
}
//Determine third track
Kevin Whitaker
committed
int trackNotGenreCount = session->query<int>("select count(fingerprint) from tracks").where("genre != ?").bind(getCurrentTrack(session)->trackGenre);
Kevin Whitaker
committed
if(requestQueue.size() > 0)
{
//There's a request. Pick one up front and put as third item.
selectedTracks.push_back(requestQueue.front());
}
else if(trackNotGenreCount > 0)
{
//Pick from other genre
Kevin Whitaker
committed
Wt::Dbo::ptr<AudioTrack> eligibleTrack = session->find<AudioTrack>().where("genre != ?").limit(1).offset(rand() % trackNotGenreCount).bind(getCurrentTrack(session)->trackGenre);
Kevin Whitaker
committed
selectedTracks.push_back(eligibleTrack.get());
}
else
{
//Pick randomly
Kevin Whitaker
committed
Wt::Dbo::ptr<AudioTrack> eligibleTrack = session->find<AudioTrack>().limit(1).offset(rand() % trackCount);
Kevin Whitaker
committed
selectedTracks.push_back(eligibleTrack.get());
}
return selectedTracks;
Kevin Whitaker
committed
void GroovePlayerMgr::grooveEventLoop()
Kevin Whitaker
committed
{
/**
Kevin Whitaker
committed
* On first boot, random track that has been voted before will start(or random if no track has been played before).
Kevin Whitaker
committed
* When voting ends, if no vote was cast, the first will be picked 60% of the time with second being 30% of the time and third being picked 10% of the time.
Kevin Whitaker
committed
* Exception is if third track is the top request, then it is picked. If another song is voted over this, it will be put in back of request queue.
Kevin Whitaker
committed
*/
Kevin Whitaker
committed
Kevin Whitaker
committed
Wt::Dbo::Session sqlSession;
sqlSession.setConnectionPool(connectionPool);
sqlSession.mapClass<User>("user");
sqlSession.mapClass<AudioTrack>("tracks");
sqlSession.mapClass<UserAction>("actions");
Wt::Dbo::Transaction transaction(sqlSession);
Kevin Whitaker
committed
//Wait until at least one track is in DB.
Kevin Whitaker
committed
if(sqlSession.query<int>("select count(fingerprint) from tracks") < 1)
Kevin Whitaker
committed
{
Kevin Whitaker
committed
while(sqlSession.query<int>("select count(fingerprint) from tracks") < 1){}
Kevin Whitaker
committed
}
Kevin Whitaker
committed
//Pick initial track to bootstrap.
int tracksPlayedBefore = sqlSession.query<int>("select count(fingerprint) from tracks as t join actions on (actions.action = 3 or actions.action = 2) and actions.track_id = t.id");
Kevin Whitaker
committed
const AudioTrack* selectedTrack;
if (tracksPlayedBefore > 0)
{
Kevin Whitaker
committed
selectedTrack = sqlSession.query<Wt::Dbo::ptr<AudioTrack>>("select t from tracks as t join actions on (actions.action = 3 or actions.action = 2) and actions.track_id = t.id").limit(1).offset(rand() % tracksPlayedBefore).resultValue().get();
Kevin Whitaker
committed
}
else
{
Kevin Whitaker
committed
int trackCount = sqlSession.query<int>("select count(fingerprint) from tracks");
selectedTrack = sqlSession.find<AudioTrack>().limit(1).offset(rand() % trackCount).resultValue().get();
Kevin Whitaker
committed
}
struct GroovePlaylist* playlist = groove_playlist_create();
Kevin Whitaker
committed
currentPlaylist = playlist;
Kevin Whitaker
committed
struct GroovePlayer* player = groove_player_create();
if(!player) {return;}
Kevin Whitaker
committed
currentPlayer = player;
Kevin Whitaker
committed
Kevin Whitaker
committed
groove_playlist_insert(playlist, groove_file_open(selectedTrack->trackPath.c_str()),1.0,1.0,nullptr);
Kevin Whitaker
committed
Kevin Whitaker
committed
//Now boostrap player with initial data
Kevin Whitaker
committed
groove_player_attach(player, playlist);
Kevin Whitaker
committed
groove_playlist_play(playlist);
Kevin Whitaker
committed
Wt::WServer::instance()->postAll([&]() {
Kevin Whitaker
committed
Wt::WApplication* app = Wt::WApplication::instance();
Kevin Whitaker
committed
if(app != nullptr)
{
static_cast<WebInterface*>(app)->songChangedFromServer(getCurrentTrack(&sqlSession).get());
Kevin Whitaker
committed
}
Kevin Whitaker
committed
}
Kevin Whitaker
committed
);
Kevin Whitaker
committed
Kevin Whitaker
committed
//Now start loop
Kevin Whitaker
committed
while(getInstance()->continueEventLoop)
{
Kevin Whitaker
committed
std::this_thread::sleep_for(std::chrono::milliseconds(200));
PlayerEvents event = getNextPlayerEvent(&sqlSession);
Kevin Whitaker
committed
if(event == NOTHING)
{
continue;
}
Kevin Whitaker
committed
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
else if(event == GROOVE_NOWPLAYING)
{
//Pick new batch of tracks to vote on and inform UI of update.
currentVoteBatch = getNextVoteBatch(&sqlSession);
currentVoteStatus = std::map<const AudioTrack*, int>();
std::for_each(currentVoteBatch.begin(),currentVoteBatch.end(),[&](const AudioTrack* item){
currentVoteStatus.insert(std::pair<const AudioTrack*, int>(item,0));
});
Wt::WServer::instance()->postAll([&]() {
Wt::WApplication* app = Wt::WApplication::instance();
if(app != nullptr)
{
static_cast<WebInterface*>(app)->songChangedFromServer(getCurrentTrack(&sqlSession).get());
static_cast<WebInterface*>(app)->voteTracksUpdatedFromServer(currentVoteBatch);
}
}
);
Wt::log("info") << "Track playing changed to: " << getCurrentTrack(&sqlSession).get()->trackName;
}
else if(event == VOTING_ENDED)
{
//Look at votes and add correct track to end of playlist.
//If more than one track has highest votes, pick random. TODO:maybe base off of other play data instead.
std::list<std::pair<const AudioTrack*,int>> trackWinners;
for(std::map<const AudioTrack*,int>::iterator track = currentVoteStatus.begin(); track != currentVoteStatus.end(); track++)
{
//There nothing yet and there was at least a vote is an initial winner.
if(trackWinners.size() == 0 && track->second > 0)
{
trackWinners.push_back(std::pair<const AudioTrack*, int>(track->first,track->second));
continue;
}
//If there is a winner. Check if higher. If so, clear the list and add this one. If equal, add to list.
if(trackWinners.size() > 0)
{
if(trackWinners.front().second < track->second)
{
trackWinners.clear();
trackWinners.push_back(std::pair<const AudioTrack*,int>(track->first, track->second));
}
else if(trackWinners.front().second == track->second)
{
trackWinners.push_back(std::pair<const AudioTrack*, int>(track->first, track->second));
}
}
}
const AudioTrack* winner;
//Randomly pick from winners(in case of tie), if there are any.
if(trackWinners.size() > 0)
{
auto iter = trackWinners.begin();
std::advance(iter,rand() % trackWinners.size());
winner = (*iter).first;
//Deal with requests if there are any.
if(requestQueue.size() > 0)
{
//If winning track was a request, now you can remove it from request queue.
if(requestQueue.front()->trackFingerprint == winner->trackFingerprint)
{
requestQueue.pop_front();
}
//Request didn't win. Put request at bottom of queue.
else
{
requestQueue.push_back(requestQueue.front());
requestQueue.pop_front();
}
}
}
else
{
//Pick request song if there are any.
if(requestQueue.size() > 0)
{
winner = requestQueue.front();
requestQueue.pop_front();
}
else
{
//Pick based on percentages.
int pick = rand() % 10 +1;
if(pick <= 6)
{
//First track wins
winner = currentVoteBatch.front();
}
else if(pick <= 9)
{
//Second track wins.
winner = (*currentVoteBatch.begin()++);
}
else
{
//Third track wins
winner = (*currentVoteBatch.begin()+=2);
}
}
}
groove_playlist_insert(currentPlaylist,groove_file_open(winner->trackPath.c_str()),1.0,1.0,nullptr);
Wt::log("info")<< "Voting has ended. Next track is: " << winner->trackName;
}
else if(event == VOTE_CAST)
{
//
}
else if(event == PLAYING_PAUSED)
{
//
}
else if(event == PLAYING_RESUMED)
{
//
}
else if(event == SKIP_REQUESTED)
{
//
}
else if(event == SKIP_VOTE_CAST)
{
//
}
else if(event == SKIP_VOTING_ENDED)
{
//
}
else if(event == ADMIN_FORCE_SKIP)
{
//
}
Kevin Whitaker
committed
//TODO
}
Kevin Whitaker
committed
currentPlaylist = nullptr;
currentPlayer = nullptr;
Kevin Whitaker
committed
groove_player_detach(player);
groove_player_destroy(player);
groove_playlist_destroy(playlist);
}
void GroovePlayerMgr::shutdown()
{
continueEventLoop = false;
Kevin Whitaker
committed
}
Kevin Whitaker
committed
GroovePlayerMgr::PlayerEvents GroovePlayerMgr::getNextPlayerEvent(Wt::Dbo::Session* session)
Kevin Whitaker
committed
double timeIntoTrack;
groove_player_position(currentPlayer,nullptr,&timeIntoTrack);
if(getCurrentTrack(session).get() != nullptr && getCurrentTrack(session)->trackLengthSeconds-timeIntoTrack < 3)
{
return VOTING_ENDED;
}
GroovePlayerEvent event;
if(groove_player_event_get(this->currentPlayer,&event, 0) > 0)
{
if(event.type == GROOVE_EVENT_NOWPLAYING)
{
return GROOVE_NOWPLAYING;
}
}
if(lastInternalEvents.size() > 0)
{
PlayerEvents event = lastInternalEvents.front();
lastInternalEvents.pop_front();
return event;
}
Kevin Whitaker
committed
return NOTHING;
Kevin Whitaker
committed
Wt::Dbo::ptr<AudioTrack> GroovePlayerMgr::getCurrentTrack(Wt::Dbo::Session* session)
Kevin Whitaker
committed
{
Kevin Whitaker
committed
Wt::Dbo::Transaction transaction(*session);
Kevin Whitaker
committed
GroovePlaylistItem* currentItem;
Kevin Whitaker
committed
groove_player_position(this->currentPlayer,¤tItem,nullptr);
if(currentItem != nullptr)
{
Wt::Dbo::ptr<AudioTrack> track = session->find<AudioTrack>().where("path = ?").bind(currentItem->file->filename);
return track;
}
return Wt::Dbo::ptr<AudioTrack>();
Kevin Whitaker
committed
}
Kevin Whitaker
committed
bool GroovePlayerMgr::addFileToTrackDBIfTagged(Wt::Dbo::Session* session, std::filesystem::path file)
Kevin Whitaker
committed
{
//Now check if tags exist and put into DB.
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
struct GrooveFile* gfile = groove_file_open(file.c_str());
struct GrooveTag* artist_tag = groove_file_metadata_get(gfile, "artist", nullptr, 0);
struct GrooveTag* album_tag = groove_file_metadata_get(gfile, "album", nullptr, 0);
struct GrooveTag* name_tag = groove_file_metadata_get(gfile, "title", nullptr, 0);
struct GrooveTag* genre_tag = groove_file_metadata_get(gfile, "genre", nullptr, 0);
if(artist_tag == nullptr || album_tag == nullptr || name_tag == nullptr || genre_tag == nullptr)
{
//Only accept song with all metadata for DB.
groove_file_close(gfile);
return false;
}
//Take fingerprint and compare to DB.
struct GrooveFingerprinter* fingerprinter = groove_fingerprinter_create();
struct GrooveFingerprinterInfo info;
struct GroovePlaylist* playlist = groove_playlist_create();
groove_playlist_insert(playlist,gfile,1.0,1.0,nullptr);
groove_fingerprinter_attach(fingerprinter, playlist);
groove_fingerprinter_info_get(fingerprinter, &info, 1);
double trackLen = info.duration;
char* encodedFP;
groove_fingerprinter_encode(info.fingerprint,info.fingerprint_size,&encodedFP);
std::string fingerprint = std::string(encodedFP);
groove_fingerprinter_dealloc(encodedFP);
groove_fingerprinter_free_info(&info);
groove_fingerprinter_detach(fingerprinter);
groove_fingerprinter_destroy(fingerprinter);
groove_playlist_destroy(playlist);
Kevin Whitaker
committed
Wt::Dbo::Transaction transaction(*session);
int existing = session->query<int>("select count(fingerprint) from tracks").where("fingerprint = ?").bind(fingerprint);
if(existing >0)
{
//This track has a duplicate already.
groove_file_close(gfile);
return false;
}
//Add to DB.
AudioTrack* newTrack = new AudioTrack();
newTrack->trackName = groove_tag_value(name_tag);
newTrack->trackAlbumName = groove_tag_value(album_tag);
newTrack->trackArtistName = groove_tag_value(artist_tag);
newTrack->trackGenre = groove_tag_value(genre_tag);
newTrack->trackLengthSeconds = trackLen;
newTrack->trackFingerprint = fingerprint;
newTrack->trackPath = file.string();
Kevin Whitaker
committed
session->add(newTrack);
groove_file_close(gfile);
return true;
Kevin Whitaker
committed
}
Kevin Whitaker
committed
void GroovePlayerMgr::removeOrphanedTracks(Wt::Dbo::Session* session)
{
Kevin Whitaker
committed
Wt::Dbo::Transaction transaction(*session);
Wt::Dbo::collection<Wt::Dbo::ptr<AudioTrack>> tracks = session->find<AudioTrack>();
for(Wt::Dbo::collection<Wt::Dbo::ptr<AudioTrack>>::const_iterator i = tracks.begin(); i != tracks.end(); ++i)
{
if(!std::filesystem::exists(std::filesystem::path((*i)->trackPath)))
{
Kevin Whitaker
committed
Wt::Dbo::ptr<AudioTrack> item = session->find<AudioTrack>().where("fingerprint = ?").bind((*i)->trackFingerprint);
Wt::log("info") << (*i)->trackPath + " not found in filesystem. Removed from DB.";
item.remove();
}
}
}
Kevin Whitaker
committed
Kevin Whitaker
committed
void GroovePlayerMgr::grooveAudioScannerLoop()
Kevin Whitaker
committed
Wt::Dbo::Session sqlSession;
sqlSession.setConnectionPool(connectionPool);
sqlSession.mapClass<User>("user");
sqlSession.mapClass<AudioTrack>("tracks");
sqlSession.mapClass<UserAction>("actions");
if(!std::filesystem::exists(musicScanDir))
{
std::filesystem::create_directory(musicScanDir);
Wt::log("info") << "Directory "+std::filesystem::canonical(musicScanDir).string()+" didn't exist, so created.";
} else
{
Wt::log("info") << "Directory "+std::filesystem::canonical(musicScanDir).string()+" exists.";
}
Kevin Whitaker
committed
for(std::filesystem::directory_entry p: std::filesystem::directory_iterator(musicScanDir))
{
std::string extensionLowered;
for(auto elem : p.path().extension().string())
{
extensionLowered.push_back(std::tolower(elem));
}
if(extensionLowered == ".mp3") //TODO:think about supporting more than mp3s.
Kevin Whitaker
committed
{
Kevin Whitaker
committed
if(addFileToTrackDBIfTagged(&sqlSession, p.path()))
Kevin Whitaker
committed
{
Wt::log("info") << p.path().string() << " was added to DB";
}
}
else
{
Wt::log("info") << p.path().string() + " was not an accepted audio file.";
}
Kevin Whitaker
committed
}
//Now check for tracks in DB without a file.
Kevin Whitaker
committed
removeOrphanedTracks(&sqlSession);
finishedLaunchScan = true;