PortAudio  2.0
pa_mac_core_internal.h
Go to the documentation of this file.
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 /*
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 
61 #ifndef PA_MAC_CORE_INTERNAL_H__
62 #define PA_MAC_CORE_INTERNAL_H__
63 
64 #include <CoreAudio/CoreAudio.h>
65 #include <CoreServices/CoreServices.h>
66 #include <AudioUnit/AudioUnit.h>
67 #include <AudioToolbox/AudioToolbox.h>
68 
69 #include "portaudio.h"
70 #include "pa_util.h"
71 #include "pa_hostapi.h"
72 #include "pa_stream.h"
73 #include "pa_allocation.h"
74 #include "pa_cpuload.h"
75 #include "pa_process.h"
76 #include "pa_ringbuffer.h"
77 
78 #include "pa_mac_core_blocking.h"
79 
80 /* function prototypes */
81 
82 #ifdef __cplusplus
83 extern "C"
84 {
85 #endif /* __cplusplus */
86 
88 
89 #ifdef __cplusplus
90 }
91 #endif /* __cplusplus */
92 
93 #define RING_BUFFER_ADVANCE_DENOMINATOR (4)
94 
95 PaError ReadStream( PaStream* stream, void *buffer, unsigned long frames );
96 PaError WriteStream( PaStream* stream, const void *buffer, unsigned long frames );
97 signed long GetStreamReadAvailable( PaStream* stream );
98 signed long GetStreamWriteAvailable( PaStream* stream );
99 /* PaMacAUHAL - host api datastructure specific to this implementation */
100 typedef struct
101 {
105 
107 
108  /* implementation specific data goes here */
109  long devCount;
110  AudioDeviceID *devIds; /*array of all audio devices*/
111  AudioDeviceID defaultIn;
112  AudioDeviceID defaultOut;
113 }
114 PaMacAUHAL;
115 
117 {
118  /* Values in Frames from property queries. */
119  UInt32 safetyOffset;
121  // UInt32 streamLatency; // Seems to be the same as deviceLatency!?
123  /* Current device sample rate. May change!
124  These are initialized to the nominal device sample rate,
125  and updated with the actual sample rate, when/where available.
126  Note that these are the *device* sample rates, prior to any required
127  SR conversion. */
128  Float64 sampleRate;
129  Float64 samplePeriod; // reciprocal
130 }
132 
133 /* stream data structure specifically for this implementation */
134 typedef struct PaMacCoreStream
135 {
139 
140  /* implementation specific data goes here */
142  AudioUnit inputUnit;
143  AudioUnit outputUnit;
144  AudioDeviceID inputDevice;
145  AudioDeviceID outputDevice;
146  size_t userInChan;
147  size_t userOutChan;
151  /* We use this ring buffer when input and out devs are different. */
153  /* We may need to do SR conversion on input. */
154  AudioConverterRef inputSRConverter;
155  /* We need to preallocate an inputBuffer for reading data. */
156  AudioBufferList inputAudioBufferList;
157  AudioTimeStamp startTime;
158  /* FIXME: instead of volatile, these should be properly memory barriered */
159  volatile uint32_t xrunFlags; /*PaStreamCallbackFlags*/
160  volatile enum {
161  STOPPED = 0, /* playback is completely stopped,
162  and the user has called StopStream(). */
163  CALLBACK_STOPPED = 1, /* callback has requested stop,
164  but user has not yet called StopStream(). */
165  STOPPING = 2, /* The stream is in the process of closing
166  because the user has called StopStream.
167  This state is just used internally;
168  externally it is indistinguishable from
169  ACTIVE.*/
170  ACTIVE = 3 /* The stream is active and running. */
171  } state;
172  double sampleRate;
175 
176  /* data updated by main thread and notifications, protected by timingInformationMutex */
178  pthread_mutex_t timingInformationMutex;
179 
180  /* These are written by the PA thread or from CoreAudio callbacks. Protected by the mutex. */
184 
185  /* Offsets in seconds to be applied to Apple timestamps to convert them to PA timestamps.
186  * While the io proc is active, the following values are only accessed and manipulated by the ioproc */
190 
191 }
193 
194 #endif /* PA_MAC_CORE_INTERNAL_H__ */
struct PaMacCoreDeviceProperties PaMacCoreDeviceProperties
Float64 samplePeriod
Definition: pa_mac_core_internal.h:129
Single-reader single-writer lock-free ring buffer.
size_t outputFramesPerBuffer
Definition: pa_mac_core_internal.h:149
long devCount
Definition: pa_mac_core_internal.h:109
AudioDeviceID defaultIn
Definition: pa_mac_core_internal.h:111
int PaHostApiIndex
Definition: portaudio.h:238
PaError PaMacCore_Initialize(PaUtilHostApiRepresentation **hostApi, PaHostApiIndex index)
Definition: pa_mac_core.c:718
Definition: pa_cpuload.h:56
Definition: pa_mac_core_internal.h:116
AudioUnit outputUnit
Definition: pa_mac_core_internal.h:143
void PaStream
Definition: portaudio.h:635
Definition: pa_ringbuffer.h:93
Float64 timestampOffsetCombined
Definition: pa_mac_core_internal.h:181
Float64 timestampOffsetInputDevice
Definition: pa_mac_core_internal.h:182
AudioDeviceID * devIds
Definition: pa_mac_core_internal.h:110
volatile uint32_t xrunFlags
Definition: pa_mac_core_internal.h:159
signed long GetStreamWriteAvailable(PaStream *stream)
Definition: pa_mac_core_blocking.c:630
PaError ReadStream(PaStream *stream, void *buffer, unsigned long framesRequested)
Definition: pa_mac_core_blocking.c:419
Float64 timestampOffsetCombined_ioProcCopy
Definition: pa_mac_core_internal.h:187
double sampleRate
Definition: pa_mac_core_internal.h:172
struct PaMacCoreStream PaMacCoreStream
Float64 timestampOffsetOutputDevice_ioProcCopy
Definition: pa_mac_core_internal.h:189
Buffer Processor prototypes. A Buffer Processor performs buffer length adaption, coordinates sample f...
Definition: pa_stream.h:67
Allocation Group prototypes. An Allocation Group makes it easy to allocate multiple blocks of memory ...
Interfaces and representation structures used by pa_front.c to manage and communicate with host API i...
UInt32 deviceLatency
Definition: pa_mac_core_internal.h:122
AudioDeviceID inputDevice
Definition: pa_mac_core_internal.h:144
The portable PortAudio API.
Definition: pa_mac_core_blocking.h:79
AudioBufferList inputAudioBufferList
Definition: pa_mac_core_internal.h:156
size_t userInChan
Definition: pa_mac_core_internal.h:146
PaUtilStreamInterface blockingStreamInterface
Definition: pa_mac_core_internal.h:104
Float64 timestampOffsetOutputDevice
Definition: pa_mac_core_internal.h:183
Stream interfaces, representation structures and helper functions used to interface between pa_front...
PaMacBlio blio
Definition: pa_mac_core_internal.h:150
Definition: pa_mac_core_internal.h:134
PaError WriteStream(PaStream *stream, const void *buffer, unsigned long framesRequested)
Definition: pa_mac_core_blocking.c:498
UInt32 safetyOffset
Definition: pa_mac_core_internal.h:119
Definition: pa_allocation.h:65
AudioDeviceID outputDevice
Definition: pa_mac_core_internal.h:145
PaUtilCpuLoadMeasurer cpuLoadMeasurer
Definition: pa_mac_core_internal.h:137
int PaError
Definition: portaudio.h:121
Definition: pa_stream.h:147
size_t inputFramesPerBuffer
Definition: pa_mac_core_internal.h:148
AudioTimeStamp startTime
Definition: pa_mac_core_internal.h:157
bool bufferProcessorIsInitialized
Definition: pa_mac_core_internal.h:141
pthread_mutex_t timingInformationMutex
Definition: pa_mac_core_internal.h:178
Definition: pa_mac_core_internal.h:165
Definition: pa_mac_core_internal.h:100
PaMacCoreDeviceProperties outputProperties
Definition: pa_mac_core_internal.h:174
PaMacCoreDeviceProperties inputProperties
Definition: pa_mac_core_internal.h:173
Definition: pa_mac_core_internal.h:161
Definition: pa_mac_core_internal.h:163
PaUtilBufferProcessor bufferProcessor
Definition: pa_mac_core_internal.h:138
signed long GetStreamReadAvailable(PaStream *stream)
Definition: pa_mac_core_blocking.c:621
Prototypes for utility functions used by PortAudio implementations.
UInt32 bufferFrameSize
Definition: pa_mac_core_internal.h:120
size_t userOutChan
Definition: pa_mac_core_internal.h:147
PaUtilStreamRepresentation streamRepresentation
Definition: pa_mac_core_internal.h:136
PaUtilAllocationGroup * allocations
Definition: pa_mac_core_internal.h:106
char buffer[NUM_BYTES]
Definition: recplay.c:26
Float64 timestampOffsetInputDevice_ioProcCopy
Definition: pa_mac_core_internal.h:188
The main buffer processor data structure.
Definition: pa_process.h:253
Functions to assist in measuring the CPU utilization of a callback stream. Used to implement the Pa_G...
enum PaMacCoreStream::@0 state
AudioConverterRef inputSRConverter
Definition: pa_mac_core_internal.h:154
AudioDeviceID defaultOut
Definition: pa_mac_core_internal.h:112
PaUtilStreamInterface callbackStreamInterface
Definition: pa_mac_core_internal.h:103
Definition: pa_hostapi.h:194
Definition: pa_mac_core_internal.h:170
Float64 sampleRate
Definition: pa_mac_core_internal.h:128
AudioUnit inputUnit
Definition: pa_mac_core_internal.h:142
int timingInformationMutexIsInitialized
Definition: pa_mac_core_internal.h:177
PaUtilRingBuffer inputRingBuffer
Definition: pa_mac_core_internal.h:152
PaUtilHostApiRepresentation inheritedHostApiRep
Definition: pa_mac_core_internal.h:102