Program Listing for File voice.hpp

Return to documentation for file (include/hri/voice.hpp)

// Copyright (c) 2023 PAL Robotics S.L. 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
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef HRI__VOICE_HPP_
#define HRI__VOICE_HPP_

#include <memory>
#include <optional>
#include <string>
#include <vector>

#include "hri_msgs/msg/live_speech.hpp"
#include "std_msgs/msg/bool.hpp"
#include "tf2_ros/buffer.h"

#include "hri/feature_tracker.hpp"
#include "hri/types.hpp"

namespace hri
{
class Voice : public FeatureTracker, public std::enable_shared_from_this<Voice>
// TODO(LJU): possibly subscribe also to the /audio and the /features sub-topics
{
  friend class HRIListener;  // for invalidate()

public:
  Voice(
    ID id,
    NodeInterfaces node_interfaces,
    rclcpp::CallbackGroup::SharedPtr callback_group,
    const tf2::BufferCore & tf_buffer,
    const std::string & reference_frame);

  virtual ~Voice();

  std::optional<bool> isSpeaking() const {return is_speaking_;}

  std::optional<std::string> speech() const {return speech_;}

  std::optional<std::string> incrementalSpeech() const {return incremental_speech_;}

  std::optional<std::string> locale() const {return locale_;}

  void onSpeaking(std::function<void(bool)> callback)
  {
    is_speaking_callbacks_.push_back(callback);
  }

  void onSpeech(std::function<void(const std::string &, const std::string &)> callback)
  {
    speech_callbacks_.push_back(callback);
  }

  void onSpeech(std::function<void(const std::string &)> callback)
  {
    speech_callbacks_.push_back(
      [callback](const std::string & text, const std::string &) {callback(text);});
  }

  void onIncrementalSpeech(std::function<void(const std::string &, const std::string &)> callback)
  {
    incremental_speech_callbacks_.push_back(callback);
  }

  void onIncrementalSpeech(std::function<void(const std::string &)> callback)
  {
    incremental_speech_callbacks_.push_back(
      [callback](const std::string & text, const std::string &) {callback(text);});
  }

private:
  void onSpeech_(hri_msgs::msg::LiveSpeech::ConstSharedPtr msg);
  void onIsSpeaking(std_msgs::msg::Bool::ConstSharedPtr msg);

  void invalidate();

  std::optional<bool> is_speaking_;
  std::optional<std::string> speech_;
  std::optional<std::string> incremental_speech_;
  std::optional<std::string> locale_;

  std::vector<std::function<void(bool)>> is_speaking_callbacks_;
  std::vector<std::function<void(const std::string &, const std::string &)>> speech_callbacks_;
  std::vector<std::function<void(const std::string &, const std::string &)>>
  incremental_speech_callbacks_;

  rclcpp::Subscription<std_msgs::msg::Bool>::SharedPtr is_speaking_subscriber_;
  rclcpp::Subscription<hri_msgs::msg::LiveSpeech>::SharedPtr speech_subscriber_;
};

typedef std::shared_ptr<Voice> VoicePtr;
typedef std::shared_ptr<const Voice> ConstVoicePtr;

}  // namespace hri

#endif  // HRI__VOICE_HPP_