-
Notifications
You must be signed in to change notification settings - Fork 0
/
.livewire.py
executable file
·204 lines (187 loc) · 11.5 KB
/
.livewire.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import os
import pwd
import grp
import time
import random
import sys
#import pygame # if you don't have pygame: sudo apt-get install python-pygame
#also that line is commented out as we import the mixer specifically a bit further down.
#
#CONFIG SECTION
#INI FILE OVERRIDES
#
#get the defaults from an ini file, default if the file doesn't exist
#
startdelay = 0 # Value (in seconds) to delay audio start. If you have a splash screen with audio and the script is playing music$
musicdir = "/home/pi/RetroPie/roms/music"
maxvolume = 1.0
volumefadespeed = 0.02
restart = False # If true, this will cause the script to fade the music out and -stop- the song rather than pause it.
startsong = "" # if this is not blank, this is the EXACT, CaSeSeNsAtIvE filename of the song you always want to play first on boot
try:
from ConfigParser import SafeConfigParser
parser = SafeConfigParser()
parser.read("/home/pi/.livewire.ini")
if parser.has_section("Settings"):
if parser.has_option("Settings", "startdelay"):
startdelay = int(parser.get("Settings", "startdelay"))
if parser.has_section("Music Locations"):
if parser.has_option("Settings", "random_music_location"):
randomlocation = int(parser.get("Settings", "random_music_location"))
if randomlocation == 0:
if parser.has_option("Music Locations", "location" + str(parser.get("Settings", "default_music_location"))):
musicdir = parser.get("Music Locations", "location" + str(parser.get("Settings", "default_music_location")))
else:
random.seed()
random_music_location = random.randint(1,randomlocation)
random_music_location_folder = "location" + str(random_music_location)
if parser.has_option("Music Locations", random_music_location_folder):
musicdir = parser.get("Music Locations", random_music_location_folder)
if parser.has_option("Settings", "maxvolume"):
maxvolume = float(parser.get("Settings", "maxvolume"))
if parser.has_option("Settings", "volumefadespeed"):
volumefadespeed = float(parser.get("Settings", "volumefadespeed"))
if parser.has_option("Settings", "restart"):
restart = eval(parser.get("Settings", "restart"))
if parser.has_option("Settings", "startsong"):
startsong = parser.get("Settings", "startsong")
except Exception:
pass
#local variables
bgm = [mp3 for mp3 in os.listdir(musicdir) if mp3[-4:] == ".mp3" or mp3[-4:] == ".ogg"] # Find everything that's .mp3 or .ogg
played = [0] * len(bgm) # keep an array to track what has been played
lastsong = -1
currentsong = -1
playedsongs = 0 # track the number of uniquely played songs
from pygame import mixer # import PyGame's music mixer
mixer.init() # Prep that bad boy up.
random.seed()
volume = maxvolume # Store this for later use to handle fading out.
#TODO: Fill in all of the current RetroPie Emulator process names in this list.This includes all new ports you add in the future.
emulatornames = ["retroarch","ags","uae4all2","uae4arm","capricerpi","linapple","hatari","stella","atari800","xroar","vice","daphne.sh","reicast","pifba","osmose","gpsp","jzintv","basiliskll","mame","advmame","dgen","openmsx","mupen64plus","gngeo","dosbox","PPSSPPSDL","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.sh","solarus_run","smw","ppsspp","lr-ppsspp","drastic","psp","amiberry","fm7","redream","oricutron","cdogs-sdl","cgenius","descent2","descent1","digger","doom","duke3d","giana_rpi","coolcv_pi","cyclone","fruitbox","as1600","dasm1600","minivmac","np2","pcsx","fba2x","px68k","quasi88.sdl","rpix86","sdltrs","ti99sim-sdl","xm7","zesarux","loader","d1x-rebirth","d2x-rebirth","zsdx","zsxd","zelda_roth_se","beebem","beebem0.13","CGeniusExe","vice.sh","PicoDrive1.81","PicoDrive1.92","mednafen","yabause","darkplaces-sdl","prince","Xorg","wolf4sdl-3dr-v14","wolf4sdl-gt-v14","wolf4sdl-spear","wolf4sdl-sw-v14","xvic","xvic cart","xplus4","xpet","x128","x64sc","x64","OpenBOR","bgdi-330","redream.elf","splitwolf-wolf3","OpenJazz","sorr","bgdi","lzdoom","doom1mods","doom2mods","doomumods","hexen2","hcl","openjk.arm","openjkded.arm","openjk_sp.arm","xash3d","iowolfsp.armv71","rtcw","iowolfded.armv7l","iowolfmp.armv7l","iowolfsp.armv7l","bstone","hurrican","dosbox-sdl2","openblox","openfodder","gzdoom"]
#With omxplayer added below. Comment above and uncomment below...
#emulatornames = ["retroarch","ags","uae4all2","uae4arm","capricerpi","linapple","hatari","stella","atari800","xroar","vice","daphne.sh","reicast","pifba","osmose","gpsp","jzintv","basiliskll","mame","advmame","dgen","openmsx","mupen64plus","gngeo","dosbox","PPSSPPSDL","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.sh","solarus_run","smw","ppsspp","lr-ppsspp","drastic","psp","amiberry","fm7","redream","oricutron","cdogs-sdl","cgenius","descent2","descent1","digger","doom","duke3d","giana_rpi","coolcv_pi","cyclone","fruitbox","as1600","dasm1600","minivmac","np2","pcsx","fba2x","px68k","quasi88.sdl","rpix86","sdltrs","ti99sim-sdl","xm7","zesarux","loader","d1x-rebirth","d2x-rebirth","zsdx","zsxd","zelda_roth_se","beebem","beebem0.13","CGeniusExe","vice.sh","PicoDrive1.81","PicoDrive1.92","mednafen","yabause","darkplaces-sdl","prince","Xorg","wolf4sdl-3dr-v14","wolf4sdl-gt-v14","wolf4sdl-spear","wolf4sdl-sw-v14","xvic","xvic cart","xplus4","xpet","x128","x64sc","x64","OpenBOR","omxplayer.bin"]
#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!)
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": # Emulation Station's actual process name is apparently short 1 letter.
esStarted=True
except IOError:
continue
#ES Should be going, see if we need to delay our start
if startdelay > 0:
time.sleep(startdelay) # Delay audio start per config option above
#Look for OMXplayer - if it's running, someone's got a splash screen going!
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] == "omxplayer" or procname[:-1] == "omxplayer.bin": # Looking for a splash screen!
while os.path.exists('/proc/'+pid):
time.sleep(1) #OMXPlayer is running, sleep 1 to prevent the need for a splash.
except IOError:
continue
#Check for a starting song
if not startsong == "":
try:
currentsong = bgm.index(startsong) #Set the currentsong to the index in BGM that is our startingsong.
except:
currentsong = -1 #If this triggers, you probably screwed up the filename, because our startsong wasn't found in the list.
#This is where the magic happens.
while True:
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.
if mixer.music.get_busy():
mixer.music.stop(); #halt the music, emulationStation is not running!
time.sleep(10)
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": # Emulation Station's actual process name is apparently short 1 letter.
esStarted=True # Will cause us to break out of the loop because ES is now running.
except IOError:
continue
#Check to see if the DisableMusic file exists; if it does, stop doing everything!
if os.path.exists('/home/pi/.DisableMusic'):
if mixer.music.get_busy():
mixer.music.stop();
while (os.path.exists('/home/pi/.DisableMusic')):
time.sleep(5)
#
# Need a better randomizer... play every song randomly until we've played them all, then reset and do it again
#
if not mixer.music.get_busy(): # We aren't currently playing any music
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.
currentsong = random.randint(0, len(bgm) - 1)
while played[currentsong] == 1:
currentsong = random.randint(0, len(bgm) - 1)
playedsongs = playedsongs + 1
played[currentsong] = 1
try:
song = os.path.join(musicdir, bgm[currentsong])
if playedsongs == len(bgm): # reset to replay all songs randomly
# print("resetting. need to make sure last song of the playlist doesn't get played first")
firstsong = currentsong
playedsongs = 0
played = [0] * len(bgm)
# write the current song name to a file
log_file = open('/home/pi/.playing_song.log', "w")
n = log_file.write('Playing song: ' + song + '\n')
log_file.close()
# make 'pi' user the owner of the file
os.chown('/home/pi/.playing_song.log', pwd.getpwnam("pi").pw_uid, grp.getgrnam("pi").gr_gid)
# load and play song
mixer.music.load(song)
lastsong=currentsong
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()
except:
continue
#Emulator check
pids = [pid for pid in os.listdir('/proc') if pid.isdigit()]
emulator = -1;
esStarted=False #New check 4-23-16 - set this to False (assume ES is no longer running until proven otherwise)
for pid in pids:
try:
procname = open(os.path.join('/proc',pid,'comm'),'rb').read()
if procname[:-1] == "emulationstatio": # Killing 2 birds with one stone, while we look for emulators, make sure EmulationStation is still running.
esStarted=True # And turn it back to True, because it wasn't done running. This will prevent the loop above from stopping the music.
if procname[:-1] in emulatornames: #If the process name is in our list of known emulators
emulator = pid;
#Turn down 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() #we aren't going to resume the audio, so stop it outright.
else:
mixer.music.pause() #we are going to resume, so pause it.
#print("Muted. Monitoring emulator.")
while os.path.exists("/proc/" + pid):
time.sleep(1); # Delay 1 second and check again.
#Turn up the music
#print "Emulator finished, resuming audio..."
if not restart:
mixer.music.unpause() #resume
while volume < maxvolume:
volume = volume + volumefadespeed;
if volume > maxvolume:
volume=maxvolume
mixer.music.set_volume(volume);
time.sleep(0.05)
#print "Restored."
volume=maxvolume # ensures that the volume is manually set (if restart is True, volume would be at zero)
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." #theoretically you should never get this far.