Async 1.8.0
AsyncAudioDevice.h
Go to the documentation of this file.
1
28#ifndef ASYNC_AUDIO_DEVICE_INCLUDED
29#define ASYNC_AUDIO_DEVICE_INCLUDED
30
31
32/****************************************************************************
33 *
34 * System Includes
35 *
36 ****************************************************************************/
37
38#include <sigc++/sigc++.h>
39#include <stdint.h>
40
41#include <string>
42#include <map>
43#include <list>
44
45
46/****************************************************************************
47 *
48 * Project Includes
49 *
50 ****************************************************************************/
51
52#include <AsyncTimer.h>
53
54
55/****************************************************************************
56 *
57 * Local Includes
58 *
59 ****************************************************************************/
60
61
62
63/****************************************************************************
64 *
65 * Forward declarations
66 *
67 ****************************************************************************/
68
69
70
71/****************************************************************************
72 *
73 * Namespace
74 *
75 ****************************************************************************/
76
77namespace Async
78{
79
80
81/****************************************************************************
82 *
83 * Forward declarations of classes inside of the declared namespace
84 *
85 ****************************************************************************/
86
87class AudioIO;
88class FdWatch;
89
90
91/****************************************************************************
92 *
93 * Defines & typedefs
94 *
95 ****************************************************************************/
96
97
98
99/****************************************************************************
100 *
101 * Exported Global Variables
102 *
103 ****************************************************************************/
104
105
106
107/****************************************************************************
108 *
109 * Class definitions
110 *
111 ****************************************************************************/
112
123class AudioDevice : public sigc::trackable
124{
125 public:
136
148 static AudioDevice *registerAudioIO(const std::string& dev_designator,
149 AudioIO *audio_io);
150
155 static void unregisterAudioIO(AudioIO *audio_io);
156
166 static void setSampleRate(int rate) { sample_rate = rate; }
167
182 static void setBlocksize(size_t size)
183 {
184 block_size_hint = size;
185 }
186
191 virtual size_t readBlocksize(void) = 0;
192
197 virtual size_t writeBlocksize(void) = 0;
198
212 static void setBlockCount(size_t count)
213 {
214 block_count_hint = (count <= 0) ? 0 : count;
215 }
216
226 static void setChannels(size_t channels)
227 {
229 }
230
235 static size_t getChannels(void) { return channels; }
236
242 virtual bool isFullDuplexCapable(void) = 0;
243
250
254 void close(void);
255
260 Mode mode(void) const { return current_mode; }
261
266 virtual void audioToWriteAvailable(void) = 0;
267
268 /*
269 * @brief Tell the audio device to flush its buffers
270 */
271 virtual void flushSamples(void) = 0;
272
283 virtual int samplesToWrite(void) const = 0;
284
289 int sampleRate(void) const { return sample_rate; }
290
295 const std::string& devName(void) const { return dev_name; }
296
297
298 protected:
299 static int sample_rate;
300 static size_t block_size_hint;
301 static size_t block_count_hint;
302 static size_t channels;
303
304 std::string dev_name;
305
310 explicit AudioDevice(const std::string& dev_name);
311
315 virtual ~AudioDevice(void);
316
322 virtual bool openDevice(Mode mode) = 0;
323
327 virtual void closeDevice(void) = 0;
328
341 void putBlocks(int16_t *buf, size_t frame_cnt);
342
356 size_t getBlocks(int16_t *buf, size_t block_cnt);
357
361 void setDeviceError(void);
362
363 private:
364 static const int DEFAULT_SAMPLE_RATE = INTERNAL_SAMPLE_RATE;
365 static const size_t DEFAULT_CHANNELS = 2;
366 static const size_t DEFAULT_BLOCK_COUNT_HINT = 4;
367 static const size_t DEFAULT_BLOCK_SIZE_HINT = 256; // Samples/channel/block
368
369 static std::map<std::string, AudioDevice*> devices;
370
371 Mode current_mode;
372 size_t use_count;
373 std::list<AudioIO*> aios;
374 Async::Timer reopen_timer {1000, Async::Timer::TYPE_PERIODIC};
375
376 void reopenDevice(void);
377
378}; /* class AudioDevice */
379
380
381} /* namespace */
382
383#endif /* ASYNC_AUDIO_DEVICE_INCLUDED */
384
385
386
387/*
388 * This file has not been truncated
389 */
390
Contains a single shot or periodic timer that emits a signal on timeout.
Base class for handling audio devices.
int sampleRate(void) const
Return the sample rate.
virtual size_t writeBlocksize(void)=0
Find out what the write (playback) blocksize is set to.
virtual int samplesToWrite(void) const =0
Find out how many samples there are in the output buffer.
virtual void audioToWriteAvailable(void)=0
Tell the audio device handler that there are audio to be written in the buffer.
static void setChannels(size_t channels)
Set the number of channels used when doing future opens.
static AudioDevice * registerAudioIO(const std::string &dev_designator, AudioIO *audio_io)
Register an AudioIO object with the given device name.
void setDeviceError(void)
Called by the device object to indicate an error condition.
static void setBlockCount(size_t count)
Set the buffer count used when opening audio devices.
const std::string & devName(void) const
Return the device name.
bool open(Mode mode)
Open the audio device.
virtual bool openDevice(Mode mode)=0
Open the audio device.
static size_t getChannels(void)
Get the number of channels used for future opens.
virtual void closeDevice(void)=0
Close the audio device.
virtual void flushSamples(void)=0
static size_t block_count_hint
size_t getBlocks(int16_t *buf, size_t block_cnt)
Read samples from upper layers to write to audio device.
void close(void)
Close the audio device.
static void setBlocksize(size_t size)
Set the blocksize used when opening audio devices.
static void unregisterAudioIO(AudioIO *audio_io)
Unregister a previously registered AudioIO object.
static size_t block_size_hint
virtual bool isFullDuplexCapable(void)=0
Check if the audio device has full duplex capability.
AudioDevice(const std::string &dev_name)
Constuctor.
Mode
The different modes to open a device in.
@ MODE_NONE
No mode. The same as close.
@ MODE_RDWR
Both read and write.
Mode mode(void) const
Get the current operating mode of this audio device.
virtual ~AudioDevice(void)
Destructor.
virtual size_t readBlocksize(void)=0
Find out what the read (recording) blocksize is set to.
static void setSampleRate(int rate)
Set the sample rate used when doing future opens.
void putBlocks(int16_t *buf, size_t frame_cnt)
Write samples read from audio device to upper layers.
A class for handling audio input/output to an audio device.
A class that produces timer events.
Definition AsyncTimer.h:117
@ TYPE_PERIODIC
A timer that restarts itself every time it expires.
Definition AsyncTimer.h:125
Namespace for the asynchronous programming classes.