blob: 020229fe4d51aff385abf56e665897f2d1aead60 [file] [log] [blame]
* Copyright 2016 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* See the License for the specific language governing permissions and
* limitations under the License.
#include <string>
#include "cobalt/dom/event_target.h"
#include "cobalt/script/environment_settings.h"
#include "cobalt/speech/speech_recognition_config.h"
#include "cobalt/speech/speech_recognition_manager.h"
namespace cobalt {
namespace speech {
// The speech recognition interface is the scripted web API for controlling a
// given recognition.
class SpeechRecognition : public dom::EventTarget {
explicit SpeechRecognition(script::EnvironmentSettings* settings);
// When the start method is called, it represents the moment in time the web
// application wishes to begin recognition.
void Start(script::ExceptionState* exception_state);
// The stop method represents an instruction to the recognition service to
// stop listening to more audio, and to try and return a result using just the
// audio that it has already received for this recognition.
void Stop();
// The abort method is a request to immediately stop listening and stop
// recognizing. It does not return any information.
void Abort();
const std::string& lang() const { return config_.lang; }
void set_lang(const std::string& lang) { config_.lang = lang; }
bool continuous() const { return config_.continuous; }
void set_continuous(bool continuous) { config_.continuous = continuous; }
bool interim_results() const { return config_.interim_results; }
void set_interim_results(bool interim_results) {
config_.interim_results = interim_results;
uint32 max_alternatives() const { return config_.max_alternatives; }
void set_max_alternatives(uint32 max_alternatives) {
config_.max_alternatives = max_alternatives;
// Fired when some sound, possibly speech, has been detected.
const EventListenerScriptObject* onsoundstart() const {
return GetAttributeEventListener(base::Tokens::soundstart());
void set_onsoundstart(const EventListenerScriptObject& listener) {
SetAttributeEventListener(base::Tokens::soundstart(), listener);
// Fired when some sound is no longer detected.
const EventListenerScriptObject* onsoundend() const {
return GetAttributeEventListener(base::Tokens::soundend());
void set_onsoundend(const EventListenerScriptObject& listener) {
SetAttributeEventListener(base::Tokens::soundend(), listener);
// Fired when the speech recognizer returns a result.
const EventListenerScriptObject* onresult() const {
return GetAttributeEventListener(base::Tokens::result());
void set_onresult(const EventListenerScriptObject& listener) {
SetAttributeEventListener(base::Tokens::result(), listener);
// Fired when the speech recognizer returns a final result with no recognition
// hypothesis that meet or exceed the confidence threshold.
const EventListenerScriptObject* onnomatch() const {
return GetAttributeEventListener(base::Tokens::nomatch());
void set_onnomatch(const EventListenerScriptObject& listener) {
SetAttributeEventListener(base::Tokens::nomatch(), listener);
// Fired when the speech recognition error occurs.
const EventListenerScriptObject* onerror() const {
return GetAttributeEventListener(base::Tokens::error());
void set_onerror(const EventListenerScriptObject& listener) {
SetAttributeEventListener(base::Tokens::error(), listener);
~SpeechRecognition() OVERRIDE {}
// Callback from recognition manager.
bool OnEventAvailable(const scoped_refptr<dom::Event>& event);
// Handles main operations of speech recognition including audio encoding,
// mic audio retrieving and audio data recognizing.
SpeechRecognitionManager manager_;
// Store the speech recognition configurations.
SpeechRecognitionConfig config_;
} // namespace speech
} // namespace cobalt