23 err = Pa_Initialize();
25 throw std::runtime_error(
"Error PortAudio initialize!\n");
26 initInputParameters();
27 initOutputParameters();
34 Pa_CloseStream(_inputStream);
35 Pa_CloseStream(_outputStream);
43void PortAudio::initInputParameters()
45 PRETTY_INFO <<
"Initialising input parameters" << std::endl;
48 _inputParameters.device = Pa_GetDefaultInputDevice();
49 if (_inputParameters.device == paNoDevice)
50 throw std::runtime_error(
"Error: No default input device.\n");
51 _inputParameters.channelCount = 1;
52 _inputParameters.sampleFormat = paFloat32;
53 _inputParameters.suggestedLatency = Pa_GetDeviceInfo(_inputParameters.device)->defaultLowInputLatency;
54 _inputParameters.hostApiSpecificStreamInfo = NULL;
67 throw std::runtime_error(
"Cannot open input Stream\n");
68 PRETTY_INFO <<
"Input parameters initialised" << std::endl;
74void PortAudio::initOutputParameters()
77 PRETTY_INFO <<
"Initialising output parameters" << std::endl;
80 _outputParameters.device = Pa_GetDefaultOutputDevice();
81 if (_outputParameters.device == paNoDevice)
82 throw std::runtime_error(
"Error: No default output device.\n");
83 _outputParameters.channelCount = 1;
84 _outputParameters.sampleFormat = paFloat32;
85 _outputParameters.suggestedLatency = Pa_GetDeviceInfo(_outputParameters.device)->defaultLowOutputLatency;
86 _outputParameters.hostApiSpecificStreamInfo = NULL;
99 throw std::runtime_error(
"Cannot open input Stream\n");
100 PRETTY_INFO <<
"Output parameters initialised" << std::endl;
110 err = Pa_StartStream(_outputStream);
111 if (err != paNoError) {
112 throw std::runtime_error(
"Unable to start output stream\n");
123 PaError err = Pa_StopStream(_outputStream);
124 if (err != paStreamIsStopped && err != paNoError) {
125 throw std::runtime_error(
"Unable to stop output stream\n");
137 err = Pa_StartStream(_inputStream);
138 if (err != paNoError) {
139 throw std::runtime_error(
"Unable to start input stream\n");
150 PaError err = Pa_StopStream(_inputStream);
151 if (err != paStreamIsStopped && err != paNoError) {
152 throw std::runtime_error(
"Unable to stop input stream\n");
167 if (packetSize > (
int)_inputBuffer.size())
170 for (
int i = 0; i != packetSize; i++) {
171 soundVector.push_back(_inputBuffer[i]);
173 for (
int i = 0; i != packetSize; i++)
174 _inputBuffer.pop_front();
176 PRETTY_INFO <<
"Sound size is " << soundVector.size() << std::endl;
189 for (
size_t i = 0; i != soundVector.size(); i++) {
190 _outputBuffer.push_back(soundVector[i]);
202 _inputBuffer.clear();
203 _outputBuffer.clear();
Declares the PortAudio class for handling audio playback and recording using PortAudio.
#define PRETTY_INFO
Info log with details and colour.
void play()
Start audio playback.
~PortAudio()
Destroy the PortAudio object.
void setPlaySound(const std::vector< float > &soundVector)
Set the sound data to be played.
void stopPlay()
Stop audio playback.
bool getSound(std::vector< float > &soundVector, int packetSize)
Get recorded sound data.
void record()
Start audio recording.
PortAudio()
Construct a new PortAudio object.
void resetBuffers()
Reset the input and output buffers.
void stopRecord()
Stop audio recording.