Advertisement
Guest User

RetroPie BGM v1.01

a guest
Apr 22nd, 2016
639
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.78 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. from pygame import mixer # import PyGame's music mixer
  19. mixer.init() # Prep that bad boy up.
  20. random.seed()
  21. volume = maxvolume # Store this for later use to handle fading out.
  22.  
  23. #TODO: Fill in all of the current RetroPie Emulator process names in this list.
  24. 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"]
  25.  
  26. #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!)
  27. esStarted = False
  28. while not esStarted:
  29.     time.sleep(1)
  30.     pids = [pid for pid in os.listdir('/proc') if pid.isdigit()]
  31.     for pid in pids:
  32.         try:
  33.             procname = open(os.path.join('/proc',pid,'comm'),'rb').read()
  34.             if procname[:-1] == "emulationstatio": # Emulation Station's actual process name is apparently short 1 letter.
  35.                 esStarted=True
  36.         except IOError:
  37.             continue
  38.  
  39. #ES Should be going, see if we need to delay our start
  40.  
  41. if startdelay > 0:
  42.     time.sleep(startdelay) # Delay audio start per config option above
  43.    
  44. #can we capture OMXPlayer here to automatically handle a long splash?
  45. pids = [pid for pid in os.listdir('/proc') if pid.isdigit()]
  46. for pid in pids:
  47.     try:
  48.         procname = open(os.path.join('/proc',pid,'comm'),'rb').read()
  49.         if procname[:-1] == "omxplayer" or procname[:-1] == "omxplayer.bin": # Looking for a splash screen!
  50.             while os.path.exists('/proc/'+pid):
  51.                 time.sleep(1) #OMXPlayer is running, sleep 1 to prevent the need for a splash.
  52.     except IOError:
  53.         continue
  54.  
  55. #This is where the magic happens.
  56. while True:
  57.     if not mixer.music.get_busy(): # We aren't currently playing any music
  58.         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.
  59.             currentsong = random.randint(0,len(bgm)-1)
  60.         song = os.path.join(musicdir,bgm[currentsong])
  61.         mixer.music.load(song)
  62.         lastsong=currentsong
  63.         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.
  64.         mixer.music.play()
  65.         print "BGM Now Playing: " + song
  66.     pids = [pid for pid in os.listdir('/proc') if pid.isdigit()]
  67.     emulator = -1;
  68.     for pid in pids:
  69.         try:
  70.             procname = open(os.path.join('/proc',pid,'comm'),'rb').read()
  71.             if procname[:-1] in emulatornames:
  72.                 emulator = pid;
  73.                 #Turn down the music
  74.                 print "Emulator found! " + procname[:-1] + " Muting the music..."
  75.                 while volume > 0:
  76.                     volume = volume - volumefadespeed
  77.                     if volume < 0:
  78.                         volume=0
  79.                     mixer.music.set_volume(volume);
  80.                     time.sleep(0.05)           
  81.                 if restart:
  82.                     mixer.music.stop() #we aren't going to resume the audio, so stop it outright.
  83.                 else:
  84.                     mixer.music.pause() #we are going to resume, so pause it.
  85.                 print("Muted.  Monitoring emulator.")
  86.                 while os.path.exists("/proc/" + pid):
  87.                     time.sleep(1); # Delay 1 second and check again.
  88.                 #Turn up the music
  89.                 print "Emulator finished, resuming audio..."
  90.                 if not restart:
  91.                     mixer.music.unpause() #resume
  92.                     while volume < maxvolume:
  93.                         volume = volume + volumefadespeed;
  94.                         if volume > maxvolume:
  95.                             volume=maxvolume
  96.                         mixer.music.set_volume(volume);
  97.                         time.sleep(0.05)               
  98.                 print "Restored."
  99.                 volume=maxvolume # ensures that the volume is manually set (if restart is True, volume would be at zero)
  100.  
  101.         except IOError: #proc has already terminated, ignore.
  102.             continue
  103.  
  104.     time.sleep(1);
  105.     #end of the main while loop
  106.    
  107. 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