FP Perception 0.1.2
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
fp_perception::MicrophoneAudioDriver Class Reference

MicrophoneAudioDriver class for handling audio input from a microphone. More...

#include <microphone_audio_driver.hpp>

Inheritance diagram for fp_perception::MicrophoneAudioDriver:
Inheritance graph
[legend]
Collaboration diagram for fp_perception::MicrophoneAudioDriver:
Collaboration graph
[legend]

Public Member Functions

 MicrophoneAudioDriver ()
 Constructor for MicrophoneAudioDriver.
 
 ~MicrophoneAudioDriver () override
 Start the audio stream.
 
void initialize (const rclcpp::Node::SharedPtr &node) override
 Initialize the driver.
 
void deinitialize () override
 Stop driver streaming. This function stops the audio stream and closes it. It also terminates the PortAudio library.
 
audio_data readChunk () override
 Get latest audio data from the driver as a stream. This function reads audio data from the microphone stream and returns it as a fp_perception::audio_data. Best approach is to use this to get a certain amount of audio data by repeatedly calling this function until enough data is collected.
 
void test () override
 Test the driver.
 
- Public Member Functions inherited from fp_perception::AudioSourceDriver
virtual audio_data readBufferedAudio (int duration_seconds)
 
- Public Member Functions inherited from fp_perception::DriverBase
virtual ~DriverBase ()=default
 
virtual std::string getName () const
 Get the name of the driver.
 
void check_directory (std::string folder_name)
 Check if the test directory exists, create it if not.
 
const std::filesystem::path check_file (const std::string &file_name)
 

Protected Member Functions

int capture_input_buffer (const void *input_buffer, unsigned long frames_per_buffer, PaStreamCallbackFlags status_flags)
 
void setup_diagnostics ()
 
void produce_diagnostics (diagnostic_updater::DiagnosticStatusWrapper &status)
 
void write_samples_locked (const int16_t *samples, size_t sample_count)
 
void read_samples_locked (int16_t *destination, size_t sample_count)
 
- Protected Member Functions inherited from fp_perception::DriverBase
void initialize_base (const rclcpp::Node::SharedPtr &node)
 Initializer base driver in place of constructor due to plugin semantics.
 
bool diagnostics_enabled () const
 
void enable_diagnostics (const std::string &hardware_id, const std::string &task_name, std::function< void(diagnostic_updater::DiagnosticStatusWrapper &)> task, std::chrono::milliseconds period=std::chrono::seconds(1))
 
void disable_diagnostics ()
 

Static Protected Member Functions

static int pa_input_callback (const void *input_buffer, void *output_buffer, unsigned long frames_per_buffer, const PaStreamCallbackTimeInfo *time_info, PaStreamCallbackFlags status_flags, void *user_data)
 
static std::vector< int16_t > resample_linear_interleaved (const std::vector< int16_t > &input, int channels, int input_rate, int output_rate)
 

Protected Attributes

PaStream * stream_
 
PaError err = paNoError
 
std::vector< int16_t > audio_buffer_
 
int device_id_
 
std::string device_name_
 
unsigned long chunk_size_
 
int sample_rate_
 
int capture_sample_rate_ { 0 }
 
int channels_
 
int capture_buffer_window_
 
int timeout_sec_ { -1 }
 
int buffer_size_ { 0 }
 
size_t read_index_ { 0 }
 
size_t write_index_ { 0 }
 
size_t buffered_sample_count_ { 0 }
 
std::atomic< uint64_t > input_overflow_count_ { 0 }
 
std::atomic< uint64_t > dropped_callback_chunks_ { 0 }
 
- Protected Attributes inherited from fp_perception::DriverBase
rclcpp::Node::SharedPtr node_
 ROS node for the driver.
 
std::string name_
 Name of the driver.
 
std::mutex buffer_mutex_
 Mutex to protect access to the driver data.
 
std::condition_variable buffer_cv_
 Condition variable to notify when new data is available.
 
std::atomic< bool > is_running_ { false }
 Flag to indicate if the driver thread is running.
 
bool diagnostics_enabled_ { false }
 Flag to enable standard ROS diagnostics publishing.
 
std::unique_ptr< diagnostic_updater::Updater > diagnostics_updater_
 
rclcpp::TimerBase::SharedPtr diagnostics_timer_
 

Detailed Description

MicrophoneAudioDriver class for handling audio input from a microphone.

This class is responsible for managing the audio input from a microphone using PortAudio. It provides methods to start and stop the audio stream, as well as retrieve audio data.

Constructor & Destructor Documentation

◆ MicrophoneAudioDriver()

fp_perception::MicrophoneAudioDriver::MicrophoneAudioDriver ( )
inline

Constructor for MicrophoneAudioDriver.

Initializes the PortAudio library and prepares the audio stream.

◆ ~MicrophoneAudioDriver()

fp_perception::MicrophoneAudioDriver::~MicrophoneAudioDriver ( )
inlineoverride

Start the audio stream.

