1 /*
2  * This is the AUHAL implementation of portaudio.
3  *
4  * Written by Bjorn Roche of XO Audio LLC, from PA skeleton code.
5  * Portions copied from code by Dominic Mazzoni (who wrote a HAL implementation)
6  *
7  * Dominic's code was based on code by Phil Burk, Darren Gibbs,
8  * Gord Peters, Stephane Letz, and Greg Pfiel.
9  *
10  * Bjorn Roche and XO Audio LLC reserve no rights to this code.
11  * The maintainers of PortAudio may redistribute and modify the code and
12  * licenses as they deam appropriate.
13  *
14  * The following people also deserve acknowledgements:
15  *
16  * Olivier Tristan for feedback and testing
17  * Glenn Zelniker and Z-Systems engineering for sponsoring the Blocking I/O
18  * interface.
19  *
20  *
21  * Based on the Open Source API proposed by Ross Bencina
22  * Copyright (c) 1999-2002 Ross Bencina, Phil Burk
23  *
24  * Permission is hereby granted, free of charge, to any person obtaining
25  * a copy of this software and associated documentation files
26  * (the "Software"), to deal in the Software without restriction,
27  * including without limitation the rights to use, copy, modify, merge,
28  * publish, distribute, sublicense, and/or sell copies of the Software,
29  * and to permit persons to whom the Software is furnished to do so,
30  * subject to the following conditions:
31  *
32  * The above copyright notice and this permission notice shall be
33  * included in all copies or substantial portions of the Software.
34  *
35  * Any person wishing to distribute modifications to the Software is
36  * requested to send the modifications to the original developer so that
37  * they can be incorporated into the canonical version.
38  *
39  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
40  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
41  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
42  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
43  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
44  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
45  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
46  */
47 
48 /**
49  @file pa_mac_core
50  @author Bjorn Roche
51  @brief AUHAL implementation of PortAudio
52 */
53 
54 #ifndef PA_MAC_CORE_INTERNAL_H__
55 #define PA_MAC_CORE_INTERNAL_H__
56 
57 #include <AudioUnit/AudioUnit.h>
58 #include <AudioToolbox/AudioToolbox.h>
59 
60 
61 #include "portaudio.h"
62 #include "pa_util.h"
63 #include "pa_hostapi.h"
64 #include "pa_stream.h"
65 #include "pa_allocation.h"
66 #include "pa_cpuload.h"
67 #include "pa_process.h"
68 #include "ringbuffer.h"
69 
70 #include "pa_mac_core_blocking.h"
71 
72 /* function prototypes */
73 
74 #ifdef __cplusplus
75 extern "C"
76 {
77 #endif /* __cplusplus */
78 
79 PaError PaMacCore_Initialize( PaUtilHostApiRepresentation **hostApi, PaHostApiIndex index );
80 
81 #ifdef __cplusplus
82 }
83 #endif /* __cplusplus */
84 
85 #define RING_BUFFER_ADVANCE_DENOMINATOR (4)
86 
87 PaError ReadStream( PaStream* stream, void *buffer, unsigned long frames );
88 PaError WriteStream( PaStream* stream, const void *buffer, unsigned long frames );
89 signed long GetStreamReadAvailable( PaStream* stream );
90 signed long GetStreamWriteAvailable( PaStream* stream );
91 /* PaMacAUHAL - host api datastructure specific to this implementation */
92 typedef struct
93 {
94     PaUtilHostApiRepresentation inheritedHostApiRep;
95     PaUtilStreamInterface callbackStreamInterface;
96     PaUtilStreamInterface blockingStreamInterface;
97 
98     PaUtilAllocationGroup *allocations;
99 
100     /* implementation specific data goes here */
101     long devCount;
102     AudioDeviceID *devIds; /*array of all audio devices*/
103     AudioDeviceID defaultIn;
104     AudioDeviceID defaultOut;
105 }
106 PaMacAUHAL;
107 
108 
109 
110 /* stream data structure specifically for this implementation */
111 typedef struct PaMacCoreStream
112 {
113     PaUtilStreamRepresentation streamRepresentation;
114     PaUtilCpuLoadMeasurer cpuLoadMeasurer;
115     PaUtilBufferProcessor bufferProcessor;
116 
117     /* implementation specific data goes here */
118     bool bufferProcessorIsInitialized;
119     AudioUnit inputUnit;
120     AudioUnit outputUnit;
121     AudioDeviceID inputDevice;
122     AudioDeviceID outputDevice;
123     size_t userInChan;
124     size_t userOutChan;
125     size_t inputFramesPerBuffer;
126     size_t outputFramesPerBuffer;
127     PaMacBlio blio;
128     /* We use this ring buffer when input and out devs are different. */
129     RingBuffer inputRingBuffer;
130     /* We may need to do SR conversion on input. */
131     AudioConverterRef inputSRConverter;
132     /* We need to preallocate an inputBuffer for reading data. */
133     AudioBufferList inputAudioBufferList;
134     AudioTimeStamp startTime;
135     volatile PaStreamCallbackFlags xrunFlags;
136     volatile enum {
137        STOPPED          = 0, /* playback is completely stopped,
138                                 and the user has called StopStream(). */
139        CALLBACK_STOPPED = 1, /* callback has requested stop,
140                                 but user has not yet called StopStream(). */
141        STOPPING         = 2, /* The stream is in the process of closing.
142                                 This state is just used internally;
143                                 externally it is indistinguishable from
144                                 ACTIVE.*/
145        ACTIVE           = 3  /* The stream is active and running. */
146     } state;
147     double sampleRate;
148 }
149 PaMacCoreStream;
150 
151 #endif /* PA_MAC_CORE_INTERNAL_H__ */
152