From e6367896dde58758fcee83e1980f1dca10fdb164 Mon Sep 17 00:00:00 2001
From: "Silas S. Brown" <ssb22@cam.ac.uk>
Date: Sun, 6 Jun 2010 16:08:23 +0000
Subject: [PATCH] Gradint update

git-svn-id: http://svn.code.sf.net/p/e-guidedog/code/ssb22/gradint@813 29193198-4895-4776-b068-10539e920549
---
 gradint-build/Makefile          |  2 +-
 gradint-build/advanced.txt      |  9 ++++++++-
 gradint-build/src/booktime.py   |  2 +-
 gradint-build/src/filescan.py   |  2 +-
 gradint-build/src/frontend.py   |  2 +-
 gradint-build/src/lessonplan.py |  2 +-
 gradint-build/src/loop.py       |  2 +-
 gradint-build/src/makeevent.py  |  2 +-
 gradint-build/src/play.py       |  2 +-
 gradint-build/src/recording.py  |  2 +-
 gradint-build/src/sequence.py   |  7 ++++---
 gradint-build/src/synth.py      | 23 +++++++++++++----------
 gradint-build/src/system.py     |  2 +-
 gradint-build/src/top.py        |  2 +-
 gradint-build/src/users.py      |  2 +-
 gradint-build/thindown.py       |  2 +-
 16 files changed, 38 insertions(+), 27 deletions(-)

diff --git a/gradint-build/Makefile b/gradint-build/Makefile
index b50f434..8311d3d 100644
--- a/gradint-build/Makefile
+++ b/gradint-build/Makefile
@@ -1,5 +1,5 @@
 # This file is part of the source code of
-# gradint v0.9957 (c) 2002-2010 Silas S. Brown. GPL v3+.
+# gradint v0.9958 (c) 2002-2010 Silas S. Brown. GPL v3+.
 #    This program is free software; you can redistribute it and/or modify
 #    it under the terms of the GNU General Public License as published by
 #    the Free Software Foundation; either version 3 of the License, or