This function starts the audio stream using PortAudio.

Member Function Documentation

◆ capture_input_buffer()

int fp_perception::MicrophoneAudioDriver::capture_input_buffer ( const void *  input_buffer,
unsigned long  frames_per_buffer,
PaStreamCallbackFlags  status_flags 
)
inlineprotected

◆ deinitialize()

void fp_perception::MicrophoneAudioDriver::deinitialize ( )
inlineoverridevirtual

Stop driver streaming. This function stops the audio stream and closes it. It also terminates the PortAudio library.

Implements fp_perception::DriverBase.

◆ initialize()

void fp_perception::MicrophoneAudioDriver::initialize ( const rclcpp::Node::SharedPtr &  node)
inlineoverridevirtual

Initialize the driver.

This function should be overridden in derived classes to provide specific initialization.

Parameters
nodeShared pointer to the ROS node

Reimplemented from fp_perception::DriverBase.

◆ pa_input_callback()

static int fp_perception::MicrophoneAudioDriver::pa_input_callback ( const void *  input_buffer,
void *  output_buffer,
unsigned long  frames_per_buffer,
const PaStreamCallbackTimeInfo *  time_info,
PaStreamCallbackFlags  status_flags,
void *  user_data 
)
inlinestaticprotected

◆ produce_diagnostics()

void fp_perception::MicrophoneAudioDriver::produce_diagnostics ( diagnostic_updater::DiagnosticStatusWrapper &  status)
inlineprotected

◆ read_samples_locked()

void fp_perception::MicrophoneAudioDriver::read_samples_locked ( int16_t *  destination,
size_t  sample_count 
)
inlineprotected

◆ readChunk()

audio_data fp_perception::MicrophoneAudioDriver::readChunk ( )
inlineoverridevirtual

Get latest audio data from the driver as a stream. This function reads audio data from the microphone stream and returns it as a fp_perception::audio_data. Best approach is to use this to get a certain amount of audio data by repeatedly calling this function until enough data is collected.

Returns
fp_perception::audio_data The latest audio chunk(s) from the driver.
Exceptions
fp_perception_exceptionif the stream is not active

Implements fp_perception::AudioSourceDriver.

◆ resample_linear_interleaved()

static std::vector< int16_t > fp_perception::MicrophoneAudioDriver::resample_linear_interleaved ( const std::vector< int16_t > &  input,
int  channels,
int  input_rate,
int  output_rate 
)
inlinestaticprotected

◆ setup_diagnostics()

void fp_perception::MicrophoneAudioDriver::setup_diagnostics ( )
inlineprotected

◆ test()

void fp_perception::MicrophoneAudioDriver::test ( )
inlineoverridevirtual

Test the driver.

This function is used to test the driver functionality. It can be overridden in derived classes.

Reimplemented from fp_perception::DriverBase.

◆ write_samples_locked()

void fp_perception::MicrophoneAudioDriver::write_samples_locked ( const int16_t *  samples,
size_t  sample_count 
)
inlineprotected

Member Data Documentation

◆ audio_buffer_

std::vector<int16_t> fp_perception::MicrophoneAudioDriver::audio_buffer_
protected

◆ buffer_size_

int fp_perception::MicrophoneAudioDriver::buffer_size_ { 0 }
protected

◆ buffered_sample_count_

size_t fp_perception::MicrophoneAudioDriver::buffered_sample_count_ { 0 }
protected

◆ capture_buffer_window_

int fp_perception::MicrophoneAudioDriver::capture_buffer_window_
protected

◆ capture_sample_rate_

int fp_perception::MicrophoneAudioDriver::capture_sample_rate_ { 0 }
protected

◆ channels_

int fp_perception::MicrophoneAudioDriver::channels_
protected

◆ chunk_size_

unsigned long fp_perception::MicrophoneAudioDriver::chunk_size_
protected

◆ device_id_

int fp_perception::MicrophoneAudioDriver::device_id_
protected

◆ device_name_

std::string fp_perception::MicrophoneAudioDriver::device_name_
protected

◆ dropped_callback_chunks_

std::atomic<uint64_t> fp_perception::MicrophoneAudioDriver::dropped_callback_chunks_ { 0 }
protected

◆ err

PaError fp_perception::MicrophoneAudioDriver::err = paNoError
protected

◆ input_overflow_count_

std::atomic<uint64_t> fp_perception::MicrophoneAudioDriver::input_overflow_count_ { 0 }
protected

◆ read_index_

size_t fp_perception::MicrophoneAudioDriver::read_index_ { 0 }
protected

◆ sample_rate_

int fp_perception::MicrophoneAudioDriver::sample_rate_
protected

◆ stream_

PaStream* fp_perception::MicrophoneAudioDriver::stream_
protected

◆ timeout_sec_

int fp_perception::MicrophoneAudioDriver::timeout_sec_ { -1 }
protected

◆ write_index_

size_t fp_perception::MicrophoneAudioDriver::write_index_ { 0 }
protected

The documentation for this class was generated from the following file: