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.
This commit is contained in:
Bryan Schumaker 2010-08-07 19:39:46 -04:00
parent 28d23faa5a
commit 25a0d0be9f
7 changed files with 187 additions and 21 deletions

View File

@ -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")

View File

@ -1,7 +0,0 @@
# Bryan Schumaker (8/7/2010)
import libsaria
from libsaria.event import call
def startup():
call("START", libsaria.startup)

View File

@ -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)

102
libsaria/music/audio.py Normal file
View File

@ -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)

View File

@ -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

View File

@ -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,") ====="

24
tests/audio.py Normal file
View File

@ -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 ... ")