1 /*
2  * Helper and utility functions for pa_mac_core.c (Apple AUHAL implementation)
3  *
4  * PortAudio Portable Real-Time Audio Library
5  * Latest Version at: http://www.portaudio.com
6  *
7  * Written by Bjorn Roche of XO Audio LLC, from PA skeleton code.
8  * Portions copied from code by Dominic Mazzoni (who wrote a HAL implementation)
9  *
10  * Dominic's code was based on code by Phil Burk, Darren Gibbs,
11  * Gord Peters, Stephane Letz, and Greg Pfiel.
12  *
13  * The following people also deserve acknowledgements:
14  *
15  * Olivier Tristan for feedback and testing
16  * Glenn Zelniker and Z-Systems engineering for sponsoring the Blocking I/O
17  * interface.
18  *
19  *
20  * Based on the Open Source API proposed by Ross Bencina
21  * Copyright (c) 1999-2002 Ross Bencina, Phil Burk
22  *
23  * Permission is hereby granted, free of charge, to any person obtaining
24  * a copy of this software and associated documentation files
25  * (the "Software"), to deal in the Software without restriction,
26  * including without limitation the rights to use, copy, modify, merge,
27  * publish, distribute, sublicense, and/or sell copies of the Software,
28  * and to permit persons to whom the Software is furnished to do so,
29  * subject to the following conditions:
30  *
31  * The above copyright notice and this permission notice shall be
32  * included in all copies or substantial portions of the Software.
33  *
34  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
35  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
36  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
37  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
38  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
39  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
40  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
41  */
42 
43 /*
44  * The text above constitutes the entire PortAudio license; however,
45  * the PortAudio community also makes the following non-binding requests:
46  *
47  * Any person wishing to distribute modifications to the Software is
48  * requested to send the modifications to the original developer so that
49  * they can be incorporated into the canonical version. It is also
50  * requested that these non-binding requests be included along with the
51  * license above.
52  */
53 
54 /**
55  @file
56  @ingroup hostapi_src
57 */
58 
59 #ifndef PA_MAC_CORE_UTILITIES_H__
60 #define PA_MAC_CORE_UTILITIES_H__
61 
62 #include <pthread.h>
63 #include "portaudio.h"
64 #include "pa_util.h"
65 #include <AudioUnit/AudioUnit.h>
66 #include <AudioToolbox/AudioToolbox.h>
67 
68 #ifndef MIN
69 #define MIN(a, b)  (((a)<(b))?(a):(b))
70 #endif
71 
72 #ifndef MAX
73 #define MAX(a, b)  (((a)<(b))?(b):(a))
74 #endif
75 
76 #define ERR(mac_error) PaMacCore_SetError(mac_error, __LINE__, 1 )
77 #define WARNING(mac_error) PaMacCore_SetError(mac_error, __LINE__, 0 )
78 
79 
80 /* Help keep track of AUHAL element numbers */
81 #define INPUT_ELEMENT  (1)
82 #define OUTPUT_ELEMENT (0)
83 
84 /* Normal level of debugging: fine for most apps that don't mind the occational warning being printf'ed */
85 /*
86  */
87 #define MAC_CORE_DEBUG
88 #ifdef MAC_CORE_DEBUG
89 # define DBUG(MSG) do { printf("||PaMacCore (AUHAL)|| "); printf MSG ; fflush(stdout); } while(0)
90 #else
91 # define DBUG(MSG)
92 #endif
93 
94 /* Verbose Debugging: useful for developement */
95 /*
96 #define MAC_CORE_VERBOSE_DEBUG
97 */
98 #ifdef MAC_CORE_VERBOSE_DEBUG
99 # define VDBUG(MSG) do { printf("||PaMacCore (v )|| "); printf MSG ; fflush(stdout); } while(0)
100 #else
101 # define VDBUG(MSG)
102 #endif
103 
104 /* Very Verbose Debugging: Traces every call. */
105 /*
106 #define MAC_CORE_VERY_VERBOSE_DEBUG
107  */
108 #ifdef MAC_CORE_VERY_VERBOSE_DEBUG
109 # define VVDBUG(MSG) do { printf("||PaMacCore (vv)|| "); printf MSG ; fflush(stdout); } while(0)
110 #else
111 # define VVDBUG(MSG)
112 #endif
113 
114 
115 
116 
117 
118 #define UNIX_ERR(err) PaMacCore_SetUnixError( err, __LINE__ )
119 
120 PaError PaMacCore_SetUnixError( int err, int line );
121 
122 /*
123  * Translates MacOS generated errors into PaErrors
124  */
125 PaError PaMacCore_SetError(OSStatus error, int line, int isError);
126 
127 /*
128  * This function computes an appropriate ring buffer size given
129  * a requested latency (in seconds), sample rate and framesPerBuffer.
130  *
131  * The returned ringBufferSize is computed using the following
132  * constraints:
133  *   - it must be at least 4.
134  *   - it must be at least 3x framesPerBuffer.
135  *   - it must be at least 2x the suggestedLatency.
136  *   - it must be a power of 2.
137  * This function attempts to compute the minimum such size.
138  *
139  */
140 long computeRingBufferSize( const PaStreamParameters *inputParameters,
141                                    const PaStreamParameters *outputParameters,
142                                    long inputFramesPerBuffer,
143                                    long outputFramesPerBuffer,
144                                    double sampleRate );
145 
146 OSStatus propertyProc(
147     AudioDeviceID inDevice,
148     UInt32 inChannel,
149     Boolean isInput,
150     AudioDevicePropertyID inPropertyID,
151     void* inClientData );
152 
153 /* sets the value of the given property and waits for the change to
154    be acknowledged, and returns the final value, which is not guaranteed
155    by this function to be the same as the desired value. Obviously, this
156    function can only be used for data whose input and output are the
157    same size and format, and their size and format are known in advance.*/
158 PaError AudioDeviceSetPropertyNowAndWaitForChange(
159     AudioDeviceID inDevice,
160     UInt32 inChannel,
161     Boolean isInput,
162     AudioDevicePropertyID inPropertyID,
163     UInt32 inPropertyDataSize,
164     const void *inPropertyData,
165     void *outPropertyData );
166 
167 /*
168  * Sets the sample rate the HAL device.
169  * if requireExact: set the sample rate or fail.
170  *
171  * otherwise      : set the exact sample rate.
172  *             If that fails, check for available sample rates, and choose one
173  *             higher than the requested rate. If there isn't a higher one,
174  *             just use the highest available.
175  */
176 PaError setBestSampleRateForDevice( const AudioDeviceID device,
177                                     const bool isOutput,
178                                     const bool requireExact,
179                                     const Float64 desiredSrate );
180 /*
181    Attempts to set the requestedFramesPerBuffer. If it can't set the exact
182    value, it settles for something smaller if available. If nothing smaller
183    is available, it uses the smallest available size.
184    actualFramesPerBuffer will be set to the actual value on successful return.
185    OK to pass NULL to actualFramesPerBuffer.
186    The logic is very simmilar too setBestSampleRate only failure here is
187    not usually catastrophic.
188 */
189 PaError setBestFramesPerBuffer( const AudioDeviceID device,
190                                 const bool isOutput,
191                                 UInt32 requestedFramesPerBuffer,
192                                 UInt32 *actualFramesPerBuffer );
193 
194 
195 /*********************
196  *
197  *  xrun handling
198  *
199  *********************/
200 
201 OSStatus xrunCallback(
202     AudioDeviceID inDevice,
203     UInt32 inChannel,
204     Boolean isInput,
205     AudioDevicePropertyID inPropertyID,
206     void* inClientData ) ;
207 
208 /** returns zero on success or a unix style error code. */
209 int initializeXRunListenerList();
210 /** returns zero on success or a unix style error code. */
211 int destroyXRunListenerList();
212 
213 /**Returns the list, so that it can be passed to CorAudio.*/
214 void *addToXRunListenerList( void *stream );
215 /**Returns the number of Listeners in the list remaining.*/
216 int removeFromXRunListenerList( void *stream );
217 
218 #endif /* PA_MAC_CORE_UTILITIES_H__*/
219