From d628cb2dfc008971977aec66e32f1f434a2a6770 Mon Sep 17 00:00:00 2001 From: Leif Schelin Date: Thu, 21 Aug 2014 22:14:53 +0200 Subject: [PATCH 1/4] New exercise test, perhaps for next year. --- exercises/speech_synth/README.md | 0 exercises/speech_synth/speech.py | 14 ++++++++++++++ 2 files changed, 14 insertions(+) create mode 100644 exercises/speech_synth/README.md create mode 100644 exercises/speech_synth/speech.py diff --git a/exercises/speech_synth/README.md b/exercises/speech_synth/README.md new file mode 100644 index 0000000..e69de29 diff --git a/exercises/speech_synth/speech.py b/exercises/speech_synth/speech.py new file mode 100644 index 0000000..ee74a45 --- /dev/null +++ b/exercises/speech_synth/speech.py @@ -0,0 +1,14 @@ + +# original: +# http://code.activestate.com/recipes/578839-python-text-to-speech-with-pyttsx/ + +import pyttsx +engine = pyttsx.init() +engine.setProperty('rate', 70) + +voices = engine.getProperty('voices') +for voice in voices: + print "Using voice:", repr(voice) + engine.setProperty('voice', voice.id) + engine.say("Hi there.") +engine.runAndWait() \ No newline at end of file From 8bb20fc48e677b283aad067bc231f7665d0c8358 Mon Sep 17 00:00:00 2001 From: Leif Schelin Date: Thu, 21 Aug 2014 22:48:39 +0200 Subject: [PATCH 2/4] Works well on MacOSX --- exercises/speech_synth/README.md | 23 ++++++++++++++++ exercises/speech_synth/speech.py | 45 ++++++++++++++++++++++++++------ 2 files changed, 60 insertions(+), 8 deletions(-) diff --git a/exercises/speech_synth/README.md b/exercises/speech_synth/README.md index e69de29..54ffbf8 100644 --- a/exercises/speech_synth/README.md +++ b/exercises/speech_synth/README.md @@ -0,0 +1,23 @@ +# Talsyntes + +Låt datorn läsa 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()`. +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/speech.py b/exercises/speech_synth/speech.py index ee74a45..44c98a5 100644 --- a/exercises/speech_synth/speech.py +++ b/exercises/speech_synth/speech.py @@ -3,12 +3,41 @@ # http://code.activestate.com/recipes/578839-python-text-to-speech-with-pyttsx/ import pyttsx + engine = pyttsx.init() -engine.setProperty('rate', 70) - -voices = engine.getProperty('voices') -for voice in voices: - print "Using voice:", repr(voice) - engine.setProperty('voice', voice.id) - engine.say("Hi there.") -engine.runAndWait() \ No newline at end of file + + +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) + engine.setProperty('voice', engine.getProperty('voices')[18].id) + +def say_something(line): + engine.say(line) + engine.runAndWait() + + + +init() +#listen_to_voices() + +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: ") From f2f912301473d426ce6eaea3a1286334746daa84 Mon Sep 17 00:00:00 2001 From: Leif Schelin Date: Thu, 21 Aug 2014 23:03:24 +0200 Subject: [PATCH 3/4] Implemented read file --- exercises/speech_synth/README.md | 4 ++-- exercises/speech_synth/sample.txt | 5 +++++ exercises/speech_synth/speech.py | 28 +++++++++++++++++++--------- 3 files changed, 26 insertions(+), 11 deletions(-) create mode 100644 exercises/speech_synth/sample.txt diff --git a/exercises/speech_synth/README.md b/exercises/speech_synth/README.md index 54ffbf8..57752d2 100644 --- a/exercises/speech_synth/README.md +++ b/exercises/speech_synth/README.md @@ -1,13 +1,13 @@ # Talsyntes -Låt datorn läsa upp text. +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()`. +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 diff --git a/exercises/speech_synth/sample.txt b/exercises/speech_synth/sample.txt new file mode 100644 index 0000000..02ecbe6 --- /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 bzzzzzt \ No newline at end of file diff --git a/exercises/speech_synth/speech.py b/exercises/speech_synth/speech.py index 44c98a5..dca7191 100644 --- a/exercises/speech_synth/speech.py +++ b/exercises/speech_synth/speech.py @@ -3,6 +3,7 @@ # http://code.activestate.com/recipes/578839-python-text-to-speech-with-pyttsx/ import pyttsx +import sys engine = pyttsx.init() @@ -22,22 +23,31 @@ def init(): """Set voice properties.""" engine.setProperty('rate', 190) engine.setProperty('volume', 1.0) - engine.setProperty('voice', engine.getProperty('voices')[18].id) + #On MacOSX 10.9 there are 24 different ones to choose from, but only one in Win7 + #engine.setProperty('voice', engine.getProperty('voices')[18].id) def say_something(line): engine.say(line) engine.runAndWait() +def read_file(file): + for line in open(file): #.read(): + say_something(line) +def loop(): + print "Exit by typing `exit` or simply an empty line ``" + say_something("At your command...") -init() -#listen_to_voices() + line = raw_input("Say something: ") + while line != "" and line != "exit": + say_something(line) + line = raw_input("Say something: ") -print "Exit by typing `exit` or simply an empty line ``" -say_something("At your command...") +init() +#listen_to_voices() -line = raw_input("Say something: ") -while line != "" and line != "exit": - say_something(line) - line = raw_input("Say something: ") +if len(sys.argv) > 1: + read_file(sys.argv[1]) +else: + loop() \ No newline at end of file From adf39e5f5e4f94c56c2426a2b44b961ec96a2e3e Mon Sep 17 00:00:00 2001 From: Leif Schelin Date: Thu, 21 Aug 2014 23:13:58 +0200 Subject: [PATCH 4/4] Seems to work good now :) --- exercises/speech_synth/sample.txt | 2 +- exercises/speech_synth/speech.py | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/exercises/speech_synth/sample.txt b/exercises/speech_synth/sample.txt index 02ecbe6..9320324 100644 --- a/exercises/speech_synth/sample.txt +++ b/exercises/speech_synth/sample.txt @@ -2,4 +2,4 @@ Hello and, again, welcome to the Aperture Science computer-aided enrichment cent 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 bzzzzzt \ No newline at end of file +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 index dca7191..286b67f 100644 --- a/exercises/speech_synth/speech.py +++ b/exercises/speech_synth/speech.py @@ -3,7 +3,7 @@ # http://code.activestate.com/recipes/578839-python-text-to-speech-with-pyttsx/ import pyttsx -import sys +import sys, time engine = pyttsx.init() @@ -24,7 +24,7 @@ def init(): 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')[18].id) + #engine.setProperty('voice', engine.getProperty('voices')[11].id) def say_something(line): engine.say(line) @@ -33,6 +33,7 @@ def say_something(line): 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 ``" @@ -44,6 +45,8 @@ def loop(): line = raw_input("Say something: ") +## Main + init() #listen_to_voices()