diff --git a/exercises/speech_synth/README.md b/exercises/speech_synth/README.md new file mode 100644 index 0000000..57752d2 --- /dev/null +++ b/exercises/speech_synth/README.md @@ -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. \ No newline at end of file diff --git a/exercises/speech_synth/sample.txt b/exercises/speech_synth/sample.txt new file mode 100644 index 0000000..9320324 --- /dev/null +++ b/exercises/speech_synth/sample.txt @@ -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 \ No newline at end of file diff --git a/exercises/speech_synth/speech.py b/exercises/speech_synth/speech.py new file mode 100644 index 0000000..286b67f --- /dev/null +++ b/exercises/speech_synth/speech.py @@ -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() \ No newline at end of file