Guest User

RetroPie BGM

a guest
Apr 19th, 2016
1,190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.70 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.  
  6. #CONFIG SECTION
  7. musicdir = '/home/pi/RetroPie/roms/music'
  8. maxvolume = 0.75
  9. volumefadespeed = 0.02
  10. restart = True # If true, this will cause the script to fade the music out and -stop- the song rather than pause it.
  11. #local variables
  12. bgm = [mp3 for mp3 in os.listdir(musicdir) if mp3[-3:] == "mp3" or mp3[-3:] == "ogg"]
  13. from pygame import mixer # import PyGame's music mixer
  14. mixer.init() # Prep that bad boy up.
  15.  
  16. random.seed()
  17. volume = maxvolume # Store this for later, we will make some nice fadeouts eventually.
  18.  
  19. #TODO: Fill in all of the current RetroPie Emulator process names in this list.
  20. emulatornames = ["retroarch"]
  21.  
  22. #test: Ran into some issues, so we're camping for emulationstation.
  23. esStarted = False
  24. while not esStarted:
  25.     time.sleep(1)
  26.     pids = [pid for pid in os.listdir('/proc') if pid.isdigit()]
  27.     for pid in pids:
  28.         try:
  29.             procname = open(os.path.join('/proc',pid,'comm'),'rb').read()
  30.             if procname[:-1] == "emulationstatio":
  31.                 esStarted=True
  32.         except IOError:
  33.             continue
  34.  
  35. #ES Should be going.
  36. while True:
  37.     if not mixer.music.get_busy():
  38.         song = os.path.join(musicdir,bgm[random.randint(0,len(bgm)-1)])
  39.         mixer.music.load(song)
  40.         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.
  41.         mixer.music.play()
  42.         print "BGM Now Playing: " + song
  43.     pids = [pid for pid in os.listdir('/proc') if pid.isdigit()]
  44.     emulator = -1;
  45.     for pid in pids:
  46.         try:
  47.             procname = open(os.path.join('/proc',pid,'comm'),'rb').read()
  48.             if procname[:-1] in emulatornames:
  49.                 emulator = pid;
  50.                 #here is where we'd start muting the music.
  51.                 print "Emulator found! " + procname[:-1] + " Muting the music..."
  52.                 while volume > 0:
  53.                     volume = volume - volumefadespeed
  54.                     if volume < 0:
  55.                         volume=0
  56.                     mixer.music.set_volume(volume);
  57.                     time.sleep(0.05)            
  58.                 if restart:
  59.                     mixer.music.stop()
  60.                 else:
  61.                     mixer.music.pause() # TODO: Fadeout, rather than just outright stop.
  62.                 print("Muted.  Monitoring emulator.")
  63.                 while os.path.exists("/proc/" + pid):
  64.                     time.sleep(1); # Delay 1 second and check again.
  65.                 #and here is where we'd turn it up.
  66.                 print "Emulator finished, resuming audio..."
  67.                 if not restart:
  68.                     mixer.music.unpause() # TODO: Fadein, rather than just outright blare the audio.
  69.                     while volume < maxvolume: # if restart is true, don't do anything - the volume resets when loading a song anyways.
  70.                         volume = volume + volumefadespeed;
  71.                         if volume > maxvolume:
  72.                             volume=maxvolume
  73.                         mixer.music.set_volume(volume);
  74.                         time.sleep(0.05)
  75.                 #if restart is true, don't do anything - when the script restarts the main loop, it will force a re-test of the music and relaunch it.
  76.                 print "Restored."
  77.                 volume=maxvolume # if restart is true, fadeouts break after the first.  this prevents this.
  78.  
  79.         except IOError: #proc has already terminated, ignore.
  80.             continue
  81.  
  82.     time.sleep(1);
  83.     #end of the main while loop
  84.      
  85. print "An error has occurred that has stopped Test1.py from executing."
Add Comment
Please, Sign In to add comment