From d6c0476fde42d3bbfe4e88ff2dee6dfa4f681e0f Mon Sep 17 00:00:00 2001 From: Kelly Davis Date: Sun, 26 Jul 2015 23:02:00 -0400 Subject: [PATCH] Bug 1185235 - Implement SpeechRecognition::maxAlternatives. r=smaug --- .../PocketSphinxSpeechRecognitionService.cpp | 26 ++++++++++++------- .../recognition/SpeechRecognition.cpp | 6 ++--- .../webspeech/recognition/SpeechRecognition.h | 11 ++++++++ .../test/FakeSpeechRecognitionService.cpp | 11 +++++--- 4 files changed, 37 insertions(+), 17 deletions(-) diff --git a/dom/media/webspeech/recognition/PocketSphinxSpeechRecognitionService.cpp b/dom/media/webspeech/recognition/PocketSphinxSpeechRecognitionService.cpp index b2f54f6a2ce3..3f5bee0ecb77 100644 --- a/dom/media/webspeech/recognition/PocketSphinxSpeechRecognitionService.cpp +++ b/dom/media/webspeech/recognition/PocketSphinxSpeechRecognitionService.cpp @@ -54,13 +54,16 @@ public: SpeechRecognitionResultList* resultList = new SpeechRecognitionResultList(mRecognition); SpeechRecognitionResult* result = new SpeechRecognitionResult(mRecognition); - SpeechRecognitionAlternative* alternative = - new SpeechRecognitionAlternative(mRecognition); + ErrorResult rv; + if (0 < mRecognition->GetMaxAlternatives(rv)) { // GetMaxAlternatives can't fail + SpeechRecognitionAlternative* alternative = + new SpeechRecognitionAlternative(mRecognition); - alternative->mTranscript = mResult; - alternative->mConfidence = 100; + alternative->mTranscript = mResult; + alternative->mConfidence = 100; - result->mItems.AppendElement(alternative); + result->mItems.AppendElement(alternative); + } resultList->mItems.AppendElement(result); event->mRecognitionResultList = resultList; @@ -330,13 +333,16 @@ PocketSphinxSpeechRecognitionService::BuildMockResultList() SpeechRecognitionResultList* resultList = new SpeechRecognitionResultList(mRecognition); SpeechRecognitionResult* result = new SpeechRecognitionResult(mRecognition); - SpeechRecognitionAlternative* alternative = - new SpeechRecognitionAlternative(mRecognition); + ErrorResult rv; + if (0 < mRecognition->GetMaxAlternatives(rv)) { // GetMaxAlternatives can't fail + SpeechRecognitionAlternative* alternative = + new SpeechRecognitionAlternative(mRecognition); - alternative->mTranscript = NS_LITERAL_STRING("Mock final result"); - alternative->mConfidence = 0.0f; + alternative->mTranscript = NS_LITERAL_STRING("Mock final result"); + alternative->mConfidence = 0.0f; - result->mItems.AppendElement(alternative); + result->mItems.AppendElement(alternative); + } resultList->mItems.AppendElement(result); return resultList; diff --git a/dom/media/webspeech/recognition/SpeechRecognition.cpp b/dom/media/webspeech/recognition/SpeechRecognition.cpp index 9f877f901613..14e4f9833ba4 100644 --- a/dom/media/webspeech/recognition/SpeechRecognition.cpp +++ b/dom/media/webspeech/recognition/SpeechRecognition.cpp @@ -117,6 +117,7 @@ SpeechRecognition::SpeechRecognition(nsPIDOMWindow* aOwnerWindow) , mSpeechDetectionTimer(do_CreateInstance(NS_TIMER_CONTRACTID)) , mSpeechGrammarList(new SpeechGrammarList(GetParentObject())) , mInterimResults(false) + , mMaxAlternatives(1) { SR_LOG("created SpeechRecognition"); @@ -685,14 +686,13 @@ SpeechRecognition::SetInterimResults(bool aArg) uint32_t SpeechRecognition::GetMaxAlternatives(ErrorResult& aRv) const { - aRv.Throw(NS_ERROR_NOT_IMPLEMENTED); - return 0; + return mMaxAlternatives; } void SpeechRecognition::SetMaxAlternatives(uint32_t aArg, ErrorResult& aRv) { - aRv.Throw(NS_ERROR_NOT_IMPLEMENTED); + mMaxAlternatives = aArg; return; } diff --git a/dom/media/webspeech/recognition/SpeechRecognition.h b/dom/media/webspeech/recognition/SpeechRecognition.h index 86aa8d405514..671dd023399d 100644 --- a/dom/media/webspeech/recognition/SpeechRecognition.h +++ b/dom/media/webspeech/recognition/SpeechRecognition.h @@ -266,6 +266,17 @@ private: // is a conforming implementation. bool mInterimResults; + // WebSpeechAPI (http://bit.ly/1JAiqeo) states: + // + // 1. Default value is 1 + // 2. Subsequent value is the "maximum number of SpeechRecognitionAlternatives per result" + // + // Pocketsphinx can only return at maximum a single SpeechRecognitionAlternative + // per SpeechRecognitionResult. So defaulting mMaxAlternatives to 1, for all non + // zero values ignoring mMaxAlternatives while for a 0 value returning no + // SpeechRecognitionAlternative per result is a conforming implementation. + uint32_t mMaxAlternatives; + void ProcessTestEventRequest(nsISupports* aSubject, const nsAString& aEventName); const char* GetName(FSMState aId); diff --git a/dom/media/webspeech/recognition/test/FakeSpeechRecognitionService.cpp b/dom/media/webspeech/recognition/test/FakeSpeechRecognitionService.cpp index ff36134aa81c..86be26ab8428 100644 --- a/dom/media/webspeech/recognition/test/FakeSpeechRecognitionService.cpp +++ b/dom/media/webspeech/recognition/test/FakeSpeechRecognitionService.cpp @@ -102,12 +102,15 @@ FakeSpeechRecognitionService::BuildMockResultList() { SpeechRecognitionResultList* resultList = new SpeechRecognitionResultList(mRecognition); SpeechRecognitionResult* result = new SpeechRecognitionResult(mRecognition); - SpeechRecognitionAlternative* alternative = new SpeechRecognitionAlternative(mRecognition); + ErrorResult rv; + if (0 < mRecognition->GetMaxAlternatives(rv)) { // GetMaxAlternatives can't fail + SpeechRecognitionAlternative* alternative = new SpeechRecognitionAlternative(mRecognition); - alternative->mTranscript = NS_LITERAL_STRING("Mock final result"); - alternative->mConfidence = 0.0f; + alternative->mTranscript = NS_LITERAL_STRING("Mock final result"); + alternative->mConfidence = 0.0f; - result->mItems.AppendElement(alternative); + result->mItems.AppendElement(alternative); + } resultList->mItems.AppendElement(result); return resultList;