×
Namespaces

Variants
Actions

Archived:A simple text-to-speech app in PySymbian

From Nokia Developer Wiki
Jump to: navigation, search

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

Article Metadata
Tested with
Devices(s): N95 8GB
Compatibility
Platform(s): S60 3rd Edition
S60 3rd Edition (initial release)
Article
Keywords: text-to-speech, TTS
Created: Rafael T. (15 Jan 2009)
Last edited: hamishwillee (28 Jun 2012)


Introduction

Some people like to listen to text instead of reading. Archived:S60 phones have an interesting feature that reads and speak the text to you, called text-to-speech. It's a very easy feature to use, considering you can you just use two lines to read a text. But you can extend this feature in many ways. Below is a code snippet using text to speech in PySymbian

Code

# iBib by Rafael Tavares
 
import e32
import appuifw
import os
import sys
import graphics
import audio
 
img = None
canvas = None
timer = e32.Ao_timer()
pause = False
m = 0
my_file = None
read_file = None
to_read = None
reading_list = []
path = 'E:\\iBib'
 
def handle_redraw(rect):
if img:
canvas.blit(img)
 
def cb_quit():
write_settings()
app_lock.signal()
appuifw.app.set_exit()
 
def about():
appuifw.note(u"(c) Rafael Tavares")
 
def main_return():
appuifw.app.menu = options[0]
appuifw.app.exit_key_handler = cb_quit
appuifw.app.body = canvas
handle_redraw(())
 
def start_line():
global m
q = appuifw.query(u"Start from line:", "float")
m = q-1
TTS()
 
def file_to_read():
global read_file
global m
global to_read
cr = read_file
p = 'E:/folder/file.txt'
read_file = appuifw.query(u"File path:", "text", unicode(p))
if read_file:
if appuifw.query(u"Add to reading list?", "query"):
reading_list.append(str(read_file))
read_file = read_file.replace("/", "\\")
if read_file != my_file:
m = 0
if read_file[-4] == '.':
try:
f = open(read_file, "r")
f.close()
to_read = read_file
open_text()
except:
appuifw.note(u"Error", "error")
 
else:
read_file = read_file+'.txt'
try:
f = open(read_file, "r")
f.close()
to_read = read_file
open_text()
except:
appuifw.note(u"Error", "error")
else:
appuifw.note(u"Please insert a path", "error")
 
def write_settings():
if not os.path.exists(path):
os.makedirs(path)
set_file = path+'\\settings.txt'
f = open(set_file, "w")
for x in reading_list:
f.write(str(x)+'\n')
f.write(repr(m)+'\n')
f.write(repr(my_file))
f.close()
 
def read_settings():
global m
global reading_list
global my_file
set_file = path+'\\settings.txt'
if not os.path.exists(set_file):
write_settings()
else:
f = open(set_file, "r")
g = f.readlines()
re = len(g)
f.close()
m = int(eval(g[-2]))
my_file = eval(g[-1])
for i in g[:-2]:
r = i
s = r.strip()
reading_list.append(s)
 
def set_pause():
global pause
pause = True
appuifw.app.menu = options[1]
 
def open_reading_list():
global to_read
global reading_list
rl = map(unicode, reading_list)
index = appuifw.selection_list(rl)
if index != None:
a = reading_list[index]
b = a.replace("/", "\\")
to_read = b
open_text()
 
def open_text():
global my_file
global m
try:
if to_read != my_file:
m = 0
f = open(to_read, "r")
r = f.read()
f.close()
t = appuifw.Text()
appuifw.app.body = t
t.set_pos(0)
t.set(unicode(r))
appuifw.app.exit_key_handler = main_return
appuifw.app.menu = options[1]
except:
appuifw.note(u"Error", "error")
op = appuifw.popup_menu([u"Try TTS", u"Back"])
if op == 0:
TTS()
elif op == 1:
pass
 
def TTS():
global pause
global m
global my_file
 
f = open(to_read, "r")
r = f.readlines()
lenght = len(r)
f.close()
 
my_file = to_read
 
appuifw.app.menu = options[2]
read = []
f = open(to_read, "r")
for i in range(lenght):
n = f.readline()
nl = n.strip()
if i >= m:
h = r[-1].strip()
if n == h:
m = 0
audio.say(nl)
if pause:
m = i
pause = False
write_settings()
break
f.close()
appuifw.app.menu = options[1]
 
options = [[(u"Open reading list", open_reading_list),
(u"New file", file_to_read),
(u"About", about),
(u"Exit", cb_quit)],
[(u"Start/Continue", TTS),
(u"Start from line", start_line),
(u"Back", main_return)],
[(u"Stop", set_pause)]]
 
appuifw.app.title = u'iBib'
appuifw.app.exit_key_handler = cb_quit
appuifw.app.menu = options[0]
 
canvas = appuifw.Canvas(redraw_callback=handle_redraw)
appuifw.app.body = canvas
img = graphics.Image.new(canvas.size)
 
x, y = canvas.size
 
img.text((x/4, y/2), u"Welcome to iBib", (0,0,0), 'title')
handle_redraw(())
 
read_settings()
 
app_lock = e32.Ao_lock()
app_lock.wait()

See also

BH Text-to-Speech by Christian Shtarkov

This page was last modified on 28 June 2012, at 15:16.
85 page views in the last 30 days.