Skip to content

Commit

Permalink
Small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
danilo-pejovic committed Feb 12, 2024
1 parent 4b418f0 commit 0796a19
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
4 changes: 3 additions & 1 deletion rae_hw/include/rae_hw/peripherals/speakers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
#include <mpg123.h>
#include <sndfile.h>
#include <rae_msgs/srv/play_audio.hpp>

#include <iostream>
#include <cstring>
#include <limits>
#include "audio_msgs/msg/audio.hpp"
#include "rclcpp/rclcpp.hpp"
#include "rclcpp_lifecycle/lifecycle_node.hpp"
Expand Down
18 changes: 10 additions & 8 deletions rae_hw/src/peripherals/speakers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,32 +129,34 @@ void SpeakersNode::play_wav(const char* wav_file) {
return;
}

snd_pcm_set_params(alsaHandle, SND_PCM_FORMAT_S32_LE, SND_PCM_ACCESS_RW_INTERLEAVED,
sfInfo.channels, sfInfo.samplerate, 1, 50000);
// Set ALSA parameters
snd_pcm_set_params(alsaHandle, SND_PCM_FORMAT_S32_LE, SND_PCM_ACCESS_RW_INTERLEAVED,
sfinfo.channels, sfinfo.samplerate, 2, 50000);

// Read and play WAV file
const int BUFFER_SIZE = 4096;
int32_t buffer[BUFFER_SIZE * sfInfo.channels]; // Use int32_t for 32-bit format
int32_t* buffer_wav = new int32_t[BUFFER_SIZE * sfinfo.channels]; // Use int32_t for 32-bit format
sf_count_t readCount;

const float gain = 4.0f; // Adjust this factor for desired gain

while ((readCount = sf_readf_int(file, buffer, BUFFER_SIZE)) > 0) {
while ((readCount = sf_readf_int(file, buffer_wav, BUFFER_SIZE)) > 0) {
// Apply gain to the samples
for (int i = 0; i < readCount * sfInfo.channels; ++i) {
float sample = static_cast<float>(buffer[i]) / std::numeric_limits<int32_t>::max();
for (int i = 0; i < readCount * sfinfo.channels; ++i) {
float sample = static_cast<float>(buffer_wav[i]) / std::numeric_limits<int32_t>::max();
sample *= gain; // Apply gain
buffer[i] = static_cast<int32_t>(sample * std::numeric_limits<int32_t>::max());
buffer_wav[i] = static_cast<int32_t>(sample * std::numeric_limits<int32_t>::max());
}

// Write the processed buffer to the playback device
if (snd_pcm_writei(alsaHandle, buffer, readCount) < 0) {
if (snd_pcm_writei(alsaHandle, buffer_wav, readCount) < 0) {
std::cerr << "Error in snd_pcm_writei: " << snd_strerror(readCount) << std::endl;
break;
}
}

// Cleanup
delete[] buffer_wav;
sf_close(file);
snd_pcm_close(alsaHandle);

Expand Down

0 comments on commit 0796a19

Please sign in to comment.