SoDaRadio-5.0.3-master:8901fb5
AudioPA.hxx
Go to the documentation of this file.
1 #ifndef AUDIO_PA_HDR
2 #define AUDIO_PA_HDR
3 
4 /*
5  Copyright (c) 2012, Matthew H. Reilly (kb1vc)
6  All rights reserved.
7 
8  Redistribution and use in source and binary forms, with or without
9  modification, are permitted provided that the following conditions are
10  met:
11 
12  Redistributions of source code must retain the above copyright
13  notice, this list of conditions and the following disclaimer.
14  Redistributions in binary form must reproduce the above copyright
15  notice, this list of conditions and the following disclaimer in
16  the documentation and/or other materials provided with the
17  distribution.
18 
19  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 
32 #include "SoDaBase.hxx"
33 #include "AudioIfc.hxx"
34 
35 #include <boost/format.hpp>
36 #if HAVE_LIBPORTAUDIO
37 # include <portaudio.h>
38 # define PORTAUDIO_DEF
39 #else
40 # define PORTAUDIO_DEF { throw SoDa::SoDaException("PortAudio Library is not enabled in this build version."); }
41 #endif
42 #include <iostream>
43 #include <stdexcept>
44 
45 namespace SoDa {
60  class AudioPA : public AudioIfc {
61  public:
68  AudioPA(unsigned int _sample_rate, AudioIfc::DataFormat _fmt, unsigned int _sample_count_hint = 1024) ;
69 
70  ~AudioPA() { }
77  int send(void * buf, unsigned int len) { (void) buf; (void) len; PORTAUDIO_DEF }
78 
84  bool sendBufferReady(unsigned int len) { (void) len; PORTAUDIO_DEF }
85 
93  int recv(void * buf, unsigned int len, bool block = true) { (void) buf; (void) len; (void) block; PORTAUDIO_DEF }
94 
100  bool recvBufferReady(unsigned int len) { (void) len; PORTAUDIO_DEF }
101 
106  void sleepOut() {
107 #if HAVE_LIBPORTAUDIO
108  pa_stat = Pa_StopStream(pa_outstream);
109  if(pa_stat != paStreamIsStopped) checkStatus(pa_stat, "sleepOut");
110 #endif
111  }
115  void wakeOut() {
116 #if HAVE_LIBPORTAUDIO
117  pa_stat = Pa_StartStream(pa_outstream);
118  if(pa_stat != paStreamIsNotStopped) checkStatus(pa_stat, "wakeOut");
119 #endif
120  }
121 
126  void sleepIn() {
127 #if HAVE_LIBPORTAUDIO
128  pa_stat = Pa_StopStream(pa_instream);
129  if(pa_stat != paStreamIsStopped) checkStatus(pa_stat, "sleepIn");
130 #endif
131  }
135  void wakeIn() {
136 #if HAVE_LIBPORTAUDIO
137  pa_stat = Pa_StartStream(pa_instream);
138  if(pa_stat != paStreamIsNotStopped) checkStatus(pa_stat, "wakeIn");
139 #endif
140  }
141 
142  protected:
143 #if HAVE_LIBPORTAUDIO
144  PaStream * pa_instream;
145  PaStream * pa_outstream;
146  PaError pa_stat;
147 
148 
154  PaSampleFormat translateFormat(AudioIfc::DataFormat fmt) PORTAUDIO_DEF ;
155 #endif
156 
163 #if HAVE_LIBPORTAUDIO
164  void checkStatus(PaError v, const std::string & exp, bool fatal = false) {
165 
166  if (v != paNoError) {
167  if(fatal) throw SoDaException((boost::format("%s %s") % exp % Pa_GetErrorText(v)).str(), this);
168  else std::cerr << boost::format("%s: %s %s\n") % getObjName() % exp % Pa_GetErrorText(v);
169  }
170  }
171 #endif
172  };
173 }
174 
175 
176 #endif
The Baseclass for all SoDa objects, and useful commonly used classes.
std::string & getObjName()
get the name of this object
Definition: SoDaBase.hxx:182
int send(void *buf, unsigned int len)
sendBuf – send a buffer to the audio output
Definition: AudioPA.hxx:77
The SoDa Exception class.
Definition: SoDaBase.hxx:217
void sleepOut()
stop the output stream so that we don&#39;t encounter a buffer underflow while the reciever is muted...
Definition: AudioPA.hxx:106
the PortAudio interface class
Definition: AudioPA.hxx:60
int recv(void *buf, unsigned int len, bool block=true)
recvBuf – get a buffer of data from the audio input
Definition: AudioPA.hxx:93
bool sendBufferReady(unsigned int len)
sendBufferReady – is there enough space in the audio device send buffer for a call from send...
Definition: AudioPA.hxx:84
bool recvBufferReady(unsigned int len)
recvBufferReady – is there enough space in the audio device recv buffer for a call from recv...
Definition: AudioPA.hxx:100
void wakeOut()
start the output stream
Definition: AudioPA.hxx:115
Generic Audio Interface Class.
Definition: AudioIfc.hxx:44
#define PORTAUDIO_DEF
Definition: AudioPA.hxx:40
void sleepIn()
stop the input stream so that we don&#39;t encounter a buffer overflow while the transmitter is inactive...
Definition: AudioPA.hxx:126
AudioPA(unsigned int _sample_rate, AudioIfc::DataFormat _fmt, unsigned int _sample_count_hint=1024)
constructor
Definition: AudioPA.cxx:107
void wakeIn()
start the input stream
Definition: AudioPA.hxx:135