Created
February 21, 2016 12:33
-
-
Save theSage21/25a7746ba7c88544b381 to your computer and use it in GitHub Desktop.
Simple book reader for reading text files using espeak
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
import time | |
import configparser | |
from threading import Thread | |
ALIVE = True # SOFTWARE IS RUNNING | |
SPEAKING = True # IS It speaking | |
book = sys.argv[1] | |
try: | |
CURRENT = int(sys.argv[2]) # CURRENT LOCATION or Start from | |
except IndexError: | |
CURRENT = 0 | |
# Only simple stuff | |
valid = 'abcdefghijklmnopqrstuvwxyz' | |
valid += valid.upper() | |
valid += '0123456789., ' | |
def getch(): | |
import termios | |
import sys, tty | |
def _getch(): | |
fd = sys.stdin.fileno() | |
old_settings = termios.tcgetattr(fd) | |
try: | |
tty.setraw(fd) | |
ch = sys.stdin.read(1) | |
finally: | |
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) | |
return ch | |
return _getch | |
def listen(): | |
global CURRENT, ALIVE, SPEAKING | |
get = getch() | |
while ALIVE: | |
char = get() | |
if char == ' ': | |
SPEAKING = not SPEAKING | |
print(">" if SPEAKING else "||") | |
if char == 's': | |
print(CURRENT) | |
if char == 'q': | |
print(CURRENT) | |
ALIVE = False | |
print('Stopped taking input. Waiting for current sentence to complete.') | |
def read(): | |
global CURRENT, ALIVE, SPEAKING, valid | |
with open(book, 'r') as fl: | |
for count, raw_line in enumerate(fl.readlines()): | |
if not ALIVE: | |
break | |
line = ''.join((i for i in raw_line.strip() if i in valid)) | |
if line.strip() == '': | |
continue | |
if count < CURRENT: | |
continue | |
CURRENT = count | |
if SPEAKING: | |
command = 'echo {}|espeak --stdin'.format(line) | |
os.system(command) | |
else: | |
while not SPEAKING: | |
time.sleep(0.2) | |
print('Closed book') | |
t = Thread(target=listen) | |
t.start() | |
read() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment