20 #define SLEEP_TIME 0.005f
23 #define PA_SAMPLE_TYPE paFloat32
25 #define SAMPLE_SILENCE (0.0f)
27 #define PA_SAMPLE_TYPE paInt16
29 #define SAMPLE_SILENCE (0)
31 #define PA_SAMPLE_TYPE paInt8
33 #define SAMPLE_SILENCE (0)
35 #define PA_SAMPLE_TYPE paUInt8
36 typedef unsigned char SAMPLE;
37 #define SAMPLE_SILENCE (128)
38 #define SAMPLE_UNSIGNED
45 #define DEFAULT_FRAMES_PER_BUFFER (512)
52 unsigned long framesPerBuffer,
53 const PaStreamCallbackTimeInfo* timeInfo,
54 PaStreamCallbackFlags statusFlags,
59 int finished = paComplete;
63 const auto* rptr = (
const SAMPLE*)inputBuffer;
73 if( framesLeft/ num_rec_channels < framesPerBuffer )
75 framesToCalc = framesLeft/ num_rec_channels;
76 #ifdef STOP_REC_ON_EMPTY_BUFFER
80 finished = paComplete;
82 finished = paContinue;
87 framesToCalc = framesPerBuffer;
90 finished = paContinue;
93 if( inputBuffer ==
nullptr )
95 for(
size_t i=0; i<framesToCalc; i++ )
97 for (
size_t j=0; j < num_rec_channels; j++)
105 for(
size_t i=0; i<framesToCalc; i++ )
107 for (
size_t j = 0; j < num_rec_channels; j++)
109 recdata->
write(*rptr++);
116 yCError(PORTAUDIORECORDER,
"No write operations requested, aborting");
123 m_system_resource(nullptr)
125 memset(&m_inputParameters, 0,
sizeof(PaStreamParameters));
136 if (config.
check(
"help"))
138 yCInfo(PORTAUDIORECORDER,
"Some examples:");
139 yCInfo(PORTAUDIORECORDER,
"yarpdev --device portaudioRecorder --help");
140 yCInfo(PORTAUDIORECORDER,
"yarpdev --device AudioRecorderWrapper --subdevice portaudioRecorder --start");
145 if (!b) {
return false; }
147 m_device_id = config.
check(
"id",
Value(-1),
"which portaudio index to use (-1=automatic)").asInt32();
153 m_err = Pa_Initialize();
154 if(m_err != paNoError )
156 yCError(PORTAUDIORECORDER,
"portaudio system failed to initialize");
161 yCInfo(PORTAUDIORECORDER,
"Device number %d", m_inputParameters.device);
164 if ((Pa_GetDeviceInfo(m_inputParameters.device ))!=
nullptr) {
165 m_inputParameters.suggestedLatency = Pa_GetDeviceInfo(m_inputParameters.device )->defaultLowInputLatency;
167 m_inputParameters.hostApiSpecificStreamInfo =
nullptr;
169 m_err = Pa_OpenStream(
179 if(m_err != paNoError )
181 yCError(PORTAUDIORECORDER,
"An error occurred while using the portaudio stream" );
182 yCError(PORTAUDIORECORDER,
"Error number: %d", m_err );
183 yCError(PORTAUDIORECORDER,
"Error message: %s", Pa_GetErrorText(m_err ) );
190 return (m_err==paNoError);
198 if(m_err != paNoError )
200 yCError(PORTAUDIORECORDER,
"An error occurred while using the portaudio stream" );
201 yCError(PORTAUDIORECORDER,
"Error number: %d", m_err );
202 yCError(PORTAUDIORECORDER,
"Error message: %s", Pa_GetErrorText(m_err ) );
209 if (m_stream !=
nullptr)
211 m_err = Pa_CloseStream(m_stream );
212 if(m_err != paNoError )
214 yCError(PORTAUDIORECORDER,
"An error occurred while closing the portaudio stream" );
215 yCError(PORTAUDIORECORDER,
"Error number: %d", m_err );
216 yCError(PORTAUDIORECORDER,
"Error message: %s", Pa_GetErrorText(m_err ) );
226 return (m_err==paNoError);
231 AudioRecorderDeviceBase::startRecording();
232 m_err = Pa_StartStream(m_stream );
234 yCInfo(PORTAUDIORECORDER) <<
"started recording";
240 yCInfo(PORTAUDIORECORDER) <<
"not yet implemented recording";
246 AudioRecorderDeviceBase::stopRecording();
247 m_err = Pa_StopStream(m_stream );
249 yCInfo(PORTAUDIORECORDER) <<
"stopped recording";
270 m_err = Pa_IsStreamActive(m_stream);
274 yCError(PORTAUDIORECORDER) <<
"Unhandled error. Calling abortSound()";
#define DEFAULT_FRAMES_PER_BUFFER
static int bufferIOCallback(const void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo *timeInfo, PaStreamCallbackFlags statusFlags, void *userData)
bool startRecording() override
Start the recording.
void run() override
Main body of the new thread.
bool threadInit() override
Initialization method.
bool setHWGain(double gain) override
Sets the hardware gain of the grabbing device (if supported by the hardware)
void threadRelease() override
Release method.
bool close() override
Close the DeviceDriver.
~PortAudioRecorderDeviceDriver() override
bool stopRecording() override
Stop the recording.
PortAudioRecorderDeviceDriver()
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
yarp::dev::CircularAudioBuffer_16t * m_inputBuffer
bool configureRecorderAudioDevice(yarp::os::Searchable &config, std::string device_name)
AudioDeviceDriverSettings m_audiorecorder_cfg
yarp::dev::AudioBufferSize getMaxSize()
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.
virtual Bottle & findGroup(const std::string &key) const =0
Gets a list corresponding to a given keyword.
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).
#define yCInfo(component,...)
#define yCError(component,...)
#define YARP_LOG_COMPONENT(name,...)
An interface for the device drivers.
void delay(double seconds)
Wait for a certain number of seconds.
An interface to the operating system, including Port based communication.