00001 /* 00002 * 00003 * pa_mac_core_utilities.c 00004 * 00005 * utilitiy functions for pa_mac_core.c 00006 * 00007 * This functions are more like helper functions than part of the core, 00008 * so I moved them to a separate file so the core code would be cleaner. 00009 * 00010 * by Bjorn Roche. 00011 */ 00012 00013 /* 00014 * Implementation of the PortAudio API for Apple AUHAL 00015 * 00016 * PortAudio Portable Real-Time Audio Library 00017 * Latest Version at: http://www.portaudio.com 00018 * 00019 * Written by Bjorn Roche of XO Audio LLC, from PA skeleton code. 00020 * Portions copied from code by Dominic Mazzoni (who wrote a HAL implementation) 00021 * 00022 * Dominic's code was based on code by Phil Burk, Darren Gibbs, 00023 * Gord Peters, Stephane Letz, and Greg Pfiel. 00024 * 00025 * Bjorn Roche and XO Audio LLC reserve no rights to this code. 00026 * The maintainers of PortAudio may redistribute and modify the code and 00027 * licenses as they deam appropriate. 00028 * 00029 * The following people also deserve acknowledgements: 00030 * 00031 * Olivier Tristan for feedback and testing 00032 * Glenn Zelniker and Z-Systems engineering for sponsoring the Blocking I/O 00033 * interface. 00034 * 00035 * 00036 * Based on the Open Source API proposed by Ross Bencina 00037 * Copyright (c) 1999-2002 Ross Bencina, Phil Burk 00038 * 00039 * Permission is hereby granted, free of charge, to any person obtaining 00040 * a copy of this software and associated documentation files 00041 * (the "Software"), to deal in the Software without restriction, 00042 * including without limitation the rights to use, copy, modify, merge, 00043 * publish, distribute, sublicense, and/or sell copies of the Software, 00044 * and to permit persons to whom the Software is furnished to do so, 00045 * subject to the following conditions: 00046 * 00047 * The above copyright notice and this permission notice shall be 00048 * included in all copies or substantial portions of the Software. 00049 * 00050 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00051 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00052 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 00053 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR 00054 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 00055 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 00056 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00057 */ 00058 00059 /* 00060 * The text above constitutes the entire PortAudio license; however, 00061 * the PortAudio community also makes the following non-binding requests: 00062 * 00063 * Any person wishing to distribute modifications to the Software is 00064 * requested to send the modifications to the original developer so that 00065 * they can be incorporated into the canonical version. It is also 00066 * requested that these non-binding requests be included along with the 00067 * license above. 00068 */ 00069 00070 00071 #ifndef PA_MAC_CORE_UTILITIES_H__ 00072 #define PA_MAC_CORE_UTILITIES_H__ 00073 00074 #include <pthread.h> 00075 #include "portaudio.h" 00076 #include "pa_util.h" 00077 #include <AudioUnit/AudioUnit.h> 00078 #include <AudioToolbox/AudioToolbox.h> 00079 00080 #ifndef MIN 00081 #define MIN(a, b) (((a)<(b))?(a):(b)) 00082 #endif 00083 00084 #ifndef MAX 00085 #define MAX(a, b) (((a)<(b))?(b):(a)) 00086 #endif 00087 00088 #define ERR(mac_error) PaMacCore_SetError(mac_error, __LINE__, 1 ) 00089 #define WARNING(mac_error) PaMacCore_SetError(mac_error, __LINE__, 0 ) 00090 00091 00092 /* Help keep track of AUHAL element numbers */ 00093 #define INPUT_ELEMENT (1) 00094 #define OUTPUT_ELEMENT (0) 00095 00096 /* Normal level of debugging: fine for most apps that don't mind the occational warning being printf'ed */ 00097 /* 00098 */ 00099 #define MAC_CORE_DEBUG 00100 #ifdef MAC_CORE_DEBUG 00101 # define DBUG(MSG) do { printf("||PaMacCore (AUHAL)|| "); printf MSG ; fflush(stdout); } while(0) 00102 #else 00103 # define DBUG(MSG) 00104 #endif 00105 00106 /* Verbose Debugging: useful for developement */ 00107 /* 00108 #define MAC_CORE_VERBOSE_DEBUG 00109 */ 00110 #ifdef MAC_CORE_VERBOSE_DEBUG 00111 # define VDBUG(MSG) do { printf("||PaMacCore (v )|| "); printf MSG ; fflush(stdout); } while(0) 00112 #else 00113 # define VDBUG(MSG) 00114 #endif 00115 00116 /* Very Verbose Debugging: Traces every call. */ 00117 /* 00118 #define MAC_CORE_VERY_VERBOSE_DEBUG 00119 */ 00120 #ifdef MAC_CORE_VERY_VERBOSE_DEBUG 00121 # define VVDBUG(MSG) do { printf("||PaMacCore (vv)|| "); printf MSG ; fflush(stdout); } while(0) 00122 #else 00123 # define VVDBUG(MSG) 00124 #endif 00125 00126 00127 00128 00129 00130 #define UNIX_ERR(err) PaMacCore_SetUnixError( err, __LINE__ ) 00131 00132 PaError PaMacCore_SetUnixError( int err, int line ); 00133 00134 /* 00135 * Translates MacOS generated errors into PaErrors 00136 */ 00137 PaError PaMacCore_SetError(OSStatus error, int line, int isError); 00138 00139 /* 00140 * This function computes an appropriate ring buffer size given 00141 * a requested latency (in seconds), sample rate and framesPerBuffer. 00142 * 00143 * The returned ringBufferSize is computed using the following 00144 * constraints: 00145 * - it must be at least 4. 00146 * - it must be at least 3x framesPerBuffer. 00147 * - it must be at least 2x the suggestedLatency. 00148 * - it must be a power of 2. 00149 * This function attempts to compute the minimum such size. 00150 * 00151 */ 00152 long computeRingBufferSize( const PaStreamParameters *inputParameters, 00153 const PaStreamParameters *outputParameters, 00154 long inputFramesPerBuffer, 00155 long outputFramesPerBuffer, 00156 double sampleRate ); 00157 00158 /* 00159 * Durring testing of core audio, I found that serious crashes could occur 00160 * if properties such as sample rate were changed multiple times in rapid 00161 * succession. The function below has some fancy logic to make sure that changes 00162 * are acknowledged before another is requested. That seems to help a lot. 00163 */ 00164 00165 typedef struct { 00166 bool once; /* I didn't end up using this. bdr */ 00167 pthread_mutex_t mutex; 00168 } MutexAndBool ; 00169 00170 OSStatus propertyProc( 00171 AudioDeviceID inDevice, 00172 UInt32 inChannel, 00173 Boolean isInput, 00174 AudioDevicePropertyID inPropertyID, 00175 void* inClientData ); 00176 00177 /* sets the value of the given property and waits for the change to 00178 be acknowledged, and returns the final value, which is not guaranteed 00179 by this function to be the same as the desired value. Obviously, this 00180 function can only be used for data whose input and output are the 00181 same size and format, and their size and format are known in advance.*/ 00182 PaError AudioDeviceSetPropertyNowAndWaitForChange( 00183 AudioDeviceID inDevice, 00184 UInt32 inChannel, 00185 Boolean isInput, 00186 AudioDevicePropertyID inPropertyID, 00187 UInt32 inPropertyDataSize, 00188 const void *inPropertyData, 00189 void *outPropertyData ); 00190 00191 /* 00192 * Sets the sample rate the HAL device. 00193 * if requireExact: set the sample rate or fail. 00194 * 00195 * otherwise : set the exact sample rate. 00196 * If that fails, check for available sample rates, and choose one 00197 * higher than the requested rate. If there isn't a higher one, 00198 * just use the highest available. 00199 */ 00200 PaError setBestSampleRateForDevice( const AudioDeviceID device, 00201 const bool isOutput, 00202 const bool requireExact, 00203 const Float64 desiredSrate ); 00204 /* 00205 Attempts to set the requestedFramesPerBuffer. If it can't set the exact 00206 value, it settles for something smaller if available. If nothing smaller 00207 is available, it uses the smallest available size. 00208 actualFramesPerBuffer will be set to the actual value on successful return. 00209 OK to pass NULL to actualFramesPerBuffer. 00210 The logic is very simmilar too setBestSampleRate only failure here is 00211 not usually catastrophic. 00212 */ 00213 PaError setBestFramesPerBuffer( const AudioDeviceID device, 00214 const bool isOutput, 00215 unsigned long requestedFramesPerBuffer, 00216 unsigned long *actualFramesPerBuffer ); 00217 #endif /* PA_MAC_CORE_UTILITIES_H__*/