comparison src/audio/macosx/SDL_coreaudio.c @ 1668:4da1ee79c9af SDL-1.3

more tweaking indent options
author Sam Lantinga <slouken@libsdl.org>
date Mon, 29 May 2006 04:04:35 +0000
parents 782fd950bd46
children
comparison
equal deleted inserted replaced
1667:1fddae038bc8 1668:4da1ee79c9af
29 #include "SDL_coreaudio.h" 29 #include "SDL_coreaudio.h"
30 30
31 31
32 /* Audio driver functions */ 32 /* Audio driver functions */
33 33
34 static int Core_OpenAudio (_THIS, SDL_AudioSpec * spec); 34 static int Core_OpenAudio(_THIS, SDL_AudioSpec * spec);
35 static void Core_WaitAudio (_THIS); 35 static void Core_WaitAudio(_THIS);
36 static void Core_PlayAudio (_THIS); 36 static void Core_PlayAudio(_THIS);
37 static Uint8 *Core_GetAudioBuf (_THIS); 37 static Uint8 *Core_GetAudioBuf(_THIS);
38 static void Core_CloseAudio (_THIS); 38 static void Core_CloseAudio(_THIS);
39 39
40 /* Audio driver bootstrap functions */ 40 /* Audio driver bootstrap functions */
41 41
42 static int 42 static int
43 Audio_Available (void) 43 Audio_Available(void)
44 { 44 {
45 return (1); 45 return (1);
46 } 46 }
47 47
48 static void 48 static void
49 Audio_DeleteDevice (SDL_AudioDevice * device) 49 Audio_DeleteDevice(SDL_AudioDevice * device)
50 { 50 {
51 SDL_free (device->hidden); 51 SDL_free(device->hidden);
52 SDL_free (device); 52 SDL_free(device);
53 } 53 }
54 54
55 static SDL_AudioDevice * 55 static SDL_AudioDevice *
56 Audio_CreateDevice (int devindex) 56 Audio_CreateDevice(int devindex)
57 { 57 {
58 SDL_AudioDevice *this; 58 SDL_AudioDevice *this;
59 59
60 /* Initialize all variables that we clean on shutdown */ 60 /* Initialize all variables that we clean on shutdown */
61 this = (SDL_AudioDevice *) SDL_malloc (sizeof (SDL_AudioDevice)); 61 this = (SDL_AudioDevice *) SDL_malloc(sizeof(SDL_AudioDevice));
62 if (this) { 62 if (this) {
63 SDL_memset (this, 0, (sizeof *this)); 63 SDL_memset(this, 0, (sizeof *this));
64 this->hidden = (struct SDL_PrivateAudioData *) 64 this->hidden = (struct SDL_PrivateAudioData *)
65 SDL_malloc ((sizeof *this->hidden)); 65 SDL_malloc((sizeof *this->hidden));
66 } 66 }
67 if ((this == NULL) || (this->hidden == NULL)) { 67 if ((this == NULL) || (this->hidden == NULL)) {
68 SDL_OutOfMemory (); 68 SDL_OutOfMemory();
69 if (this) { 69 if (this) {
70 SDL_free (this); 70 SDL_free(this);
71 } 71 }
72 return (0); 72 return (0);
73 } 73 }
74 SDL_memset (this->hidden, 0, (sizeof *this->hidden)); 74 SDL_memset(this->hidden, 0, (sizeof *this->hidden));
75 75
76 /* Set the function pointers */ 76 /* Set the function pointers */
77 this->OpenAudio = Core_OpenAudio; 77 this->OpenAudio = Core_OpenAudio;
78 this->WaitAudio = Core_WaitAudio; 78 this->WaitAudio = Core_WaitAudio;
79 this->PlayAudio = Core_PlayAudio; 79 this->PlayAudio = Core_PlayAudio;
90 Audio_Available, Audio_CreateDevice 90 Audio_Available, Audio_CreateDevice
91 }; 91 };
92 92
93 /* The CoreAudio callback */ 93 /* The CoreAudio callback */
94 static OSStatus 94 static OSStatus
95 audioCallback (void *inRefCon, 95 audioCallback(void *inRefCon,
96 AudioUnitRenderActionFlags inActionFlags, 96 AudioUnitRenderActionFlags inActionFlags,
97 const AudioTimeStamp * inTimeStamp, 97 const AudioTimeStamp * inTimeStamp,
98 UInt32 inBusNumber, AudioBuffer * ioData) 98 UInt32 inBusNumber, AudioBuffer * ioData)
99 { 99 {
100 SDL_AudioDevice *this = (SDL_AudioDevice *) inRefCon; 100 SDL_AudioDevice *this = (SDL_AudioDevice *) inRefCon;
101 UInt32 remaining, len; 101 UInt32 remaining, len;
102 void *ptr; 102 void *ptr;
103 103
104 /* Only do anything if audio is enabled and not paused */ 104 /* Only do anything if audio is enabled and not paused */
105 if (!this->enabled || this->paused) { 105 if (!this->enabled || this->paused) {
106 SDL_memset (ioData->mData, this->spec.silence, ioData->mDataByteSize); 106 SDL_memset(ioData->mData, this->spec.silence, ioData->mDataByteSize);
107 return 0; 107 return 0;
108 } 108 }
109 109
110 /* No SDL conversion should be needed here, ever, since we accept 110 /* No SDL conversion should be needed here, ever, since we accept
111 any input format in OpenAudio, and leave the conversion to CoreAudio. 111 any input format in OpenAudio, and leave the conversion to CoreAudio.
118 remaining = ioData->mDataByteSize; 118 remaining = ioData->mDataByteSize;
119 ptr = ioData->mData; 119 ptr = ioData->mData;
120 while (remaining > 0) { 120 while (remaining > 0) {
121 if (bufferOffset >= bufferSize) { 121 if (bufferOffset >= bufferSize) {
122 /* Generate the data */ 122 /* Generate the data */
123 SDL_memset (buffer, this->spec.silence, bufferSize); 123 SDL_memset(buffer, this->spec.silence, bufferSize);
124 SDL_mutexP (this->mixer_lock); 124 SDL_mutexP(this->mixer_lock);
125 (*this->spec.callback) (this->spec.userdata, buffer, bufferSize); 125 (*this->spec.callback) (this->spec.userdata, buffer, bufferSize);
126 SDL_mutexV (this->mixer_lock); 126 SDL_mutexV(this->mixer_lock);
127 bufferOffset = 0; 127 bufferOffset = 0;
128 } 128 }
129 129
130 len = bufferSize - bufferOffset; 130 len = bufferSize - bufferOffset;
131 if (len > remaining) 131 if (len > remaining)
132 len = remaining; 132 len = remaining;
133 SDL_memcpy (ptr, (char *) buffer + bufferOffset, len); 133 SDL_memcpy(ptr, (char *) buffer + bufferOffset, len);
134 ptr = (char *) ptr + len; 134 ptr = (char *) ptr + len;
135 remaining -= len; 135 remaining -= len;
136 bufferOffset += len; 136 bufferOffset += len;
137 } 137 }
138 138
139 return 0; 139 return 0;
140 } 140 }
141 141
142 /* Dummy functions -- we don't use thread-based audio */ 142 /* Dummy functions -- we don't use thread-based audio */
143 void 143 void
144 Core_WaitAudio (_THIS) 144 Core_WaitAudio(_THIS)
145 { 145 {
146 return; 146 return;
147 } 147 }
148 148
149 void 149 void
150 Core_PlayAudio (_THIS) 150 Core_PlayAudio(_THIS)
151 { 151 {
152 return; 152 return;
153 } 153 }
154 154
155 Uint8 * 155 Uint8 *
156 Core_GetAudioBuf (_THIS) 156 Core_GetAudioBuf(_THIS)
157 { 157 {
158 return (NULL); 158 return (NULL);
159 } 159 }
160 160
161 void 161 void
162 Core_CloseAudio (_THIS) 162 Core_CloseAudio(_THIS)
163 { 163 {
164 OSStatus result; 164 OSStatus result;
165 struct AudioUnitInputCallback callback; 165 struct AudioUnitInputCallback callback;
166 166
167 /* stop processing the audio unit */ 167 /* stop processing the audio unit */
168 result = AudioOutputUnitStop (outputAudioUnit); 168 result = AudioOutputUnitStop(outputAudioUnit);
169 if (result != noErr) { 169 if (result != noErr) {
170 SDL_SetError ("Core_CloseAudio: AudioOutputUnitStop"); 170 SDL_SetError("Core_CloseAudio: AudioOutputUnitStop");
171 return; 171 return;
172 } 172 }
173 173
174 /* Remove the input callback */ 174 /* Remove the input callback */
175 callback.inputProc = 0; 175 callback.inputProc = 0;
176 callback.inputProcRefCon = 0; 176 callback.inputProcRefCon = 0;
177 result = AudioUnitSetProperty (outputAudioUnit, 177 result = AudioUnitSetProperty(outputAudioUnit,
178 kAudioUnitProperty_SetInputCallback, 178 kAudioUnitProperty_SetInputCallback,
179 kAudioUnitScope_Input, 179 kAudioUnitScope_Input,
180 0, &callback, sizeof (callback)); 180 0, &callback, sizeof(callback));
181 if (result != noErr) { 181 if (result != noErr) {
182 SDL_SetError 182 SDL_SetError
183 ("Core_CloseAudio: AudioUnitSetProperty (kAudioUnitProperty_SetInputCallback)"); 183 ("Core_CloseAudio: AudioUnitSetProperty (kAudioUnitProperty_SetInputCallback)");
184 return; 184 return;
185 } 185 }
186 186
187 result = CloseComponent (outputAudioUnit); 187 result = CloseComponent(outputAudioUnit);
188 if (result != noErr) { 188 if (result != noErr) {
189 SDL_SetError ("Core_CloseAudio: CloseComponent"); 189 SDL_SetError("Core_CloseAudio: CloseComponent");
190 return; 190 return;
191 } 191 }
192 192
193 SDL_free (buffer); 193 SDL_free(buffer);
194 } 194 }
195 195
196 #define CHECK_RESULT(msg) \ 196 #define CHECK_RESULT(msg) \
197 if (result != noErr) { \ 197 if (result != noErr) { \
198 SDL_SetError("Failed to start CoreAudio: " msg); \ 198 SDL_SetError("Failed to start CoreAudio: " msg); \
199 return -1; \ 199 return -1; \
200 } 200 }
201 201
202 202
203 int 203 int
204 Core_OpenAudio (_THIS, SDL_AudioSpec * spec) 204 Core_OpenAudio(_THIS, SDL_AudioSpec * spec)
205 { 205 {
206 OSStatus result = noErr; 206 OSStatus result = noErr;
207 Component comp; 207 Component comp;
208 ComponentDescription desc; 208 ComponentDescription desc;
209 struct AudioUnitInputCallback callback; 209 struct AudioUnitInputCallback callback;
233 desc.componentSubType = kAudioUnitSubType_Output; 233 desc.componentSubType = kAudioUnitSubType_Output;
234 desc.componentManufacturer = kAudioUnitID_DefaultOutput; 234 desc.componentManufacturer = kAudioUnitID_DefaultOutput;
235 desc.componentFlags = 0; 235 desc.componentFlags = 0;
236 desc.componentFlagsMask = 0; 236 desc.componentFlagsMask = 0;
237 237
238 comp = FindNextComponent (NULL, &desc); 238 comp = FindNextComponent(NULL, &desc);
239 if (comp == NULL) { 239 if (comp == NULL) {
240 SDL_SetError 240 SDL_SetError
241 ("Failed to start CoreAudio: FindNextComponent returned NULL"); 241 ("Failed to start CoreAudio: FindNextComponent returned NULL");
242 return -1; 242 return -1;
243 } 243 }
244 244
245 /* Open & initialize the default output audio unit */ 245 /* Open & initialize the default output audio unit */
246 result = OpenAComponent (comp, &outputAudioUnit); 246 result = OpenAComponent(comp, &outputAudioUnit);
247 CHECK_RESULT ("OpenAComponent") 247 CHECK_RESULT("OpenAComponent")
248 result = AudioUnitInitialize (outputAudioUnit); 248 result = AudioUnitInitialize(outputAudioUnit);
249 CHECK_RESULT ("AudioUnitInitialize") 249 CHECK_RESULT("AudioUnitInitialize")
250 /* Set the input format of the audio unit. */ 250 /* Set the input format of the audio unit. */
251 result = AudioUnitSetProperty (outputAudioUnit, 251 result = AudioUnitSetProperty(outputAudioUnit,
252 kAudioUnitProperty_StreamFormat, 252 kAudioUnitProperty_StreamFormat,
253 kAudioUnitScope_Input, 253 kAudioUnitScope_Input,
254 0, 254 0,
255 &requestedDesc, 255 &requestedDesc, sizeof(requestedDesc));
256 sizeof (requestedDesc)); 256 CHECK_RESULT("AudioUnitSetProperty (kAudioUnitProperty_StreamFormat)")
257 CHECK_RESULT ("AudioUnitSetProperty (kAudioUnitProperty_StreamFormat)")
258 /* Set the audio callback */ 257 /* Set the audio callback */
259 callback.inputProc = audioCallback; 258 callback.inputProc = audioCallback;
260 callback.inputProcRefCon = this; 259 callback.inputProcRefCon = this;
261 result = AudioUnitSetProperty (outputAudioUnit, 260 result = AudioUnitSetProperty(outputAudioUnit,
262 kAudioUnitProperty_SetInputCallback, 261 kAudioUnitProperty_SetInputCallback,
263 kAudioUnitScope_Input, 262 kAudioUnitScope_Input,
264 0, &callback, sizeof (callback)); 263 0, &callback, sizeof(callback));
265 CHECK_RESULT 264 CHECK_RESULT("AudioUnitSetProperty (kAudioUnitProperty_SetInputCallback)")
266 ("AudioUnitSetProperty (kAudioUnitProperty_SetInputCallback)")
267 /* Calculate the final parameters for this audio specification */ 265 /* Calculate the final parameters for this audio specification */
268 SDL_CalculateAudioSpec (spec); 266 SDL_CalculateAudioSpec(spec);
269 267
270 /* Allocate a sample buffer */ 268 /* Allocate a sample buffer */
271 bufferOffset = bufferSize = this->spec.size; 269 bufferOffset = bufferSize = this->spec.size;
272 buffer = SDL_malloc (bufferSize); 270 buffer = SDL_malloc(bufferSize);
273 271
274 /* Finally, start processing of the audio unit */ 272 /* Finally, start processing of the audio unit */
275 result = AudioOutputUnitStart (outputAudioUnit); 273 result = AudioOutputUnitStart(outputAudioUnit);
276 CHECK_RESULT ("AudioOutputUnitStart") 274 CHECK_RESULT("AudioOutputUnitStart")
277 /* We're running! */ 275 /* We're running! */
278 return (1); 276 return (1);
279 } 277 }
280 278
281 /* vi: set ts=4 sw=4 expandtab: */ 279 /* vi: set ts=4 sw=4 expandtab: */