[Tutor] Text Editor With Speech

FT chester_lab at fltg.net
Wed Jul 9 20:42:16 CEST 2008


Hi!

    I am coming closer to the talking editor and voice adjustments inside my
editor/talking module.

    When reading the events over and over again I am slowly understanding
who wants what. Inside an editor there are key commands that do certain
things and I try to give some voice to them.

    Below I do not use the OnKey function yet, but will erase the comments
and such and only have a say key inside with a toggle on/off for key typing.
Before this was the only event call with keys, now it is only for say key
pressed in the future.

    At the moment I use the KEY_UP event to look at what was done by the
editor after the key is released. This allows no need for location
calculations since the move has been made and the final position in most
cases has been done.

    So, I moved the values for all line parms over to the OnKey_Up function
so I know where the pointer is at.

    I do a word forward and back inside of this function so the word forward
and back are pronounced.

    You may replace the speech engine with the pyTTS if you so desire. But I
have not added the method for pitch to there module, so the pitch adjustment
would not work.

    I mention this because I have not done a dictionary for the engine, so
some things get pronounced a little different. Like AM for the time does not
come out as A.M. nor am, but likes to take the m out for something else.
Little things like that.

        Anyway, this is the latest using events and have removed the button
list. I left the textctrl control methods in the 2 function calls for future
usage and reference.

    Enjoy.

        Bruce

#Editor.py
import wx
import os
import Sapi5
tts = Sapi5.Create( {"name":"Mary"})
purge = tts._purge
async = tts._async
punc = tts._punc
ID=0
HK=1
KH=2
MK=3
MF=4
MF2=5
DW_ID=1000
class MainWindow(wx.Frame):
    def __init__(self, parent, id, title):
        self.dirname=os.getcwd()  #SEARCH FROM PRESENT DIRECTORY!
        self.filename=""
        self.items4menu = {"File": [
            {ID:102, HK:"&Open", KH:" Open a file to edit", MF:self.OnOpen},
            {ID:103, HK:"&Save", KH:" save file to disk", MF:self.OnSave},
            {ID:104, HK:"&Edit", KH:" Do editing", MF:self.OnEdit},
            {ID:101, HK:"&About", KH:" Information about this program",
MF:self.OnAbout},
            {ID:109, HK:"E&xit", KH:" Terminate the program",
MF:self.OnExit}
        ],  #END OF FILE MENU!
        "Voice": [
            {ID:202, HK:"&Read", KH:" Open a file to read",
MF:self.OnWav2Read},
            {ID:203, HK:"&Save", KH:" save text to audio file",
MF:self.OnSave2Wav},
            {ID:204, HK:"Te&xt", KH:" read text field", MF:self.OnRead},
            {ID:205, HK:"&Quit", KH:" Stop Reading", MF:self.OnQuitRead}
        ],  #END OF VOICE MENU!
        "Settings": [
            {ID:302, HK:"&Speaker", KH:" Name for voice.", MF:self.OnEnter,
MF2:self.OnVoice},
            {ID:303, HK:"&Rate", KH:" Rate for voice.", MF:self.OnEnter,
MF2:self.OnVoice},
            {ID:304, HK:"&Pitch", KH:" Pitch for voice.", MF:self.OnEnter,
MF2:self.OnVoice},
            {ID:305, HK:"&Volume", KH:" Volume for voice.", MF:self.OnEnter,
MF2:self.OnVoice}
        ],  #END OF SETTINGS MENU!
        "Down": [
            {ID:DW_ID, HK:"&Down", KH:" Lower Setting.", MF:self.OnEnter,
MF2:self.OnVoice}
        ]  #END OF DOWN MENU!
        }  #END OF ITEMS FOR MENU!
        wx.Frame.__init__(self, parent, wx.ID_ANY, title)
        self.control = wx.TextCtrl(self, 1, style=wx.TE_MULTILINE)
        self.control.Bind( wx.EVT_KEY_UP, self.OnKey_Up) #, self.control)
