Skip to content

Commit

Permalink
Display cover art and metadata for Spotify Connect
Browse files Browse the repository at this point in the history
  • Loading branch information
moodeaudio committed Nov 4, 2024
1 parent 84002b2 commit 771f1dd
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
15 changes: 14 additions & 1 deletion var/local/www/commandw/spotevent.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

LOGFILE="/var/log/moode_spotevent.log"
DEBUG=$(sudo moodeutl -d -gv debuglog)
SPOTMETA_FILE="/var/local/www/spotmeta.txt"

debug_log () {
if [[ $DEBUG == '0' ]]; then
Expand All @@ -19,6 +20,7 @@ debug_log () {
PLAYER_EVENTS=(
session_connected
session_disconnected
track_changed
)

MATCH=0
Expand Down Expand Up @@ -82,7 +84,6 @@ if [[ $PLAYER_EVENT == "session_connected" ]]; then
fi
fi

#if [[ $PLAYER_EVENT == "paused" ]] || [[ $PLAYER_EVENT == "stopped" ]]; then
if [[ $PLAYER_EVENT == "session_disconnected" ]]; then
$(sqlite3 $SQLDB "UPDATE cfg_system SET value='0' WHERE param='spotactive'")

Expand Down Expand Up @@ -111,3 +112,15 @@ if [[ $PLAYER_EVENT == "session_disconnected" ]]; then
/usr/bin/mpc play > /dev/null
fi
fi

if [[ $PLAYER_EVENT == "track_changed" ]]; then
COVER=$(echo $COVERS | cut -d " " -f 1)
#ARTIST=$(echo $ARTISTS | cut -d " " -f 1) should be \n delimited
#ARTISTS=$(echo "$ARTISTS" | tr '\n' ', ') should be \n delimited
METADATA=$NAME";"$ARTISTS";"$ALBUM";"$DURATION_MS";"$COVER
echo -e $ARTISTS > /home/pi/tim.txt
echo -e $ALBUM_ARTISTS >> /home/pi/tim.txt
echo -e $COVERS >> /home/pi/tim.txt
echo -e $METADATA > $SPOTMETA_FILE
/var/www/util/send-fecmd.php "update_spotmeta,$METADATA"
fi
3 changes: 3 additions & 0 deletions www/command/renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
echo json_encode('worker busy');
}
break;
case 'get_spotmeta':
echo json_encode(file_get_contents(SPOTMETA_FILE));
break;
default:
echo 'Unknown command';
break;
Expand Down
3 changes: 3 additions & 0 deletions www/css/panels.css
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ html {background-color:inherit;}
#inpsrc-msg-text {display:block;font-size:1em;margin-bottom:0.5em;}
/* Volume control */
#inpsrc-preamp-volume, #multiroom-receiver-volume {font-size:.7em !important;}
/* Spotify metadata */
#spotify-metadata {display:block;margin-top:.5em;font-size:1em;font-weight:500;background-color:rgba(128,128,128,0.5);}
#spotify-metadata span {font-size:.75em;font-weight:400;}
/* Buttons */
.audioinfo-renderer,
.configure-renderer,
Expand Down
2 changes: 2 additions & 0 deletions www/inc/constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
const MPD_MUSICROOT = '/var/lib/mpd/music/';
const MPD_PLAYLIST_ROOT = '/var/lib/mpd/playlists/';
const MPD_LOG = '/var/log/mpd/log';
// Spotify Connect
const SPOTMETA_FILE = '/var/local/www/spotmeta.txt';
// SQLite
const SQLDB = 'sqlite:/var/local/www/db/moode-sqlite3.db';
const SQLDB_PATH = '/var/local/www/db/moode-sqlite3.db';
Expand Down
26 changes: 24 additions & 2 deletions www/js/playerlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,11 @@ function engineCmd() {
'<span id="inpsrc-msg-text">Spotify Active</span>' +
'<button class="btn disconnect-renderer" data-job="spotifysvc">Disconnect</button>' +
receiversBtn() +
audioInfoBtn());
audioInfoBtn() +
'<span id="spotify-metadata"></span>');
break;
case 'update_spotmeta':
updateSpotmeta(cmd[1]);
break;
case 'slactive1':
case 'slactive0':
Expand Down Expand Up @@ -760,6 +764,19 @@ function inpSrcIndicator(cmd, msgText) {
}
}

function updateSpotmeta(data) {
// data = title;artists;album;duration;coverurl
console.log(data);
var metadata = data.split(';');
$('#inpsrc-backdrop').html('<img class="ss-backdrop" ' + 'src="' + metadata[4] + '">');
$('#inpsrc-backdrop').css('filter', 'blur(0px)');
$('#inpsrc-backdrop').css('transform', 'scale(1.0)');
$('#spotify-metadata').html(
metadata[0] + ' (' + formatSongTime(Math.round(parseInt(metadata[3]) / 1000)) + ')' +
'<br>' + '<span>' + metadata[1] + '<br>' + metadata[2] + '</span>'
);
}

// Show/hide CoverView screen saver
function screenSaver(cmd) {
if ($('#inpsrc-indicator').css('display') == 'block' || UI.mobile) {
Expand Down Expand Up @@ -1326,7 +1343,12 @@ function renderUI() {
'<span id="inpsrc-msg-text">Spotify Active</span>' +
'<button class="btn disconnect-renderer" data-job="spotifysvc">Disconnect</button>' +
receiversBtn() +
audioInfoBtn());
audioInfoBtn() +
'<span id="spotify-metadata"></span>');

$.getJSON('command/renderer.php?cmd=get_spotmeta', function(data) {
updateSpotmeta(data);
});
}
// Squeezelite renderer
if (SESSION.json['slactive'] == '1') {
Expand Down

2 comments on commit 771f1dd

@tbgreen
Copy link

@tbgreen tbgreen commented on 771f1dd Nov 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that you have som paths hardcoded to "/home/pi/tim.txt". What happens if you do not have the directory of "/home/pi"?

@moodeaudio
Copy link
Contributor Author

@moodeaudio moodeaudio commented on 771f1dd Nov 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats just some debug code. No harm if those echo's fail. They will be removed from the production release.

Please sign in to comment.