Posts Tagged 'Lullaby'

Lullaby

A couple people will know that over the summer I tried my hand at programming. When I was about 10 or 11, my Dad tried to teach me a little BASIC, but I never really took to it. However, seeing how much David enthuses/rambles about Python, I thought I’d try my hand at the basics. I’m never going to get that far with it, as it really is only something I have a go at occasionally, and I don’t really have anything I need it for; it’s just for playing around with.

I therefore introduce Lullaby. At the start of the summer, I came up with an idea for something that would essentially function like a nap-timer; play quiet music for n minutes, then loud music for another period of time. It was essentially a teaching tool; David guided me through the python tutorial with the aim of being able to write the bits for this. I last worked on this at least a month ago, and I’m pretty sure I debugged it (a month seems like a hell of a long time ago). I was planning to play around with the first input to make it a little more user-friendly, but that seems unlikely to happen now. For those people who already know python, this will look breathtakingly simple (or breathtakingly wrong); for those who don’t do programming, it’ll look like gobbledegook. However, hopefully going public with my programming will prompt me into picking it up again.

WordPress is playing merry hell with my formatting; I promise my original is properly done :p I’m playing around with methods of getting it to work. If you have any tips, leave a comment

#imports
import os
import sys
import time

#inputs and calls
def input_seconds():
	"""asks user for inputs and sorts into minutes or seconds. Returns countdown timer in seconds"""
	input = raw_input("Set length of timer")
	#(append h for hours, m for minutes or s for seconds) eg '1h 5m 10s'
	
	input2 = raw_input("Is this minutes or seconds?")
	if 'm' in input2:
		timer = int(input) * 60	
	else:
		timer = int(input)
	return timer
	
def input_playlist():
	"""allows user to choose playlist"""	
	playlist = raw_input("Which playlist do you want?")
	return playlist
	
def play_amarok(timer, playlist):
	"""starts amarok playing the defined playlist"""
	if 'pause' in playlist:
		command = 'dcop amarok player stop'
	else:
		command = 'dcop amarok playlistbrowser loadPlaylist'
		command = command + " '" + playlist +"'"
	os.system(command)
	print "loading", playlist
	sleep(5)
	print "playing"
	next()
	sleep(timer)
	pause()

def next():
	"""plays next song in amarok"""
	os.system('dcop amarok player next')

def play():
	"""starts amarok playing"""
	os.system('dcop amarok player play')

def stop():
	"""stops amarok playing"""
	os.system('dcop amarok player stop')
	
def pause():
	"""pauses amarok
	WARNING: this may not actually work. use stop() instead."""
	os.system('dcop amarok player playPause')

def pause_amarok(timer):
	"""Pauses Amarok for length of time specified in timer"""
	pause()
	sleep(timer)
	play()

def sleep(timer):
	"""runs timer for defined time and counts down"""
	for countdown in range(timer, 0, -1): 
		print countdown,
		sys.stdout.flush()
		time.sleep(1)
	print

def main():
	"""ask for playlist and time, ask whether user wants to include another lot of playlist and time, ask for these inputs and store in a list. Keep asking for inputs until user says they do not want to add more. Once all inputs have been given, play playlists for given times sequentially."""
	
	subsequent_playlist = 'yes'
	pairs = []
	while 'yes' in subsequent_playlist:
		playlist= input_playlist()
		timer = input_seconds()
		pair = (playlist, timer)
		pairs.append(pair)
		subsequent_playlist = raw_input("Select another playlist? Yes/no")
	for pair in pairs:
		(playlist, timer) = pair
		play_amarok(timer, playlist)

main()