#        self.control.Bind( wx.EVT_CHAR, self.OnKey) #, self.control)
        self.CreateStatusBar()  #A Statusbar in the bottom of the window
        #Setting up the menu.
        filemenu = wx.Menu()
        for o in self.items4menu["File"]:
            filemenu.Append( o[ID], o[HK], o[KH])
            filemenu.AppendSeparator()
        voicemenu = wx.Menu()
        for o in self.items4menu["Voice"]:
            voicemenu.Append( o[ID], o[HK], o[KH])
            voicemenu.AppendSeparator()
        self.setting_menu = wx.Menu()
        for o in self.items4menu["Settings"]:
            down_menu = wx.Menu()
            down_menu.Append( o[ID], o[HK], o[KH])
            d = self.items4menu["Down"][0]
            down_menu.Append( d[ID]+o[ID], d[HK], d[KH])
            self.setting_menu.AppendMenu( o[ID], o[HK], down_menu)
            self.setting_menu.AppendSeparator()
        voicemenu.AppendMenu(-1, "&VoiceSettings", self.setting_menu)
        # Creating the menubar.
        menuBar = wx.MenuBar()
        menuBar.Append( filemenu,"&File") # Adding the "filemenu" to the
MenuBar
        menuBar.Append( voicemenu,"&Voice") # Adding the "voicemenu" to the
MenuBar
        self.SetMenuBar( menuBar)  # Adding the MenuBar to the Frame
content.
        self.data4menu = {}
        for o in self.items4menu["File"]:
            wx.EVT_MENU(self, o[ID], o[MF])
            self.data4menu[ o[ID]] = o
        for o in self.items4menu["Voice"]:
            wx.EVT_MENU(self, o[ID], o[MF])
            self.data4menu[ o[ID]] = o
        for o in self.items4menu["Settings"]:
            wx.EVT_MENU(self, o[ID], o[MF2])
            self.data4menu[ o[ID]] = o
            wx.EVT_MENU(self, o[ID]+DW_ID, o[MF2])
            self.data4menu[ o[ID]+DW_ID] = o
        # Use some sizers to see layout options
        self.sizer=wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.control,1,wx.EXPAND)
        #Layout sizers
        self.SetSizer(self.sizer)
        self.SetAutoLayout(1)
        self.sizer.Fit(self)
        self.Show(1)
#SAY LABEL OF MENU ITEM!
    def OnEnter(self, event):
        "WHEN ENTERING SAY LABEL OF BUTTON!"
#        self.button2bind = self.Bind(wx.EVT_BUTTON, self.OnVoice,
id=event.GetId())
        eventType = event.GetEventType()
#        tts.Speak( event.GetPosition())
        label4btn = self.setting_menu.GetLabelText( event.GetId())
        tts.Speak( label4btn, async, punc)
        set_value = " Error In Label!"
        if label4btn == "Down":
            set_value = str(-1)
        if label4btn == "Speaker":
            set_value = tts.getVoiceName()
        elif label4btn == "Rate":
            set_value = str( tts.getRate())
        elif label4btn == "Pitch":
            set_value = str( tts.getPitch())
        elif label4btn == "Volume":
            set_value = str( tts.getVolume()) + "%"
        text = label4btn +" Button " +set_value
        tts.Speak( text, async)
#TEXT CONTROL KEYS!
    def OnKey(self, event):
        "KEY CAPTURE FOR EDITING! MUST USE EVT_CHAR FOR ALL CODES."
        k = event.GetKeyCode()
        m = event.GetModifiers()
        txt = self.control.GetValue()
        event.Skip()
#comment        rk = event.GetRawKeyCode()   #not in all platforms
#comment        uk = event.GetUnicodeKey()  #Not on platforms that unicode
was not installed!#comment
#comment        tts.Speak("%d %d" %  (k,m))
#comment        x2y4m = event.GetPosition()  #Mouse position in event
window, textctrl!
        c = self.control.GetInsertionPoint() #present line column position
when event fired!
        tl = self.control.GetLastPosition()  #last point in textctrl
        colmax, linemax = self.control.PositionToXY( tl)
        col, line = self.control.PositionToXY( c)
        lx = self.control.GetLineLength( line) #length of line specified!
        if k==wx.WXK_LEFT:
            col-=1
            if col < 0:
                col = 0
                line-=1
                if line < 0: line=0
                else: col = self.control.GetLineLength( line); lx=col
        if k==wx.WXK_RIGHT:
            col+=1
            if col > lx:
                col = 0
                line+=1
                if line > linemax: line=linemax; col=colmax
        if k==wx.WXK_UP:
            line-=1
            if line<0: line=0
            lx = self.control.GetLineLength( line) #length of line
