Allolib  1.0
C++ Components For Interactive Multimedia
al_AudioIO.hpp
1 #ifndef INCLUDE_AL_AUDIO_IO_HPP
2 #define INCLUDE_AL_AUDIO_IO_HPP
3 
4 /* Allocore --
5  Multimedia / virtual environment application class library
6 
7  Copyright (C) 2009. AlloSphere Research Group, Media Arts & Technology,
8  UCSB. Copyright (C) 2012. The Regents of the University of California. All
9  rights reserved.
10 
11  Redistribution and use in source and binary forms, with or without
12  modification, are permitted provided that the following conditions are
13  met:
14 
15  Redistributions of source code must retain the above copyright
16  notice, this list of conditions and the following disclaimer.
17 
18  Redistributions in binary form must reproduce the above
19  copyright notice, this list of conditions and the following disclaimer in the
20  documentation and/or other materials provided with the
21  distribution.
22 
23  Neither the name of the University of California nor the names
24  of its contributors may be used to endorse or promote products derived from
25  this software without specific prior written permission.
26 
27  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
28  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
31  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
34  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
35  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
36  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 
39  File description:
40  An interface to low-level audio device streams
41 
42  File author(s):
43  Lance Putnam, 2010, putnam.lance@gmail.com
44  Andres Cabrera, 2017 mantaraya36@gmail.com
45 */
46 
47 #include <functional>
48 #include <memory>
49 #include <string>
50 #include <vector>
51 
52 #include "al/io/al_AudioIOData.hpp"
53 
54 namespace al {
55 
57 typedef void (*audioCallback)(AudioIOData &io);
58 
59 class AudioDevice;
60 
64 class AudioBackend {
65 public:
66  AudioBackend();
67 
68  ~AudioBackend() {}
69 
70  bool isOpen() const;
71  bool isRunning() const;
72  bool error() const;
73 
74  void printError(const char *text = "") const;
75  void printInfo() const;
76 
77  bool supportsFPS(double fps);
78 
79  void inDevice(int index);
80  void outDevice(int index);
81 
82  void channels(int num, bool forOutput);
83  void setStreamName(std::string name);
84 
85  int inDeviceChans();
86  int outDeviceChans();
87  void setInDeviceChans(int num);
88  void setOutDeviceChans(int num);
89 
90  double time();
91 
92  bool open(int framesPerSecond, unsigned int framesPerBuffer, void *userdata);
93  bool close();
94 
95  bool start(int framesPerSecond, int framesPerBuffer, void *userdata);
96  bool stop();
97  double cpu();
98 
99  // Device information
100  static AudioDevice defaultInput();
101  static AudioDevice defaultOutput();
102  static bool deviceIsValid(int num);
103  static int deviceMaxInputChannels(int num);
104  static int deviceMaxOutputChannels(int num);
105  static double devicePreferredSamplingRate(int num);
106  static std::string deviceName(int num);
107  static int numDevices();
108 
109 protected:
110  bool mRunning{false};
111  bool mOpen{false};
112  std::shared_ptr<void> mBackendData;
113 };
114 
118 class AudioDevice : public AudioDeviceInfo {
119 public:
121  enum StreamMode {
122  INPUT = 1,
123  OUTPUT = 2
124  };
125 
127  AudioDevice(int deviceNum = -1);
128 
132  AudioDevice(const std::string &nameKeyword,
133  StreamMode stream = StreamMode(INPUT | OUTPUT));
134 
135  virtual bool valid() const { return mValid; }
136  virtual bool hasInput() const {
137  return channelsInMax() > 0;
138  }
139  virtual bool hasOutput() const {
140  return channelsOutMax() > 0;
141  }
142 
143  virtual void
144  print() const;
145 
148  static int numDevices();
149  static void
151 
152 protected:
153  void setImpl(int deviceNum);
154  static void initDevices();
155 };
156 
157 inline AudioDevice::StreamMode operator|(const AudioDevice::StreamMode &a,
158  const AudioDevice::StreamMode &b) {
159  return static_cast<AudioDevice::StreamMode>(+a | +b);
160 }
161 
165 class AudioIO : public AudioIOData {
166 public:
169  virtual ~AudioIO();
177  void init(void (*callback)(AudioIOData &), void *userData,
178  int framesPerBuf = 64, double framesPerSec = 44100.0,
179  int outChans = 2, int inChans = 0);
180 
181  void init(void (*callback)(AudioIOData &), void *userData, AudioDevice &dev,
182  int framesPerBuf = 64, double framesPerSec = 44100.0,
183  int outChans = 2, int inChans = 0);
184 
194  void initWithDefaults(void (*callback)(AudioIOData &), void *userData,
195  bool use_out, bool use_in, int framesPerBuffer = 256);
196 
197  bool open();
198  bool close();
199  bool start();
200  bool stop();
201  void processAudio();
202 
203  bool isOpen();
204  bool isRunning();
205 
206  bool autoZeroOut() const { return mAutoZeroOut; }
208  const;
210  const;
211  bool clipOut() const { return mClipOut; }
212  double cpu() const;
213  bool
214  supportsFPS(double fps);
215  bool zeroNANs()
216  const;
217 
223  void channels(int num, bool forOutput) override;
224  void channelsBus(int num) override;
225 
227  void setStreamName(std::string name);
228 
229  void clipOut(bool v);
230  void
231  device(const AudioDevice &v);
232  void deviceIn(const AudioDevice &v);
233  void deviceOut(const AudioDevice &v);
234  virtual void
235  framesPerSecond(double v) override;
236  virtual void framesPerBuffer(
237  unsigned int n) override;
238  void zeroNANs(bool v) {
239  mZeroNANs = v;
240  }
241 
242  void print() const;
243  static const char *errorText(int errNum);
244 
245  double time() const;
246  double time(int frame) const;
247 
250  AudioIO &prepend(AudioCallback &v);
251  AudioIO &insertBefore(AudioCallback &v, AudioCallback &beforeThis);
252  AudioIO &insertAfter(AudioCallback &v, AudioCallback &afterThis);
253 
256 
262 
264 
265 private:
266  AudioDevice mInDevice, mOutDevice;
267  bool mZeroNANs; // whether to zero NANs
268  bool mClipOut; // whether to clip output between -1 and 1
269  bool mAutoZeroOut; // whether to automatically zero output buffers each block
270  std::vector<AudioCallback *> mAudioCallbacks;
271 
272  void reopen(); // reopen stream (restarts stream if needed)
273  void resizeBuffer(bool forOutput);
274  void operator=(const AudioIO &) = delete; // Disallow copy
275 
276  std::unique_ptr<AudioBackend> mBackend;
277 };
278 
279 } // namespace al
280 
281 #endif
static int numDevices()
Returns number of audio i/o devices available.
static AudioDevice defaultOutput()
Get system's default output device.
AudioDevice(int deviceNum=-1)
virtual bool hasOutput() const
Returns whether device has output.
Definition: al_AudioIO.hpp:139
StreamMode
Stream mode.
Definition: al_AudioIO.hpp:121
virtual bool valid() const
Returns whether device is valid.
Definition: al_AudioIO.hpp:135
AudioDevice(const std::string &nameKeyword, StreamMode stream=StreamMode(INPUT|OUTPUT))
static void printAll()
Prints info about all available i/o devices to stdout.
virtual bool hasInput() const
Returns whether device has input.
Definition: al_AudioIO.hpp:136
static AudioDevice defaultInput()
Get system's default input device.
virtual void print() const
Prints info about specific i/o device to stdout.
virtual int channelsOutMax() const
Get maximum number of output channels supported.
virtual int channelsInMax() const
Get maximum number of input channels supported.
uint64_t framesPerBuffer() const
Get frames/buffer of audio I/O stream.
double framesPerSecond() const
Get frames/second of audio I/O streams.
unsigned int channelsIn() const
Get effective number of input channels.
unsigned int channelsOut() const
Get effective number of output channels.
unsigned int frame() const
Get current frame number.
unsigned int channelsBus() const
Get number of allocated bus channels.
bool clipOut() const
Returns clipOut setting.
Definition: al_AudioIO.hpp:211
int channelsOutDevice() const
Get number of channels opened on output device.
int channelsInDevice() const
Get number of channels opened on input device.
bool open()
Opens audio device.
uint64_t framesPerBuffer() const
Get frames/buffer of audio I/O stream.
AudioIO & remove(AudioCallback &v)
Remove all input event handlers matching argument.
void init(void(*callback)(AudioIOData &), void *userData, int framesPerBuf=64, double framesPerSec=44100.0, int outChans=2, int inChans=0)
void channels(int num, bool forOutput) override
void clipOut(bool v)
Set whether to clip output between -1 and 1.
bool stop()
Stops the audio IO.
bool supportsFPS(double fps)
Return true if fps supported, otherwise false.
bool zeroNANs() const
Returns whether to zero NANs in output buffer going to DAC.
double time() const
Get current stream time in seconds.
bool isOpen()
Returns true if device has been opened.
void print() const
Prints info about current i/o devices to stdout.
bool start()
Starts the audio IO. Will open audio device if necessary.
virtual void framesPerBuffer(unsigned int n) override
Set number of frames per processing buffer.
double time(int frame) const
Get current stream time in seconds of frame.
double cpu() const
Returns current CPU usage of audio thread.
void setStreamName(std::string name)
Set name of this stream. Currently only has an effect when using jack.
audioCallback callback
User specified callback function.
Definition: al_AudioIO.hpp:263
void deviceOut(const AudioDevice &v)
Set output device.
void processAudio()
Call callback manually.
bool isRunning()
Returns true if audio is running.
virtual void framesPerSecond(double v) override
Set number of frames per second.
bool close()
Closes audio device. Will stop active IO.
AudioIO & append(AudioCallback &v)
Add an AudioCallback handler (internal callback is always called first)
void device(const AudioDevice &v)
Set input/output device (must be duplex)
static const char * errorText(int errNum)
Returns error string.
void deviceIn(const AudioDevice &v)
Set input device.
void initWithDefaults(void(*callback)(AudioIOData &), void *userData, bool use_out, bool use_in, int framesPerBuffer=256)
AudioIO()
Creates AudioIO using default I/O devices.
void zeroNANs(bool v)
Set whether to zero NANs in output buffer going to DAC.
Definition: al_AudioIO.hpp:238
void channelsBus(int num) override
Set number of bus channels.
Definition: al_App.hpp:23
void(* audioCallback)(AudioIOData &io)
Audio callback type.
Definition: al_AudioIO.hpp:57