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();
63 };
64 
76 {
77 private:
78  PaStreamParameters inputParameters;
79  PaStreamParameters outputParameters;
80  PaStream* stream;
81  PaError err;
82  circularDataBuffers dataBuffers;
83  size_t numSamples;
84  size_t numBytes;
85  streamThread pThread;
86 
88 
89 public:
91 
92  virtual ~PortAudioDeviceDriver();
93 
94  bool open(yarp::os::Searchable& config) override;
95 
113  bool open(PortAudioDeviceDriverSettings& config);
114 
115  bool close() override;
116  bool getSound(yarp::sig::Sound& sound, size_t min_number_of_samples, size_t max_number_of_samples, double max_samples_timeout_s) override;
117  bool renderSound(const yarp::sig::Sound& sound) override;
118  bool startRecording() override;
119  bool stopRecording() override;
120  bool startPlayback() override;
121  bool stopPlayback() override;
122 
123  bool abortSound();
124  bool immediateSound(const yarp::sig::Sound& sound);
125  bool appendSound(const yarp::sig::Sound& sound);
126 
129  bool resetPlaybackAudioBuffer() override;
130 
133  bool resetRecordingAudioBuffer() override;
134 
135 protected:
142 
145  void handleError();
146 };
147 
148 #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::handleError
void handleError()
Definition: PortAudioDeviceDriver.cpp:340
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:69
PortAudioDeviceDriver::m_system_resource
void * m_system_resource
Definition: PortAudioDeviceDriver.h:136
PortAudioDeviceDriverSettings
Definition: PortAudioDeviceDriver.h:38
yarp::dev::AudioBufferSize
Definition: AudioBufferSize.h:26
PortAudioDeviceDriver::m_getSoundIsNotBlocking
bool m_getSoundIsNotBlocking
Definition: PortAudioDeviceDriver.h:141
PortAudioDeviceDriver::resetRecordingAudioBuffer
bool resetRecordingAudioBuffer() override
Definition: PortAudioDeviceDriver.cpp:631
PortAudioDeviceDriver::m_driverConfig
PortAudioDeviceDriverSettings m_driverConfig
Definition: PortAudioDeviceDriver.h:143
PortAudioDeviceDriver::m_numRecordChannels
size_t m_numRecordChannels
Definition: PortAudioDeviceDriver.h:138
PortAudioBuffer.h
PortAudioDeviceDriverSettings::playChannels
int playChannels
Definition: PortAudioDeviceDriver.h:42
PortAudioDeviceDriver::~PortAudioDeviceDriver
virtual ~PortAudioDeviceDriver()
Definition: PortAudioDeviceDriver.cpp:207
AudioGrabberInterfaces.h
yarp::os::Thread
An abstraction for a thread of execution.
Definition: Thread.h:25
PortAudioDeviceDriver::startRecording
bool startRecording() override
Start the recording.
Definition: PortAudioDeviceDriver.cpp:381
PortAudioDeviceDriver::abortSound
bool abortSound()
Definition: PortAudioDeviceDriver.cpp:449
yarp::dev::IAudioRender
Definition: IAudioRender.h:21
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
Read a YARP-format sound block from a device.
Definition: IAudioGrabberSound.h:26
PortAudioDeviceDriver::RENDER_APPEND
@ RENDER_APPEND
Definition: PortAudioDeviceDriver.h:144
ManagedBytes.h
streamThread::something_to_play
bool something_to_play
Definition: PortAudioDeviceDriver.h:53
PortAudioDeviceDriver::m_loopBack
bool m_loopBack
Definition: PortAudioDeviceDriver.h:140
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
PortAudioDeviceDriver::close
bool close() override
Close the DeviceDriver.
Definition: PortAudioDeviceDriver.cpp:353
Thread.h
PortAudioDeviceDriver::resetPlaybackAudioBuffer
bool resetPlaybackAudioBuffer() override
Definition: PortAudioDeviceDriver.cpp:613
PortAudioDeviceDriver
portaudio: Documentation to be added
Definition: PortAudioDeviceDriver.h:76
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::m_numPlaybackChannels
size_t m_numPlaybackChannels
Definition: PortAudioDeviceDriver.h:137
PortAudioDeviceDriver::RENDER_IMMEDIATE
@ RENDER_IMMEDIATE
Definition: PortAudioDeviceDriver.h:144
yarp::dev::DeprecatedDeviceDriver
Interface implemented by deprecated device drivers.
Definition: DeviceDriver.h:119
yarp::sig::Sound
Class for storing sounds.
Definition: Sound.h:28
PortAudioDeviceDriverSettings::samples
int samples
Definition: PortAudioDeviceDriver.h:41
PortAudioDeviceDriver::m_frequency
int m_frequency
Definition: PortAudioDeviceDriver.h:139
PortAudioDeviceDriverSettings::wantWrite
bool wantWrite
Definition: PortAudioDeviceDriver.h:45
PortAudioDeviceDriver::appendSound
bool appendSound(const yarp::sig::Sound &sound)
Definition: PortAudioDeviceDriver.cpp:587
PortAudioDeviceDriver::renderMode
enum PortAudioDeviceDriver::@0 renderMode
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:29
PortAudioDeviceDriverSettings::wantRead
bool wantRead
Definition: PortAudioDeviceDriver.h:44
streamThread
Definition: PortAudioDeviceDriver.h:51
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