Bug 1254738 - Fix utterance rate conversion for speech dispatcher. r?smaug
MozReview-Commit-ID: 4P4QVWqmY1Y
--- 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);