YARP
Yet Another Robot Platform
PortAudioDeviceDriver.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 PortAudioDeviceDriverh
20 #define PortAudioDeviceDriverh
21 
22 #include <yarp/os/ManagedBytes.h>
23 #include <yarp/os/Thread.h>
24 
25 #include <yarp/dev/DeviceDriver.h>
27 #include <portaudio.h>
28 #include "PortAudioBuffer.h"
29 
30 #define DEFAULT_SAMPLE_RATE (44100)
31 #define DEFAULT_NUM_CHANNELS (2)
32 #define DEFAULT_DITHER_FLAG (0)
33 #define DEFAULT_FRAMES_PER_BUFFER (512)
34 //#define DEFAULT_FRAMES_PER_BUFFER (1024)
35 
36 
38 {
39 public:
40  int rate;
41  int samples;
44  bool wantRead;
45  bool wantWrite;
47 };
48 
49 class streamThread :
50  public yarp::os::Thread
51 {
52  public:
55  PaStream* stream;
56  void threadRelease() override;
57  bool threadInit() override;
58  void run() override;
59 
60  private:
61  PaError err;
62  void handleError(void);
63 };
64 
69 {
70 private:
71  PaStreamParameters inputParameters;
72  PaStreamParameters outputParameters;
73  PaStream* stream;
74  PaError err;
75  circularDataBuffers dataBuffers;
76  size_t numSamples;
77  size_t numBytes;
78  streamThread pThread;
79 
81 
82 public:
84 
85  virtual ~PortAudioDeviceDriver();
86 
87  bool open(yarp::os::Searchable& config) override;
88 
106  bool open(PortAudioDeviceDriverSettings& config);
107 
108  bool close(void) override;
109  bool getSound(yarp::sig::Sound& sound, size_t min_number_of_samples, size_t max_number_of_samples, double max_samples_timeout_s) override;
110  bool renderSound(const yarp::sig::Sound& sound) override;
111  bool startRecording() override;
112  bool stopRecording() override;
113  bool startPlayback() override;
114  bool stopPlayback() override;
115 
116  bool abortSound(void);
117  bool immediateSound(const yarp::sig::Sound& sound);
118  bool appendSound(const yarp::sig::Sound& sound);
119 
122  bool resetPlaybackAudioBuffer() override;
123 
126  bool resetRecordingAudioBuffer() override;
127 
128 protected:
135 
138  void handleError(void);
139 };
140 
141 
152 #endif
streamThread::stream
PaStream * stream
Definition: PortAudioDeviceDriver.h:55
streamThread::run
void run() override
Main body of the new thread.
Definition: PortAudioDeviceDriver.cpp:477
PortAudioDeviceDriverSettings::recChannels
int recChannels
Definition: PortAudioDeviceDriver.h:43
PortAudioDeviceDriver::stopPlayback
bool stopPlayback() override
Stop the playback.
Definition: PortAudioDeviceDriver.cpp:643
PortAudioDeviceDriver::renderMode
enum PortAudioDeviceDriver::@90 renderMode
PortAudioDeviceDriver::startPlayback
bool startPlayback() override
Start the playback.
Definition: PortAudioDeviceDriver.cpp:637
yarp::os::Searchable
A base class for nested structures that can be searched.
Definition: Searchable.h:68
PortAudioDeviceDriver::m_system_resource
void * m_system_resource
Definition: PortAudioDeviceDriver.h:129
PortAudioDeviceDriver::handleError
void handleError(void)
Definition: PortAudioDeviceDriver.cpp:340
PortAudioDeviceDriverSettings
Definition: PortAudioDeviceDriver.h:37
yarp::dev::AudioBufferSize
Definition: AudioBufferSize.h:24
PortAudioDeviceDriver::m_getSoundIsNotBlocking
bool m_getSoundIsNotBlocking
Definition: PortAudioDeviceDriver.h:134
PortAudioDeviceDriver::resetRecordingAudioBuffer
bool resetRecordingAudioBuffer() override
Definition: PortAudioDeviceDriver.cpp:631
PortAudioDeviceDriver::m_driverConfig
PortAudioDeviceDriverSettings m_driverConfig
Definition: PortAudioDeviceDriver.h:136
PortAudioDeviceDriver::m_numRecordChannels
size_t m_numRecordChannels
Definition: PortAudioDeviceDriver.h:131
PortAudioBuffer.h
PortAudioDeviceDriverSettings::playChannels
int playChannels
Definition: PortAudioDeviceDriver.h:42
PortAudioDeviceDriver::~PortAudioDeviceDriver
virtual ~PortAudioDeviceDriver()
Definition: PortAudioDeviceDriver.cpp:207
PortAudioDeviceDriver::RENDER_IMMEDIATE
@ RENDER_IMMEDIATE
Definition: PortAudioDeviceDriver.h:137
AudioGrabberInterfaces.h
yarp::os::Thread
Definition: Thread.h:24
PortAudioDeviceDriver::startRecording
bool startRecording() override
Start the recording.
Definition: PortAudioDeviceDriver.cpp:381
yarp::dev::IAudioRender
Definition: IAudioRender.h:20
PortAudioDeviceDriverSettings::deviceNumber
int deviceNumber
Definition: PortAudioDeviceDriver.h:46
PortAudioDeviceDriver::getSound
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.
Definition: PortAudioDeviceDriver.cpp:397
PortAudioDeviceDriver::stopRecording
bool stopRecording() override
Stop the recording.
Definition: PortAudioDeviceDriver.cpp:389
yarp::dev::IAudioGrabberSound
Definition: IAudioGrabberSound.h:25
ManagedBytes.h
PortAudioDeviceDriver::abortSound
bool abortSound(void)
Definition: PortAudioDeviceDriver.cpp:449
streamThread::something_to_play
bool something_to_play
Definition: PortAudioDeviceDriver.h:53
PortAudioDeviceDriver::m_loopBack
bool m_loopBack
Definition: PortAudioDeviceDriver.h:133
PortAudioDeviceDriver::getPlaybackAudioBufferMaxSize
bool getPlaybackAudioBufferMaxSize(yarp::dev::AudioBufferSize &size) override
Definition: PortAudioDeviceDriver.cpp:607
PortAudioDeviceDriver::PortAudioDeviceDriver
PortAudioDeviceDriver()
Definition: PortAudioDeviceDriver.cpp:189
PortAudioDeviceDriver::getRecordingAudioBufferCurrentSize
bool getRecordingAudioBufferCurrentSize(yarp::dev::AudioBufferSize &size) override
Definition: PortAudioDeviceDriver.cpp:619
Thread.h
PortAudioDeviceDriver::resetPlaybackAudioBuffer
bool resetPlaybackAudioBuffer() override
Definition: PortAudioDeviceDriver.cpp:613
PortAudioDeviceDriver
Definition: PortAudioDeviceDriver.h:65
PortAudioDeviceDriverSettings::rate
int rate
Definition: PortAudioDeviceDriver.h:40
PortAudioDeviceDriver::getPlaybackAudioBufferCurrentSize
bool getPlaybackAudioBufferCurrentSize(yarp::dev::AudioBufferSize &size) override
Definition: PortAudioDeviceDriver.cpp:601
PortAudioDeviceDriver::renderSound
bool renderSound(const yarp::sig::Sound &sound) override
Render a sound using a device (i.e.
Definition: PortAudioDeviceDriver.cpp:551
PortAudioDeviceDriver::immediateSound
bool immediateSound(const yarp::sig::Sound &sound)
Definition: PortAudioDeviceDriver.cpp:535
PortAudioDeviceDriver::close
bool close(void) override
Close the DeviceDriver.
Definition: PortAudioDeviceDriver.cpp:353
PortAudioDeviceDriver::m_numPlaybackChannels
size_t m_numPlaybackChannels
Definition: PortAudioDeviceDriver.h:130
yarp::dev::DeprecatedDeviceDriver
Definition: DeviceDriver.h:118
yarp::sig::Sound
Definition: Sound.h:27
PortAudioDeviceDriverSettings::samples
int samples
Definition: PortAudioDeviceDriver.h:41
PortAudioDeviceDriver::m_frequency
int m_frequency
Definition: PortAudioDeviceDriver.h:132
PortAudioDeviceDriverSettings::wantWrite
bool wantWrite
Definition: PortAudioDeviceDriver.h:45
PortAudioDeviceDriver::appendSound
bool appendSound(const yarp::sig::Sound &sound)
Definition: PortAudioDeviceDriver.cpp:587
PortAudioDeviceDriver::getRecordingAudioBufferMaxSize
bool getRecordingAudioBufferMaxSize(yarp::dev::AudioBufferSize &size) override
Definition: PortAudioDeviceDriver.cpp:625
streamThread::threadRelease
void threadRelease() override
Release method.
Definition: PortAudioDeviceDriver.cpp:465
streamThread::threadInit
bool threadInit() override
Initialization method.
Definition: PortAudioDeviceDriver.cpp:469
circularDataBuffers
Definition: PortAudioBuffer.h:28
PortAudioDeviceDriverSettings::wantRead
bool wantRead
Definition: PortAudioDeviceDriver.h:44
PortAudioDeviceDriver::RENDER_APPEND
@ RENDER_APPEND
Definition: PortAudioDeviceDriver.h:137
streamThread
Definition: PortAudioDeviceDriver.h:49
streamThread::something_to_record
bool something_to_record
Definition: PortAudioDeviceDriver.h:54
DeviceDriver.h
PortAudioDeviceDriver::open
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
Definition: PortAudioDeviceDriver.cpp:213