summaryrefslogtreecommitdiff
path: root/portaudio/src/hostapi/coreaudio/pa_mac_core_utilities.h
blob: c305f17d95a8803e7a6bfdcffcfb6088c00140dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
 * Helper and utility functions for pa_mac_core.c (Apple AUHAL implementation)
 *
 * PortAudio Portable Real-Time Audio Library
 * Latest Version at: http://www.portaudio.com
 *
 * Written by Bjorn Roche of XO Audio LLC, from PA skeleton code.
 * Portions copied from code by Dominic Mazzoni (who wrote a HAL implementation)
 *
 * Dominic's code was based on code by Phil Burk, Darren Gibbs,
 * Gord Peters, Stephane Letz, and Greg Pfiel.
 *
 * The following people also deserve acknowledgements:
 *
 * Olivier Tristan for feedback and testing
 * Glenn Zelniker and Z-Systems engineering for sponsoring the Blocking I/O
 * interface.
 *
 *
 * Based on the Open Source API proposed by Ross Bencina
 * Copyright (c) 1999-2002 Ross Bencina, Phil Burk
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files
 * (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/*
 * The text above constitutes the entire PortAudio license; however,
 * the PortAudio community also makes the following non-binding requests:
 *
 * Any person wishing to distribute modifications to the Software is
 * requested to send the modifications to the original developer so that
 * they can be incorporated into the canonical version. It is also
 * requested that these non-binding requests be included along with the
 * license above.
 */

/**
 @file
 @ingroup hostapi_src
*/

#ifndef PA_MAC_CORE_UTILITIES_H__
#define PA_MAC_CORE_UTILITIES_H__

#include <pthread.h>
#include "portaudio.h"
#include "pa_util.h"
#include <AudioUnit/AudioUnit.h>
#include <AudioToolbox/AudioToolbox.h>

#ifndef MIN
#define MIN(a, b)  (((a)<(b))?(a):(b))
#endif

#ifndef MAX
#define MAX(a, b)  (((a)<(b))?(b):(a))
#endif

#define ERR(mac_error) PaMacCore_SetError(mac_error, __LINE__, 1 )
#define WARNING(mac_error) PaMacCore_SetError(mac_error, __LINE__, 0 )


/* Help keep track of AUHAL element numbers */
#define INPUT_ELEMENT  (1)
#define OUTPUT_ELEMENT (0)

/* Normal level of debugging: fine for most apps that don't mind the occasional warning being printf'ed */
/*
 */
#define MAC_CORE_DEBUG
#ifdef MAC_CORE_DEBUG
# define DBUG(MSG) do { printf("||PaMacCore (AUHAL)|| "); printf MSG ; fflush(stdout); } while(0)
#else
# define DBUG(MSG)
#endif

/* Verbose Debugging: useful for development */
/*
#define MAC_CORE_VERBOSE_DEBUG
*/
#ifdef MAC_CORE_VERBOSE_DEBUG
# define VDBUG(MSG) do { printf("||PaMacCore (v )|| "); printf MSG ; fflush(stdout); } while(0)
#else
# define VDBUG(MSG)
#endif

/* Very Verbose Debugging: Traces every call. */
/*
#define MAC_CORE_VERY_VERBOSE_DEBUG
 */
#ifdef MAC_CORE_VERY_VERBOSE_DEBUG
# define VVDBUG(MSG) do { printf("||PaMacCore (vv)|| "); printf MSG ; fflush(stdout); } while(0)
#else
# define VVDBUG(MSG)
#endif

OSStatus PaMacCore_AudioHardwareGetProperty(
    AudioHardwarePropertyID inPropertyID,
    UInt32*                 ioPropertyDataSize,
    void*                   outPropertyData );

OSStatus PaMacCore_AudioHardwareGetPropertySize(
    AudioHardwarePropertyID inPropertyID,
    UInt32*                 outSize );

OSStatus PaMacCore_AudioDeviceGetProperty(
    AudioDeviceID         inDevice,
    UInt32                inChannel,
    Boolean               isInput,
    AudioDevicePropertyID inPropertyID,
    UInt32*               ioPropertyDataSize,
    void*                 outPropertyData );

OSStatus PaMacCore_AudioDeviceSetProperty(
    AudioDeviceID         inDevice,
    const AudioTimeStamp* inWhen,
    UInt32                inChannel,
    Boolean               isInput,
    AudioDevicePropertyID inPropertyID,
    UInt32                inPropertyDataSize,
    const void*           inPropertyData );

OSStatus PaMacCore_AudioDeviceGetPropertySize(
    AudioDeviceID         inDevice,
    UInt32                inChannel,
    Boolean               isInput,
    AudioDevicePropertyID inPropertyID,
    UInt32*               outSize );

OSStatus PaMacCore_AudioDeviceAddPropertyListener(
    AudioDeviceID                   inDevice,
    UInt32                          inChannel,
    Boolean                         isInput,
    AudioDevicePropertyID           inPropertyID,
    AudioObjectPropertyListenerProc inProc,
    void*                           inClientData );

OSStatus PaMacCore_AudioDeviceRemovePropertyListener(
    AudioDeviceID                   inDevice,
    UInt32                          inChannel,
    Boolean                         isInput,
    AudioDevicePropertyID           inPropertyID,
    AudioObjectPropertyListenerProc inProc,
    void*                           inClientData );

OSStatus PaMacCore_AudioStreamGetProperty(
    AudioStreamID         inStream,
    UInt32                inChannel,
    AudioDevicePropertyID inPropertyID,
    UInt32*               ioPropertyDataSize,
    void*                 outPropertyData );

#define UNIX_ERR(err) PaMacCore_SetUnixError( err, __LINE__ )

PaError PaMacCore_SetUnixError( int err, int line );

/*
 * Translates MacOS generated errors into PaErrors
 */
PaError PaMacCore_SetError(OSStatus error, int line, int isError);

/*
 * This function computes an appropriate ring buffer size given
 * a requested latency (in seconds), sample rate and framesPerBuffer.
 *
 * The returned ringBufferSize is computed using the following
 * constraints:
 *   - it must be at least 4.
 *   - it must be at least 3x framesPerBuffer.
 *   - it must be at least 2x the suggestedLatency.
 *   - it must be a power of 2.
 * This function attempts to compute the minimum such size.
 *
 */
long computeRingBufferSize( const PaStreamParameters *inputParameters,
                            const PaStreamParameters *outputParameters,
                            long inputFramesPerBuffer,
                            long outputFramesPerBuffer,
                            double sampleRate );

OSStatus propertyProc(
        AudioObjectID inObjectID,
        UInt32 inNumberAddresses,
        const AudioObjectPropertyAddress* inAddresses,
        void* inClientData );

/* sets the value of the given property and waits for the change to
   be acknowledged, and returns the final value, which is not guaranteed
   by this function to be the same as the desired value. Obviously, this
   function can only be used for data whose input and output are the
   same size and format, and their size and format are known in advance.*/
PaError AudioDeviceSetPropertyNowAndWaitForChange(
        AudioDeviceID inDevice,
        UInt32 inChannel,
        Boolean isInput,
        AudioDevicePropertyID inPropertyID,
        UInt32 inPropertyDataSize,
        const void *inPropertyData,
        void *outPropertyData );

/*
 * Sets the sample rate the HAL device.
 * if requireExact: set the sample rate or fail.
 *
 * otherwise      : set the exact sample rate.
 *             If that fails, check for available sample rates, and choose one
 *             higher than the requested rate. If there isn't a higher one,
 *             just use the highest available.
 */
PaError setBestSampleRateForDevice( const AudioDeviceID device,
                                    const bool isOutput,
                                    const bool requireExact,
                                    const Float64 desiredSrate );
/*
   Attempts to set the requestedFramesPerBuffer. If it can't set the exact
   value, it settles for something smaller if available. If nothing smaller
   is available, it uses the smallest available size.
   actualFramesPerBuffer will be set to the actual value on successful return.
   OK to pass NULL to actualFramesPerBuffer.
   The logic is very similar too setBestSampleRate only failure here is
   not usually catastrophic.
*/
PaError setBestFramesPerBuffer( const AudioDeviceID device,
                                const bool isOutput,
                                UInt32 requestedFramesPerBuffer,
                                UInt32 *actualFramesPerBuffer );


/*********************
 *
 *  xrun handling
 *
 *********************/

OSStatus xrunCallback(
        AudioObjectID inObjectID,
        UInt32 inNumberAddresses,
        const AudioObjectPropertyAddress* inAddresses,
        void * inClientData );

/** returns zero on success or a unix style error code. */
int initializeXRunListenerList( void );
/** returns zero on success or a unix style error code. */
int destroyXRunListenerList( void );

/**Returns the list, so that it can be passed to CorAudio.*/
void *addToXRunListenerList( void *stream );
/**Returns the number of Listeners in the list remaining.*/
int removeFromXRunListenerList( void *stream );

#endif /* PA_MAC_CORE_UTILITIES_H__*/