Added song info class, checks if command line input is in library before playing

git-svn-id: file:///home/anna/Desktop/ocarina-legacy/mithos/ocarina@5 1daee41c-8060-4895-b1f0-2197c00d777a
This commit is contained in:
bjschuma 2009-05-31 21:31:09 +00:00
parent 8f322fc343
commit 068de4a7ee
5 changed files with 97 additions and 29 deletions

View File

@ -1,3 +1,4 @@
from songInfo import SongInfo
# This class is used to store all library data
class LibData:

View File

@ -2,7 +2,8 @@ import os
import re
import cPickle as pickle
from libdata import LibData
#from song import Song
from songInfo import SongInfo
class Library():
def __init__(self,prnt):
@ -25,6 +26,9 @@ class Library():
def scan(self,dir):
self.data = LibData()
self.data.path = os.path.expanduser(dir)
if os.path.exists(self.data.path) == False:
self.prnt(["Directory not found: "+dir])
return
self.prnt(["Scanning: "+self.data.path])
self.traverse("")
num = len(self.data.files)
@ -48,16 +52,23 @@ class Library():
tSplit = entry.rsplit('.')
type = tSplit[len(tSplit)-1].lower()
if (type in self.goodTypes) == True:
self.hash(joined)
self.add(self.hash(joined),joined)
# Hash a file and add to library
# Hash a file and return list of words
def hash(self,file):
index = len(self.data.files)
self.data.files+=[file]
file = file.lower()
# Only keep letters and numbers
words = re.sub('[^a-z0-9]',' ',file).split()
return words
# Add song to library
def add(self,words,file):
index = len(self.data.files)
info = SongInfo()
info.filename = os.path.join(self.data.path,file)
self.data.files+=[info]
for word in words:
if (word in self.data.map.keys()) == True:
self.data.map[word]+=[index]
@ -71,3 +82,18 @@ class Library():
p = pickle.Pickler(out,2)
p.dump(self.data)
out.close()
# Return true if file is in the library
def has(self,file):
words = self.hash(file)
if len(words) == 0:
return -1
if (words[0] in self.data.map.keys()) == False:
return -1
indices = set(self.data.map[words[0]])
for word in words[1:]:
indices = indices & set(self.data.map[word])
if len(indices) == 1:
return indices.pop()
return -1

View File

@ -1,4 +1,5 @@
import gobject
import os
import sys
import thread
@ -6,21 +7,38 @@ from song import Song
from cline import CLine
from duration import Duration
from library import Library
from songInfo import SongInfo
#import cmnds
class main:
def __init__(self,argv):
if len(argv) == 0:
print "python ocarina.py /path/to/song/"
return
#if len(argv) == 0:
# print "python ocarina.py /path/to/song/"
# return
self.commands = CLine()
self.registerCmnds()
self.song = Song(argv[0],self.quit,self.commands.printLines)
self.song.play()
self.library = Library(self.commands.printLines)
self.song = None
# If we were given a song as input, check that it exists and begin playback
if len(argv) > 0:
file = os.path.expanduser(argv[0])
if os.path.exists(file) == True:
split = file.split(self.library.data.path)
file = split[len(split)-1]
index = self.library.has(file)
info = None
if index != -1:
info = self.library.data.files[index]
else:
info = SongInfo()
info.filename = file
#se
self.song = Song(info,self.quit,self.commands.printLines)
self.song.play()
# Start main loop as a thread so we can get bus calls and use command line
gobject.threads_init()
mainloop = gobject.MainLoop()
@ -47,37 +65,49 @@ class main:
# Begin playback
def play(self,unused):
if self.song == None:
return
self.song.play()
# Pause music
def pause(self,unused):
if self.song == None:
return
self.song.pause()
# Show running time info
def time(self,unused):
if self.song == None:
return
cur = self.song.curTime()
tot = self.song.length
self.commands.printLine(cur.toStr()+" / "+tot.toStr())
# Show detailed song info
def info(self,unused):
# Return if no song found
if self.song == None:
return
# Return if no tags found
if self.song.taglist == None:
if self.song.info.tags == None:
self.commands.printLine("Could not find any tags")
return
for tag in self.song.taglist.keys():
self.commands.printLine(tag+": "+str(self.song.taglist[tag]))
for tag in self.song.info.tags.keys():
self.commands.printLine(tag+": "+str(self.song.info.tags[tag]))
# Show basic song info
def this(self,unused):
# Return if no song found
if self.song == None:
return
# Return if no tags found
if self.song.taglist == None:
if self.song.info.tags == None:
self.commands.printLine("Could not find any tags")
return
fields = ["title","artist","track-number","track-count","album"]
for field in fields:
if (field in self.song.taglist.keys()) == True:
self.commands.printLine(field+": "+str(self.song.taglist[field]))
if (field in self.song.info.tags.keys()) == True:
self.commands.printLine(field+": "+str(self.song.info.tags[field]))
def scanLib(self,dir):

View File

@ -7,13 +7,14 @@ import gst
from duration import Duration
class Song():
def __init__(self,file,exitFunc,prnt):
def __init__(self,info,exitFunc,prnt):
self.quit=exitFunc
self.prnt=prnt
self.info = info
# initialize player pipeline
self.player = gst.Pipeline("player")
bin = gst.element_factory_make("playbin",None)
bin.set_property("uri","file://"+file)
bin.set_property("uri","file://"+self.info.filename)
self.player.add(bin)
# initialize bus
@ -26,8 +27,8 @@ class Song():
# Initialize stuff for finding duration
self.time_format = gst.Format(gst.FORMAT_TIME)
self.length = None
self.taglist = None
#self.length = None
#self.taglist = None
# Called on bus messages
@ -43,10 +44,11 @@ class Song():
if self.prnt != None:
self.prnt(["Error: "+ str(err) + " " +str(debug)])
#print "Error: %s" % err, debug
if self.quit != None:
self.quit("")
#if self.quit != None:
# self.quit("")
elif t == gst.MESSAGE_TAG:
self.taglist = message.parse_tag()
self.info.tags = message.parse_tag()
#self.taglist = message.parse_tag()
return
@ -54,9 +56,9 @@ class Song():
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.5)
self.duration()
#if self.info.length == None:
# time.sleep(0.5)
# self.duration()
# Change state to "paused"
@ -66,9 +68,9 @@ class Song():
# Find the duration of the pipeline
def duration(self):
self.length = Duration()
self.info.length = Duration()
length = self.player.query_duration(self.time_format,None)[0]
self.length.setTime(length)
self.info.length.setTime(length)
#self.length.disp(self.prnt)

9
trunk/songInfo.py Normal file
View File

@ -0,0 +1,9 @@
from duration import Duration
class SongInfo:
def __init__(self):
self.filename = ""
self.tags = None
self.playCount = 0
self.banned = False
self.length = None