23#define SLEEP_TIME 0.005f
26#define PA_SAMPLE_TYPE paFloat32
28#define SAMPLE_SILENCE (0.0f)
30#define PA_SAMPLE_TYPE paInt16
32#define SAMPLE_SILENCE (0)
34#define PA_SAMPLE_TYPE paInt8
36#define SAMPLE_SILENCE (0)
38#define PA_SAMPLE_TYPE paUInt8
39typedef unsigned char SAMPLE;
40#define SAMPLE_SILENCE (128)
41#define SAMPLE_UNSIGNED
65 if (dataBuffers->canRec)
81#ifdef STOP_REC_ON_EMPTY_BUFFER
123 if (dataBuffers->canPlay)
155#ifdef STOP_PLAY_ON_EMPTY_BUFFER
196 m_system_resource(nullptr),
197 m_numPlaybackChannels(0),
198 m_numRecordChannels(0),
201 m_getSoundIsNotBlocking(
true),
202 renderMode(RENDER_APPEND)
218 m_driverConfig.
samples = config.
check(
"samples",
Value(0),
"number of samples per network packet (0=automatic). For chunks of 1 second of recording set samples=rate. Channels number is handled internally.").asInt32();
230 if (config.
check(
"loopback",
"if present, send audio read from microphone immediately back to speaker"))
236 if (config.
check(
"render_mode_append"))
240 if (config.
check(
"render_mode_immediate"))
250 int rate = config.
rate;
258 if (playChannels == 0) {
261 if (recChannels == 0) {
275 numSamples = samples;
285 if (dataBuffers.
playData ==
nullptr) {
288 if (dataBuffers.
recData ==
nullptr) {
292 dataBuffers.
canRec =
true;
310 inputParameters.suggestedLatency =
Pa_GetDeviceInfo( inputParameters.device )->defaultLowInputLatency;
312 inputParameters.hostApiSpecificStreamInfo =
nullptr;
317 outputParameters.suggestedLatency =
Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
318 outputParameters.hostApiSpecificStreamInfo =
nullptr;
322 wantRead?(&inputParameters):
nullptr,
323 wantWrite?(&outputParameters):
nullptr,
344void streamThread::handleError()
371 if (stream !=
nullptr)
382 if (this->dataBuffers.
playData !=
nullptr)
385 this->dataBuffers.
playData =
nullptr;
387 if (this->dataBuffers.
recData !=
nullptr)
389 delete this->dataBuffers.
recData;
390 this->dataBuffers.
recData =
nullptr;
429 yCError(
PORTAUDIO) <<
"PortAudioDeviceDriver::getSound() Buffer size is still zero after 100 iterations, returning";
434 yCDebug(
PORTAUDIO) <<
"PortAudioDeviceDriver::getSound() Buffer size is " <<
buff_size <<
"/" << this->numSamples <<
" after 100 iterations";
437 yCError(
PORTAUDIO) <<
"PortAudioDeviceDriver::getSound() is in not-blocking mode, returning";
455 for (
size_t i = 0;
i < this->numSamples;
i++) {
471 yCError(
PORTAUDIO,
"abortSound: error occurred while stopping the portaudio stream" );
501 if( err !=
paNoError ) {handleError();
return;}
583 yCInfo(
PORTAUDIO,
"***** audio driver configuration changed, resetting");
585 chans <<
"channels, " <<
freq <<
" Hz";
592 yCError(
PORTAUDIO,
"error occurred during audio driver reconfiguration, aborting");
static int bufferIOCallback(const void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo *timeInfo, PaStreamCallbackFlags statusFlags, void *userData)
#define DEFAULT_FRAMES_PER_BUFFER
#define DEFAULT_NUM_CHANNELS
#define DEFAULT_SAMPLE_RATE
bool getSound(yarp::sig::Sound &sound, size_t min_number_of_samples, size_t max_number_of_samples, double max_samples_timeout_s) override
Get a sound from a device.
bool startPlayback() override
Start the playback.
size_t m_numPlaybackChannels
bool setSWGain(double gain) override
Sets a software gain for the grabbed audio.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
bool m_getSoundIsNotBlocking
bool stopRecording() override
Stop the recording.
virtual ~PortAudioDeviceDriver()
bool isPlaying(bool &playback_enabled) override
Check if the playback has been enabled (e.g.
PortAudioDeviceDriverSettings m_driverConfig
bool resetRecordingAudioBuffer() override
bool setHWGain(double gain) override
Sets the hardware gain of the grabbing device (if supported by the hardware)
bool startRecording() override
Start the recording.
bool resetPlaybackAudioBuffer() override
bool renderSound(const yarp::sig::Sound &sound) override
Render a sound using a device (i.e.
bool isRecording(bool &recording_enabled) override
Check if the recording has been enabled (e.g.
bool appendSound(const yarp::sig::Sound &sound)
bool getPlaybackAudioBufferMaxSize(yarp::sig::AudioBufferSize &size) override
enum PortAudioDeviceDriver::@90 renderMode
bool close() override
Close the DeviceDriver.
bool stopPlayback() override
Stop the playback.
bool getPlaybackAudioBufferCurrentSize(yarp::sig::AudioBufferSize &size) override
bool getRecordingAudioBufferMaxSize(yarp::sig::AudioBufferSize &size) override
bool immediateSound(const yarp::sig::Sound &sound)
bool getRecordingAudioBufferCurrentSize(yarp::sig::AudioBufferSize &size) override
size_t m_numRecordChannels
void run() override
Main body of the new thread.
bool threadInit() override
Initialization method.
void threadRelease() override
Release method.
yarp::sig::AudioBufferSize size()
yarp::sig::AudioBufferSize getMaxSize()
A mini-server for performing network communication in the background.
T * read(bool shouldWait=true) override
Read an available object from the port.
void write(bool forceStrict=false)
Write the current object being returned by BufferedPort::prepare.
A base class for nested structures that can be searched.
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
static void delaySystem(double seconds)
bool stop()
Stop the thread.
bool isStopping()
Returns true if the thread is stopping (Thread::stop has been called).
bool start()
Start the new thread running.
A single value (typically within a Bottle).
Class for storing sounds See Audio in YARP for additional documentation on YARP audio.
void setFrequency(int freq)
Set the frequency of the sound (i.e.
size_t getChannels() const
Get the number of channels of the sound.
void resize(size_t samples, size_t channels=1)
Set the sound size.
int getFrequency() const
Get the frequency of the sound (i.e.
audio_sample get(size_t sample, size_t channel=0) const
void set(audio_sample value, size_t sample, size_t channel=0)
size_t getSamples() const
Get the number of samples contained in the sound.
#define yCInfo(component,...)
#define yCError(component,...)
#define yCTrace(component,...)
#define yCDebug(component,...)
#define YARP_LOG_COMPONENT(name,...)
For streams capable of holding different kinds of content, check what they actually have.
yarp::dev::CircularAudioBuffer< unsigned short int > CircularAudioBuffer_16t
void delay(double seconds)
Wait for a certain number of seconds.
An interface to the operating system, including Port based communication.
yarp::dev::CircularAudioBuffer_16t * playData
yarp::dev::CircularAudioBuffer_16t * recData