Skip to content
This repository was archived by the owner on Jun 29, 2021. It is now read-only.

Speech synth #17

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions exercises/speech_synth/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Talsyntes

Skapa ett program som läser upp text.

- **Svårighetsgrad:** 1

## Delmoment

1. Låt programmet läsa upp en lämplig sträng på engelska med hjälp av `engine.say()`.
2. Lyssna igenom de olika röster som finns tillgängliga på ert system och låt programmet använda denna med hjälp av `engine.setProperty()`. **Obs!** I windows verkar det bara finnas en röst att välja på.
3. Skapa en loop som låter användaren skriva in rader som programmet då läser upp. Loopen ska avbrytas av lämpligt kommando.

## Utbyggnad

- Låt användaren kunna läsa upp en text-fil genom att ange den som argument till programmet. **Svårighetsgrad:** 1
- Låt användaren kunna ändra volym, röst och hastighet utan att behöva starta om programmet.

## Externa bibliotek

- [pyttsx](http://pyttsx.readthedocs.org/en/latest/engine.html)
[Installation](http://pyttsx.readthedocs.org/en/latest/install.html)

På MacOSX räcker det med `sudo pip install pyttsx`. På windows behöver man även installera ett extra bibliotek enligt instruktioner i länk ovan.
5 changes: 5 additions & 0 deletions exercises/speech_synth/sample.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Hello and, again, welcome to the Aperture Science computer-aided enrichment center.
We hope your brief detention in the relaxation vault has been a pleasant one.
Your specimen has been processed and we are now ready to begin the test proper.
Before we start, however, keep in mind that although fun and learning are the primary goals of all enrichment center activities, serious injuries may occur.
For your own safety and the safety of others, please refrain from
56 changes: 56 additions & 0 deletions exercises/speech_synth/speech.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@

# original:
# http://code.activestate.com/recipes/578839-python-text-to-speech-with-pyttsx/

import pyttsx
import sys, time

engine = pyttsx.init()


def listen_to_voices():
"""Listen to available voices on your system. Choose one to be used in init()."""
voices = engine.getProperty('voices')
i = 0
for voice in voices:
print "Using voice:", repr(voice)
engine.setProperty('voice', voice.id)
engine.say("Hi there.")
engine.say("I am number %s" % str(i))
i = i+1

def init():
"""Set voice properties."""
engine.setProperty('rate', 190)
engine.setProperty('volume', 1.0)
#On MacOSX 10.9 there are 24 different ones to choose from, but only one in Win7
#engine.setProperty('voice', engine.getProperty('voices')[11].id)

def say_something(line):
engine.say(line)
engine.runAndWait()

def read_file(file):
for line in open(file): #.read():
say_something(line)
time.sleep(0.2)

def loop():
print "Exit by typing `exit` or simply an empty line ``"
say_something("At your command...")

line = raw_input("Say something: ")
while line != "" and line != "exit":
say_something(line)
line = raw_input("Say something: ")


## Main

init()
#listen_to_voices()

if len(sys.argv) > 1:
read_file(sys.argv[1])
else:
loop()