From 25a0d0be9fa86c1882006c45360d63b2bdd89895 Mon Sep 17 00:00:00 2001 From: Bryan Schumaker Date: Sat, 7 Aug 2010 19:39:46 -0400 Subject: [PATCH] Added audio functions I have reworked the gstreamer functions from Ocarina 4.0 and combined everything into one file. I have also created an audio test that plays a song from my library. --- libsaria/__init__.py | 7 ++- libsaria/fns.py | 7 --- libsaria/music/__init__.py | 49 ++++++++++++++++++ libsaria/music/audio.py | 102 +++++++++++++++++++++++++++++++++++++ libsaria/path.py | 15 ++---- saria-test.py | 4 +- tests/audio.py | 24 +++++++++ 7 files changed, 187 insertions(+), 21 deletions(-) delete mode 100644 libsaria/fns.py create mode 100644 libsaria/music/__init__.py create mode 100644 libsaria/music/audio.py create mode 100644 tests/audio.py diff --git a/libsaria/__init__.py b/libsaria/__init__.py index 564f3fc6..6e55a58e 100644 --- a/libsaria/__init__.py +++ b/libsaria/__init__.py @@ -1,17 +1,20 @@ # Bryan Schumaker (8/7/2010) -__all__ = ["data", "event", "fns", "map", "path"] +__all__ = [ "music", + "data", "event", "map", "path"] import event +import path from map import Map # Variables are not saved across sessions, but preferences are vars = None prefs = None + def startup(): global vars global prefs vars = Map() prefs = Map() - event.start("POSTSTART") + event.start("POSTINIT") diff --git a/libsaria/fns.py b/libsaria/fns.py deleted file mode 100644 index a4b6e2ef..00000000 --- a/libsaria/fns.py +++ /dev/null @@ -1,7 +0,0 @@ -# Bryan Schumaker (8/7/2010) - -import libsaria -from libsaria.event import call - -def startup(): - call("START", libsaria.startup) diff --git a/libsaria/music/__init__.py b/libsaria/music/__init__.py new file mode 100644 index 00000000..053b0d17 --- /dev/null +++ b/libsaria/music/__init__.py @@ -0,0 +1,49 @@ +# Bryan Schumake (8/7/2010) + +__all__ = ['audio'] + +import libsaria +call = libsaria.event.call +expand = libsaria.path.expand +exists = libsaria.path.exists +audio = None + + +def init(): + global audio + import audio + audio.init() + + +def end_of_song(): + global audio + return call("EOS", audio.reset) + + +def load(file): + global audio + if audio == None: + init() + + global expand + file = expand(file) + if not exists(file): + return False + return call("LOAD", audio.load, file) + + +def play(): + global audio + return call("PLAY", audio.play) + + +def pause(): + global audio + return call("PAUSE", audio.pause) + + +def stop(): + global audio + return call("STOP", audio.stop) + + diff --git a/libsaria/music/audio.py b/libsaria/music/audio.py new file mode 100644 index 00000000..6f9a7a08 --- /dev/null +++ b/libsaria/music/audio.py @@ -0,0 +1,102 @@ +# Bryan Schumaker (8/7/2010) + +import libsaria + +gst = None +player = None +time = None +bus = None +length = None + +# An init function for faster startup +def init(): + global gst + global player + global time + import gst + + player = gst.element_factory_make("playbin2", "player") + time = gst.Format(gst.FORMAT_TIME) + bus = player.get_bus() + bus.add_signal_watch() + bus.connect("message", on_message) + + +def reset(): + global player + global length + player.set_state(gst.STATE_NULL) + length = None + + +def on_message(bus, message): + if message.type == gst.MESSAGE_EOS: + libsaria.music.end_of_song() + + +def on_quit(): + global player + player.set_state(gst.STATE_NULL) +libsaria.event.invite("PREQUIT", on_quit) + + +def get_state(): + global player + return player.get_state()[1] + + +# Returns the length of the song in ms +def duration(): + global length + global player + global time + if get_state() == gst.STATE_NULL: + return 0 + if length == None: + length = player.query_duration(time)[0] + return length + + +def position(): + global time + global player + return player.query_position(time)[0] + + +def load(file): + global gst + global player + player.set_state(gst.STATE_NULL) + player.set_property("uri", "file://%s"%file) + player.set_state(gst.STATE_PAUSED) + return file + + +def play(): + global gst + global player + player.set_state(gst.STATE_PLAYING) + + +def pause(): + global gst + global player + player.set_state(gst.STATE_PAUSED) + + +def stop(): + pause() + seek(0) + + +def seek(prcnt): + global player + global time + global gst + spot = duration() * prcnt + player.seek_simple(time, gst.SEEK_FLAG_FLUSH, spot) + + +def volume(prcnt): + global player + player.set_property("volume", prcnt) diff --git a/libsaria/path.py b/libsaria/path.py index 9ac01828..f4f38cd5 100644 --- a/libsaria/path.py +++ b/libsaria/path.py @@ -8,17 +8,12 @@ join = os.path.join mkdir = os.mkdir rm = os.remove -dir +dir = None def sariadir(): global dir - dir = join(expand("~"), ".saria") - if exists(dir) == False: - mkdir(dir) - sariadir = get_sariadir() - return get_sariadir() - - -def get_sariadir(): - global dir + if dir == None: + dir = join(expand("~"), ".saria") + if exists(dir) == False: + mkdir(dir) return dir diff --git a/saria-test.py b/saria-test.py index ad92d8c9..110db4ae 100644 --- a/saria-test.py +++ b/saria-test.py @@ -30,8 +30,8 @@ for file in sys.argv[1:]: print print "=====", file, "=====" print - begin = now() + before = now() __import__("tests."+file.rsplit(".")[0]) - end = now() + after = now() print print "=====", file, "(", after-before,") =====" diff --git a/tests/audio.py b/tests/audio.py new file mode 100644 index 00000000..7cd52db1 --- /dev/null +++ b/tests/audio.py @@ -0,0 +1,24 @@ +# Bryan Schumaker (8/7/2010) + +from libsaria import music + +path = "~/Music/Eagles/Hotel California.mp3" + +music.load(path) + +raw_input("Press ENTER to play ... ") +music.play() + +raw_input("Press ENTER to pause ... ") +music.pause() + +raw_input("Press ENTER to play ... ") +music.play() + +raw_input("Press ENTER to stop ... ") +music.stop() + +raw_input("Press ENTER to play ... ") +music.play() + +raw_input("Press ENTER to quit ... ")