Background Music [Continued from Help/Support]
So approximately a week and a half ago I had inquired if background music was an option available within RetroPie; long story short not any time soon unless I coded it myself. Spent about 2-3 hours last night and came up with a (semi-complete) version that does work and should be easy enough to adjust/adapt for later:
Version 1: http://pastebin.com/rFdxRivq
Version 1.01: http://pastebin.com/BVBxM41X (4/22/16)
Version 1.02: http://pastebin.com/H8hT6YeW (4/22/16, afternoon)
Version 1.03: http://pastebin.com/7E9JiZGQ (4/23/16)
(Hopefully no one minds Pastebin, I'm still editing the file on my Dropbox and I'd rather not risk someone grabbing a broken version)
Script's purpose was simple, play some background music whenever you're searching for a game inside Emulation Station. Long story short, it just watches for any emulator in the internal list "emulatornames" to be running, turns the music volume down, and waits for the emulator to exit before turning it back up.
First, this script relies on the pygame module being installed, which RetroPie does not come installed with. I used SSH for the below, but if you exit Emulation Station you should be able to do this at a regular prompt.
- Installing PyGame is simple; run "sudo apt-get install python-pygame", and answer "yes" when prompted. Let it finish, and move on.
Next is putting the script on the Pi and changing some configuration stuff in the script.
- Stick the file somewhere you won't lose it; for me I dumped it in /home/pi/PyScript. Name it whatever you want, as long as it ends in .py and don't forget what you called it. Because I am terrible at naming, I named it Test1.py.
- Execute the command "sudo nano /home/pi/PyScripts/Test1.py" replacing Test1.py with the (hopefully better) name you've saved the script as. At the top is a small section labeled "CONFIG."
- Change the musicdir variable to the appropriate music location. If you're keeping it simple like me, I made a folder titled "music" where the roms are.
- Change the maxvolume to a value between 0.0 (muted) and 1.0 (maximum). I have it set to 0.75, which is about the right volume so the music is not louder than the games, your mileage may vary.
- You don't need to change this now, but make note of the volumefadespeed setting. If the music is fading in too fast or slow for your tastes, adjust this value higher or lower to make it fade in faster or slower. Small amounts make a big difference!
- You can either choose to have the song change whenever you exit the emulator, or have it resume where it left off. If you want it to start a new song, make sure restart = True, otherwise change it to read "restart = False". The capital letters are important as well, or the script will crash and do nothing at all.
- Lastly, if there is a specific song you want to play whenever the script starts (such as immediately after boot, every time it boots), change startsong = "" to startsong = "song name here.mp3". The song name is case sensitive, so make sure it's an EXACT match, including the file extension!
- Press Control+x, then y, then enter to exit nano and save your changes to the file, if you made any.
Still with me? There's one last change we need to make, and that's actually making the script start on boot.
- Run "sudo nano /etc/rc.local"
- This file should have a bunch of commented stuff at the top, and "exit 0;" near the bottom. Above exit 0, put the following code, replacing the path with where you installed the script to earlier: "(sudo python /home/pi/PyScripts/Test1.py) &". For full disclosure, the & at the end runs the script in the background, which is necessary as it will run infinitely until the system shuts down.
- As before, press control+x, y, and enter to save changes.
The last step is arguably the most important. Put some music on your Pi! From my Windows PC I connected to the RetroPie, navigated to the roms folder, and created a new folder named "music". Stick some MP3's in there, reboot the box, and you should hear music when Emulation Station starts.
- PyGame has a message warning about using MP3's. If you experience any crashes on specific songs, try re-encoding them as OGG files, which the script supports as well.
If you run into any problems with it lemme know; I can't guarantee I'll be able to fix it (I learned just enough Python to make this work), but we won't know until you ask.
DISCLAIMER: This script is free. It comes without warranty, or any guarantee of support/bug fixes. I take no responsibility for any issues this script may create, so MAKE BACKUPS before you start messing around!
Thanks to: The RetroPie Crew, for creating RetroPie and giving me something to mess around with in my free time :)
Edit: Not sure why the dot-py filenames are turning into links, anyone know how to stop them from doing that?
I look forward to testing this out!
Great Work! Working beautifully. Only issue I had was that music would overlap with my video splash screen. Added a bash script to sleep 20 seconds before starting python script. Works like a charm. Looking forward to seeing complete script.
@HoolyHoo Never thought about that, I'll add a quick config option to the next version to have it snooze a bit in case someone else has a similar splash screen (mine doesn't have audio so needing the extra delay never crossed my mind).
Updated, added a link to 1.01. Same installation directions as before, new config options/functions:
Proper Random - If there's 2 or more songs in the music folder, it will NOT play the same one twice in a row. May not be an issue for some, but it's saving me from actually hating Whitesnake's Still of the Night, which always seemed to play two or three times in a row before letting something else play.
Delayed Start. Change startdelay to a value (in seconds) if you need the script to delay itself for some reason. At first this would have been for something like a video splash screen, but:
Will not overrule OMXPlayer. I realized the same code I used to watch for EmulationStation to start could be used to make sure OMXPlayer was -not- running, so I added it. Behind the scenes, once ES is running, it runs one last check against all PID's to see if any are omxplayer or omxplayer.bin - if it finds one, it sleeps until it stops running, almost like an emulator. (For those who don't know, OMXPlayer is what the video splash screens play in - this means no more music over the top of your sweet splashes!)
I also went through the wiki and added what should be all of the emulators to the internal emulator list. If anyone happens to find one that they use that the music doesn't shut up for, lemme know which emulator so I can figure out why it isn't detecting it.
Honestly not sure what else to do with this script at this point. If anyone's got any ideas or runs into problems let me know, but otherwise this might actually be done :)
Awesome! Not to be a pain but would it be possible to add the option of always starting with a particular mp3 when starting emulation station and then going random?
It's not over until
the fat lady singsthere is a wiki entry for it!
Just to add, was looking for a way to enable and disable the background music while in emulation station and creating a way for it to stick when rebooting. Don't know if there is a better way of doing it but I created two simple scripts . One that would kill the python background music and rewrite the rc.local file and the other that would start the python background script and rewrite the rc.local file. I then added those scripts to the retropie settings menu. Works well. Thanks again!
@HoolyHoo I may have a slightly less hacky-way to put it together, I'll do some checking with it (theoretically we could just abuse os.path.exists as part of the loop, to see if a file "disablemusic" exists. The script would then just never play music as long as that file exists, and the shell scripts would just need to create and erase that specific file rather than editing rc.local).
I'm working on the quick fix for starting with one specific song now; I have it coded but I need to test it on the cab downstairs, assuming no problems I'll have it posted within 30 minutes.
Thanks @Livewire !
Made it in JUST under 30 minutes (well, at least before I started typing my reply) :) 1.02's up:
/home/pi/PyScripts/DisableMusic . If this location exists (either as a folder, OR as a file), the script will cease to play music. Once found, it waits 15 seconds before checking again, so after removing that file it may take up to 15 seconds for the script to resume playing music. Did this mostly because if you're disabling music, you're probably disabling it for a while, not just for 2 or 3 seconds, so why check it every second.
startmusic="" (near the top of the script). Stick in the exact case-sensitive filename, and if that file is found in the bgm list, it will always play first when the script starts.
Invariably about 3 seconds after I posted it to pastebin I did notice that I have startmusic in the "#local variables" section instead of config; it's there, just go down a few lines :) Also, if you don't want it to check /home/pi/PyScripts/DisableMusic, such as if you have it in a different install location, the lines that check for that file are 66 and 70; make sure they match, but you can change them freely.
mike last edited by
Thanks for this script @Livewire . I've been playing around with it a bit today, works great.
I'm still very much a beginner with python and I'm a bit stuck finding a way to stop the music if I quit out of emulationstation.
At the moment the music still plays after ES has exited, I don't wan't to quit the script, just stop the music until ES starts again.
I'm trying to do a bit more reading to understand your code for myself but if you made another update could you add that feature?
@mike I might not get a good chance to test it before tomorrow night but I should be able to do that, it's another use-case I didn't think of :)
@mike Done, ended up having time to test before the weekly gamer meetup :) Added a few extra comments to (hopefully) help with dissecting the code as well.
- Just what Mike wanted; the music stops when ES is not running, and resumes within 10 seconds of it restarting.
- I moved startsong up into the config section where it should have been, cause I'm a derp.
mike last edited by
That's great, thanks. The comments really help too.
This is a horrible idea. This means that if the file exists on an external hard drive, the hard drive will never spin down because it's constantly being accessed. This also bad if your pi does not have a constant power source.
How about instead of looking for a file, it simply kills the script off and restarts it?
To be truly honest, that I'm not actually sure how to do (without running an entirely separate script I mean, since there's not really a way to have a python script kill and restart itself at a later time that I know of). If you've got it running on an external HDD, I'd recommend not using 1.03 then.
Hi. My English is very bad. Could you make a video tutorial? In this way, we would see how to do it and we would see progress.
I followed step by step and it does not work. Version 3.7 Retropie in a Raspberry 3b. .mp3 and .ogg files. A video tutorial clarify everything. Thank you.