commit e03e553a517d32720800ecbdc1def6ea57db7d41 Author: bjschuma Date: Wed May 27 03:07:46 2009 +0000 Initial import with python git-svn-id: file:///home/anna/Desktop/ocarina-legacy/mithos/ocarina@1 1daee41c-8060-4895-b1f0-2197c00d777a diff --git a/trunk/cline.py b/trunk/cline.py new file mode 100644 index 00000000..82d63b84 --- /dev/null +++ b/trunk/cline.py @@ -0,0 +1,68 @@ +import threading +import curses + +class CLine(threading.Thread): + # Initialize ncurses stuff + def __init__(self): + threading.Thread.__init__(self) + self.stdscr = curses.initscr() + self.maxSize = self.stdscr.getmaxyx() + curses.cbreak() + curses.noecho() + self.stdscr.keypad(1) + self.input = "" + self.pos = (0,0) # REMEMBER: (y,x) + self.start() + + + # Reverse ncurses stuff + def quit(self): + curses.nocbreak() + curses.echo() + self.stdscr.keypad(0) + curses.endwin() + + + # Print the prompt + def prompt(self): + self.stdscr.addstr(self.pos[0],0,">>> ") + + + # Run the command line + def run(self): + try: + self.prompt() + while(True): + #self.prompt() + self.pos=self.stdscr.getyx() + c = self.stdscr.getch() + if c==10: + if self.enter()==False: + return + else: + self.input+=curses.keyname(c) + self.changeline() + finally: + self.quit() + + + # Enter key pressed + # Returns true if we keep going + def enter(self): + if self.input=="": + return True + elif self.input=="quit": + self.quit() + return False + else: + self.input = "" + + + # Change the current line to reflect self.input + def changeline(self): + (y,x) = self.stdscr.getyx() + self.stdscr.deleteln() + self.stdscr.move(y,0) + self.prompt() + self.stdscr.addstr(self.input) + #self.stdscr.move(y,x+10) diff --git a/trunk/duration.py b/trunk/duration.py new file mode 100644 index 00000000..e1619d77 --- /dev/null +++ b/trunk/duration.py @@ -0,0 +1,30 @@ + +class Duration: + def __init__(self): + self.set = False + self.hour=0 + self.min=0 + self.sec=0 + + # Set the duration + # Takes nano-seconds + def setTime(self,ns): + # Convert to seconds + time = ns/1000000000 + # Hours + if time >= 3600: + self.hour = time/3600 + time = time-(self.hour*3600) + + # Minutes + if time >= 60: + self.min = time/60 + time = time-(self.min*60) + + # Seconds + self.sec = time + + + # Write time to screen + def disp(self): + print self.hour,self.min,self.sec diff --git a/trunk/ocarina.py b/trunk/ocarina.py new file mode 100644 index 00000000..ea081024 --- /dev/null +++ b/trunk/ocarina.py @@ -0,0 +1,19 @@ +import gobject +import sys +import thread + +from song import Song +from cline import CLine + +def main(argv): + commands = CLine() + + s = Song(argv[0]) + s.play() + + # Start main loop as a thread + mainloop = gobject.MainLoop() + thread.start_new_thread(mainloop.run,()) + + +if __name__=='__main__':main(sys.argv[1:]) diff --git a/trunk/song.py b/trunk/song.py new file mode 100644 index 00000000..39a8b60d --- /dev/null +++ b/trunk/song.py @@ -0,0 +1,77 @@ +import pygst,sys,time,gobject +pygst.require("0.10") +import gst +import gst + +from duration import Duration + +class Song(): + def __init__(self,file): + # initialize player pipeline + self.player = gst.Pipeline("player") + bin = gst.element_factory_make("playbin",None) + bin.set_property("uri","file://"+file) + self.player.add(bin) + + # initialize bus + bus = self.player.get_bus() + bus.add_signal_watch() + bus.connect("message",self.onMessage) + + # Pause song + self.pause() + + # Initialize stuff for finding duration + self.time_format = gst.Format(gst.FORMAT_TIME) + self.length = None + + # Start mainloop + gobject.threads_init() + + + # Called on bus messages + def onMessage(self,bus,message): + t = message.type + if t == gst.MESSAGE_EOS: + print "End of stream" + sys.exit(0) + elif t == gst.MESSAGE_ERROR: + print "Error" + sys.exit(0) + elif t == gst.MESSAGE_TAG: + print "Tags:" + # Probably should convert this to some other data structure + # For easier use later + taglist = message.parse_tag() + for key in taglist.keys(): + print key,taglist[key] + return + + + # Change state to "playing" + def play(self): + self.player.set_state(gst.STATE_PLAYING) + # Start main loop and find duration + # If this hasn't been done yet + if self.length == None: + time.sleep(0.2) + self.duration() + + + # Change state to "paused" + def pause(self): + self.player.set_state(gst.STATE_PAUSED) + + + # Find the duration of the pipeline + def duration(self): + self.length = Duration() + length = self.player.query_duration(self.time_format,None)[0] + self.length.setTime(length) + self.length.disp() + + + # Print out current running time + def curTime(self): + length = self.player.query_position(self.time_format,None)[0] + return Duration().setTime(length)