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 
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() 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();
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();
139 };
140 
141 
152 #endif
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 startPlayback() override
Start the playback.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
bool stopRecording() override
Stop the recording.
bool getPlaybackAudioBufferCurrentSize(yarp::dev::AudioBufferSize &size) override
PortAudioDeviceDriverSettings m_driverConfig
bool resetRecordingAudioBuffer() override
bool getPlaybackAudioBufferMaxSize(yarp::dev::AudioBufferSize &size) override
bool startRecording() override
Start the recording.
bool getRecordingAudioBufferCurrentSize(yarp::dev::AudioBufferSize &size) override
bool resetPlaybackAudioBuffer() override
bool renderSound(const yarp::sig::Sound &sound) override
Render a sound using a device (i.e.
bool appendSound(const yarp::sig::Sound &sound)
enum PortAudioDeviceDriver::@90 renderMode
bool close() override
Close the DeviceDriver.
bool stopPlayback() override
Stop the playback.
bool immediateSound(const yarp::sig::Sound &sound)
bool getRecordingAudioBufferMaxSize(yarp::dev::AudioBufferSize &size) override
void run() override
Main body of the new thread.
bool threadInit() override
Initialization method.
void threadRelease() override
Release method.
Interface implemented by deprecated device drivers.
Definition: DeviceDriver.h:119
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