From 09bf218f51a3586a34ec6a3a25b23ec1d00f18da Mon Sep 17 00:00:00 2001 From: Assim Date: Sun, 27 Jul 2008 21:34:19 +0000 Subject: phoneme support + bug fixes + GUI modified --- diff --git a/conv.sh b/conv.sh index 8f8a9f4..55f1874 100644..100755 --- a/conv.sh +++ b/conv.sh @@ -10,7 +10,7 @@ rm $1.db fi - mysqldump -h 10.250.100.69 -u assim -p --compact --compatible=ansi --default-character-set=binary $1 | + mysqldump -h 192.168.145.1 -u AI -p --compact --compatible=ansi --default-character-set=binary $1 | grep -v ' KEY "' | grep -v ' UNIQUE KEY "' | grep -v ' PRIMARY KEY ' | diff --git a/dict.db b/dict.db index 7be9ab5..913ad33 100644 --- a/dict.db +++ b/dict.db Binary files differ diff --git a/dict.py b/dict.py index aa953c9..d202439 100644..100755 --- a/dict.py +++ b/dict.py @@ -148,6 +148,31 @@ class Word: self.cur.execute("INSERT into las_score (wordid, num_played) VALUES (?,?) ", (wordid,1, )) self.conn.commit() + + + + def get_phoneme(self): + self.cur.execute("SELECT * from las_phoneme where wordid = ?", (self.wordid,)) + t = self.cur.fetchone() + if t != None: + (wordid, phoneme, num_syllabe, is_correct) = t + return (phoneme, is_correct) + else:return None + + def update_phoneme(self, phoneme, is_correct = True): + #print phoneme + try: + if is_correct == True: + self.cur.execute("UPDATE las_phoneme SET phoneme = ?, is_correct = ? where wordid = ?", (phoneme, 1, self.wordid,)) + else: + self.cur.execute("UPDATE las_phoneme SET phoneme = ?, is_correct = ? where wordid = ?", (phoneme, 0, self.wordid,)) + except: + if is_correct == True: + self.cur.execute("INSERT into las_phoneme (wordid, phoneme, is_correct ) VALUES (?,?,?) ", (self.wordid, phoneme, 1, )) + else: + self.cur.execute("INSERT into las_phoneme (wordid, phoneme, is_correct ) VALUES (?,?,?) ", (self.wordid, phoneme, 0, )) + self.conn.commit() + if __name__ == "__main__": k = Dict() num_words = k.get_num_words() diff --git a/las-gui.py b/las-gui.py index 796dc2e..4d833fa 100755 --- a/las-gui.py +++ b/las-gui.py @@ -1,6 +1,5 @@ #!/usr/bin/env python -# example helloworld.py import pygtk pygtk.require('2.0') @@ -12,10 +11,11 @@ class LS_gui: # This is a callback function. The data arguments are ignored # in this example. More on callbacks below. def __init__(self, handle): - self.__init__ + self.__init__() def __init__(self): DBname = "dict.db" + self.use_phoneme = False self.las = Listenspell(DBname) self.load_activity_interface() self.play_game("start") @@ -24,7 +24,7 @@ class LS_gui: def destroy(self, widget, data=None): #print "destroy signal occurred" - gtk.main_quit() + self.game_exit() def load_activity_interface(self): self.main_window = gtk.Window(gtk.WINDOW_TOPLEVEL) @@ -59,15 +59,15 @@ class LS_gui: #self.label_v_left_a.show() #self.label_v_left_b.show() - self.main_output_view = gtk.TextView() - self.main_output_buffer = gtk.TextBuffer() + #self.main_output_view = gtk.TextView() + #self.main_output_buffer = gtk.TextBuffer() #self.main_output_buffer.set_text("This is main output") - self.main_output_view.set_editable(False) - self.main_output_view.set_buffer(self.main_output_buffer) + #self.main_output_view.set_editable(False) + #self.main_output_view.set_buffer(self.main_output_buffer) - self.display_main_output("This is main output") + #self.display_main_output("This is main output") - self.main_output_view.show_all() + #self.main_output_view.show_all() self.text_input = gtk.Entry() #self.text_input.set_text("Preset input text") @@ -95,7 +95,7 @@ class LS_gui: self.text_submit_button.connect("clicked", self.submit_button_clicked, None) self.console_text_buffer = gtk.TextBuffer() - self.console_text_buffer.set_text("This is hint text") + #self.console_text_buffer.set_text("This is hint text") self.console_text_view.set_editable(False) self.console_text_view.set_buffer(self.console_text_buffer) @@ -103,12 +103,12 @@ class LS_gui: self.console_text_frame.add(self.console_text_sw) self.console_text_frame.show_all() - self.vcontainer_left.pack_start(self.main_output_view, True, True ) + #self.vcontainer_left.pack_start(self.main_output_view, True, True ) #self.vcontainer_left.pack_start(self.label_v_left_b, True, False ) + + self.vcontainer_left.pack_start(self.console_text_frame, True, True ) self.vcontainer_left.pack_start(self.text_input, False, True ) self.vcontainer_left.pack_start(self.text_submit_button, False, False ) - self.vcontainer_left.pack_start(self.console_text_frame, True, True ) - ################################################True################ @@ -209,7 +209,10 @@ class LS_gui: return False def repeat_word_button_clicked(self, widget, data = None): - self.las.say_text(self.elem) + if self.elem != self.pronounelem and self.use_phoneme: + self.las.say_text(self.pronounelem, is_phoneme = True) + else: + self.las.say_text(self.elem) def get_def_button_clicked(self, widget, data = None): def_list = self.las.get_word_info(self.wordid, "def") @@ -268,7 +271,7 @@ class LS_gui: elif response == gtk.RESPONSE_NONE: self.game_exit() - def local_skill_level_focus(self, widget, event, data= None): + def local_skill_level_focus(self, widget, event, data= None): # not in use #print "text_input_focus_in" return False @@ -282,6 +285,7 @@ class LS_gui: self.las.say_text(char) self.las.say_text(string) + ################################################################ #####################Update methods############################# @@ -323,20 +327,19 @@ class LS_gui: self.pronounelem = self.las.get_word_info(self.wordid, "phnm") #get a pronounciation from the list self.display_console("Spell...") # say the explanation if the word is ambiguous. - if self.elem != self.pronounelem: # determine whether to bother pronouncing a description - - self.las.say_text("Spell... ... " + self.elem + "... " + self.pronounelem) + if self.elem != self.pronounelem and self.use_phoneme: # determine whether to bother pronouncing a description + self.las.say_text("Spell..." + self.pronounelem, is_phoneme = True) else: #print "Spell... " - self.las.say_text("Spell... ... " + self.elem) + self.las.say_text("Spell..." + self.elem) def play_game(self,mode = "start"): if mode == "start": self.las.play_sound("begin") self.this_level_size = 7 self.this_level_max_error = 3 - self.display_main_output("Welcome") - self.las.say_text("Welcome") + #self.display_main_output("Welcome") + self.las.say_text("Welcome", wait = False) self.ask_skill_level() self.wordid_list = self.las.load_wordid(self.this_level_size + self.this_level_max_error) self.this_level_num_words = len(self.wordid_list) @@ -367,19 +370,20 @@ class LS_gui: data = "\n" + data self.console_text_buffer.insert_at_cursor(data) - def display_main_output(self, data, newline = True, clear_previous = True): - if clear_previous == True: - self.main_output_buffer.set_text(data) - elif newline == True: - text = "\n" + data - self.main_output_buffer.insert_at_cursor(data) - else: - self.main_output_buffer.insert_at_cursor(data) + #def display_main_output(self, data, newline = True, clear_previous = True): + #if clear_previous == True: + #self.main_output_buffer.set_text(data) + #elif newline == True: + #text = "\n" + data + #self.main_output_buffer.insert_at_cursor(data) + #else: + #self.main_output_buffer.insert_at_cursor(data) ################################################################ def game_exit(self): + self.las.exit_game() gtk.main_quit() ########################Game Logic############################## diff --git a/las.py b/las.py index 9f31b57..96fd52e 100644..100755 --- a/las.py +++ b/las.py @@ -4,7 +4,9 @@ import os import dbus import random import time -import csnd +import speechd +import commands +#import csnd from dict import Dict from dict import Word @@ -51,26 +53,39 @@ class Listenspell(): self.points = 0 self.words_played = 0 self.words_correct = 0 - self.init = False + self.speechd_init = False self.dict_obj = Dict(SQLiteDB) #Always intitiate first Dict object then Word object self.word_obj = Word() - def start_speechd(self): # Currently not in use have to manually start sugar-speechd - pid = os.fork() - if pid: - return - else: - # we are the child - os.popen("./sugar-speechd") - def play_sound(self,event): - if event == "begin": - os.popen("aplay --quiet begin.wav") - elif event == "correct": - os.popen("aplay --quiet correct.wav") - elif event == "incorrect": - os.popen("aplay --quiet incorrect.wav") + + #file= event + '.wav' + #from wave import open as waveOpen + #from ossaudiodev import open as ossOpen + #s = waveOpen(file,'rb') + #(nc,sw,fr,nf,comptype, compname) = s.getparams( ) + #dsp = ossOpen('/dev/dsp','w') + #try: + #from ossaudiodev import AFMT_S16_NE + #except ImportError: + #if byteorder == "little": + #AFMT_S16_NE = ossaudiodev.AFMT_S16_LE + #else: + #AFMT_S16_NE = ossaudiodev.AFMT_S16_BE + #dsp.setparameters(AFMT_S16_NE, nc, fr) + #data = s.readframes(nf) + #s.close() + #dsp.write(data) + #dsp.close() + + os.popen("aplay --quiet " + event + ".wav") + #if event == "begin": + #os.popen("aplay --quiet begin.wav") + #elif event == "correct": + #os.popen("aplay --quiet correct.wav") + #elif event == "incorrect": + #os.popen("aplay --quiet incorrect.wav") def set_skill_level(self,level): self.skill_level = level @@ -132,22 +147,56 @@ class Listenspell(): elif attribute == "word": return self.word_obj.get_word() elif attribute == "phnm": - return self.word_obj.get_word() # For the time being - - else: return "Invalid attribute" - - def say_text(self, text): - if self.init == False: - try: - bus = dbus.SessionBus() - self.espeak_object = bus.get_object('org.laptop.Speech','/org/laptop/Speech') - self.init = True - except dbus.exceptions.DBusException: - print "Speech Server not turned on." + phnm = self.word_obj.get_phoneme() + if phnm == None: + phnm = self.get_phoneme(self.word_obj.get_word()) + return phnm + else: + (phoneme, is_correct) = phnm + return phoneme + else: return False + + def get_phoneme(self, word = None): + if word == None: + return False + phnm = commands.getoutput("/usr/bin/espeak -q -x " + word) + self.word_obj.update_phoneme(phnm) + return phnm + + def start_speechd(self): + try: + self.client = speechd.SSIPClient('spd-test') + self.client.set_output_module('espeak') + self.client.set_language('en') + self.client.set_punctuation(speechd.PunctuationMode.SOME) + self.speech_state = None + except dbus.exceptions.DBusException: + print "Speech Dispatcher is not turned on." + return False + + def callback(self,callback_type): + self.speech_state = callback_type + + def say_text(self, text, wait= True, is_phoneme = False): + #wait: to wait for the text to be spoken or not + #os.popen("espeak " + text) + if self.speechd_init == False: + if self.start_speechd() == False: return False - + self.speechd_init = True + text = str(text) - self.espeak_object.SayText(text) + if is_phoneme: + text = "[" + text + "]" + + self.client.speak(text, callback=self.callback,event_types=(speechd.CallbackType.BEGIN, + speechd.CallbackType.CANCEL, + speechd.CallbackType.END)) + if wait == True: + while(self.speech_state != "end"): + time.sleep(1) + + def get_key(self): for longestinput in range(15): @@ -160,4 +209,5 @@ class Listenspell(): def exit_game(self): self.say_text("goodbye") + self.client.close() sys.exit() diff --git a/ls.py b/ls.py index ce0521c..ce0521c 100644..100755 --- a/ls.py +++ b/ls.py -- cgit v0.9.1