YARP
Yet Another Robot Platform
PortAudioPlayerDeviceDriver.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 PortAudioPlayerDeviceDriverh
20 #define PortAudioPlayerDeviceDriverh
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_playChannels = 0;
45 };
46 
48 {
49 public:
51 
52  void threadRelease() override;
53  bool threadInit() override;
54  void run() override;
55 
57  PaStream* stream;
58 
59 private:
60  PaError err;
61  void handleError();
62 };
63 
67 {
68 private:
69  PaStreamParameters m_outputParameters;
70  PaStream* m_stream;
71  PaError m_err;
72  yarp::dev::CircularAudioBuffer_16t* m_playDataBuffer;
74  PlayStreamThread m_pThread;
75  std::mutex m_mutex;
76 
77 public:
83 
85 
86  bool open(yarp::os::Searchable& config) override;
87 
102 
103  bool close() override;
104  bool renderSound(const yarp::sig::Sound& sound) override;
105  bool startPlayback() override;
106  bool stopPlayback() override;
107 
108  bool abortSound();
109  bool immediateSound(const yarp::sig::Sound& sound);
110  bool appendSound(const yarp::sig::Sound& sound);
111 
114  bool resetPlaybackAudioBuffer() override;
115 
116 protected:
118 
121  void handleError();
122 };
123 
124 
135 #endif
void run() override
Main body of the new thread.
bool threadInit() override
Initialization method.
void threadRelease() override
Release method.
bool getPlaybackAudioBufferCurrentSize(yarp::dev::AudioBufferSize &size) override
bool appendSound(const yarp::sig::Sound &sound)
PortAudioPlayerDeviceDriver(PortAudioPlayerDeviceDriver &&)=delete
PortAudioPlayerDeviceDriver & operator=(PortAudioPlayerDeviceDriver &&)=delete
bool stopPlayback() override
Stop the playback.
enum PortAudioPlayerDeviceDriver::@91 renderMode
PortAudioPlayerDeviceDriver & operator=(const PortAudioPlayerDeviceDriver &)=delete
bool getPlaybackAudioBufferMaxSize(yarp::dev::AudioBufferSize &size) override
PortAudioPlayerDeviceDriver(const PortAudioPlayerDeviceDriver &)=delete
bool renderSound(const yarp::sig::Sound &sound) override
Render a sound using a device (i.e.
bool close() override
Close the DeviceDriver.
bool startPlayback() override
Start the playback.
PortAudioPlayerDeviceDriverSettings m_driverConfig
bool immediateSound(const yarp::sig::Sound &sound)
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
Interface implemented by all device drivers.
Definition: DeviceDriver.h:38
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