Advertisement
Push28

GW-Bgm

Mar 21st, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.78 KB | None | 0 0
  1. #!/usr/bin/python
  2. import os
  3. import time
  4. import random
  5. import commands
  6. import RPi.GPIO as GPIO
  7.  
  8. #import pygame # if you don't have pygame: sudo apt-get install python-pygame
  9. #also that line is commented out as we import the mixer specifically a bit further down.
  10.  
  11. #CONFIG SECTION
  12. 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.
  13. musicdir = '/home/pi/RetroPie/roms/music'
  14. maxvolume = 1.0
  15. volumefadespeed = 0.02
  16. restart = True # If true, this will cause the script to fade the music out and -stop- the song rather than pause it.
  17. startsong = "" # if this is not blank, this is the EXACT, CaSeSeNsAtIvE filename of the song you always want to play first on boot.
  18.  
  19. #local variables
  20. bgm = [mp3 for mp3 in os.listdir(musicdir) if mp3[-4:] == ".mp3" or mp3[-4:] == ".ogg"] # Find everything that's .mp3 or .ogg
  21. lastsong = -1
  22. currentsong = -1
  23. from pygame import mixer # import PyGame's music mixer
  24. mixer.init() # Prep that bad boy up.
  25. random.seed()
  26. volume = maxvolume # Store this for later use to handle fading out.
  27.  
  28. #TODO: Fill in all of the current RetroPie Emulator process names in this list.
  29. emulatornames = ["retroarch","vlc-nox","ags","uae4all2","mp4","irssi","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"]
  30.  
  31. #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!)
  32. esStarted = False
  33. while not esStarted:
  34.     time.sleep(1)
  35.     pids = [pid for pid in os.listdir('/proc') if pid.isdigit()]
  36.     for pid in pids:
  37.         try:
  38.             procname = open(os.path.join('/proc',pid,'comm'),'rb').read()
  39.             if procname[:-1] == "emulationstatio": # Emulation Station's actual process name is apparently short 1 letter.
  40.                 esStarted=True
  41.         except IOError:
  42.             continue
  43.  
  44. #ES Should be going, see if we need to delay our start
  45.  
  46. if startdelay > 0:
  47.     time.sleep(startdelay) # Delay audio start per config option above
  48.  
  49. #Look for OMXplayer - if it's running, someone's got a splash screen going!
  50. pids = [pid for pid in os.listdir('/proc') if pid.isdigit()]
  51. for pid in pids:
  52.     try:
  53.         procname = open(os.path.join('/proc',pid,'comm'),'rb').read()
  54.         if procname[:-1] == "omxplayer" or procname[:-1] == "omxplayer.bin": # Looking for a splash screen!
  55.             while os.path.exists('/proc/'+pid):
  56.                 time.sleep(1) #OMXPlayer is running, sleep 1 to prevent the need for a splash.
  57.     except IOError:
  58.         continue
  59.  
  60. #Check for a starting song
  61. if not startsong == "":
  62.     try:
  63.         currentsong = bgm.index(startsong) #Set the currentsong to the index in BGM that is our startingsong.
  64.     except:
  65.         currentsong = -1 #If this triggers, you probably screwed up the filename, because our startsong wasn't found in the list.
  66.  
  67. #This is where the magic happens.
  68. while True:
  69.     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.
  70.         if mixer.music.get_busy():
  71.             mixer.music.stop(); #halt the music, emulationStation is not running!
  72.         time.sleep(10)
  73.         pids = [pid for pid in os.listdir('/proc') if pid.isdigit()]
  74.         for pid in pids:
  75.             try:
  76.                 procname = open(os.path.join('/proc',pid,'comm'),'rb').read()
  77.                 if procname[:-1] == "emulationstatio": # Emulation Station's actual process name is apparently short 1 letter.
  78.                     esStarted=True # Will cause us to break out of the loop because ES is now running.
  79.             except IOError:
  80.                 continue
  81.     #Check to see if the DisableMusic file exists; if it does, stop doing everything!
  82.     if os.path.exists('/home/pi/RetroPie/roms/music/DisableMusic'):
  83.         print "DisableMusic found!"
  84.         f=open("/home/pi/RetroPie/roms/music/status","w")
  85.                 r="DisableMusic found!"
  86.                 print >> f, r
  87.                 f.close()
  88.         if mixer.music.get_busy():
  89.             mixer.music.stop();
  90.         while (os.path.exists('/home/pi/RetroPie/roms/music/DisableMusic')):
  91.             time.sleep(15)
  92.         print "DisableMusic gone!"
  93.         f=open("/home/pi/RetroPie/roms/music/status","w")
  94.                 r="DisableMusic gone! party time "
  95.                 print >> f, r
  96.                 f.close()
  97.  
  98.     if not mixer.music.get_busy(): # We aren't currently playing any music
  99.         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.
  100.             currentsong = random.randint(0,len(bgm)-1)
  101.         song = os.path.join(musicdir,bgm[currentsong])
  102.         mixer.music.load(song)
  103.         lastsong=currentsong
  104.         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.
  105.         mixer.music.play()
  106.         #print "BGM Now Playing: " + song
  107.         f=open("/home/pi/RetroPie/roms/music/status","w")
  108.         r="BGM Now Playing: " + song
  109.         print >> f, r
  110.         f.close()
  111.     #Emulator check
  112.     pids = [pid for pid in os.listdir('/proc') if pid.isdigit()]
  113.     emulator = -1;
  114.     esStarted=False #New check 4-23-16 - set this to False (assume ES is no longer running until proven otherwise)
  115.     for pid in pids:
  116.         try:
  117.             procname = open(os.path.join('/proc',pid,'comm'),'rb').read()
  118.             if procname[:-1] == "emulationstatio": # Killing 2 birds with one stone, while we look for emulators, make sure EmulationStation is still running.
  119.                     esStarted=True # And turn it back to True, because it wasn't done running.  This will prevent the loop above from stopping the music.
  120.             if procname[:-1] in emulatornames: #If the process name is in our list of known emulators
  121.                 emulator = pid;
  122.                 #Turn down the music
  123.                 #print "Emulator found! " + procname[:-1] + " Muting the music..."
  124.                 f=open("/home/pi/RetroPie/roms/music/status","w")
  125.                         r="Emulator found! " + procname[:-1] + " Muting the music..."
  126.                         print >> f, r
  127.                         f.close()
  128.                 while volume > 0:
  129.                     volume = volume - volumefadespeed
  130.                     if volume < 0:
  131.                         volume=0
  132.                     mixer.music.set_volume(volume);
  133.                     time.sleep(0.05)
  134.                 if restart:
  135.                     mixer.music.stop() #we aren't going to resume the audio, so stop it outright.
  136.                 else:
  137.                     mixer.music.pause() #we are going to resume, so pause it.
  138.                 #print("Muted.  Monitoring emulator.")
  139.                 f=open("/home/pi/RetroPie/roms/music/status","w")
  140.                         r="Muted.  Monitoring emulator."
  141.                         print >> f, r
  142.                         f.close()
  143.                 while os.path.exists("/proc/" + pid):
  144.                     time.sleep(1); # Delay 1 second and check again.
  145.                 #Turn up the music
  146.                 #print "Emulator finished, resuming audio..."
  147.                 f=open("/home/pi/RetroPie/roms/music/status","w")
  148.                                 r="Emulator finished, resuming audio..."
  149.                                 print >> f, r
  150.                                 f.close()
  151.                 if not restart:
  152.                     mixer.music.unpause() #resume
  153.                     while volume < maxvolume:
  154.                         volume = volume + volumefadespeed;
  155.                         if volume > maxvolume:
  156.                             volume=maxvolume
  157.                         mixer.music.set_volume(volume);
  158.                         time.sleep(0.05)
  159.                 #print "Restored."
  160.                 volume=maxvolume # ensures that the volume is manually set (if restart is True, volume would be at zero)
  161.  
  162.         except IOError: #proc has already terminated, ignore.
  163.             continue
  164.  
  165.     time.sleep(1);
  166.     #end of the main while loop
  167.  
  168. 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