specified!
            if col>lx: col=lx
        if k==wx.WXK_DOWN:
            line+=1
            if line>linemax: line=linemax
            lx = self.control.GetLineLength( line) #length of line
specified!
            if col>lx: col=lx
        c = self.control.XYToPosition( col, line)
        cs = self.control.XYToPosition( 0, line)
        ce = self.control.XYToPosition( lx, line)
        if c<0: c=0
        if c>tl: c=tl
        lxy = self.control.PositionToXY( c)
#comment        tts.Speak( "Col %d Line %d" % lxy, async, purge)
#comment        col, line = lxy
        lx = self.control.GetLineLength( line) #length of line specified!
#comment        tts.Speak( " line %d col %d length %d" % (line, col, lx),
async, purge)
#comment        tts.Speak( self.control.HitTest( lxy))  #mouse char pixels
using coordinate values
        ch = self.control.GetRange( c,c+1)
        if ch and ord(ch)==10: ch="Return"
        elif ch and ord(ch)==32: ch="space"
        if k in [wx.WXK_UP, wx.WXK_DOWN]: ch = self.control.GetRange( cs,
ce)
        tts.Speak( " %s " % ch, async, purge)
#comment        if k==wx.WXK_LEFT and ord(ch)==10: c-=1
#comment        if k==wx.WXK_RIGHT and ord(ch)==10: c+=1
#comment        tts.Speak(" %s %d %d" % (ch, c, ord(ch)), async)
#KEY UP OR KEY RELEASED!
    def OnKey_Up(self, event):
        "SAY CHAR OR LINE ON WHEN KEY RELEASED!"
        event.Skip()
        k = event.GetKeyCode()
        m = event.GetModifiers()
        txt = self.control.GetValue()
        c = self.control.GetInsertionPoint() #present line column position
when event fired!
        tl = self.control.GetLastPosition()  #last point in textctrl
        colmax, linemax = self.control.PositionToXY( tl)
        col, line = self.control.PositionToXY( c)
        lx = self.control.GetLineLength( line) #length of line specified!
#comment        c = self.control.XYToPosition( col, line)  #CURSOR POSITION!
        cs = self.control.XYToPosition( 0, line)  #LINE START!
        ce = self.control.XYToPosition( lx, line)  #LINE END!
        ch = self.control.GetRange( c,c+1)
        if ch:
            if ord(ch)==10: ch="Return"
            elif ord(ch)==wx.WXK_SPACE: ch="space"
        elif c==tl and k in [wx.WXK_RIGHT, wx.WXK_END]: ch="end of text"
        if c<tl and event.ControlDown() and k in [wx.WXK_LEFT,
wx.WXK_RIGHT]:
            for i in range(c+1, c+lx-col):
                ch2 = self.control.GetRange( i,i+1)
                if ch2=="" or ch2 in  "!@ #$%^&*()[]{}_.,;:": break
                else: ch+=ch2
        if k in [wx.WXK_UP, wx.WXK_DOWN, wx.WXK_PAGEUP, wx.WXK_PAGEDOWN]: ch
= self.control.GetRange( cs,ce)
        if k in [wx.WXK_INSERT, wx.WXK_DELETE, wx.WXK_HOME, wx.WXK_END,
wx.WXK_LEFT, wx.WXK_RIGHT, wx.WXK_UP, wx.WXK_DOWN, wx.WXK_PAGEUP,
wx.WXK_PAGEDOWN]:
            tts.Speak( " %s " % ch, async, punc, purge)
# VOICE SETTINGS!
    def OnVoice(self, event):
        "BUTTON CLICKED ON AND IN FUTURE GET BUTTON POS!"
#comment        tts.Speak( 'Event Name: %s Time Stamp: %s ' %
(event.GetClassName(), event.GetTimestamp()))
#comment        print 'Event Name: %s Time Stamp: %s ' %
(event.GetClassName(), event.GetTimestamp())
#comment IF KEY EVENT:
#comment        key = event.GetKeyCode()
#comment        pos = event.GetPositionTuple()
        eventType = event.GetEventType()
