audio_common for ROS 2
Loading...
Searching...
No Matches
music_node.hpp
Go to the documentation of this file.
1// MIT License
2//
3// Copyright (c) 2024 Miguel Ángel González Santamarta
4//
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to deal
7// in the Software without restriction, including without limitation the rights
8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9// copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in
13// all copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21// SOFTWARE.
22
23#ifndef AUDIO_COMMON__MUSIC_NODE
24#define AUDIO_COMMON__MUSIC_NODE
25
26#include <atomic>
27#include <mutex>
28#include <string>
29#include <thread>
30
31#include <ament_index_cpp/get_package_share_directory.hpp>
32#include <audio_common_msgs/msg/audio_stamped.hpp>
33#include <audio_common_msgs/srv/music_play.hpp>
34#include <rclcpp/rclcpp.hpp>
35#include <std_srvs/srv/trigger.hpp>
36
37namespace audio_common {
38
39class MusicNode : public rclcpp::Node {
40public:
41 MusicNode();
42 ~MusicNode() override;
43
44private:
45 // ROS 2 parameters
46 int chunk_;
47 std::string frame_id_;
48
49 // Audio control flags
50 std::atomic<bool> pause_music_;
51 std::atomic<bool> stop_music_;
53 std::atomic<bool> is_thread_alive_;
54
55 // Threads and synchronization
56 std::thread publish_thread_;
57 std::mutex pause_mutex_;
58 std::condition_variable pause_cv_;
59
60 // ROS 2 publisher and services
61 rclcpp::Publisher<audio_common_msgs::msg::AudioStamped>::SharedPtr
63 rclcpp::Service<audio_common_msgs::srv::MusicPlay>::SharedPtr play_service_;
64 rclcpp::Service<std_srvs::srv::Trigger>::SharedPtr stop_service_;
65 rclcpp::Service<std_srvs::srv::Trigger>::SharedPtr pause_service_;
66 rclcpp::Service<std_srvs::srv::Trigger>::SharedPtr resume_service_;
67
68 // Methods
69 void publish_audio(const std::string &file_path);
70 void play_callback(
71 const std::shared_ptr<audio_common_msgs::srv::MusicPlay::Request> request,
72 std::shared_ptr<audio_common_msgs::srv::MusicPlay::Response> response);
73 void
74 pause_callback(const std::shared_ptr<std_srvs::srv::Trigger::Request> request,
75 std::shared_ptr<std_srvs::srv::Trigger::Response> response);
76 void resume_callback(
77 const std::shared_ptr<std_srvs::srv::Trigger::Request> request,
78 std::shared_ptr<std_srvs::srv::Trigger::Response> response);
79 void
80 stop_callback(const std::shared_ptr<std_srvs::srv::Trigger::Request> request,
81 std::shared_ptr<std_srvs::srv::Trigger::Response> response);
82};
83
84} // namespace audio_common
85
86#endif
void pause_callback(const std::shared_ptr< std_srvs::srv::Trigger::Request > request, std::shared_ptr< std_srvs::srv::Trigger::Response > response)
Definition music_node.cpp:165
std::atomic< bool > pause_music_
Definition music_node.hpp:50
std::mutex pause_mutex_
Definition music_node.hpp:57
MusicNode()
Definition music_node.cpp:43
std::condition_variable pause_cv_
Definition music_node.hpp:58
std::thread publish_thread_
Definition music_node.hpp:56
std::string frame_id_
Definition music_node.hpp:47
~MusicNode() override
Definition music_node.cpp:72
rclcpp::Service< std_srvs::srv::Trigger >::SharedPtr stop_service_
Definition music_node.hpp:64
rclcpp::Service< std_srvs::srv::Trigger >::SharedPtr pause_service_
Definition music_node.hpp:65
void play_callback(const std::shared_ptr< audio_common_msgs::srv::MusicPlay::Request > request, std::shared_ptr< audio_common_msgs::srv::MusicPlay::Response > response)
Definition music_node.cpp:130
std::atomic< bool > is_thread_alive_
Definition music_node.hpp:53
bool audio_loop_
Definition music_node.hpp:52
void resume_callback(const std::shared_ptr< std_srvs::srv::Trigger::Request > request, std::shared_ptr< std_srvs::srv::Trigger::Response > response)
Definition music_node.cpp:180
void stop_callback(const std::shared_ptr< std_srvs::srv::Trigger::Request > request, std::shared_ptr< std_srvs::srv::Trigger::Response > response)
Definition music_node.cpp:196
int chunk_
Definition music_node.hpp:46
rclcpp::Service< audio_common_msgs::srv::MusicPlay >::SharedPtr play_service_
Definition music_node.hpp:63
rclcpp::Publisher< audio_common_msgs::msg::AudioStamped >::SharedPtr player_pub_
Definition music_node.hpp:62
std::atomic< bool > stop_music_
Definition music_node.hpp:51
rclcpp::Service< std_srvs::srv::Trigger >::SharedPtr resume_service_
Definition music_node.hpp:66
void publish_audio(const std::string &file_path)
Definition music_node.cpp:79
Definition audio_capturer_node.hpp:32