33 #define SLEEP_TIME 0.005f
36 #define PA_SAMPLE_TYPE paFloat32
38 #define SAMPLE_SILENCE (0.0f)
40 #define PA_SAMPLE_TYPE paInt16
42 #define SAMPLE_SILENCE (0)
44 #define PA_SAMPLE_TYPE paInt8
46 #define SAMPLE_SILENCE (0)
48 #define PA_SAMPLE_TYPE paUInt8
49 typedef unsigned char SAMPLE;
50 #define SAMPLE_SILENCE (128)
51 #define SAMPLE_UNSIGNED
54 #define DEFAULT_SAMPLE_RATE (44100)
55 #define DEFAULT_NUM_CHANNELS (2)
56 #define DEFAULT_DITHER_FLAG (0)
57 #define DEFAULT_FRAMES_PER_BUFFER (512)
69 unsigned long framesPerBuffer,
70 const PaStreamCallbackTimeInfo* timeInfo,
71 PaStreamCallbackFlags statusFlags,
76 int finished = paComplete;
80 const auto* rptr = (
const SAMPLE*)inputBuffer;
91 if( framesLeft/ num_rec_channels < framesPerBuffer )
93 framesToCalc = framesLeft/ num_rec_channels;
94 #ifdef STOP_REC_ON_EMPTY_BUFFER
98 finished = paComplete;
100 finished = paContinue;
105 framesToCalc = framesPerBuffer;
108 finished = paContinue;
111 if( inputBuffer ==
nullptr )
113 for( i=0; i<framesToCalc; i++ )
116 if(num_rec_channels == 2 ) recdata->
write(0);
122 yCDebug(PORTAUDIORECORDER) <<
"Writing" << framesToCalc*2*2 <<
"bytes in the circular buffer";
124 for( i=0; i<framesToCalc; i++ )
126 recdata->
write(*rptr++);
127 if(num_rec_channels == 2 ) recdata->
write(*rptr++);
133 yCError(PORTAUDIORECORDER,
"No write operations requested, aborting");
140 m_system_resource(nullptr)
142 memset(&m_inputParameters, 0,
sizeof(PaStreamParameters));
154 m_audiorecorder_cfg.
numSamples = 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();
157 int driver_frame_size = config.
check(
"driver_frame_size",
Value(0),
"" ).asInt32();
169 m_err = Pa_Initialize();
170 if(m_err != paNoError )
172 yCError(PORTAUDIORECORDER,
"portaudio system failed to initialize");
177 yCInfo(PORTAUDIORECORDER,
"Device number %d", m_inputParameters.device);
180 if ((Pa_GetDeviceInfo(m_inputParameters.device ))!=
nullptr) {
181 m_inputParameters.suggestedLatency = Pa_GetDeviceInfo(m_inputParameters.device )->defaultLowInputLatency;
183 m_inputParameters.hostApiSpecificStreamInfo =
nullptr;
185 m_err = Pa_OpenStream(
195 if(m_err != paNoError )
197 yCError(PORTAUDIORECORDER,
"An error occurred while using the portaudio stream" );
198 yCError(PORTAUDIORECORDER,
"Error number: %d", m_err );
199 yCError(PORTAUDIORECORDER,
"Error message: %s", Pa_GetErrorText(m_err ) );
206 return (m_err==paNoError);
214 if(m_err != paNoError )
216 yCError(PORTAUDIORECORDER,
"An error occurred while using the portaudio stream" );
217 yCError(PORTAUDIORECORDER,
"Error number: %d", m_err );
218 yCError(PORTAUDIORECORDER,
"Error message: %s", Pa_GetErrorText(m_err ) );
225 if (m_stream !=
nullptr)
227 m_err = Pa_CloseStream(m_stream );
228 if(m_err != paNoError )
230 yCError(PORTAUDIORECORDER,
"An error occurred while closing the portaudio stream" );
231 yCError(PORTAUDIORECORDER,
"Error number: %d", m_err );
232 yCError(PORTAUDIORECORDER,
"Error message: %s", Pa_GetErrorText(m_err ) );
242 return (m_err==paNoError);
247 AudioRecorderDeviceBase::startRecording();
248 m_err = Pa_StartStream(m_stream );
250 yCInfo(PORTAUDIORECORDER) <<
"PortAudioRecorderDeviceDriver started recording";
256 AudioRecorderDeviceBase::stopRecording();
257 m_err = Pa_StopStream(m_stream );
259 yCInfo(PORTAUDIORECORDER) <<
"PortAudioRecorderDeviceDriver stopped recording";
279 while( ( m_err = Pa_IsStreamActive(m_stream) ) == 1 )
286 Pa_StopStream(m_stream);
287 yCDebug(PORTAUDIORECORDER) <<
"The recording stream has been stopped";