SoDaRadio-5.0.3-master:8901fb5
AudioPA.cxx
Go to the documentation of this file.
1 #include "AudioPA.hxx"
2 /*
3  Copyright (c) 2012, Matthew H. Reilly (kb1vc)
4  All rights reserved.
5 
6  Redistribution and use in source and binary forms, with or without
7  modification, are permitted provided that the following conditions are
8  met:
9 
10  Redistributions of source code must retain the above copyright
11  notice, this list of conditions and the following disclaimer.
12  Redistributions in binary form must reproduce the above copyright
13  notice, this list of conditions and the following disclaimer in
14  the documentation and/or other materials provided with the
15  distribution.
16 
17  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29 
30 #include <boost/format.hpp>
31 
32 namespace SoDa {
33 #if HAVE_LIBPORTAUDIO
34  AudioPA::AudioPA(unsigned int _sample_rate, DataFormat _fmt, unsigned int _sample_count_hint) :
35  AudioIfc(_sample_rate, _fmt, _sample_count_hint, "AudioPA Port Audio Interface") {
36  checkStatus(pa_stat = Pa_Initialize(), "Init");
37 
38  checkStatus(Pa_OpenDefaultStream(&pa_instream, 1, 0, translateFormat(_fmt), sample_rate, sample_count_hint, NULL, NULL), "OpenIn");
39 
40  checkStatus(Pa_OpenDefaultStream(&pa_outstream, 0, 1, translateFormat(_fmt), sample_rate, sample_count_hint, NULL, NULL), "OpenOut");
41 
42 
43  }
44 
45  bool AudioPA::sendBufferReady(unsigned int len) {
46  int bavail = Pa_GetStreamWriteAvailable(pa_outstream);
47  if(bavail < 0) {
48  checkStatus(bavail, "sendBufferReady:");
49  }
50  return bavail >= len;
51  }
52 
53  bool AudioPA::recvBufferReady(unsigned int len) {
54  int bavail = Pa_GetStreamReadAvailable(pa_instream);
55  return bavail >= len;
56  }
57 
58 
59  int AudioPA::send(void * buf, unsigned int len) {
60  int err;
61  int olen = len;
62 
63  if(!Pa_IsStreamActive(pa_outstream)) {
64  checkStatus(Pa_StartStream(pa_outstream), "sendWake");
65  }
66  pa_stat = Pa_WriteStream(pa_outstream, buf, len);
67 
68  if(pa_stat != 0) {
69  std::cerr << boost::format("write to audio interface failed (%s)\n") % Pa_GetErrorText(pa_stat);
70  }
71  return olen;
72  }
73 
74  int AudioPA::recv(void * buf, unsigned int len, bool block) {
75  int err;
76  int olen = len;
77 
78  if(!Pa_IsStreamActive(pa_instream)) {
79  checkStatus(Pa_StartStream(pa_instream), "recvWake");
80  }
81  pa_stat = Pa_ReadStream(pa_instream, buf, len);
82  if(pa_stat != paNoError) {
83  std::cerr << boost::format("read from audio interface failed (%s)\n") % Pa_GetErrorText(pa_stat);
84  }
85  return olen;
86  }
87 
88 
89  PaSampleFormat AudioPA::translateFormat(AudioIfc::DataFormat fmt) {
90  switch(fmt) {
91  case FLOAT: return paFloat32;
92  break;
93  case DFLOAT:
94  throw (new SoDaException("Unsupported data type DFLOAT for PortAudio driver.", this));
95  break;
96  case INT32: return paInt32;
97  break;
98  case INT16: return paInt16;
99  break;
100  case INT8: return paInt8;
101  break;
102  }
103  return paInt16;
104  }
105 
106 #else
107  AudioPA::AudioPA(unsigned int _sample_rate, DataFormat _fmt, unsigned int _sample_count_hint) :
108  AudioIfc(_sample_rate, _fmt, _sample_count_hint, "AudioPA Port Audio Interface") {
109  throw SoDaException("PortAudio Library is not enabled in this build version.");
110  }
111 #endif // HAVE_LIBPORTAUDIO
112 }
unsigned int sample_rate
Definition: AudioIfc.hxx:169
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
unsigned int sample_count_hint
Definition: AudioIfc.hxx:171
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
Generic Audio Interface Class.
Definition: AudioIfc.hxx:44
AudioPA(unsigned int _sample_rate, AudioIfc::DataFormat _fmt, unsigned int _sample_count_hint=1024)
constructor
Definition: AudioPA.cxx:107