diff --git a/gradint-build/advanced.txt b/gradint-build/advanced.txt
index 6dc3197..59ecd15 100644
--- a/gradint-build/advanced.txt
+++ b/gradint-build/advanced.txt
@@ -271,7 +271,7 @@ sapiVoices = {
 # Here you can set extra SAPI 5 voices.  The default voice
 # will be used for English unless overridden here.  If you
 # have Neospeech Lily then that will be used for Chinese unless
-# overridden here.  (Although Lily is not always accurate.)
+# overridden here (although Lily is not always accurate).
 # Add them in this format but without the # at the beginning:
     # "language abbrev" : ("full voice name", rate),
 # For example, if you have "ScanSoft Mei-Ling_Full_22kHz"
@@ -292,6 +292,13 @@ sapiVoices = {
 # in synth_priorities above.
 }
 
+sapiSpeeds = {
+# Here you can change the speeds of SAPI voices.  Speed ranges from
+# -10 (slowest) to +10 (fastest).  For example, to slow down the
+# English SAPI voice and speed up the Chinese, you can have
+# "en": -5, "zh": +3   (but without the # at the beginning)
+}
+
 # Another thing you can do is to selectively override the
 # speech synthesis of certain words.  To do this, set
 # synthCache (below) to a directory, and in that directory
diff --git a/gradint-build/src/booktime.py b/gradint-build/src/booktime.py
index c206d76..b247f1b 100644
--- a/gradint-build/src/booktime.py
+++ b/gradint-build/src/booktime.py
@@ -1,5 +1,5 @@
 # This file is part of the source code of
-# gradint v0.9957 (c) 2002-2010 Silas S. Brown. GPL v3+.
+# gradint v0.9958 (c) 2002-2010 Silas S. Brown. GPL v3+.
 #    This program is free software; you can redistribute it and/or modify
 #    it under the terms of the GNU General Public License as published by
 #    the Free Software Foundation; either version 3 of the License, or
diff --git a/gradint-build/src/filescan.py b/gradint-build/src/filescan.py
index 815f52c..956e2a3 100644
--- a/gradint-build/src/filescan.py
+++ b/gradint-build/src/filescan.py
@@ -1,5 +1,5 @@
 # This file is part of the source code of
-# gradint v0.9957 (c) 2002-2010 Silas S. Brown. GPL v3+.
+# gradint v0.9958 (c) 2002-2010 Silas S. Brown. GPL v3+.
 #    This program is free software; you can redistribute it and/or modify
 #    it under the terms of the GNU General Public License as published by
 #    the Free Software Foundation; either version 3 of the License, or
diff --git a/gradint-build/src/frontend.py b/gradint-build/src/frontend.py
index 1de2f88..f939019 100644
--- a/gradint-build/src/frontend.py
+++ b/gradint-build/src/frontend.py
@@ -1,5 +1,5 @@
 # This file is part of the source code of
-# gradint v0.9957 (c) 2002-2010 Silas S. Brown. GPL v3+.
+# gradint v0.9958 (c) 2002-2010 Silas S. Brown. GPL v3+.
 #    This program is free software; you can redistribute it and/or modify
 #    it under the terms of the GNU General Public License as published by
 #    the Free Software Foundation; either version 3 of the License, or
diff --git a/gradint-build/src/lessonplan.py b/gradint-build/src/lessonplan.py
index 3edcaa3..e59caf1 100644
--- a/gradint-build/src/lessonplan.py
+++ b/gradint-build/src/lessonplan.py
@@ -1,5 +1,5 @@
 # This file is part of the source code of
-# gradint v0.9957 (c) 2002-2010 Silas S. Brown. GPL v3+.
+# gradint v0.9958 (c) 2002-2010 Silas S. Brown. GPL v3+.
 #    This program is free software; you can redistribute it and/or modify
 #    it under the terms of the GNU General Public License as published by
 #    the Free Software Foundation; either version 3 of the License, or
diff --git a/gradint-build/src/loop.py b/gradint-build/src/loop.py
index 666e740..4230a25 100644
--- a/gradint-build/src/loop.py
+++ b/gradint-build/src/loop.py
@@ -1,5 +1,5 @@
 # This file is part of the source code of
-# gradint v0.9957 (c) 2002-2010 Silas S. Brown. GPL v3+.
+# gradint v0.9958 (c) 2002-2010 Silas S. Brown. GPL v3+.
 #    This program is free software; you can redistribute it and/or modify
 #    it under the terms of the GNU General Public License as published by
 #    the Free Software Foundation; either version 3 of the License, or
diff --git a/gradint-build/src/makeevent.py b/gradint-build/src/makeevent.py
index a4f8e5c..b7491b2 100644
--- a/gradint-build/src/makeevent.py
+++ b/gradint-build/src/makeevent.py
@@ -1,5 +1,5 @@
 # This file is part of the source code of
-# gradint v0.9957 (c) 2002-2010 Silas S. Brown. GPL v3+.
+# gradint v0.9958 (c) 2002-2010 Silas S. Brown. GPL v3+.
 #    This program is free software; you can redistribute it and/or modify
 #    it under the terms of the GNU General Public License as published by
 #    the Free Software Foundation; either version 3 of the License, or
diff --git a/gradint-build/src/play.py b/gradint-build/src/play.py
index 2dd90f1..421e6c3 100644
--- a/gradint-build/src/play.py
+++ b/gradint-build/src/play.py
@@ -1,5 +1,5 @@
 # This file is part of the source code of
-# gradint v0.9957 (c) 2002-2010 Silas S. Brown. GPL v3+.
+# gradint v0.9958 (c) 2002-2010 Silas S. Brown. GPL v3+.
 #    This program is free software; you can redistribute it and/or modify
 #    it under the terms of the GNU General Public License as published by
 #    the Free Software Foundation; either version 3 of the License, or
diff --git a/gradint-build/src/recording.py b/gradint-build/src/recording.py
index dd08d66..9a3512b 100644
--- a/gradint-build/src/recording.py
+++ b/gradint-build/src/recording.py
@@ -1,5 +1,5 @@
 # This file is part of the source code of
-# gradint v0.9957 (c) 2002-2010 Silas S. Brown. GPL v3+.
+# gradint v0.9958 (c) 2002-2010 Silas S. Brown. GPL v3+.
 #    This program is free software; you can redistribute it and/or modify
 #    it under the terms of the GNU General Public License as published by
 #    the Free Software Foundation; either version 3 of the License, or
diff --git a/gradint-build/src/sequence.py b/gradint-build/src/sequence.py
index 6043bfa..5a72218 100644
--- a/gradint-build/src/sequence.py
+++ b/gradint-build/src/sequence.py
@@ -1,5 +1,5 @@
 # This file is part of the source code of
-# gradint v0.9957 (c) 2002-2010 Silas S. Brown. GPL v3+.
+# gradint v0.9958 (c) 2002-2010 Silas S. Brown. GPL v3+.
 #    This program is free software; you can redistribute it and/or modify
 #    it under the terms of the GNU General Public License as published by
 #    the Free Software Foundation; either version 3 of the License, or
@@ -14,8 +14,9 @@
 def randomInstruction(numTimesBefore,promptsData,language):
     if not numTimesBefore: return (availablePrompts.getPromptList("repeatAfterMe",promptsData,language),0)
     if numTimesBefore==1: return (availablePrompts.getPromptList("sayAgain",promptsData,language),1)
-    if language==secondLanguage and ((dbase.veryExperienced() and numTimesBefore>=reallyKnownThreshold) or (meaningTestThreshold and numTimesBefore>meaningTestThreshold and not random.choice([1,2,3])==1)):
-        return (None,1) # no instruction needed
+    if (dbase.veryExperienced() and numTimesBefore>=reallyKnownThreshold) or (meaningTestThreshold and numTimesBefore>meaningTestThreshold and not random.choice([1,2,3])==1):
+        if language==secondLanguage: return (None,1) # no instruction needed
+        else: return (availablePrompts.getPromptList(language,promptsData,language),1) # just need the language name
     r = availablePrompts.getRandomPromptList(promptsData,language)
     # horrible hack: whatSay goes after the 1st-language
     # word; others go before
diff --git a/gradint-build/src/synth.py b/gradint-build/src/synth.py
index 0e49b8c..5b6212a 100644
--- a/gradint-build/src/synth.py
+++ b/gradint-build/src/synth.py
@@ -1,5 +1,5 @@
 # This file is part of the source code of
-# gradint v0.9957 (c) 2002-2010 Silas S. Brown. GPL v3+.
+# gradint v0.9958 (c) 2002-2010 Silas S. Brown. GPL v3+.
 #    This program is free software; you can redistribute it and/or modify
 #    it under the terms of the GNU General Public License as published by
 #    the Free Software Foundation; either version 3 of the License, or
@@ -148,33 +148,36 @@ class PttsSynth(Synth):
         if self.offlineOnly: return SampleEvent(self.makefile_cached(lang,text)).play()
         if lang in sapiVoices:
             d=os.getcwd()
-            ret=self.sapi_unicode(sapiVoices[lang][0],ensure_unicode(text))
+            ret=self.sapi_unicode(sapiVoices[lang][0],ensure_unicode(text),speed=sapiSpeeds.get(lang,None))
             os.chdir(d) ; return ret
         elif lang=='en':
-            p=os.popen(self.program+toNull,"w")
+            p=os.popen(self.program+self.speedParam(sapiSpeeds.get(lang,None))+toNull,"w")
             p.write(text+"\n")
             return p.close()
         elif lang=='zh':
             d=os.getcwd()
-            ret=self.sapi_unicode("VW Lily",self.preparePinyinPhrase(text))
+            ret=self.sapi_unicode("VW Lily",self.preparePinyinPhrase(text),speed=sapiSpeeds.get(lang,None))
             self.restore_lily_dict()
             os.chdir(d) ; return ret
-    def sapi_unicode(self,voice,unicode_string,toFile=None,rate=None):
+    def sapi_unicode(self,voice,unicode_string,toFile=None,sampleRate=None,speed=None):
         # Speaks unicode_string in 'voice'.  toFile (if present) must be something that was returned by tempnam.  May change the current directory.
         unifile=os.tempnam() ; open(unifile,"wb").write(codecs.utf_16_encode(unicode_string)[0])
         if not toFile: extra=""
         else:
             extra=' -w '+changeToDirOf(toFile,1)+' -c 1'
-            if rate: extra += (' -s '+str(rate))
-        ret=system(self.program+' -u '+changeToDirOf(unifile,1)+' -voice "'+voice+'"'+extra+toNull) # (both changeToDirOf will give same directory because both from tempnam)
+            if sampleRate: extra += (' -s '+str(sampleRate))
+        ret=system(self.program+' -u '+changeToDirOf(unifile,1)+' -voice "'+voice+'"'+extra+self.speedParam(speed)+toNull) # (both changeToDirOf will give same directory because both from tempnam)
         os.unlink(unifile) ; return ret
+    def speedParam(self,speed):
+        if speed: return " -r "+str(speed)
+        else: return ""
     def makefile(self,lang,text):
         fname = os.tempnam()+dotwav
         oldcwd=os.getcwd()
-        if lang in sapiVoices: self.sapi_unicode(sapiVoices[lang][0],ensure_unicode(text),fname,sapiVoices[lang][1])
-        elif lang=="en": os.popen(self.program+' -c 1 -w '+changeToDirOf(fname,1)+toNull,"w").write(text+"\n") # (can specify mono but can't specify sample rate if it wasn't mentioned in sapiVoices - might make en synth-cache bigger than necessary but otherwise no great problem)
+        if lang in sapiVoices: self.sapi_unicode(sapiVoices[lang][0],ensure_unicode(text),fname,sapiVoices[lang][1],speed=sapiSpeeds.get(lang,None))
+        elif lang=="en": os.popen(self.program+speed+' -c 1 -w '+changeToDirOf(fname,1)+self.speedParam(sapiSpeeds.get(lang,None))+toNull,"w").write(text+"\n") # (can specify mono but can't specify sample rate if it wasn't mentioned in sapiVoices - might make en synth-cache bigger than necessary but otherwise no great problem)
         elif lang=='zh':
-            self.sapi_unicode("VW Lily",self.preparePinyinPhrase(text),fname,16000)
+            self.sapi_unicode("VW Lily",self.preparePinyinPhrase(text),fname,16000,speed=sapiSpeeds.get(lang,None))
             self.restore_lily_dict()
         os.chdir(oldcwd)
         d = sapi_sox_bug_workaround(open(fname,"rb").read()); open(fname,"wb").write(d)
diff --git a/gradint-build/src/system.py b/gradint-build/src/system.py
index 511dbd0..056a5b3 100644
--- a/gradint-build/src/system.py
+++ b/gradint-build/src/system.py
@@ -1,5 +1,5 @@
 # This file is part of the source code of
-# gradint v0.9957 (c) 2002-2010 Silas S. Brown. GPL v3+.
+# gradint v0.9958 (c) 2002-2010 Silas S. Brown. GPL v3+.
 #    This program is free software; you can redistribute it and/or modify
 #    it under the terms of the GNU General Public License as published by
 #    the Free Software Foundation; either version 3 of the License, or
diff --git a/gradint-build/src/top.py b/gradint-build/src/top.py
index a6e810d..e4791c1 100644
--- a/gradint-build/src/top.py
+++ b/gradint-build/src/top.py
@@ -1,7 +1,7 @@
 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
 
-program_name = "gradint v0.9957 (c) 2002-2010 Silas S. Brown. GPL v3+."
+program_name = "gradint v0.9958 (c) 2002-2010 Silas S. Brown. GPL v3+."
 
 #    This program is free software; you can redistribute it and/or modify
 #    it under the terms of the GNU General Public License as published by
diff --git a/gradint-build/src/users.py b/gradint-build/src/users.py
index b017500..b50df01 100644
--- a/gradint-build/src/users.py
+++ b/gradint-build/src/users.py
@@ -1,5 +1,5 @@
 # This file is part of the source code of
-# gradint v0.9957 (c) 2002-2010 Silas S. Brown. GPL v3+.
+# gradint v0.9958 (c) 2002-2010 Silas S. Brown. GPL v3+.
 #    This program is free software; you can redistribute it and/or modify
 #    it under the terms of the GNU General Public License as published by
 #    the Free Software Foundation; either version 3 of the License, or
diff --git a/gradint-build/thindown.py b/gradint-build/thindown.py
index 0e072be..c60fe4a 100644
--- a/gradint-build/thindown.py
+++ b/gradint-build/thindown.py
@@ -1,5 +1,5 @@
 # This file is part of the source code of
-# gradint v0.9957 (c) 2002-2010 Silas S. Brown. GPL v3+.
+# gradint v0.9958 (c) 2002-2010 Silas S. Brown. GPL v3+.
 #    This program is free software; you can redistribute it and/or modify
 #    it under the terms of the GNU General Public License as published by
 #    the Free Software Foundation; either version 3 of the License, or
-- 
GitLab