Advertisement
Guest User

RetroPie BM v1.02

a guest
Apr 22nd, 2016
465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.54 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.  
  14. #local variables
  15. bgm = [mp3 for mp3 in os.listdir(musicdir) if mp3[-4:] == ".mp3" or mp3[-4:] == ".ogg"] # Find everything that's .mp3 or .ogg
  16. lastsong = -1
  17. currentsong = -1
  18. startsong = "" # if this is not blank, this is the EXACT, CaSeSeNsAtIvE filename of the song you always want to play first on boot.
  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. #can we capture OMXPlayer here to automatically handle a long splash?
  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.     #Check to see if the DisableMusic file exists; if it does, stop doing everything!
  66.     if os.path.exists('/home/pi/PyScripts/DisableMusic'):
  67.         print "DisableMusic found!"
  68.         if mixer.music.get_busy():
  69.             mixer.music.stop();
  70.         while (os.path.exists('/home/pi/PyScripts/DisableMusic')):
  71.             time.sleep(15)
  72.         print "DisableMusic gone!"
  73.  
  74.     if not mixer.music.get_busy(): # We aren't currently playing any music
  75.         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.
  76.             currentsong = random.randint(0,len(bgm)-1)
  77.         song = os.path.join(musicdir,bgm[currentsong])
  78.         mixer.music.load(song)
  79.         lastsong=currentsong
  80.         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.
  81.         mixer.music.play()
  82.         print "BGM Now Playing: " + song
  83.     pids = [pid for pid in os.listdir('/proc') if pid.isdigit()]
  84.     emulator = -1;
  85.    
  86.     for pid in pids:
  87.         try:
  88.             procname = open(os.path.join('/proc',pid,'comm'),'rb').read()
  89.             if procname[:-1] in emulatornames:
  90.                 emulator = pid;
  91.                 #Turn down the music
  92.                 print "Emulator found! " + procname[:-1] + " Muting the music..."
  93.                 while volume > 0:
  94.                     volume = volume - volumefadespeed
  95.                     if volume < 0:
  96.                         volume=0
  97.                     mixer.music.set_volume(volume);
  98.                     time.sleep(0.05)           
  99.                 if restart:
  100.                     mixer.music.stop() #we aren't going to resume the audio, so stop it outright.
  101.                 else:
  102.                     mixer.music.pause() #we are going to resume, so pause it.
  103.                 print("Muted.  Monitoring emulator.")
  104.                 while os.path.exists("/proc/" + pid):
  105.                     time.sleep(1); # Delay 1 second and check again.
  106.                 #Turn up the music
  107.                 print "Emulator finished, resuming audio..."
  108.                 if not restart:
  109.                     mixer.music.unpause() #resume
  110.                     while volume < maxvolume:
  111.                         volume = volume + volumefadespeed;
  112.                         if volume > maxvolume:
  113.                             volume=maxvolume
  114.                         mixer.music.set_volume(volume);
  115.                         time.sleep(0.05)               
  116.                 print "Restored."
  117.                 volume=maxvolume # ensures that the volume is manually set (if restart is True, volume would be at zero)
  118.  
  119.         except IOError: #proc has already terminated, ignore.
  120.             continue
  121.  
  122.     time.sleep(1);
  123.     #end of the main while loop
  124.    
  125. 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