-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5ac5685
commit 197ba90
Showing
9 changed files
with
141 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,34 @@ | ||
#ifndef RAE_HW_SPEAKERS_NODE_HPP_ | ||
#define RAE_HW_SPEAKERS_NODE_HPP_ | ||
|
||
#include "rclcpp/rclcpp.hpp" | ||
#include "audio_msgs/msg/audio.hpp" | ||
|
||
#include "std_msgs/msg/int32.hpp" | ||
#include <alsa/asoundlib.h> | ||
namespace rae_hw{ | ||
class SpeakersNode : public rclcpp::Node | ||
{ | ||
#include <mpg123.h> | ||
#include <rae_msgs/srv/play_audio.hpp> | ||
|
||
namespace rae_hw { | ||
|
||
class SpeakersNode : public rclcpp::Node { | ||
public: | ||
SpeakersNode(const rclcpp::NodeOptions &options); | ||
~SpeakersNode(); | ||
|
||
private: | ||
void audio_callback(const audio_msgs::msg::Audio::SharedPtr msg); | ||
void play_mp3(const char *); | ||
rclcpp::Service<rae_msgs::srv::PlayAudio>::SharedPtr play_audio_service_; | ||
|
||
rclcpp::Subscription<audio_msgs::msg::Audio>::SharedPtr subscription_; | ||
void play_audio_service_callback( | ||
const std::shared_ptr<rmw_request_id_t> request_header, | ||
const std::shared_ptr<rae_msgs::srv::PlayAudio::Request> request, | ||
const std::shared_ptr<rae_msgs::srv::PlayAudio::Response> response); | ||
snd_pcm_t *alsaHandle; | ||
|
||
unsigned char *buffer; | ||
mpg123_handle *mh; | ||
}; | ||
|
||
} | ||
} // namespace rae_hw | ||
|
||
#endif // RAE_HW_SPEAKERS_NODE_HPP_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,100 @@ | ||
#include "rclcpp/rclcpp.hpp" | ||
#include "rae_hw/peripherals/speakers.hpp" | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
rclcpp::init(argc, argv); | ||
namespace rae_hw { | ||
|
||
auto node = std::make_shared<rae_hw::SpeakersNode>(rclcpp::NodeOptions()); | ||
rclcpp::executors::StaticSingleThreadedExecutor executor; | ||
executor.add_node(node); | ||
executor.spin(); | ||
SpeakersNode::SpeakersNode(const rclcpp::NodeOptions &options) | ||
: Node("speakers_node", options) { | ||
// Initialize ALSA or any other audio setup code here | ||
|
||
rclcpp::shutdown(); | ||
// Create Audio Service | ||
play_audio_service_ = create_service<rae_msgs::srv::PlayAudio>( | ||
"play_audio", std::bind(&SpeakersNode::play_audio_service_callback, this, | ||
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); | ||
|
||
return 0; | ||
} | ||
|
||
// Any other initialization code here | ||
|
||
RCLCPP_INFO(this->get_logger(), "Speakers node running!"); | ||
} | ||
|
||
SpeakersNode::~SpeakersNode() { | ||
delete[] buffer; | ||
mpg123_close(mh); | ||
mpg123_delete(mh); | ||
mpg123_exit(); | ||
} | ||
|
||
void SpeakersNode::play_audio_service_callback( | ||
const std::shared_ptr<rmw_request_id_t> request_header, | ||
const std::shared_ptr<rae_msgs::srv::PlayAudio::Request> request, | ||
const std::shared_ptr<rae_msgs::srv::PlayAudio::Response> response) { | ||
// Use request->mp3_file to get the MP3 file location | ||
const char *mp3_file = request->mp3_file.c_str(); | ||
|
||
// Call the play_mp3 function | ||
play_mp3(mp3_file); | ||
|
||
// Respond with success (modify based on your play_mp3 result) | ||
response->success = true; | ||
} | ||
|
||
void SpeakersNode::play_mp3(const char *mp3_file) { | ||
// Initialize libmpg123 | ||
mpg123_init(); | ||
mh = mpg123_new(NULL, NULL); | ||
long rate; // Set your desired sample rate here | ||
int channels, encoding; | ||
if (mpg123_open(mh, mp3_file) != MPG123_OK || | ||
mpg123_getformat(mh, &rate, &channels, &encoding) != MPG123_OK) { | ||
RCLCPP_ERROR(this->get_logger(), "Cant read MP3 file"); | ||
return; | ||
} | ||
|
||
// Open ALSA device | ||
if (snd_pcm_open(&alsaHandle, "default", SND_PCM_STREAM_PLAYBACK, 0) < 0) { | ||
RCLCPP_ERROR(this->get_logger(), "Failed to open ALSA playback device."); | ||
return ; | ||
} | ||
|
||
|
||
// Set ALSA parameters | ||
if (channels==1) { | ||
snd_pcm_set_params(alsaHandle, SND_PCM_FORMAT_S16_LE, SND_PCM_ACCESS_RW_INTERLEAVED, 1,44100, 2, 50000); | ||
} else if (channels==2) { | ||
snd_pcm_set_params(alsaHandle, SND_PCM_FORMAT_S16_LE, SND_PCM_ACCESS_RW_INTERLEAVED, 1,88200, 2, 50000);} | ||
|
||
// Read and play MP3 file | ||
size_t buffer_size = mpg123_outblock(mh)*4; | ||
unsigned char *buffer = new unsigned char[buffer_size]; | ||
size_t err; | ||
|
||
while (mpg123_read(mh, buffer, buffer_size, &err) == MPG123_OK) { | ||
if (snd_pcm_writei(alsaHandle, buffer, buffer_size/2) < 0) { | ||
std::cerr << "Error in snd_pcm_writei: " << snd_strerror(err) << std::endl; | ||
} | ||
} | ||
|
||
// Cleanup | ||
delete[] buffer; | ||
snd_pcm_close(alsaHandle); | ||
mpg123_close(mh); | ||
mpg123_delete(mh); | ||
mpg123_exit(); | ||
|
||
return ; | ||
} | ||
|
||
} // namespace rae_hw | ||
|
||
int main(int argc, char *argv[]) { | ||
rclcpp::init(argc, argv); | ||
|
||
auto node = std::make_shared<rae_hw::SpeakersNode>(rclcpp::NodeOptions()); | ||
rclcpp::executors::StaticSingleThreadedExecutor executor; | ||
executor.add_node(node); | ||
executor.spin(); | ||
|
||
rclcpp::shutdown(); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
string mp3_file | ||
--- | ||
bool success |