Speech based reaction

<< return to examples index

Overview

ALSpeechBasedReaction example module shows how to react to a specific voice command using speech recognition.

This module can be remote or local.

Downloads

Header: alspeechbasedreaction.h

alspeechbasedreaction.h

/**
* @author Gwennael Gate
* Copyright (c) Aldebaran Robotics 2010
*/

#ifndef SPEECHBASEDREACTION_H
#define SPEECHBASEDREACTION_H

#include <boost/shared_ptr.hpp>
#include <alcommon/almodule.h>
#include <string>
#include <alproxies/almemoryproxy.h>

namespace AL
{
  class ALBroker;
}

class ALSpeechBasedReaction : public AL::ALModule
{

public:

  ALSpeechBasedReaction(boost::shared_ptr<AL::ALBroker> broker, const std::string& name);
  virtual ~ALSpeechBasedReaction();

  void startRecognition();

  void stopRecognition();

  void onSpeechRecognized(const std::string& name,
                          const AL::ALValue& val,
                          const std::string& myName);

private:
  std::string fCommand;
  boost::shared_ptr<AL::ALProxy> fTextToSpeech;
  boost::shared_ptr<AL::ALMemoryProxy> fMemory;
  boost::shared_ptr<AL::ALProxy> fSpeechRecognition;
};

#endif  // SPEECHBASEDREACTION_H

Source: alspeechbasedreaction.cpp

alspeechbasedreaction.cpp

/**
* @author Gwennael Gate
* Copyright (c) Aldebaran Robotics 2010
*/

#include "alspeechbasedreaction.h"

#include <alvalue/alvalue.h>
#include <alcommon/alproxy.h>
#include <alcommon/albroker.h>
#include <iostream>


ALSpeechBasedReaction::ALSpeechBasedReaction(
  boost::shared_ptr<AL::ALBroker> broker,
  const std::string& name) : AL::ALModule(broker, name)
{
  /// This line will display a description of this module on the web page of the robot
  setModuleDescription("This module launch a text to speech command when a "
                       "specific voice command is heard. To start "
                       "this module, call its startRecognition() method in Choregraphe.");

  /// Class functions that need to be bound to be called in Python or by remote processes.
  functionName("startRecognition",
               getName(),
               "This function will launch the ASR engine.");
  BIND_METHOD(ALSpeechBasedReaction::startRecognition);

  functionName("stopRecognition",
               getName(),
               "This function will stop the ASR engine.");
  BIND_METHOD(ALSpeechBasedReaction::stopRecognition);

  functionName("onSpeechRecognized",
               getName(),
               "Called by ALMemory when a word is recognized.");
  BIND_METHOD(ALSpeechBasedReaction::onSpeechRecognized);

  /// Specify a command to which the robot will react.
  fCommand = "speak";
}

ALSpeechBasedReaction::~ALSpeechBasedReaction()
{

}

void ALSpeechBasedReaction::startRecognition()
{
  /// Setting up a proxy to ALTextToSpeech
  fTextToSpeech = getParentBroker()->getProxy("ALTextToSpeech");
  /// Setting the language to "English"
  fTextToSpeech->callVoid("setLanguage",std::string("English"));

  /// Setting up a proxy to ALMemory
  fMemory = getParentBroker()->getMemoryProxy();

  /// Setting up a proxy to ALSpeechRecognition
  fSpeechRecognition = getParentBroker()->getProxy("ALSpeechRecognition");
  /// Setting the working language of speech recognition engine
  fSpeechRecognition->callVoid("setLanguage",std::string("English"));

  /// Setting the word list that should be recognized
  std::vector<std::string> wordlist;
  wordlist.push_back(fCommand);
  fSpeechRecognition->callVoid("setWordListAsVocabulary", wordlist);

  /// Launching the speech recognition engine by subscribing to a ALMemory key
  /// that is fed by ALSpeechRecognition. Note that a callback function is specified.
  fMemory->subscribeToEvent("WordRecognized",getName(),"onSpeechRecognized");
}

void ALSpeechBasedReaction::stopRecognition()
{
  /// Stopping the speech recognition engine by unsubscribing to the ALMemory key
  fMemory->unsubscribeToEvent("WordRecognized",getName());
}


/// This function is called by ALMemory every time the value of
/// the key "WordRecognized" is modified by the speech recognition engine.
void ALSpeechBasedReaction::onSpeechRecognized(const std::string& name,
                                             const AL::ALValue& val,
                                             const std::string& myName)
{
  /// Parse the list of words that has been recognized by ALSpeechRecognition
  for(unsigned int i = 0; i < val.getSize()/2 ; ++i)
  {
    std::cout << "word recognized: " << val[i*2].toString()
              << " with confidence: " << (float)val[i*2+1] << std::endl;

    /// If our "command" has been recognized, start a speech synthesis reaction
    if((std::string)val[i*2] == fCommand && (float)val[i*2+1] > 0.15)
    {
      fTextToSpeech->callVoid("say",std::string("Ok, Hello There!"));
    }
  }
}

Main: main.cpp

speechbasedreactionmain.cpp

/**
* @author Gwennael Gate
* Copyright (c) Aldebaran Robotics 2010
*
*/

#include <signal.h>
#include <boost/shared_ptr.hpp>
#include <alcommon/albroker.h>
#include <alcommon/almodule.h>
#include <alcommon/albrokermanager.h>
#include <alcommon/altoolsmain.h>

#include "alspeechbasedreaction.h"



#ifdef SPEECHBASEDREACTION_IS_REMOTE
# define ALCALL
#else
# ifdef _WIN32
#  define ALCALL __declspec(dllexport)
# else
#   define ALCALL
# endif
#endif

extern "C"
{

  ALCALL int _createModule( boost::shared_ptr<AL::ALBroker> pBroker )
  {
    // init broker with the main broker instance
    // from the parent executable
    AL::ALBrokerManager::setInstance(pBroker->fBrokerManager.lock());
    AL::ALBrokerManager::getInstance()->addBroker(pBroker);

    AL::ALModule::createModule<ALSpeechBasedReaction>(pBroker, "ALSpeechBasedReaction");

    return 0;
  }

  ALCALL int _closeModule(  )
  {
    return 0;
  }

}

#ifdef SPEECHBASEDREACTION_IS_REMOTE

int main(int argc, char *argv[])
{
  // pointer to createModule
  TMainType sig;
  sig = &_createModule;

  // call main
  ALTools::mainFunction("alspeechbasedreaction", argc, argv, sig);
}
#endif

CMakeLists.txt

CMakeLists.txt

# Copyright (C) 2010 Aldebaran Robotics

cmake_minimum_required(VERSION 2.6.4 FATAL_ERROR)
project(speechbasedreaction)
find_package(qibuild)

option(SPEECHBASEDREACTION_IS_REMOTE
  "module is compiled as a remote module (ON or OFF)"
  ON)

set(_srcs
    main.cpp
    alspeechbasedreaction.h
    alspeechbasedreaction.cpp
)

if(SPEECHBASEDREACTION_IS_REMOTE)
  add_definitions(" -DSPEECHBASEDREACTION_IS_REMOTE ")
  qi_create_bin(speechbasedreaction ${_srcs})
else()
  qi_create_lib(speechbasedreaction SHARED ${_srcs} SUBFOLDER naoqi)
endif()

qi_use_lib(speechbasedreaction ALCOMMON ALAUDIO)