#comment        if eventType == wx.EVT_BUTTON: print "Mouse! "
        eventName = event.GetClassName()
        eventId = event.GetId()
#comment        tts.Speak( event.GetEventType())
#comment        self.control.SetValue( eventName)
#comment: CHECKING ID AND SETTING DIRECTION!
#comment        tts.Speak( self.data4menu[ event.GetId()][HK])
        label4btn = self.setting_menu.GetLabelText( eventId)
        dir = 1
        if label4btn == "Down":
            dir = -1
            label4btn = self.setting_menu.GetLabelText( eventId-DW_ID)
        set_value = "Error! Wrong Button!"
        if label4btn == "Speaker":
            value = tts.getVoiceNum() + dir
            if value < 1:               value += 0
            if value >= tts.getVoiceCount():
                value = tts.getVoiceCount()-1
            tts.setVoice( value)
            set_value = tts.getVoiceName()
        elif label4btn == "Rate":
            value = tts.getRate()
            value += dir
            if value > 10: value = 10
            if value < -10: value = -10
            tts.setRate( value)
            set_value = str( value)
        elif label4btn == "Pitch":
            value = tts.getPitch()
            value += dir
            if value > 10: value = 10
            if value < -10: value = -10
            tts.setPitch( value)
            set_value = str( value)
        elif label4btn == "Volume":
            value = tts.getVolume()
            value += 10*dir
            if value > 100: value = 100
            if value < 0:
                tts.setVolume( 50)
                tts.Speak( "Volume Minimum!")
                value = 0
            tts.setVolume( value)
            set_value = str( value)+"%"
        tts.Speak( set_value, async)
    def OnAbout(self,e):
        "A dialog box saying what the editor is about!"
        text = " A sample editor with voice \n in wxPython."
        tts.Speak( "About A Sample Editor!"+text)
        d= wx.MessageDialog( self, text, "About Sample Editor", wx.OK)
                            # Create a message dialog box
        d.ShowModal() # Shows it
        d.Destroy() # finally destroy it when finished.
    def OnExit(self,e):
        self.Close(True)  # Close the frame.
    def OnOpen(self,e):
        """ Open a file"""
        self.setFilePath( "o")
        f=open(os.path.join(self.dirname, self.filename),'r')
        self.control.SetValue(f.read())
        f.close()
    def OnSave(self,e):
        """ Save a file"""
        self.setFilePath( "s")
        f=open(os.path.join(self.dirname, self.filename),'w')
#       self.control.SetValue(f.read())
        f.write( self.control.GetValue())
        f.close()
    def setFilePath(self, type4file=""):
        " Search directory for path and file name!"
        fn=self.filename
        t4f = wx.OPEN
        if type4file[0] in "sS":
            t4f = wx.SAVE|wx.FD_OVERWRITE_PROMPT
#            fn = self.filename
        dlg = wx.FileDialog(self, self.filename +" or Choose a file",
self.dirname, fn, "*.*", t4f)
        if dlg.ShowModal() == wx.ID_OK:
            self.filename = dlg.GetFilename()
            self.dirname = dlg.GetDirectory()
        dlg.Destroy()
    def OnWav2Read(self,e):
        """ Open a file to read"""
        self.setFilePath( "o")
        tts.SpeakFromWav( os.path.join(self.dirname, self.filename), async,
purge)
    def OnSave2Wav(self,e):
        """ Save text to a wav file"""
        self.setFilePath( "s")
        tts.SpeakToWav( os.path.join(self.dirname, self.filename),
self.control.GetValue())
    def OnRead(self,e):
        """ Read the text"""
        tts.Speak( self.control.GetValue(), async, purge)
    def OnQuitRead(self,e):
        """ Quit the reading of the text"""
        tts.Speak( " Talking Stopped!", purge)
    def OnEdit(self,e):
        """ Edit the file"""
        self.control.SetFocus()
app = wx.PySimpleApp()
frame = MainWindow(None, -1, "Sample editor")
app.MainLoop()
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: Editor.py
URL: <http://mail.python.org/pipermail/tutor/attachments/20080709/2d011c6e/attachment-0001.txt>


More information about the Tutor mailing list