Bug 1254738 - Fix utterance rate conversion for speech dispatcher. r?smaug draft
authorEitan Isaacson <eitan@monotonous.org>
Tue, 08 Mar 2016 15:14:16 -0800
changeset 338369 334b6fff5c20e4453b1efc993d94e84ce1db16a5
parent 337411 be593a64d7c6a826260514fe758ef32a6ee580f7
child 515785 8cce0b076a953caf12693d854ab9ddddaa6be5c3
push id12493
push userbmo:eitan@monotonous.org
push dateTue, 08 Mar 2016 23:15:21 +0000
reviewerssmaug
bugs1254738
milestone48.0a1
Bug 1254738 - Fix utterance rate conversion for speech dispatcher. r?smaug MozReview-Commit-ID: 4P4QVWqmY1Y
dom/media/webspeech/synth/speechd/SpeechDispatcherService.cpp
--- a/dom/media/webspeech/synth/speechd/SpeechDispatcherService.cpp
+++ b/dom/media/webspeech/synth/speechd/SpeechDispatcherService.cpp
@@ -12,16 +12,19 @@
 #include "nsEscape.h"
 #include "nsISupports.h"
 #include "nsPrintfCString.h"
 #include "nsReadableUtils.h"
 #include "nsServiceManagerUtils.h"
 #include "nsThreadUtils.h"
 #include "prlink.h"
 
+#include <math.h>
+#include <stdlib.h>
+
 #define URI_PREFIX "urn:moz-tts:sapi:"
 
 // Some structures for libspeechd
 typedef enum {
   SPD_EVENT_BEGIN,
   SPD_EVENT_END,
   SPD_EVENT_INDEX_MARK,
   SPD_EVENT_CANCEL,
@@ -469,26 +472,21 @@ SpeechDispatcherService::Speak(const nsA
 
   spd_set_synthesis_voice(mSpeechdClient,
                           NS_ConvertUTF16toUTF8(voice->mName).get());
 
   // We provide a volume of 0.0 to 1.0, speech-dispatcher expects 0 - 100.
   spd_set_volume(mSpeechdClient, static_cast<int>(aVolume * 100));
 
   // We provide a rate of 0.1 to 10 with 1 being default.
-  // speech-dispatcher expects -100 to 100 with 0 being default.
-  int rate = 0;
+  // The rate range in speechd is roughly x3 at both ends (0.334 .. 3).
+  int rate = std::max<float>(std::min<float>(aRate, 3), 0.334);
 
-  if (aRate > 1) {
-    rate = static_cast<int>((aRate - 1) * 10);
-  } else if (aRate <= 1) {
-    rate = static_cast<int>((aRate - 1) * (100/0.9));
-  }
-
-  spd_set_voice_rate(mSpeechdClient, rate);
+  // speech-dispatcher expects -100 to 100 with 0 being default.
+  spd_set_voice_rate(mSpeechdClient, log10(rate) / log10(3) * 100);
 
   // We provide a pitch of 0 to 2 with 1 being the default.
   // speech-dispatcher expects -100 to 100 with 0 being default.
   spd_set_voice_pitch(mSpeechdClient, static_cast<int>((aPitch - 1) * 100));
 
   // The last three parameters don't matter for an indirect service
   nsresult rv = aTask->Setup(callback, 0, 0, 0);