pa_mac_core_utilities.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. * The text above constitutes the entire PortAudio license; however,
  44. * the PortAudio community also makes the following non-binding requests:
  45. *
  46. * Any person wishing to distribute modifications to the Software is
  47. * requested to send the modifications to the original developer so that
  48. * they can be incorporated into the canonical version. It is also
  49. * requested that these non-binding requests be included along with the
  50. * license above.
  51. */
  52. /**
  53. @file
  54. @ingroup hostapi_src
  55. */
  56. #ifndef PA_MAC_CORE_UTILITIES_H__
  57. #define PA_MAC_CORE_UTILITIES_H__
  58. #include <pthread.h>
  59. #include "portaudio.h"
  60. #include "pa_util.h"
  61. #include <AudioUnit/AudioUnit.h>
  62. #include <AudioToolbox/AudioToolbox.h>
  63. #ifndef MIN
  64. #define MIN(a, b) (((a)<(b))?(a):(b))
  65. #endif
  66. #ifndef MAX
  67. #define MAX(a, b) (((a)<(b))?(b):(a))
  68. #endif
  69. #define ERR(mac_error) PaMacCore_SetError(mac_error, __LINE__, 1 )
  70. #define WARNING(mac_error) PaMacCore_SetError(mac_error, __LINE__, 0 )
  71. /* Help keep track of AUHAL element numbers */
  72. #define INPUT_ELEMENT (1)
  73. #define OUTPUT_ELEMENT (0)
  74. /* Normal level of debugging: fine for most apps that don't mind the occational warning being printf'ed */
  75. /*
  76. */
  77. #define MAC_CORE_DEBUG
  78. #ifdef MAC_CORE_DEBUG
  79. # define DBUG(MSG) do { printf("||PaMacCore (AUHAL)|| "); printf MSG ; fflush(stdout); } while(0)
  80. #else
  81. # define DBUG(MSG)
  82. #endif
  83. /* Verbose Debugging: useful for developement */
  84. /*
  85. #define MAC_CORE_VERBOSE_DEBUG
  86. */
  87. #ifdef MAC_CORE_VERBOSE_DEBUG
  88. # define VDBUG(MSG) do { printf("||PaMacCore (v )|| "); printf MSG ; fflush(stdout); } while(0)
  89. #else
  90. # define VDBUG(MSG)
  91. #endif
  92. /* Very Verbose Debugging: Traces every call. */
  93. /*
  94. #define MAC_CORE_VERY_VERBOSE_DEBUG
  95. */
  96. #ifdef MAC_CORE_VERY_VERBOSE_DEBUG
  97. # define VVDBUG(MSG) do { printf("||PaMacCore (vv)|| "); printf MSG ; fflush(stdout); } while(0)
  98. #else
  99. # define VVDBUG(MSG)
  100. #endif
  101. #define UNIX_ERR(err) PaMacCore_SetUnixError( err, __LINE__ )
  102. PaError PaMacCore_SetUnixError( int err, int line );
  103. /*
  104. * Translates MacOS generated errors into PaErrors
  105. */
  106. PaError PaMacCore_SetError(OSStatus error, int line, int isError);
  107. /*
  108. * This function computes an appropriate ring buffer size given
  109. * a requested latency (in seconds), sample rate and framesPerBuffer.
  110. *
  111. * The returned ringBufferSize is computed using the following
  112. * constraints:
  113. * - it must be at least 4.
  114. * - it must be at least 3x framesPerBuffer.
  115. * - it must be at least 2x the suggestedLatency.
  116. * - it must be a power of 2.
  117. * This function attempts to compute the minimum such size.
  118. *
  119. */
  120. long computeRingBufferSize( const PaStreamParameters *inputParameters,
  121. const PaStreamParameters *outputParameters,
  122. long inputFramesPerBuffer,
  123. long outputFramesPerBuffer,
  124. double sampleRate );
  125. OSStatus propertyProc(
  126. AudioDeviceID inDevice,
  127. UInt32 inChannel,
  128. Boolean isInput,
  129. AudioDevicePropertyID inPropertyID,
  130. void* inClientData );
  131. /* sets the value of the given property and waits for the change to
  132. be acknowledged, and returns the final value, which is not guaranteed
  133. by this function to be the same as the desired value. Obviously, this
  134. function can only be used for data whose input and output are the
  135. same size and format, and their size and format are known in advance.*/
  136. PaError AudioDeviceSetPropertyNowAndWaitForChange(
  137. AudioDeviceID inDevice,
  138. UInt32 inChannel,
  139. Boolean isInput,
  140. AudioDevicePropertyID inPropertyID,
  141. UInt32 inPropertyDataSize,
  142. const void *inPropertyData,
  143. void *outPropertyData );
  144. /*
  145. * Sets the sample rate the HAL device.
  146. * if requireExact: set the sample rate or fail.
  147. *
  148. * otherwise : set the exact sample rate.
  149. * If that fails, check for available sample rates, and choose one
  150. * higher than the requested rate. If there isn't a higher one,
  151. * just use the highest available.
  152. */
  153. PaError setBestSampleRateForDevice( const AudioDeviceID device,
  154. const bool isOutput,
  155. const bool requireExact,
  156. const Float64 desiredSrate );
  157. /*
  158. Attempts to set the requestedFramesPerBuffer. If it can't set the exact
  159. value, it settles for something smaller if available. If nothing smaller
  160. is available, it uses the smallest available size.
  161. actualFramesPerBuffer will be set to the actual value on successful return.
  162. OK to pass NULL to actualFramesPerBuffer.
  163. The logic is very simmilar too setBestSampleRate only failure here is
  164. not usually catastrophic.
  165. */
  166. PaError setBestFramesPerBuffer( const AudioDeviceID device,
  167. const bool isOutput,
  168. UInt32 requestedFramesPerBuffer,
  169. UInt32 *actualFramesPerBuffer );
  170. /*********************
  171. *
  172. * xrun handling
  173. *
  174. *********************/
  175. OSStatus xrunCallback(
  176. AudioDeviceID inDevice,
  177. UInt32 inChannel,
  178. Boolean isInput,
  179. AudioDevicePropertyID inPropertyID,
  180. void* inClientData ) ;
  181. /** returns zero on success or a unix style error code. */
  182. int initializeXRunListenerList();
  183. /** returns zero on success or a unix style error code. */
  184. int destroyXRunListenerList();
  185. /**Returns the list, so that it can be passed to CorAudio.*/
  186. void *addToXRunListenerList( void *stream );
  187. /**Returns the number of Listeners in the list remaining.*/
  188. int removeFromXRunListenerList( void *stream );
  189. #endif /* PA_MAC_CORE_UTILITIES_H__*/