Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made it work with upnp and possible radio stations #57

Open
wants to merge 32 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions audioscrobbler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ std::string CAudioScrobbler::CreateScrobbleMessage(int index, const CacheEntry&
msg.AddField("albumArtist", song.getAlbumArtist());
}

if(!song.getMusicBrainzId().empty()) {
msg.AddField("mbid", song.getMusicBrainzId());
}

if(!song.getTrack().empty()) {
msg.AddField("trackNumber", song.getTrack());
}

return msg.GetMessage();
}

Expand Down Expand Up @@ -236,6 +244,14 @@ bool CAudioScrobbler::SendNowPlaying(const Song& song)
msg.AddField("albumArtist", song.getAlbumArtist());
}

if(!song.getMusicBrainzId().empty()) {
msg.AddField("mbid", song.getMusicBrainzId());
}

if(!song.getTrack().empty()) {
msg.AddField("trackNumber", song.getTrack());
}

OpenURL(GetServiceURL(), msg.GetMessage().c_str());

if(_response.find("<lfm status=\"ok\">") != std::string::npos) {
Expand Down
10 changes: 8 additions & 2 deletions cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ std::ofstream& operator <<(std::ofstream& outstream, const CacheEntry& inobj)
outstream << song.getArtist() << std::endl
<< song.getTitle() << std::endl
<< song.getAlbum() << std::endl
<< song.getAlbumArtist() << std::endl
<< song.getTrack() << std::endl
<< song.getMusicBrainzId() << std::endl
<< song.getDuration() << std::endl
<< inobj.getStartTime();

Expand All @@ -94,20 +97,23 @@ std::ofstream& operator <<(std::ofstream& outstream, const CacheEntry& inobj)

std::ifstream& operator >>(std::ifstream& instream, CacheEntry& outobj)
{
std::string artist, title, album;
std::string artist, title, album, albumartist, track, mbid;
int duration;
time_t starttime;

getline(instream, artist);
getline(instream, title);
getline(instream, album);
getline(instream, albumartist);
getline(instream, track);
getline(instream, mbid);

instream >> duration;
instream.ignore(1);
instream >> starttime;
instream.ignore(1);

Song song(artist, title, album, duration);
Song song(artist, title, album, albumartist, duration, track, mbid);
outobj = CacheEntry(song, starttime);

return instream;
Expand Down
53 changes: 41 additions & 12 deletions mpd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ CMPD* MPD = 0;
void CMPD::SetSong(const Song *song)
{
_cached = false;
if(song && !song->getArtist().empty() && !song->getTitle().empty()) {
if(song && !song->getArtist().empty() && !song->getTitle().empty()) {
_song = *song;
_gotsong = true;
iprintf("New song: %s - %s", _song.getArtist().c_str(), _song.getTitle().c_str());
Expand Down Expand Up @@ -34,6 +34,7 @@ CMPD::CMPD(CConfig *cfg)
_connected = false;
_cached = false;
_songid = -1;
_song = Song();
_songpos = -1;

if(Connect())
Expand Down Expand Up @@ -69,9 +70,9 @@ bool CMPD::Connect()
return _connected;
}

void CMPD::GotNewSong(struct mpd_song *song)
void CMPD::GotNewSong(struct mpd_song *song, int duration)
{
Song *s = new Song(song);
Song *s = new Song(song, duration);
SetSong(s);
delete s;
}
Expand All @@ -96,27 +97,48 @@ void CMPD::Update()
int newsongid = mpd_status_get_song_id(status);
int newsongpos = mpd_status_get_elapsed_time(status);
int curplaytime = mpd_stats_get_play_time(stats);
// new song (or the same song but from the beginning after it has been played long enough before)
if(newsongid != _songid || (_song.getDuration() != -1 && _songpos > (_song.getDuration()/2) && newsongpos < _songpos && newsongpos < 10)) {
int duration = mpd_status_get_total_time(status);
mpd_song *song = mpd_run_current_song(_conn);
Song *song_ = song ? new Song(song, duration) : NULL;

if(duration == 0){
if (song)
mpd_song_free(song);
if (song_)
delete song_;
mpd_status_free(status);
mpd_stats_free(stats);
return;
}


// new song (or the same song but from the beginning after it has been played long enough before)
if(newsongid != _songid ||
(song_ and _song != *song_) ||
(_song.getDuration() != -1 && _songpos > (_song.getDuration()/2) && newsongpos < _songpos && newsongpos < 10)) {
_songid = newsongid;
_songpos = newsongpos;
_start = curplaytime;

mpd_song *song = mpd_run_current_song(_conn);
//_start = 0;

if(song) {
GotNewSong(song);
mpd_song_free(song);
GotNewSong(song, duration);
} else {
_song = Song();
}
}

// song playing
if(newsongpos != _songpos) {
if(newsongpos != _songpos) {
_songpos = newsongpos;
CheckSubmit(curplaytime);
}

if (song)
mpd_song_free(song);
if (song_)
delete song_;

// check for client-to-client messages
if(mpd_send_read_messages(_conn)) {
mpd_message *msg;
Expand Down Expand Up @@ -144,7 +166,7 @@ void CMPD::Update()
}
}

Song::Song(struct mpd_song *song)
Song::Song(struct mpd_song *song, int duration)
{
const char* temp;

Expand All @@ -160,5 +182,12 @@ Song::Song(struct mpd_song *song)
temp = mpd_song_get_tag(song, MPD_TAG_ALBUM_ARTIST, 0);
albumartist = temp ? temp : "";

duration = mpd_song_get_duration(song);
temp = mpd_song_get_tag(song, MPD_TAG_TRACK, 0);
track = temp ? temp : "";

temp = mpd_song_get_tag(song, MPD_TAG_MUSICBRAINZ_TRACKID , 0);
mbid = temp ? temp : "";

//duration = mpd_song_get_duration(song);
this->duration = duration;
}
18 changes: 14 additions & 4 deletions mpd.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,31 @@
class Song {
public:
Song() {};
Song(struct mpd_song *song);
Song(std::string artist, std::string title, std::string album, int duration) {
Song(struct mpd_song *song, int duration);
Song(std::string artist, std::string title, std::string album, std::string albumartist, int duration, std::string track, std::string mbid) {
this->artist = artist;
this->title = title;
this->album = album;
this->albumartist = albumartist;
this->duration = duration;
this->track = track;
this->mbid = mbid;
}

std::string getArtist() const { return artist; }
std::string getTitle() const { return title; }
std::string getAlbum() const { return album; }
std::string getAlbumArtist() const { return albumartist; }
int getDuration() const { return duration; }
std::string getTrack() const { return track; }
std::string getMusicBrainzId() const { return mbid; }
bool operator != (const Song &other) const {
return this->getArtist() != other.getArtist() or
this->getAlbum() != other.getAlbum() or
this->getTitle() != other.getTitle();
}
private:
std::string albumartist, artist, title, album;
std::string albumartist, artist, title, album, track, mbid;
int duration = -1;
};

Expand All @@ -36,7 +46,7 @@ class CMPD

inline bool isConnected() { return _connected; }
private:
void GotNewSong(struct mpd_song *song);
void GotNewSong(struct mpd_song *song, int duration);

CConfig *_cfg;
mpd_connection *_conn;
Expand Down