YARP
Yet Another Robot Platform
PortAudioRecorderDeviceDriver.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #ifndef PortAudioRecorderDeviceDriverh
20 #define PortAudioRecorderDeviceDriverh
21 
22 #include <yarp/os/ManagedBytes.h>
23 #include <yarp/os/Thread.h>
24 
25 #include <yarp/dev/DeviceDriver.h>
28 #include <portaudio.h>
29 #include <mutex>
30 
31 #define DEFAULT_SAMPLE_RATE (44100)
32 #define DEFAULT_NUM_CHANNELS (2)
33 #define DEFAULT_DITHER_FLAG (0)
34 #define DEFAULT_FRAMES_PER_BUFFER (512)
35 //#define DEFAULT_FRAMES_PER_BUFFER (1024)
36 
37 
39 {
40 public:
41  size_t cfg_rate = 0;
42  size_t cfg_samples = 0;
43  size_t cfg_recChannels = 0;
45 };
46 
50  public yarp::os::Thread
51 {
52 private:
53  PaStreamParameters m_inputParameters;
54  PaStream* m_stream;
55  PaError m_err;
56  yarp::dev::CircularAudioBuffer_16t* m_recDataBuffer;
58  std::mutex m_mutex;
59  bool m_isRecording;
60 
61 public:
67 
69 
70  bool open(yarp::os::Searchable& config) override;
71 
86 
87  bool close() override;
88  bool getSound(yarp::sig::Sound& sound, size_t min_number_of_samples, size_t max_number_of_samples, double max_samples_timeout_s) override;
89  bool startRecording() override;
90  bool stopRecording() override;
91 
94  bool resetRecordingAudioBuffer() override;
95 
96  void threadRelease() override;
97  bool threadInit() override;
98  void run() override;
99 
100 protected:
102 
104  void handleError();
105 };
106 
107 
118 #endif
bool startRecording() override
Start the recording.
bool getRecordingAudioBufferCurrentSize(yarp::dev::AudioBufferSize &size) override
PortAudioRecorderDeviceDriver(const PortAudioRecorderDeviceDriver &)=delete
void run() override
Main body of the new thread.
bool threadInit() override
Initialization method.
bool getRecordingAudioBufferMaxSize(yarp::dev::AudioBufferSize &size) override
void threadRelease() override
Release method.
PortAudioRecorderDeviceDriverSettings m_driverConfig
PortAudioRecorderDeviceDriver(PortAudioRecorderDeviceDriver &&)=delete
bool close() override
Close the DeviceDriver.
PortAudioRecorderDeviceDriver & operator=(const PortAudioRecorderDeviceDriver &)=delete
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 stopRecording() override
Stop the recording.
PortAudioRecorderDeviceDriver & operator=(PortAudioRecorderDeviceDriver &&)=delete
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
Interface implemented by all device drivers.
Definition: DeviceDriver.h:38
Read a YARP-format sound block from a device.
A base class for nested structures that can be searched.
Definition: Searchable.h:69
An abstraction for a thread of execution.
Definition: Thread.h:25
Class for storing sounds.
Definition: Sound.h:28