Skip to content

Instantly share code, notes, and snippets.

@BleuLlama
Created July 28, 2017 03:08
Show Gist options
  • Save BleuLlama/72bc22d6872f6a4d7a1325dc4134fe96 to your computer and use it in GitHub Desktop.
Save BleuLlama/72bc22d6872f6a4d7a1325dc4134fe96 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
#
# an attempt to make real life more like playing SimCity
#
# It picks a random amount of time from 15-90 seconds
# if there's silence for the entire thing, it will pick a random
# track in the SC2000 directory and play it.
# then it repeats... until you ctrl-c out of it
#
# v1 2017-07-27 [email protected]
#
# Requires: - timidity to be installed (brew install timidity)
# - OS X (10.12 tested)
import sys
import os
import time
import random
import subprocess
sys.dont_write_bytecode = True
mididir = "SC2000/"
def audioPlaying():
process = os.popen('/usr/bin/pmset -g' );
text = process.read()
process.close()
if( "coreaudio" not in text ):
return False
return True
def playRandomMIDI():
files = os.listdir( mididir )
fname = mididir + random.choice( files )
print( "playing {}".format( fname ))
cmd = "/usr/local/bin/timidity --no-loop " + fname
print( cmd )
process = subprocess.Popen( "/usr/local/bin/timidity --no-loop " + fname, shell=True )
process.wait()
def pickTimeout():
return random.randint(15, 90)
def main():
print( "scanning..." );
silenceTimer = 0
timeout = 99999
while True:
if( audioPlaying() ):
audioWasPlaying = True
print( "Audio is playing... Waiting." )
time.sleep( 1 )
else:
if( silenceTimer == 0 ):
timeout = pickTimeout()
silenceTimer = silenceTimer+1
if( silenceTimer > 0 ):
print( "Shhh! {} of {} seconds has passed".format( silenceTimer, timeout ))
silenceTimer = silenceTimer +1
if( silenceTimer >= timeout ):
playRandomMIDI()
timeout = pickTimeout()
silenceTimer = 0
else:
time.sleep( 1 )
################################################################################
# put this in your main app as well.
if __name__ == "__main__":
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment