[opendev] The Background Music Box
-
Hello dear community members,
since there is no possibility to change the background music in ES, I wrote a small script for it. But wait! I make it freely available to the community so that it can be actively developed further. I have consciously renounced selection menus, file dialogs etc. even then there is no song limit...
So you put your song selections into the playlist and then with select you start the playlist party (...)
Rush to my github account to analyse the codeThe script offers you the following
- You see the current song in the background
- You can select a song from the directory list
- You can add your songs to a playlist
- You can activate the shuffle mode (endless)
- You can cancel the action and nothing changes
Have fun
To install v2 type:
wget http://raw.githubusercontent.com/crcerror/RetroPie-Shares/master/BGM_mp3play_v2 -O "/home/pi/RetroPie/retropiemenu/Background Music Box v2.sh"
To install the old version
wget http://raw.githubusercontent.com/crcerror/RetroPie-Shares/master/BGM_mp3play.sh -O "/home/pi/RetroPie/retropiemenu/Background Music Box v1.sh"
The script will be located to ES settingsmenu and you will see the script after reboot.The script will fail to start
- If the directory of BGM is wrong setted - default is
/home/pi/BGM/
- If no instance of the music player (default is mpg123) is running
- If there is more than one instance of your player is running ;)
- If there are no mp3 or ogg files in BGM-folder
error: expect 5 tokens to launch menu but only 4 available
Other features:
Please install the tool
lsof
to make the song that is currently played visible.
Install withsudo apt install lsof
. So we have a strong solutionps aux
will work, too but needs some extra coding.Please install the tool
mp3info
to get song length of current played song. Install withsudo apt install mp3info
.These 2 packages are very little in size (I think 2x100kB) so don't worry.
How it looks?
New version
Old version
Additional Info and future features that YOU will develop
We have also the possibilty for volume fading. You can try the script here
So it's also possible to use pngview and imagemagick as the python script to display a graphical overlay ;)Multiple selections of songs (kind of flow control)
Supress error messages with dialogs, so a msg of missing pathes?
If player instance isn't running, so why not start from this script?
Support for real file navigation with --fselect dialog
Reconfigure joy2key for more features ... ;) -
Small Update.... Now I'm done ... So it's time for YOUR improvements
- Added "PlayList" feature!
-
does this work with the fade script that you made?
-
@ExarKunIv Yes of course. It's absolutly independent from the FadeIn/Out script. Nothing changed so I take care that only ONE instance of the player is currently running.
My intention is a kind of "community project" to improve the code and have a full set of control behind ES. It is easily possible to get the same function as the pyhton script and to make it more "controllable"
Btw: This script should also work for XU4 without modification.
-
Some code snippets ;)
BGM_PATH="/home/pi/BGM" SONGLOOP=$(ps aux | grep "$BGM_PLAYER" | grep -o "$BGM_PATH" | wc -l)
This counts all songs that are currently in loop. So we can show the number of tracks till mpg123 finishes (except you use the -Z parameter this will cycle endless through your songs).
Annother question...
What behaviour is more natural? If I use PLAYLIST-button then the selected song will be send to Playlist array. If I now go to SELECT-button the marked song will be also send to Playlist array and mpg123 starts the tracklist.Or is is better to use the SELECT-button without adding a song to playlist array and just start playing (except if no other song is holded into playlist?) Maybe should rename the button to PLAY then ;)
-
Some more code snipptes....
How to detect current song playing?
- Method using
lsof
This tool is so versatile and should be part of the standard image. It detects every opened file or opened network connection (security reasons). So to detect the current song running a useful code snippet withlsof
-usage can be
songname=$(lsof -c $BGM_PLAYER -F | grep "$BGM_PATH") songname="${songname##*/}"
- Method is using
pgrep aux
command
This tool is a process monitoring tool that is also very versatile. It show the state of process and the whole command structure. Together with a simple mp3 player likempg123
it's possible to show all truncated filenames as "playlist". So a working code snippet is...
songname=$(ps aux | grep mpg123 | grep -o -E "$BGM_PATH.*") songname="${songname#*$BGM_PATH/}" songname="${songname%% /*}"
Which method is better? For comfort reasons Method 1 is more versatile because this will always work with every music player. Remember?
lsof
shows file access of processes whereas Method 2 shows just the whole command line ;)In the case of the Raspberry I would prefer Method2 becasue
lsof
is not installed by default :(More to come ;)
- Method using
-
Annother crippling lines of code
so take a swift look, I put the playlist entries in a ARRAY named
farray
.
I asked the question of how I could make better navgation through the items.
You know you putsong1.mp3
andsong2.mp3
into a Playlist by select the dedicated button. Now you stay onsong2.mp3
and press play. The usually this song would also be put into Playlist now. Not good? Yes I think so!So we will look to the codeline
[[ ${#farray[@]} -eq 0 || "${farray[-1]}" != "$BGM_PATH/$file" ]] && farray+=("$BGM_PATH/$file")
Inside the bash test command
[[ .... ]]
I have two conditionsIf
- the array is empty (that makes the # in front of the array)
OR - The last entry of the array HAS NOT the same name of the current selected file
THEN
add the file to the array and start playing your music ;)I just want you to show how, to count entries in arrays and the crazy thing (-1) read the latest array position (very usefull and also new to me)
more to come ;)
- the array is empty (that makes the # in front of the array)
-
Added restart if musicplayer is not available.
We try to start the Player with Shuffle mode.
I will release script code soon ;)! [[ -d $BGM_PATH ]] && dialog_error "Directory $BGM_PATH not found! Exit!" && exit if [[ $PLAYER_INSTANCE -eq 0 ]]; then dialog_yesno "$BGM_PLAYER not running!\nShould I try to start it using shuffle mode?\n\nShuffle command: $PLAYER_SHUFFLE [[ $? -eq 0 ]] || exit $PLAYER_SHUFFLE & exit fi
-
Finished
All improvements made here in this topic are merged to the final code
Sadly noone made suggestions ;)
So luckily I'm finished now - SCRUM run in 2 weeks
So Team, PO, SM in one person with 3 sprints ;) -
am i missing a step because when i try to launch the script all i get is "error expect 5 tokens to launch menu but only 4 available"
i am running 4.4 on a raspberry pi 3 b+
-
@Superman550 Then your array for mp3 files is empty. Place your mp3 files directly to
/home/pi/BGM
not in subfolders. Our can you tell me how your mp3 files are arranged? They need a mp3 file externsion as well.PS: If there are subfolders you can try to change line 76
-maxdepth 1
, to 2 or completly remove this argument. You see sadly I got no response for this as I coded so I wasn't aware of different setups ;)EDIT: Tried with
-maxdepth 2
and yes I can now acess subfolders within playlists. So this could be a possible solution. I do not change the code as long there are some responses. -
@cyperghost i did have the files in the BGM folder and i only did one to test the script out and i did have the music player script installed but when i did disabled the music player from running at start up your script worked again
-
Loving the script!
1 question: I notice when I select a song, once that song is over mpg123 stops by default.
How would I go about, or is it possible to modify the command so that once the selected song is over, it returns to shuffle mode on its own without the need to re-enter the script?Thanks for your work!
-
@ketaketish Thank you
I notice when I select a song, once that song is over mpg123 stops by default.
Yes that's a behaviour I'm aware of. But see my BGM tracks have a length of more then 60 min per track. If you want to continue music then you need a second call to mpg123 inside the script. But this is not good coding because the second will check every x seconds if the current session of mpg123 is still running or you can use
wait
command.I think it is not a great deal to restart the player in shuffle mode if you want to select a new track. Everything else is not clean coding imho. The best solution would be a player inside ES ;)
-
@cyperghost Thank you for your response!
I must admit, I have not listened to your tracks... I kind of immediately added my own :S
60min tracks would definitely be less of a nuance compared to my 2mins tracks haha.That explanation makes perfect sense. I am not as well-versed so I was more curious if it were possible but I do see that making a check every X seconds is a waste of resources.
Keep up the good work! :)
-
@cyperghost Just had a thought (if its a no then all good):
Would there be a way to analyse the selected mp3 file for its duration? Then output that into say "Length", then use "wait $Length" before returning to shuffle?
I love learning this stuff and have been doing most of it so far through these forums and "Trial and Error" and have learned all the Linux I know so far through it. So I do appreciate your explanations :)
-
@ketaketish about mp3 lenght... There are surly tools available that can analyse track length.
Easiest approch would be a small script like this
#!/bin/bash while pidof mpg123 > /dev/nul; do sleep 10 done mpg123 -q -Z /path/to/mp3files/*.mp3
Then you send this script to background alongside with the script. You add a line between 101 and 102 like
/path/to/script/above/shuffle.sh &
It's a dirty hack but will work
EDIT: Improved the dirty hack a bit
-
@ketaketish Even if this is a very old topic. I've added the length of mp3 files (for current song only)
There is a packagemp3info
- it's very capable in getting info of tracks ;)The whole script is improved alot.
Please install the tools lsof and mp3info before withsudo apt install lsof mp3info
The script will run without these but it will look more nice with the tools installed.Default player is mpg123
Please change the player inside the script or install withsudo apt install mpg123
mpg123 is the smallest mp3 I know for Debian .... it's very relyable and small in footprint and won't hurt your system setup!Changes:
- Code Cleanup
- Show length of current played song in mm:ss
- use realpath for path resolving
- use
/dev/null
hack to use mpg123 with Raspbian stretch (=RetroPie 4.5.1)
-
It's possible to see tracklength of current selected track...
Ugly hack!! removed!!!
and add
--item-help
to your dialog config, then you can see tracklength of selected mp3 title ;) Maybe somebody can improve the coding .... I did it just in a swift way. -
So this is an updated version of the music player with additional info to selected mp3 file. See bottom line ....
Get it from here
Contributions to the project are always appreciated, so if you would like to support us with a donation you can do so here.
Hosting provided by Mythic-Beasts. See the Hosting Information page for more information.