| 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 occasional 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 development */ |
| 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 |
OSStatus PaMacCore_AudioHardwareGetProperty( |
| 115 |
AudioHardwarePropertyID inPropertyID, |
| 116 |
UInt32* ioPropertyDataSize, |
| 117 |
void* outPropertyData ); |
| 118 |
|
| 119 |
OSStatus PaMacCore_AudioHardwareGetPropertySize( |
| 120 |
AudioHardwarePropertyID inPropertyID, |
| 121 |
UInt32* outSize ); |
| 122 |
|
| 123 |
OSStatus PaMacCore_AudioDeviceGetProperty( |
| 124 |
AudioDeviceID inDevice, |
| 125 |
UInt32 inChannel, |
| 126 |
Boolean isInput, |
| 127 |
AudioDevicePropertyID inPropertyID, |
| 128 |
UInt32* ioPropertyDataSize, |
| 129 |
void* outPropertyData ); |
| 130 |
|
| 131 |
OSStatus PaMacCore_AudioDeviceSetProperty( |
| 132 |
AudioDeviceID inDevice, |
| 133 |
const AudioTimeStamp* inWhen, |
| 134 |
UInt32 inChannel, |
| 135 |
Boolean isInput, |
| 136 |
AudioDevicePropertyID inPropertyID, |
| 137 |
UInt32 inPropertyDataSize, |
| 138 |
const void* inPropertyData ); |
| 139 |
|
| 140 |
OSStatus PaMacCore_AudioDeviceGetPropertySize( |
| 141 |
AudioDeviceID inDevice, |
| 142 |
UInt32 inChannel, |
| 143 |
Boolean isInput, |
| 144 |
AudioDevicePropertyID inPropertyID, |
| 145 |
UInt32* outSize ); |
| 146 |
|
| 147 |
OSStatus PaMacCore_AudioDeviceAddPropertyListener( |
| 148 |
AudioDeviceID inDevice, |
| 149 |
UInt32 inChannel, |
| 150 |
Boolean isInput, |
| 151 |
AudioDevicePropertyID inPropertyID, |
| 152 |
AudioObjectPropertyListenerProc inProc, |
| 153 |
void* inClientData ); |
| 154 |
|
| 155 |
OSStatus PaMacCore_AudioDeviceRemovePropertyListener( |
| 156 |
AudioDeviceID inDevice, |
| 157 |
UInt32 inChannel, |
| 158 |
Boolean isInput, |
| 159 |
AudioDevicePropertyID inPropertyID, |
| 160 |
AudioObjectPropertyListenerProc inProc, |
| 161 |
void* inClientData ); |
| 162 |
|
| 163 |
OSStatus PaMacCore_AudioStreamGetProperty( |
| 164 |
AudioStreamID inStream, |
| 165 |
UInt32 inChannel, |
| 166 |
AudioDevicePropertyID inPropertyID, |
| 167 |
UInt32* ioPropertyDataSize, |
| 168 |
void* outPropertyData ); |
| 169 |
|
| 170 |
#define UNIX_ERR(err) PaMacCore_SetUnixError( err, __LINE__ ) |
| 171 |
|
| 172 |
PaError PaMacCore_SetUnixError( int err, int line ); |
| 173 |
|
| 174 |
/* |
| 175 |
* Translates MacOS generated errors into PaErrors |
| 176 |
*/ |
| 177 |
PaError PaMacCore_SetError(OSStatus error, int line, int isError); |
| 178 |
|
| 179 |
/* |
| 180 |
* This function computes an appropriate ring buffer size given |
| 181 |
* a requested latency (in seconds), sample rate and framesPerBuffer. |
| 182 |
* |
| 183 |
* The returned ringBufferSize is computed using the following |
| 184 |
* constraints: |
| 185 |
* - it must be at least 4. |
| 186 |
* - it must be at least 3x framesPerBuffer. |
| 187 |
* - it must be at least 2x the suggestedLatency. |
| 188 |
* - it must be a power of 2. |
| 189 |
* This function attempts to compute the minimum such size. |
| 190 |
* |
| 191 |
*/ |
| 192 |
long computeRingBufferSize( const PaStreamParameters *inputParameters, |
| 193 |
const PaStreamParameters *outputParameters, |
| 194 |
long inputFramesPerBuffer, |
| 195 |
long outputFramesPerBuffer, |
| 196 |
double sampleRate ); |
| 197 |
|
| 198 |
OSStatus propertyProc( |
| 199 |
AudioObjectID inObjectID, |
| 200 |
UInt32 inNumberAddresses, |
| 201 |
const AudioObjectPropertyAddress* inAddresses, |
| 202 |
void* inClientData ); |
| 203 |
|
| 204 |
/* sets the value of the given property and waits for the change to |
| 205 |
be acknowledged, and returns the final value, which is not guaranteed |
| 206 |
by this function to be the same as the desired value. Obviously, this |
| 207 |
function can only be used for data whose input and output are the |
| 208 |
same size and format, and their size and format are known in advance.*/ |
| 209 |
PaError AudioDeviceSetPropertyNowAndWaitForChange( |
| 210 |
AudioDeviceID inDevice, |
| 211 |
UInt32 inChannel, |
| 212 |
Boolean isInput, |
| 213 |
AudioDevicePropertyID inPropertyID, |
| 214 |
UInt32 inPropertyDataSize, |
| 215 |
const void *inPropertyData, |
| 216 |
void *outPropertyData ); |
| 217 |
|
| 218 |
/* |
| 219 |
* Sets the sample rate the HAL device. |
| 220 |
* if requireExact: set the sample rate or fail. |
| 221 |
* |
| 222 |
* otherwise : set the exact sample rate. |
| 223 |
* If that fails, check for available sample rates, and choose one |
| 224 |
* higher than the requested rate. If there isn't a higher one, |
| 225 |
* just use the highest available. |
| 226 |
*/ |
| 227 |
PaError setBestSampleRateForDevice( const AudioDeviceID device, |
| 228 |
const bool isOutput, |
| 229 |
const bool requireExact, |
| 230 |
const Float64 desiredSrate ); |
| 231 |
/* |
| 232 |
Attempts to set the requestedFramesPerBuffer. If it can't set the exact |
| 233 |
value, it settles for something smaller if available. If nothing smaller |
| 234 |
is available, it uses the smallest available size. |
| 235 |
actualFramesPerBuffer will be set to the actual value on successful return. |
| 236 |
OK to pass NULL to actualFramesPerBuffer. |
| 237 |
The logic is very similar too setBestSampleRate only failure here is |
| 238 |
not usually catastrophic. |
| 239 |
*/ |
| 240 |
PaError setBestFramesPerBuffer( const AudioDeviceID device, |
| 241 |
const bool isOutput, |
| 242 |
UInt32 requestedFramesPerBuffer, |
| 243 |
UInt32 *actualFramesPerBuffer ); |
| 244 |
|
| 245 |
|
| 246 |
/********************* |
| 247 |
* |
| 248 |
* xrun handling |
| 249 |
* |
| 250 |
*********************/ |
| 251 |
|
| 252 |
OSStatus xrunCallback( |
| 253 |
AudioObjectID inObjectID, |
| 254 |
UInt32 inNumberAddresses, |
| 255 |
const AudioObjectPropertyAddress* inAddresses, |
| 256 |
void * inClientData ); |
| 257 |
|
| 258 |
/** returns zero on success or a unix style error code. */ |
| 259 |
int initializeXRunListenerList( void ); |
| 260 |
/** returns zero on success or a unix style error code. */ |
| 261 |
int destroyXRunListenerList( void ); |
| 262 |
|
| 263 |
/**Returns the list, so that it can be passed to CorAudio.*/ |
| 264 |
void *addToXRunListenerList( void *stream ); |
| 265 |
/**Returns the number of Listeners in the list remaining.*/ |
| 266 |
int removeFromXRunListenerList( void *stream ); |
| 267 |
|
| 268 |
#endif /* PA_MAC_CORE_UTILITIES_H__*/ |