Advertisement
Guest User

RetroPie BGM v1.03

a guest
Apr 23rd, 2016
12,930
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.71 KB | None | 0 0
  1. import os
  2. import time
  3. import random
  4. #import pygame # if you don't have pygame: sudo apt-get install python-pygame
  5. #also that line is commented out as we import the mixer specifically a bit further down.
  6.  
  7. #CONFIG SECTION
  8. startdelay = 0 # Value (in seconds) to delay audio start.  If you have a splash screen with audio and the script is playing music over the top of it, increase this value to delay the script from starting.
  9. musicdir = '/home/pi/RetroPie/roms/music'
  10. maxvolume = 0.75
  11. volumefadespeed = 0.02
  12. restart = True # If true, this will cause the script to fade the music out and -stop- the song rather than pause it.
  13. startsong = "" # if this is not blank, this is the EXACT, CaSeSeNsAtIvE filename of the song you always want to play first on boot.
  14.  
  15. #local variables
  16. bgm = [mp3 for mp3 in os.listdir(musicdir) if mp3[-4:] == ".mp3" or mp3[-4:] == ".ogg"] # Find everything that's .mp3 or .ogg
  17. lastsong = -1
  18. currentsong = -1
  19. from pygame import mixer # import PyGame's music mixer
  20. mixer.init() # Prep that bad boy up.
  21. random.seed()
  22. volume = maxvolume # Store this for later use to handle fading out.
  23.  
  24. #TODO: Fill in all of the current RetroPie Emulator process names in this list.
  25. emulatornames = ["retroarch","ags","uae4all2","uae4arm","capricerpi","linapple","hatari","stella","atari800","xroar","vice","daphne","reicast","pifba","osmose","gpsp","jzintv","basiliskll","mame","advmame","dgen","openmsx","mupen64plus","gngeo","dosbox","ppsspp","simcoupe","scummvm","snes9x","pisnes","frotz","fbzx","fuse","gemrb","cgenesis","zdoom","eduke32","lincity","love","kodi","alephone","micropolis","openbor","openttd","opentyrian","cannonball","tyrquake","ioquake3","residualvm","xrick","sdlpop","uqm","stratagus","wolf4sdl","solarus"]
  26.  
  27. #test: Ran into some issues with script crashing on a cold boot, so we're camping for emulationstation (if ES can start, so can we!)
  28. esStarted = False
  29. while not esStarted:
  30.     time.sleep(1)
  31.     pids = [pid for pid in os.listdir('/proc') if pid.isdigit()]
  32.     for pid in pids:
  33.         try:
  34.             procname = open(os.path.join('/proc',pid,'comm'),'rb').read()
  35.             if procname[:-1] == "emulationstatio": # Emulation Station's actual process name is apparently short 1 letter.
  36.                 esStarted=True
  37.         except IOError:
  38.             continue
  39.  
  40. #ES Should be going, see if we need to delay our start
  41.  
  42. if startdelay > 0:
  43.     time.sleep(startdelay) # Delay audio start per config option above
  44.    
  45. #Look for OMXplayer - if it's running, someone's got a splash screen going!
  46. pids = [pid for pid in os.listdir('/proc') if pid.isdigit()]
  47. for pid in pids:
  48.     try:
  49.         procname = open(os.path.join('/proc',pid,'comm'),'rb').read()
  50.         if procname[:-1] == "omxplayer" or procname[:-1] == "omxplayer.bin": # Looking for a splash screen!
  51.             while os.path.exists('/proc/'+pid):
  52.                 time.sleep(1) #OMXPlayer is running, sleep 1 to prevent the need for a splash.
  53.     except IOError:
  54.         continue
  55.        
  56. #Check for a starting song
  57. if not startsong == "":
  58.     try:
  59.         currentsong = bgm.index(startsong) #Set the currentsong to the index in BGM that is our startingsong.
  60.     except:
  61.         currentsong = -1 #If this triggers, you probably screwed up the filename, because our startsong wasn't found in the list.
  62.  
  63. #This is where the magic happens.
  64. while True:
  65.     while not esStarted: #New check (4/23/16) - Make sure EmulationStation is actually started.  There is code further down that, as part of the emulator loop, makes sure eS is running.
  66.         if mixer.music.get_busy():
  67.             mixer.music.stop(); #halt the music, emulationStation is not running!
  68.         time.sleep(10)
  69.         pids = [pid for pid in os.listdir('/proc') if pid.isdigit()]
  70.         for pid in pids:
  71.             try:
  72.                 procname = open(os.path.join('/proc',pid,'comm'),'rb').read()
  73.                 if procname[:-1] == "emulationstatio": # Emulation Station's actual process name is apparently short 1 letter.
  74.                     esStarted=True # Will cause us to break out of the loop because ES is now running.
  75.             except IOError:
  76.                 continue
  77.                
  78.     #Check to see if the DisableMusic file exists; if it does, stop doing everything!
  79.     if os.path.exists('/home/pi/PyScripts/DisableMusic'):
  80.         print "DisableMusic found!"
  81.         if mixer.music.get_busy():
  82.             mixer.music.stop();
  83.         while (os.path.exists('/home/pi/PyScripts/DisableMusic')):
  84.             time.sleep(15)
  85.         print "DisableMusic gone!"
  86.  
  87.     if not mixer.music.get_busy(): # We aren't currently playing any music
  88.         while currentsong == lastsong and len(bgm) > 1: #If we have more than one BGM, choose a new one until we get one that isn't what we just played.
  89.             currentsong = random.randint(0,len(bgm)-1)
  90.         song = os.path.join(musicdir,bgm[currentsong])
  91.         mixer.music.load(song)
  92.         lastsong=currentsong
  93.         mixer.music.set_volume(maxvolume) # Pygame sets this to 1.0 on new song; in case max volume -isnt- 1, set it to max volume.
  94.         mixer.music.play()
  95.         print "BGM Now Playing: " + song
  96.        
  97.     #Emulator check
  98.     pids = [pid for pid in os.listdir('/proc') if pid.isdigit()]
  99.     emulator = -1;
  100.     esStarted=False #New check 4-23-16 - set this to False (assume ES is no longer running until proven otherwise)
  101.     for pid in pids:
  102.         try:
  103.             procname = open(os.path.join('/proc',pid,'comm'),'rb').read()
  104.             if procname[:-1] == "emulationstatio": # Killing 2 birds with one stone, while we look for emulators, make sure EmulationStation is still running.
  105.                     esStarted=True # And turn it back to True, because it wasn't done running.  This will prevent the loop above from stopping the music.
  106.            
  107.             if procname[:-1] in emulatornames: #If the process name is in our list of known emulators
  108.                 emulator = pid;
  109.                 #Turn down the music
  110.                 print "Emulator found! " + procname[:-1] + " Muting the music..."
  111.                 while volume > 0:
  112.                     volume = volume - volumefadespeed
  113.                     if volume < 0:
  114.                         volume=0
  115.                     mixer.music.set_volume(volume);
  116.                     time.sleep(0.05)           
  117.                 if restart:
  118.                     mixer.music.stop() #we aren't going to resume the audio, so stop it outright.
  119.                 else:
  120.                     mixer.music.pause() #we are going to resume, so pause it.
  121.                 print("Muted.  Monitoring emulator.")
  122.                 while os.path.exists("/proc/" + pid):
  123.                     time.sleep(1); # Delay 1 second and check again.
  124.                 #Turn up the music
  125.                 print "Emulator finished, resuming audio..."
  126.                 if not restart:
  127.                     mixer.music.unpause() #resume
  128.                     while volume < maxvolume:
  129.                         volume = volume + volumefadespeed;
  130.                         if volume > maxvolume:
  131.                             volume=maxvolume
  132.                         mixer.music.set_volume(volume);
  133.                         time.sleep(0.05)               
  134.                 print "Restored."
  135.                 volume=maxvolume # ensures that the volume is manually set (if restart is True, volume would be at zero)
  136.  
  137.         except IOError: #proc has already terminated, ignore.
  138.             continue
  139.  
  140.     time.sleep(1);
  141.     #end of the main while loop
  142.    
  143. print "An error has occurred that has stopped Test1.py from executing." #theoretically you should never get this far.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement