Allolib  1.0
C++ Components For Interactive Multimedia
al_AudioIOData.hpp
1 #ifndef INCLUDE_AL_AUDIODATA_IO_HPP
2 #define INCLUDE_AL_AUDIODATA_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 
40  File description:
41  An interface to low-level audio device streams
42 
43  File author(s):
44  Lance Putnam, 2010, putnam.lance@gmail.com
45  Andres Cabrera 2017 mantaraya36@gmail.com
46 */
47 
48 #include <cassert>
49 #include <cinttypes>
50 #include <cstdio>
51 #include <cstring>
52 
53 namespace al {
54 
55 template <class T>
56 static void deleteBuf(T*& buf) {
57  delete[] buf;
58  buf = 0;
59 }
60 
61 template <class T>
62 static int resize(T*& buf, int n) {
63  deleteBuf(buf);
64  buf = new T[n];
65  return n;
66 }
67 
69 template <class T>
70 static void zero(T* buf, unsigned int n) {
71  memset(buf, 0, n * sizeof(T));
72 }
73 
75 template <class T>
76 static void deinterleave(T* dst, const T* src, int numFrames, int numChannels) {
77  int numSamples = numFrames * numChannels;
78  for (int c = 0; c < numChannels; c++) {
79  for (int i = c; i < numSamples; i += numChannels) {
80  *dst++ = src[i];
81  }
82  }
83 }
84 
86 template <class T>
87 static void interleave(T* dst, const T* src, int numFrames, int numChannels) {
88  int numSamples = numFrames * numChannels;
89  for (int c = 0; c < numChannels; c++) {
90  for (int i = c; i < numSamples; i += numChannels) {
91  dst[i] = *src++;
92  }
93  }
94 }
95 
97 template <class T>
98 static void interleave(T *dst, T **src, int numFrames, int numChannels) {
99  int numSamples = numFrames * numChannels;
100  for (int c = 0; c < numChannels; c++) {
101  float *channelSrc = src[c];
102  for (int i = c; i < numSamples; i += numChannels) {
103  dst[i] = *channelSrc++;
104  }
105  }
106 }
107 
112  public:
114  enum StreamMode {
115  INPUT = 1,
116  OUTPUT = 2
117  };
118 
120  AudioDeviceInfo(int deviceNum);
121 
122  // /// @param[in] nameKeyword Keyword to search for in device name
123  // /// @param[in] stream Whether to search for input and/or
124  // output
125  // devices
126  // AudioDeviceInfo(const std::string& nameKeyword, StreamMode stream =
127  // StreamMode(INPUT | OUTPUT)) : mID(-1) {}
128 
129  virtual ~AudioDeviceInfo() {}
130 
131  virtual bool valid() const;
132  virtual int id() const;
133  virtual const char* name() const;
134  virtual int channelsInMax()
135  const;
136  virtual int channelsOutMax()
137  const;
138  virtual double defaultSampleRate() const;
139 
140  virtual void setID(int iD);
141  virtual void setName(char* name);
142  virtual void setChannelsInMax(
143  int num);
144  virtual void setChannelsOutMax(
145  int num);
146  virtual void setDefaultSampleRate(double rate);
147 
148  virtual bool hasInput() const = 0;
149  virtual bool hasOutput() const = 0;
150 
151  virtual void print()
152  const = 0;
153 
154  protected:
155  int mID{-1};
156  char mName[128];
157  int mChannelsInMax{0};
158  int mChannelsOutMax{0};
159  double mDefaultSampleRate{0};
160  bool mValid{false};
161 };
162 
163 inline AudioDeviceInfo::StreamMode operator|(
165  const AudioDeviceInfo::StreamMode& b) {
166  return static_cast<AudioDeviceInfo::StreamMode>(+a | +b);
167 }
168 
174 class AudioIOData {
175  public:
177  AudioIOData(void* user = nullptr);
178 
179  virtual ~AudioIOData();
180 
181  typedef enum { PORTAUDIO, RTAUDIO, DUMMY } Backend;
182 
184  bool operator()() const { return (++mFrame) < framesPerBuffer(); }
185 
187  unsigned int frame() const { return mFrame; }
188 
190  float& bus(unsigned int chan) const { return bus(chan, frame()); }
191 
193  float& bus(unsigned int chan, unsigned int frame) const;
194 
196  float* busBuffer(unsigned int chan = 0) const { return &bus(chan, 0); }
197 
199  const float& in(unsigned int chan) const { return in(chan, frame()); }
200 
202  const float& in(unsigned int chan, unsigned int frame) const;
203 
205  const float* inBuffer(unsigned int chan = 0) const { return &in(chan, 0); }
206 
208  float& out(unsigned int chan) const { return out(chan, frame()); }
209 
211  float& out(unsigned int chan, unsigned int frame) const;
212 
214  float* outBuffer(unsigned int chan = 0) const { return &out(chan, 0); }
215 
217  void sum(float v, unsigned int chan) const { out(chan) += v; }
218 
220  void sum(float v, unsigned int ch1, unsigned int ch2) const {
221  sum(v, ch1);
222  sum(v, ch2);
223  }
224 
226  float& temp(unsigned int frame) const;
227 
229  float* tempBuffer() const { return &temp(0); }
230 
231  void* user() const { return mUser; }
232 
233  template <class UserDataType>
234  UserDataType& user() const {
235  return *(static_cast<UserDataType*>(mUser));
236  }
237 
238  int channels(bool forOutput) const;
239  unsigned int channelsIn() const;
240  unsigned int channelsOut()
241  const;
242  unsigned int channelsBus() const;
243  uint64_t framesPerBuffer() const;
244  double framesPerSecond() const;
245  double fps() const { return framesPerSecond(); }
246  double secondsPerBuffer() const;
247 
248  void user(void* v) { mUser = v; }
249  void frame(unsigned int v) {
250  assert(v >= 0);
251  mFrame = v - 1;
252  }
253  void zeroBus();
254  void zeroOut();
255 
258 
262  virtual void channels(int num, bool forOutput);
263 
264  void channelsIn(int n);
265  void channelsOut(int n);
266  virtual void channelsBus(int num);
267 
268  virtual void framesPerSecond(double v);
269  virtual void framesPerBuffer(
270  unsigned int n);
271 
272  AudioIOData& gain(float v) {
273  mGain = v;
274  return *this;
275  }
276  bool usingGain() const { return mGain != 1.f || mGainPrev != 1.f; }
277 
278  float mGain, mGainPrev;
279 
280  protected:
281  void* mUser; // User specified data
282  mutable unsigned int mFrame;
283  unsigned int mFramesPerBuffer;
284  double mFramesPerSecond;
285  float *mBufI, *mBufO, *mBufB; // input, output, and aux buffers
286  float* mBufT; // temporary one channel buffer
287  unsigned int mNumI, mNumO, mNumB; // input, output, and aux channels
288 
289  void resizeBuffer(bool forOutput);
290 
291  private:
292  void operator=(const AudioIOData&); // Disallow copy
293 };
294 
299  public:
300  virtual ~AudioCallback() {}
301  virtual void onAudioCB(AudioIOData& io) = 0;
302 };
303 
304 //==============================================================================
305 inline float& AudioIOData::bus(unsigned int c, unsigned int f) const {
306  assert(c < mNumB);
307  assert(f < framesPerBuffer());
308  return mBufB[c * framesPerBuffer() + f];
309 }
310 
311 inline const float& AudioIOData::in(unsigned int c, unsigned int f) const {
312  assert(c < mNumI);
313  assert(f < framesPerBuffer());
314  return mBufI[c * framesPerBuffer() + f];
315 }
316 
317 inline float& AudioIOData::out(unsigned int c, unsigned int f) const {
318  assert(c < mNumO);
319  assert(f < framesPerBuffer());
320  return mBufO[c * framesPerBuffer() + f];
321 }
322 inline float& AudioIOData::temp(unsigned int f) const { return mBufT[f]; }
323 
324 } // namespace al
325 
326 #endif
virtual void onAudioCB(AudioIOData &io)=0
Callback.
virtual bool hasInput() const =0
Returns whether device has input.
virtual int channelsOutMax() const
Get maximum number of output channels supported.
virtual int channelsInMax() const
Get maximum number of input channels supported.
virtual void setChannelsInMax(int num)
Sets maximum number of Input channels supported.
virtual void setID(int iD)
Sets unique ID.
virtual int id() const
Get device unique ID.
virtual void setName(char *name)
Sets device name.
virtual void print() const =0
Prints info about specific i/o device to stdout.
virtual double defaultSampleRate() const
Get default sample rate.
virtual bool valid() const
Returns whether device is valid.
virtual const char * name() const
Get device name.
virtual bool hasOutput() const =0
Returns whether device has output.
virtual void setChannelsOutMax(int num)
Sets maximum number of Output channels supported.
virtual void setDefaultSampleRate(double rate)
Sets default sample rate.
AudioDeviceInfo(int deviceNum)
float & bus(unsigned int chan) const
Get bus sample at current frame iteration on specified channel.
uint64_t framesPerBuffer() const
Get frames/buffer of audio I/O stream.
void * user() const
Get pointer to user data.
double secondsPerBuffer() const
Get seconds/buffer of audio I/O stream.
void zeroBus()
Zeros all the bus buffers.
float * tempBuffer() const
Get non-interleaved temporary buffer on specified channel.
float & out(unsigned int chan) const
Get output sample at current frame iteration on specified channel.
void frame(unsigned int v)
Set frame count for next iteration.
void user(void *v)
Set user data.
AudioIOData(void *user=nullptr)
Constructor.
virtual void channelsBus(int num)
Set number of bus channels.
const float & in(unsigned int chan) const
Get input sample at current frame iteration on specified channel.
float * outBuffer(unsigned int chan=0) const
Get non-interleaved output buffer on specified channel.
void sum(float v, unsigned int chan) const
Add value to current output sample on specified channel.
virtual void framesPerSecond(double v)
Set number of frames per second.
virtual void framesPerBuffer(unsigned int n)
Set number of frames per processing buffer.
const float * inBuffer(unsigned int chan=0) const
Get non-interleaved input buffer on specified channel.
double framesPerSecond() const
Get frames/second of audio I/O streams.
bool operator()() const
Iterate frame counter, returning true while more frames.
unsigned int channelsIn() const
Get effective number of input channels.
float * busBuffer(unsigned int chan=0) const
Get non-interleaved bus buffer on specified channel.
void zeroOut()
Zeros all the internal output buffers.
unsigned int channelsOut() const
Get effective number of output channels.
float & temp(unsigned int frame) const
Get sample from temporary buffer at specified frame.
virtual void channels(int num, bool forOutput)
unsigned int frame() const
Get current frame number.
unsigned int channelsBus() const
Get number of allocated bus channels.
void channelsIn(int n)
Set number of input channels.
void sum(float v, unsigned int ch1, unsigned int ch2) const
Add value to current output sample on specified channels.
void channelsOut(int n)
Set number of output channels.
Definition: al_App.hpp:23