pa_mac_core_internal.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Internal interfaces for PortAudio 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 pa_mac_core
  54. @ingroup hostapi_src
  55. @author Bjorn Roche
  56. @brief AUHAL implementation of PortAudio
  57. */
  58. #ifndef PA_MAC_CORE_INTERNAL_H__
  59. #define PA_MAC_CORE_INTERNAL_H__
  60. #include <CoreAudio/CoreAudio.h>
  61. #include <CoreServices/CoreServices.h>
  62. #include <AudioUnit/AudioUnit.h>
  63. #include <AudioToolbox/AudioToolbox.h>
  64. #include "portaudio.h"
  65. #include "pa_util.h"
  66. #include "pa_hostapi.h"
  67. #include "pa_stream.h"
  68. #include "pa_allocation.h"
  69. #include "pa_cpuload.h"
  70. #include "pa_process.h"
  71. #include "pa_ringbuffer.h"
  72. #include "pa_mac_core_blocking.h"
  73. /* function prototypes */
  74. #ifdef __cplusplus
  75. extern "C"
  76. {
  77. #endif /* __cplusplus */
  78. PaError PaMacCore_Initialize( PaUtilHostApiRepresentation **hostApi, PaHostApiIndex index );
  79. #ifdef __cplusplus
  80. }
  81. #endif /* __cplusplus */
  82. #define RING_BUFFER_ADVANCE_DENOMINATOR (4)
  83. PaError ReadStream( PaStream* stream, void *buffer, unsigned long frames );
  84. PaError WriteStream( PaStream* stream, const void *buffer, unsigned long frames );
  85. signed long GetStreamReadAvailable( PaStream* stream );
  86. signed long GetStreamWriteAvailable( PaStream* stream );
  87. /* PaMacAUHAL - host api datastructure specific to this implementation */
  88. typedef struct
  89. {
  90. PaUtilHostApiRepresentation inheritedHostApiRep;
  91. PaUtilStreamInterface callbackStreamInterface;
  92. PaUtilStreamInterface blockingStreamInterface;
  93. PaUtilAllocationGroup *allocations;
  94. /* implementation specific data goes here */
  95. long devCount;
  96. AudioDeviceID *devIds; /*array of all audio devices*/
  97. AudioDeviceID defaultIn;
  98. AudioDeviceID defaultOut;
  99. }
  100. PaMacAUHAL;
  101. typedef struct PaMacCoreDeviceProperties
  102. {
  103. /* Values in Frames from property queries. */
  104. UInt32 safetyOffset;
  105. UInt32 bufferFrameSize;
  106. // UInt32 streamLatency; // Seems to be the same as deviceLatency!?
  107. UInt32 deviceLatency;
  108. /* Current device sample rate. May change!
  109. These are initialized to the nominal device sample rate,
  110. and updated with the actual sample rate, when/where available.
  111. Note that these are the *device* sample rates, prior to any required
  112. SR conversion. */
  113. Float64 sampleRate;
  114. Float64 samplePeriod; // reciprocal
  115. }
  116. PaMacCoreDeviceProperties;
  117. /* stream data structure specifically for this implementation */
  118. typedef struct PaMacCoreStream
  119. {
  120. PaUtilStreamRepresentation streamRepresentation;
  121. PaUtilCpuLoadMeasurer cpuLoadMeasurer;
  122. PaUtilBufferProcessor bufferProcessor;
  123. /* implementation specific data goes here */
  124. bool bufferProcessorIsInitialized;
  125. AudioUnit inputUnit;
  126. AudioUnit outputUnit;
  127. AudioDeviceID inputDevice;
  128. AudioDeviceID outputDevice;
  129. size_t userInChan;
  130. size_t userOutChan;
  131. size_t inputFramesPerBuffer;
  132. size_t outputFramesPerBuffer;
  133. PaMacBlio blio;
  134. /* We use this ring buffer when input and out devs are different. */
  135. PaUtilRingBuffer inputRingBuffer;
  136. /* We may need to do SR conversion on input. */
  137. AudioConverterRef inputSRConverter;
  138. /* We need to preallocate an inputBuffer for reading data. */
  139. AudioBufferList inputAudioBufferList;
  140. AudioTimeStamp startTime;
  141. /* FIXME: instead of volatile, these should be properly memory barriered */
  142. volatile uint32_t xrunFlags; /*PaStreamCallbackFlags*/
  143. volatile enum {
  144. STOPPED = 0, /* playback is completely stopped,
  145. and the user has called StopStream(). */
  146. CALLBACK_STOPPED = 1, /* callback has requested stop,
  147. but user has not yet called StopStream(). */
  148. STOPPING = 2, /* The stream is in the process of closing
  149. because the user has called StopStream.
  150. This state is just used internally;
  151. externally it is indistinguishable from
  152. ACTIVE.*/
  153. ACTIVE = 3 /* The stream is active and running. */
  154. } state;
  155. double sampleRate;
  156. PaMacCoreDeviceProperties inputProperties;
  157. PaMacCoreDeviceProperties outputProperties;
  158. /* data updated by main thread and notifications, protected by timingInformationMutex */
  159. int timingInformationMutexIsInitialized;
  160. pthread_mutex_t timingInformationMutex;
  161. /* These are written by the PA thread or from CoreAudio callbacks. Protected by the mutex. */
  162. Float64 timestampOffsetCombined;
  163. Float64 timestampOffsetInputDevice;
  164. Float64 timestampOffsetOutputDevice;
  165. /* Offsets in seconds to be applied to Apple timestamps to convert them to PA timestamps.
  166. * While the io proc is active, the following values are only accessed and manipulated by the ioproc */
  167. Float64 timestampOffsetCombined_ioProcCopy;
  168. Float64 timestampOffsetInputDevice_ioProcCopy;
  169. Float64 timestampOffsetOutputDevice_ioProcCopy;
  170. }
  171. PaMacCoreStream;
  172. #endif /* PA_MAC_CORE_INTERNAL_H__ */