comparison src/audio/macosx/SDL_coreaudio.c @ 4189:95213cf5efcc SDL-1.2

Fixed CoreAudio to compile with Mac OS X 10.6 SDK and 64-bit targets.
author Ryan C. Gordon <icculus@icculus.org>
date Sat, 12 Sep 2009 15:04:32 +0000
parents a1b03ba2fcd0
children
comparison
equal deleted inserted replaced
4188:9701c96e2cbb 4189:95213cf5efcc
19 Sam Lantinga 19 Sam Lantinga
20 slouken@libsdl.org 20 slouken@libsdl.org
21 */ 21 */
22 #include "SDL_config.h" 22 #include "SDL_config.h"
23 23
24 #include <CoreAudio/CoreAudio.h>
25 #include <CoreServices/CoreServices.h>
24 #include <AudioUnit/AudioUnit.h> 26 #include <AudioUnit/AudioUnit.h>
25 #ifdef AVAILABLE_MAC_OS_X_VERSION_10_5_AND_LATER 27 #if MAC_OS_X_VERSION_MAX_ALLOWED <= 1050
26 #include <AudioUnit/AUNTComponent.h> 28 #include <AudioUnit/AUNTComponent.h>
27 #endif 29 #endif
28 30
29 #include "SDL_audio.h" 31 #include "SDL_audio.h"
30 #include "../SDL_audio_c.h" 32 #include "../SDL_audio_c.h"
89 "coreaudio", "Mac OS X CoreAudio", 91 "coreaudio", "Mac OS X CoreAudio",
90 Audio_Available, Audio_CreateDevice 92 Audio_Available, Audio_CreateDevice
91 }; 93 };
92 94
93 /* The CoreAudio callback */ 95 /* The CoreAudio callback */
94 static OSStatus audioCallback (void *inRefCon, 96 static OSStatus audioCallback (void *inRefCon,
95 AudioUnitRenderActionFlags inActionFlags, 97 AudioUnitRenderActionFlags *ioActionFlags,
96 const AudioTimeStamp *inTimeStamp, 98 const AudioTimeStamp *inTimeStamp,
97 UInt32 inBusNumber, 99 UInt32 inBusNumber,
98 AudioBuffer *ioData) 100 UInt32 inNumberFrames,
101 AudioBufferList *ioData)
99 { 102 {
100 SDL_AudioDevice *this = (SDL_AudioDevice *)inRefCon; 103 SDL_AudioDevice *this = (SDL_AudioDevice *)inRefCon;
101 UInt32 remaining, len; 104 UInt32 remaining, len;
105 AudioBuffer *abuf;
102 void *ptr; 106 void *ptr;
107 UInt32 i;
103 108
104 /* Only do anything if audio is enabled and not paused */ 109 /* Only do anything if audio is enabled and not paused */
105 if ( ! this->enabled || this->paused ) { 110 if ( ! this->enabled || this->paused ) {
106 SDL_memset(ioData->mData, this->spec.silence, ioData->mDataByteSize); 111 for (i = 0; i < ioData->mNumberBuffers; i++) {
112 abuf = &ioData->mBuffers[i];
113 SDL_memset(abuf->mData, this->spec.silence, abuf->mDataByteSize);
114 }
107 return 0; 115 return 0;
108 } 116 }
109 117
110 /* No SDL conversion should be needed here, ever, since we accept 118 /* No SDL conversion should be needed here, ever, since we accept
111 any input format in OpenAudio, and leave the conversion to CoreAudio. 119 any input format in OpenAudio, and leave the conversion to CoreAudio.
112 */ 120 */
113 /* 121 /*
114 assert(!this->convert.needed); 122 assert(!this->convert.needed);
115 assert(this->spec.channels == ioData->mNumberChannels); 123 assert(this->spec.channels == ioData->mNumberChannels);
116 */ 124 */
117 125
118 remaining = ioData->mDataByteSize; 126 for (i = 0; i < ioData->mNumberBuffers; i++) {
119 ptr = ioData->mData; 127 abuf = &ioData->mBuffers[i];
120 while (remaining > 0) { 128 remaining = abuf->mDataByteSize;
121 if (bufferOffset >= bufferSize) { 129 ptr = abuf->mData;
122 /* Generate the data */ 130 while (remaining > 0) {
123 SDL_memset(buffer, this->spec.silence, bufferSize); 131 if (bufferOffset >= bufferSize) {
124 SDL_mutexP(this->mixer_lock); 132 /* Generate the data */
125 (*this->spec.callback)(this->spec.userdata, 133 SDL_memset(buffer, this->spec.silence, bufferSize);
126 buffer, bufferSize); 134 SDL_mutexP(this->mixer_lock);
127 SDL_mutexV(this->mixer_lock); 135 (*this->spec.callback)(this->spec.userdata,
128 bufferOffset = 0; 136 buffer, bufferSize);
137 SDL_mutexV(this->mixer_lock);
138 bufferOffset = 0;
139 }
140
141 len = bufferSize - bufferOffset;
142 if (len > remaining)
143 len = remaining;
144 SDL_memcpy(ptr, (char *)buffer + bufferOffset, len);
145 ptr = (char *)ptr + len;
146 remaining -= len;
147 bufferOffset += len;
129 } 148 }
130 149 }
131 len = bufferSize - bufferOffset; 150
132 if (len > remaining)
133 len = remaining;
134 SDL_memcpy(ptr, (char *)buffer + bufferOffset, len);
135 ptr = (char *)ptr + len;
136 remaining -= len;
137 bufferOffset += len;
138 }
139
140 return 0; 151 return 0;
141 } 152 }
142 153
143 /* Dummy functions -- we don't use thread-based audio */ 154 /* Dummy functions -- we don't use thread-based audio */
144 void Core_WaitAudio(_THIS) 155 void Core_WaitAudio(_THIS)
157 } 168 }
158 169
159 void Core_CloseAudio(_THIS) 170 void Core_CloseAudio(_THIS)
160 { 171 {
161 OSStatus result; 172 OSStatus result;
162 struct AudioUnitInputCallback callback; 173 struct AURenderCallbackStruct callback;
163 174
164 /* stop processing the audio unit */ 175 /* stop processing the audio unit */
165 result = AudioOutputUnitStop (outputAudioUnit); 176 result = AudioOutputUnitStop (outputAudioUnit);
166 if (result != noErr) { 177 if (result != noErr) {
167 SDL_SetError("Core_CloseAudio: AudioOutputUnitStop"); 178 SDL_SetError("Core_CloseAudio: AudioOutputUnitStop");
170 181
171 /* Remove the input callback */ 182 /* Remove the input callback */
172 callback.inputProc = 0; 183 callback.inputProc = 0;
173 callback.inputProcRefCon = 0; 184 callback.inputProcRefCon = 0;
174 result = AudioUnitSetProperty (outputAudioUnit, 185 result = AudioUnitSetProperty (outputAudioUnit,
175 kAudioUnitProperty_SetInputCallback, 186 kAudioUnitProperty_SetRenderCallback,
176 kAudioUnitScope_Input, 187 kAudioUnitScope_Input,
177 0, 188 0,
178 &callback, 189 &callback,
179 sizeof(callback)); 190 sizeof(callback));
180 if (result != noErr) { 191 if (result != noErr) {
201 int Core_OpenAudio(_THIS, SDL_AudioSpec *spec) 212 int Core_OpenAudio(_THIS, SDL_AudioSpec *spec)
202 { 213 {
203 OSStatus result = noErr; 214 OSStatus result = noErr;
204 Component comp; 215 Component comp;
205 ComponentDescription desc; 216 ComponentDescription desc;
206 struct AudioUnitInputCallback callback; 217 struct AURenderCallbackStruct callback;
207 AudioStreamBasicDescription requestedDesc; 218 AudioStreamBasicDescription requestedDesc;
208 219
209 /* Setup a AudioStreamBasicDescription with the requested format */ 220 /* Setup a AudioStreamBasicDescription with the requested format */
210 requestedDesc.mFormatID = kAudioFormatLinearPCM; 221 requestedDesc.mFormatID = kAudioFormatLinearPCM;
211 requestedDesc.mFormatFlags = kLinearPCMFormatFlagIsPacked; 222 requestedDesc.mFormatFlags = kLinearPCMFormatFlagIsPacked;
222 requestedDesc.mBytesPerFrame = requestedDesc.mBitsPerChannel * requestedDesc.mChannelsPerFrame / 8; 233 requestedDesc.mBytesPerFrame = requestedDesc.mBitsPerChannel * requestedDesc.mChannelsPerFrame / 8;
223 requestedDesc.mBytesPerPacket = requestedDesc.mBytesPerFrame * requestedDesc.mFramesPerPacket; 234 requestedDesc.mBytesPerPacket = requestedDesc.mBytesPerFrame * requestedDesc.mFramesPerPacket;
224 235
225 236
226 /* Locate the default output audio unit */ 237 /* Locate the default output audio unit */
227 desc.componentType = kAudioUnitComponentType; 238 desc.componentType = kAudioUnitType_Output;
228 desc.componentSubType = kAudioUnitSubType_Output; 239 desc.componentSubType = kAudioUnitSubType_DefaultOutput;
229 desc.componentManufacturer = kAudioUnitID_DefaultOutput; 240 desc.componentManufacturer = kAudioUnitManufacturer_Apple;
230 desc.componentFlags = 0; 241 desc.componentFlags = 0;
231 desc.componentFlagsMask = 0; 242 desc.componentFlagsMask = 0;
232 243
233 comp = FindNextComponent (NULL, &desc); 244 comp = FindNextComponent (NULL, &desc);
234 if (comp == NULL) { 245 if (comp == NULL) {
254 265
255 /* Set the audio callback */ 266 /* Set the audio callback */
256 callback.inputProc = audioCallback; 267 callback.inputProc = audioCallback;
257 callback.inputProcRefCon = this; 268 callback.inputProcRefCon = this;
258 result = AudioUnitSetProperty (outputAudioUnit, 269 result = AudioUnitSetProperty (outputAudioUnit,
259 kAudioUnitProperty_SetInputCallback, 270 kAudioUnitProperty_SetRenderCallback,
260 kAudioUnitScope_Input, 271 kAudioUnitScope_Input,
261 0, 272 0,
262 &callback, 273 &callback,
263 sizeof(callback)); 274 sizeof(callback));
264 CHECK_RESULT("AudioUnitSetProperty (kAudioUnitProperty_SetInputCallback)") 275 CHECK_RESULT("AudioUnitSetProperty (kAudioUnitProperty_SetInputCallback)")