Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import time
- import random
- #import pygame # if you don't have pygame: sudo apt-get install python-pygame
- #CONFIG SECTION
- musicdir = '/home/pi/RetroPie/roms/music'
- maxvolume = 0.75
- volumefadespeed = 0.02
- restart = True # If true, this will cause the script to fade the music out and -stop- the song rather than pause it.
- #local variables
- bgm = [mp3 for mp3 in os.listdir(musicdir) if mp3[-3:] == "mp3" or mp3[-3:] == "ogg"]
- from pygame import mixer # import PyGame's music mixer
- mixer.init() # Prep that bad boy up.
- random.seed()
- volume = maxvolume # Store this for later, we will make some nice fadeouts eventually.
- #TODO: Fill in all of the current RetroPie Emulator process names in this list.
- emulatornames = ["retroarch"]
- #test: Ran into some issues, so we're camping for emulationstation.
- esStarted = False
- while not esStarted:
- time.sleep(1)
- pids = [pid for pid in os.listdir('/proc') if pid.isdigit()]
- for pid in pids:
- try:
- procname = open(os.path.join('/proc',pid,'comm'),'rb').read()
- if procname[:-1] == "emulationstatio":
- esStarted=True
- except IOError:
- continue
- #ES Should be going.
- while True:
- if not mixer.music.get_busy():
- song = os.path.join(musicdir,bgm[random.randint(0,len(bgm)-1)])
- mixer.music.load(song)
- 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.
- mixer.music.play()
- print "BGM Now Playing: " + song
- pids = [pid for pid in os.listdir('/proc') if pid.isdigit()]
- emulator = -1;
- for pid in pids:
- try:
- procname = open(os.path.join('/proc',pid,'comm'),'rb').read()
- if procname[:-1] in emulatornames:
- emulator = pid;
- #here is where we'd start muting the music.
- print "Emulator found! " + procname[:-1] + " Muting the music..."
- while volume > 0:
- volume = volume - volumefadespeed
- if volume < 0:
- volume=0
- mixer.music.set_volume(volume);
- time.sleep(0.05)
- if restart:
- mixer.music.stop()
- else:
- mixer.music.pause() # TODO: Fadeout, rather than just outright stop.
- print("Muted. Monitoring emulator.")
- while os.path.exists("/proc/" + pid):
- time.sleep(1); # Delay 1 second and check again.
- #and here is where we'd turn it up.
- print "Emulator finished, resuming audio..."
- if not restart:
- mixer.music.unpause() # TODO: Fadein, rather than just outright blare the audio.
- while volume < maxvolume: # if restart is true, don't do anything - the volume resets when loading a song anyways.
- volume = volume + volumefadespeed;
- if volume > maxvolume:
- volume=maxvolume
- mixer.music.set_volume(volume);
- time.sleep(0.05)
- #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.
- print "Restored."
- volume=maxvolume # if restart is true, fadeouts break after the first. this prevents this.
- except IOError: #proc has already terminated, ignore.
- continue
- time.sleep(1);
- #end of the main while loop
- print "An error has occurred that has stopped Test1.py from executing."
Add Comment
Please, Sign In to add comment