| 1 |
/* ______ ___ ___ |
| 2 |
* /\ _ \ /\_ \ /\_ \ |
| 3 |
* \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___ |
| 4 |
* \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\ |
| 5 |
* \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \ |
| 6 |
* \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/ |
| 7 |
* \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/ |
| 8 |
* /\____/ |
| 9 |
* \_/__/ |
| 10 |
* |
| 11 |
* Apple Audio Queue driver |
| 12 |
* |
| 13 |
* By Trent Gamblin. |
| 14 |
* |
| 15 |
* See readme.txt for copyright information. |
| 16 |
*/ |
| 17 |
|
| 18 |
#include <stdio.h> |
| 19 |
|
| 20 |
#include "allegro5/allegro.h" |
| 21 |
#include "allegro5/internal/aintern.h" |
| 22 |
#include "allegro5/allegro_audio.h" |
| 23 |
#include "allegro5/internal/aintern_audio.h" |
| 24 |
#include "allegro5/internal/aintern_audio_cfg.h" |
| 25 |
#include "allegro5/internal/aintern_vector.h" |
| 26 |
|
| 27 |
#import <CoreAudio/CoreAudioTypes.h> |
| 28 |
#import <AudioToolbox/AudioToolbox.h> |
| 29 |
|
| 30 |
#ifdef ALLEGRO_IPHONE |
| 31 |
#import <AVFoundation/AVFoundation.h> |
| 32 |
#endif |
| 33 |
|
| 34 |
#import <Foundation/NSAutoreleasePool.h> |
| 35 |
#import <AudioToolbox/AudioQueue.h> |
| 36 |
|
| 37 |
#define THREAD_BEGIN NSAutoreleasePool *___p = [[NSAutoreleasePool alloc] init]; |
| 38 |
#define THREAD_END [___p drain]; |
| 39 |
|
| 40 |
// Make configurable |
| 41 |
#define BUFFER_SIZE 1024*2 // in samples |
| 42 |
#define NUM_BUFFERS 4 |
| 43 |
|
| 44 |
ALLEGRO_DEBUG_CHANNEL("AudioQueue") |
| 45 |
|
| 46 |
typedef struct ALLEGRO_AQ_DATA { |
| 47 |
int bits_per_sample; |
| 48 |
int channels; |
| 49 |
bool playing; |
| 50 |
unsigned int buffer_size; |
| 51 |
unsigned char *silence; |
| 52 |
ALLEGRO_VOICE *voice; |
| 53 |
AudioQueueRef queue; |
| 54 |
AudioQueueBufferRef buffers[NUM_BUFFERS]; |
| 55 |
} ALLEGRO_AQ_DATA; |
| 56 |
|
| 57 |
static _AL_VECTOR saved_voices = _AL_VECTOR_INITIALIZER(ALLEGRO_VOICE*); |
| 58 |
static _AL_LIST* output_device_list; |
| 59 |
|
| 60 |
/* Audio queue callback */ |
| 61 |
static void handle_buffer( |
| 62 |
void *in_data, |
| 63 |
AudioQueueRef inAQ, |
| 64 |
AudioQueueBufferRef inBuffer) |
| 65 |
{ |
| 66 |
ALLEGRO_AQ_DATA *ex_data = in_data; |
| 67 |
const void *data; |
| 68 |
|
| 69 |
(void)inAQ; // unsused |
| 70 |
|
| 71 |
unsigned int samples = (ex_data->buffer_size / ex_data->channels) / |
| 72 |
(ex_data->bits_per_sample / 8); |
| 73 |
|
| 74 |
data = _al_voice_update(ex_data->voice, ex_data->voice->mutex, &samples); |
| 75 |
if (data == NULL) |
| 76 |
data = ex_data->silence; |
| 77 |
|
| 78 |
unsigned int copy_bytes = samples * ex_data->channels * |
| 79 |
(ex_data->bits_per_sample / 8); |
| 80 |
copy_bytes = _ALLEGRO_MIN(copy_bytes, inBuffer->mAudioDataBytesCapacity); |
| 81 |
|
| 82 |
memcpy(inBuffer->mAudioData, data, copy_bytes); |
| 83 |
inBuffer->mAudioDataByteSize = copy_bytes; |
| 84 |
|
| 85 |
AudioQueueEnqueueBuffer( |
| 86 |
ex_data->queue, |
| 87 |
inBuffer, |
| 88 |
0, |
| 89 |
NULL |
| 90 |
); |
| 91 |
} |
| 92 |
|
| 93 |
#ifdef ALLEGRO_IPHONE |
| 94 |
static int _aqueue_start_voice(ALLEGRO_VOICE *voice); |
| 95 |
static int _aqueue_stop_voice(ALLEGRO_VOICE* voice); |
| 96 |
|
| 97 |
static void interruption_callback(void *inClientData, UInt32 inInterruptionState) |
| 98 |
{ |
| 99 |
unsigned i; |
| 100 |
(void)inClientData; |
| 101 |
for (i = 0; i < _al_vector_size(&saved_voices); i++) { |
| 102 |
ALLEGRO_VOICE **voice = _al_vector_ref(&saved_voices, i); |
| 103 |
if (inInterruptionState == kAudioSessionBeginInterruption) { |
| 104 |
_aqueue_stop_voice(*voice); |
| 105 |
} |
| 106 |
else { |
| 107 |
_aqueue_start_voice(*voice); |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
// This allows plugging/unplugging of hardware/bluetooth/speakers etc while keeping the sound playing |
| 113 |
static void property_listener(void *inClientData, AudioSessionPropertyID inID, UInt32 inDataSize, const void *inData) |
| 114 |
{ |
| 115 |
unsigned i; |
| 116 |
(void)inClientData; |
| 117 |
(void)inDataSize; |
| 118 |
|
| 119 |
if (inID == kAudioSessionProperty_AudioRouteChange) { |
| 120 |
for (i = 0; i < _al_vector_size(&saved_voices); i++) { |
| 121 |
ALLEGRO_VOICE **voice = _al_vector_ref(&saved_voices, i); |
| 122 |
UInt32 reason = (UInt32)inData; |
| 123 |
if (reason == kAudioSessionRouteChangeReason_NewDeviceAvailable) { |
| 124 |
_aqueue_stop_voice(*voice); |
| 125 |
_aqueue_start_voice(*voice); |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
#endif |
| 131 |
|
| 132 |
static bool _aqueue_device_has_scope(AudioObjectID deviceID, AudioObjectPropertyScope scope) |
| 133 |
{ |
| 134 |
AudioObjectPropertyAddress propertyAddress = { |
| 135 |
.mSelector = kAudioDevicePropertyStreamConfiguration, |
| 136 |
.mScope = scope, |
| 137 |
.mElement = kAudioObjectPropertyElementWildcard |
| 138 |
}; |
| 139 |
|
| 140 |
UInt32 dataSize = 0; |
| 141 |
if (AudioObjectGetPropertyDataSize(deviceID, &propertyAddress, 0, NULL, &dataSize) != noErr) { |
| 142 |
return false; |
| 143 |
} |
| 144 |
|
| 145 |
AudioBufferList *bufferList = al_malloc(dataSize); |
| 146 |
if(bufferList == NULL) { |
| 147 |
return false; |
| 148 |
} |
| 149 |
|
| 150 |
if (AudioObjectGetPropertyData(deviceID, &propertyAddress, 0, NULL, &dataSize, bufferList) != noErr) { |
| 151 |
al_free(bufferList); |
| 152 |
return false; |
| 153 |
} |
| 154 |
|
| 155 |
BOOL supportsScope = bufferList->mNumberBuffers > 0; |
| 156 |
al_free(bufferList); |
| 157 |
return supportsScope; |
| 158 |
} |
| 159 |
|
| 160 |
static _AL_LIST* _aqueue_get_output_devices(void) |
| 161 |
{ |
| 162 |
return output_device_list; |
| 163 |
} |
| 164 |
|
| 165 |
static void _output_device_list_dtor(void* value, void* userdata) |
| 166 |
{ |
| 167 |
(void)userdata; |
| 168 |
|
| 169 |
ALLEGRO_AUDIO_DEVICE* device = (ALLEGRO_AUDIO_DEVICE*)value; |
| 170 |
al_free(device->name); |
| 171 |
al_free(device->identifier); |
| 172 |
al_free(device); |
| 173 |
} |
| 174 |
|
| 175 |
static void _aqueue_list_audio_output_devices(void) |
| 176 |
{ |
| 177 |
output_device_list = _al_list_create(); |
| 178 |
|
| 179 |
AudioObjectPropertyAddress propertyAddress; |
| 180 |
AudioObjectID *deviceIDs; |
| 181 |
UInt32 propertySize; |
| 182 |
NSInteger numDevices; |
| 183 |
|
| 184 |
propertyAddress.mSelector = kAudioHardwarePropertyDevices; |
| 185 |
propertyAddress.mScope = kAudioObjectPropertyScopeGlobal; |
| 186 |
propertyAddress.mElement = kAudioObjectPropertyElementMaster; |
| 187 |
|
| 188 |
if (AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &propertySize) != noErr) { |
| 189 |
return; |
| 190 |
} |
| 191 |
|
| 192 |
numDevices = propertySize / sizeof(AudioDeviceID); |
| 193 |
deviceIDs = (AudioDeviceID *)al_calloc(numDevices, sizeof(AudioDeviceID)); |
| 194 |
|
| 195 |
if (AudioObjectGetPropertyData(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &propertySize, deviceIDs) != noErr) { |
| 196 |
return; |
| 197 |
} |
| 198 |
|
| 199 |
for (NSInteger idx=0; idx<numDevices; idx++) { |
| 200 |
AudioObjectPropertyAddress deviceAddress; |
| 201 |
char deviceName[64]; |
| 202 |
|
| 203 |
propertySize = sizeof(deviceName); |
| 204 |
deviceAddress.mSelector = kAudioDevicePropertyDeviceName; |
| 205 |
deviceAddress.mScope = kAudioObjectPropertyScopeGlobal; |
| 206 |
#if (__MAC_OS_X_VERSION_MAX_ALLOWED >= 120000) |
| 207 |
deviceAddress.mElement = kAudioObjectPropertyElementMain; |
| 208 |
#else |
| 209 |
deviceAddress.mElement = kAudioObjectPropertyElementMaster; |
| 210 |
#endif |
| 211 |
if (AudioObjectGetPropertyData(deviceIDs[idx], &deviceAddress, 0, NULL, &propertySize, deviceName) != noErr) { |
| 212 |
continue; |
| 213 |
} |
| 214 |
|
| 215 |
if (_aqueue_device_has_scope(deviceIDs[idx], kAudioObjectPropertyScopeOutput)) { |
| 216 |
ALLEGRO_AUDIO_DEVICE* device = (ALLEGRO_AUDIO_DEVICE*)al_malloc(sizeof(ALLEGRO_AUDIO_DEVICE)); |
| 217 |
device->identifier = (void*)al_malloc(sizeof(AudioDeviceID)); |
| 218 |
device->name = (char*)al_malloc(sizeof(deviceName)); |
| 219 |
|
| 220 |
memcpy(device->identifier, &deviceIDs[idx], sizeof(AudioDeviceID)); |
| 221 |
memcpy(device->name, deviceName, sizeof(deviceName)); |
| 222 |
|
| 223 |
_al_list_push_back_ex(output_device_list, device, _output_device_list_dtor); |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
al_free(deviceIDs); |
| 228 |
} |
| 229 |
|
| 230 |
/* The open method starts up the driver and should lock the device, using the |
| 231 |
previously set paramters, or defaults. It shouldn't need to start sending |
| 232 |
audio data to the device yet, however. */ |
| 233 |
static int _aqueue_open(void) |
| 234 |
{ |
| 235 |
#ifdef ALLEGRO_IPHONE |
| 236 |
/* These settings allow ipod music playback simultaneously with |
| 237 |
* our Allegro music/sfx, and also do not stop the streams when |
| 238 |
* a phone call comes in (it's muted for the duration of the call). |
| 239 |
*/ |
| 240 |
AudioSessionInitialize(NULL, NULL, interruption_callback, NULL); |
| 241 |
|
| 242 |
UInt32 sessionCategory = kAudioSessionCategory_AmbientSound; |
| 243 |
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, |
| 244 |
sizeof(sessionCategory), &sessionCategory); |
| 245 |
|
| 246 |
UInt32 mix = TRUE; |
| 247 |
AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, |
| 248 |
sizeof(mix), &mix); |
| 249 |
|
| 250 |
AudioSessionAddPropertyListener(kAudioSessionProperty_AudioRouteChange, property_listener, NULL); |
| 251 |
#endif |
| 252 |
|
| 253 |
_aqueue_list_audio_output_devices(); |
| 254 |
|
| 255 |
return 0; |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
/* The close method should close the device, freeing any resources, and allow |
| 260 |
other processes to use the device */ |
| 261 |
static void _aqueue_close(void) |
| 262 |
{ |
| 263 |
_al_list_destroy(output_device_list); |
| 264 |
_al_vector_free(&saved_voices); |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
/* The allocate_voice method should grab a voice from the system, and allocate |
| 269 |
any data common to streaming and non-streaming sources. */ |
| 270 |
static int _aqueue_allocate_voice(ALLEGRO_VOICE *voice) |
| 271 |
{ |
| 272 |
ALLEGRO_AQ_DATA *ex_data; |
| 273 |
int bits_per_sample; |
| 274 |
int channels; |
| 275 |
|
| 276 |
switch (voice->depth) |
| 277 |
{ |
| 278 |
case ALLEGRO_AUDIO_DEPTH_UINT16: |
| 279 |
case ALLEGRO_AUDIO_DEPTH_INT16: |
| 280 |
bits_per_sample = 16; |
| 281 |
break; |
| 282 |
default: |
| 283 |
return 1; |
| 284 |
} |
| 285 |
|
| 286 |
switch (voice->chan_conf) { |
| 287 |
case ALLEGRO_CHANNEL_CONF_1: |
| 288 |
channels = 1; |
| 289 |
break; |
| 290 |
case ALLEGRO_CHANNEL_CONF_2: |
| 291 |
channels = 2; |
| 292 |
break; |
| 293 |
default: |
| 294 |
fprintf(stderr, "Unsupported number of channels\n"); |
| 295 |
return 1; |
| 296 |
} |
| 297 |
|
| 298 |
ex_data = (ALLEGRO_AQ_DATA *)al_calloc(1, sizeof(*ex_data)); |
| 299 |
if (!ex_data) { |
| 300 |
fprintf(stderr, "Could not allocate voice data memory\n"); |
| 301 |
return 1; |
| 302 |
} |
| 303 |
|
| 304 |
ex_data->bits_per_sample = bits_per_sample; |
| 305 |
ex_data->channels = channels; |
| 306 |
ex_data->buffer_size = BUFFER_SIZE*channels*(bits_per_sample/8); |
| 307 |
ex_data->playing = false; |
| 308 |
|
| 309 |
voice->extra = ex_data; |
| 310 |
ex_data->voice = voice; |
| 311 |
|
| 312 |
ex_data->silence = al_calloc(1, ex_data->buffer_size); |
| 313 |
|
| 314 |
return 0; |
| 315 |
} |
| 316 |
|
| 317 |
/* The deallocate_voice method should free the resources for the given voice, |
| 318 |
but still retain a hold on the device. The voice should be stopped and |
| 319 |
unloaded by the time this is called */ |
| 320 |
static void _aqueue_deallocate_voice(ALLEGRO_VOICE *voice) |
| 321 |
{ |
| 322 |
ALLEGRO_AQ_DATA *ex_data = voice->extra; |
| 323 |
al_free(ex_data->silence); |
| 324 |
al_free(ex_data); |
| 325 |
voice->extra = NULL; |
| 326 |
} |
| 327 |
|
| 328 |
/* The load_voice method loads a sample into the driver's memory. The voice's |
| 329 |
'streaming' field will be set to false for these voices, and it's |
| 330 |
'buffer_size' field will be the total length in bytes of the sample data. |
| 331 |
The voice's attached sample's looping mode should be honored, and loading |
| 332 |
must fail if it cannot be. */ |
| 333 |
static int _aqueue_load_voice(ALLEGRO_VOICE *voice, const void *data) |
| 334 |
{ |
| 335 |
/* FIXME */ |
| 336 |
(void)voice; |
| 337 |
(void)data; |
| 338 |
return 1; |
| 339 |
} |
| 340 |
|
| 341 |
/* The unload_voice method unloads a sample previously loaded with load_voice. |
| 342 |
This method should not be called on a streaming voice. */ |
| 343 |
static void _aqueue_unload_voice(ALLEGRO_VOICE *voice) |
| 344 |
{ |
| 345 |
/* FIXME */ |
| 346 |
(void)voice; |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
static void *stream_proc(void *in_data) |
| 351 |
{ |
| 352 |
THREAD_BEGIN |
| 353 |
|
| 354 |
ALLEGRO_VOICE *voice = in_data; |
| 355 |
ALLEGRO_AQ_DATA *ex_data = voice->extra; |
| 356 |
|
| 357 |
AudioStreamBasicDescription desc; |
| 358 |
|
| 359 |
desc.mSampleRate = voice->frequency; |
| 360 |
desc.mFormatID = kAudioFormatLinearPCM; |
| 361 |
if (voice->depth == ALLEGRO_AUDIO_DEPTH_INT16) |
| 362 |
desc.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | |
| 363 |
kLinearPCMFormatFlagIsPacked; |
| 364 |
else |
| 365 |
desc.mFormatFlags = kLinearPCMFormatFlagIsFloat | |
| 366 |
kLinearPCMFormatFlagIsPacked; |
| 367 |
#ifdef ALLEGRO_BIG_ENDIAN |
| 368 |
desc.mFormatFlags |= kLinearPCMFormatFlagIsBigEndian; |
| 369 |
#endif |
| 370 |
desc.mBytesPerPacket = ex_data->channels * (ex_data->bits_per_sample/8); |
| 371 |
desc.mFramesPerPacket = 1; |
| 372 |
desc.mBytesPerFrame = ex_data->channels * (ex_data->bits_per_sample/8); |
| 373 |
desc.mChannelsPerFrame = ex_data->channels; |
| 374 |
desc.mBitsPerChannel = ex_data->bits_per_sample; |
| 375 |
|
| 376 |
int ret = AudioQueueNewOutput( |
| 377 |
&desc, |
| 378 |
handle_buffer, |
| 379 |
ex_data, |
| 380 |
CFRunLoopGetCurrent(), |
| 381 |
kCFRunLoopCommonModes, |
| 382 |
0, |
| 383 |
&ex_data->queue); |
| 384 |
(void)ret; // FIXME: handle failure? |
| 385 |
|
| 386 |
int i; |
| 387 |
for (i = 0; i < NUM_BUFFERS; ++i) { |
| 388 |
AudioQueueAllocateBuffer( |
| 389 |
ex_data->queue, |
| 390 |
ex_data->buffer_size, |
| 391 |
&ex_data->buffers[i] |
| 392 |
); |
| 393 |
|
| 394 |
memcpy(ex_data->buffers[i]->mAudioData, ex_data->silence, ex_data->buffer_size); |
| 395 |
ex_data->buffers[i]->mAudioDataByteSize = ex_data->buffer_size; |
| 396 |
ex_data->buffers[i]->mUserData = ex_data; |
| 397 |
// FIXME: Safe to comment this out? |
| 398 |
//ex_data->buffers[i]->mPacketDescriptionCount = 0; |
| 399 |
|
| 400 |
AudioQueueEnqueueBuffer( |
| 401 |
ex_data->queue, |
| 402 |
ex_data->buffers[i], |
| 403 |
0, |
| 404 |
NULL |
| 405 |
); |
| 406 |
} |
| 407 |
|
| 408 |
AudioQueueSetParameter( |
| 409 |
ex_data->queue, |
| 410 |
kAudioQueueParam_Volume, |
| 411 |
1.0f |
| 412 |
); |
| 413 |
|
| 414 |
ret = AudioQueueStart( |
| 415 |
ex_data->queue, |
| 416 |
NULL |
| 417 |
); |
| 418 |
|
| 419 |
do { |
| 420 |
THREAD_BEGIN |
| 421 |
CFRunLoopRunInMode( |
| 422 |
kCFRunLoopDefaultMode, |
| 423 |
0.05, |
| 424 |
false |
| 425 |
); |
| 426 |
THREAD_END |
| 427 |
} while (ex_data->playing); |
| 428 |
|
| 429 |
THREAD_END |
| 430 |
|
| 431 |
return NULL; |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
/* The start_voice should, surprise, start the voice. For streaming voices, it |
| 436 |
should start polling the device and call _al_voice_update for audio data. |
| 437 |
For non-streaming voices, it should resume playing from the last set |
| 438 |
position */ |
| 439 |
static int _aqueue_start_voice(ALLEGRO_VOICE *voice) |
| 440 |
{ |
| 441 |
ALLEGRO_AQ_DATA *ex_data = voice->extra; |
| 442 |
|
| 443 |
if (voice->is_streaming && !ex_data->playing) { |
| 444 |
*(ALLEGRO_VOICE**)_al_vector_alloc_back(&saved_voices) = voice; |
| 445 |
ex_data->playing = true; |
| 446 |
al_run_detached_thread(stream_proc, voice); |
| 447 |
return 0; |
| 448 |
} |
| 449 |
|
| 450 |
/* FIXME */ |
| 451 |
return 1; |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
/* The stop_voice method should stop playback. For non-streaming voices, it |
| 456 |
should leave the data loaded, and reset the voice position to 0. */ |
| 457 |
static int _aqueue_stop_voice(ALLEGRO_VOICE* voice) |
| 458 |
{ |
| 459 |
ALLEGRO_AQ_DATA *ex_data = voice->extra; |
| 460 |
|
| 461 |
if (ex_data->playing) { |
| 462 |
_al_vector_find_and_delete(&saved_voices, &voice); |
| 463 |
ex_data->playing = false; |
| 464 |
|
| 465 |
AudioQueueDispose( |
| 466 |
ex_data->queue, |
| 467 |
false /* Do it asynchronously so the feeder thread can exit. */ |
| 468 |
); |
| 469 |
} |
| 470 |
|
| 471 |
return 0; |
| 472 |
} |
| 473 |
|
| 474 |
/* The voice_is_playing method should only be called on non-streaming sources, |
| 475 |
and should return true if the voice is playing */ |
| 476 |
static bool _aqueue_voice_is_playing(const ALLEGRO_VOICE *voice) |
| 477 |
{ |
| 478 |
ALLEGRO_AQ_DATA *ex_data = (ALLEGRO_AQ_DATA *)voice->extra; |
| 479 |
|
| 480 |
return ex_data->playing; |
| 481 |
} |
| 482 |
|
| 483 |
/* The get_voice_position method should return the current sample position of |
| 484 |
the voice (sample_pos = byte_pos / (depth/8) / channels). This should never |
| 485 |
be called on a streaming voice. */ |
| 486 |
static unsigned int _aqueue_get_voice_position(const ALLEGRO_VOICE *voice) |
| 487 |
{ |
| 488 |
/* FIXME */ |
| 489 |
(void)voice; |
| 490 |
return 0; |
| 491 |
} |
| 492 |
|
| 493 |
/* The set_voice_position method should set the voice's playback position, |
| 494 |
given the value in samples. This should never be called on a streaming |
| 495 |
voice. */ |
| 496 |
static int _aqueue_set_voice_position(ALLEGRO_VOICE *voice, unsigned int val) |
| 497 |
{ |
| 498 |
/* FIXME */ |
| 499 |
(void)voice; |
| 500 |
(void)val; |
| 501 |
return 0; |
| 502 |
} |
| 503 |
|
| 504 |
typedef struct RECORDER_DATA |
| 505 |
{ |
| 506 |
AudioStreamBasicDescription data_format; |
| 507 |
AudioQueueRef queue; |
| 508 |
|
| 509 |
/* AudioQueue buffer information */ |
| 510 |
AudioQueueBufferRef *buffers; |
| 511 |
int buffer_count; |
| 512 |
uint32_t buffer_size; |
| 513 |
|
| 514 |
/* User fragment buffer information. |
| 515 |
* Which buffer are we writing into, and |
| 516 |
* how much data has already been written? |
| 517 |
*/ |
| 518 |
unsigned int fragment_i; |
| 519 |
unsigned int samples_written; |
| 520 |
|
| 521 |
} RECORDER_DATA; |
| 522 |
|
| 523 |
static void _aqueue_recording_callback(void *user_data, AudioQueueRef aq, |
| 524 |
AudioQueueBufferRef aq_buffer, const AudioTimeStamp *start_time, |
| 525 |
UInt32 sample_count, const AudioStreamPacketDescription *descs) |
| 526 |
{ |
| 527 |
ALLEGRO_AUDIO_RECORDER *recorder = (ALLEGRO_AUDIO_RECORDER *) user_data; |
| 528 |
RECORDER_DATA *data = (RECORDER_DATA *) recorder->extra; |
| 529 |
char *input = aq_buffer->mAudioData; |
| 530 |
|
| 531 |
(void) aq; |
| 532 |
(void) start_time; |
| 533 |
(void) descs; |
| 534 |
|
| 535 |
if (sample_count == 0 && data->data_format.mBytesPerPacket != 0) { |
| 536 |
sample_count = aq_buffer->mAudioDataByteSize / data->data_format.mBytesPerPacket; |
| 537 |
} |
| 538 |
|
| 539 |
al_lock_mutex(recorder->mutex); |
| 540 |
|
| 541 |
if (recorder->is_recording) { |
| 542 |
/* Process the full buffer, putting it into as many user fragments as it needs. |
| 543 |
* Send an event for every full user fragment. |
| 544 |
*/ |
| 545 |
while (sample_count > 0) { |
| 546 |
unsigned int samples_left = recorder->samples - data->samples_written; |
| 547 |
unsigned int samples_to_write = sample_count < samples_left ? sample_count : samples_left; |
| 548 |
|
| 549 |
/* Copy the incoming data into the user's fragment buffer */ |
| 550 |
memcpy((char*)recorder->fragments[data->fragment_i] + data->samples_written * recorder->sample_size, |
| 551 |
input, samples_to_write * recorder->sample_size); |
| 552 |
|
| 553 |
input += samples_to_write * recorder->sample_size; |
| 554 |
data->samples_written += samples_to_write; |
| 555 |
sample_count -= samples_to_write; |
| 556 |
|
| 557 |
/* We should have never written more samples than the user asked for */ |
| 558 |
ALLEGRO_ASSERT(recorder->samples >= data->samples_written); |
| 559 |
|
| 560 |
if (data->samples_written == recorder->samples) { |
| 561 |
ALLEGRO_EVENT user_event; |
| 562 |
ALLEGRO_AUDIO_RECORDER_EVENT *e; |
| 563 |
user_event.user.type = ALLEGRO_EVENT_AUDIO_RECORDER_FRAGMENT; |
| 564 |
e = al_get_audio_recorder_event(&user_event); |
| 565 |
e->buffer = recorder->fragments[data->fragment_i]; |
| 566 |
e->samples = recorder->samples; |
| 567 |
al_emit_user_event(&recorder->source, &user_event, NULL); |
| 568 |
|
| 569 |
if (++data->fragment_i == recorder->fragment_count) { |
| 570 |
data->fragment_i = 0; |
| 571 |
} |
| 572 |
|
| 573 |
data->samples_written = 0; |
| 574 |
} |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
al_unlock_mutex(recorder->mutex); |
| 579 |
|
| 580 |
|
| 581 |
AudioQueueEnqueueBuffer(data->queue, aq_buffer, 0, NULL); |
| 582 |
} |
| 583 |
|
| 584 |
static int _aqueue_allocate_recorder(ALLEGRO_AUDIO_RECORDER *recorder) |
| 585 |
{ |
| 586 |
RECORDER_DATA *data; |
| 587 |
int i; |
| 588 |
int ret; |
| 589 |
|
| 590 |
data = al_calloc(1, sizeof(*data)); |
| 591 |
if (!data) return 1; |
| 592 |
|
| 593 |
data->buffer_count = 3; |
| 594 |
data->buffers = al_calloc(data->buffer_count, sizeof(AudioQueueBufferRef)); |
| 595 |
if (!data->buffers) { |
| 596 |
al_free(data); |
| 597 |
return 1; |
| 598 |
} |
| 599 |
|
| 600 |
data->data_format.mFormatID = kAudioFormatLinearPCM; |
| 601 |
data->data_format.mSampleRate = recorder->frequency; |
| 602 |
data->data_format.mChannelsPerFrame = al_get_channel_count(recorder->chan_conf); |
| 603 |
data->data_format.mFramesPerPacket = 1; |
| 604 |
data->data_format.mBitsPerChannel = al_get_audio_depth_size(recorder->depth) * 8; |
| 605 |
data->data_format.mBytesPerFrame = |
| 606 |
data->data_format.mBytesPerPacket = data->data_format.mChannelsPerFrame * al_get_audio_depth_size(recorder->depth); |
| 607 |
|
| 608 |
data->data_format.mFormatFlags = kLinearPCMFormatFlagIsPacked; |
| 609 |
#ifdef ALLEGRO_BIG_ENDIAN |
| 610 |
data->data_format.mFormatFlags |= kLinearPCMFormatFlagIsBigEndian; |
| 611 |
#endif |
| 612 |
if (recorder->depth == ALLEGRO_AUDIO_DEPTH_FLOAT32) |
| 613 |
data->data_format.mFormatFlags |= kLinearPCMFormatFlagIsFloat; |
| 614 |
else if (!(recorder->depth & ALLEGRO_AUDIO_DEPTH_UNSIGNED)) |
| 615 |
data->data_format.mFormatFlags |= kLinearPCMFormatFlagIsSignedInteger; |
| 616 |
|
| 617 |
data->buffer_size = 2048; // in bytes |
| 618 |
|
| 619 |
ret = AudioQueueNewInput( |
| 620 |
&data->data_format, |
| 621 |
_aqueue_recording_callback, |
| 622 |
recorder, |
| 623 |
NULL, |
| 624 |
kCFRunLoopCommonModes, |
| 625 |
0, |
| 626 |
&data->queue |
| 627 |
); |
| 628 |
|
| 629 |
if (ret) { |
| 630 |
ALLEGRO_ERROR("AudioQueueNewInput failed (%d)\n", ret); |
| 631 |
al_free(data->buffers); |
| 632 |
al_free(data); |
| 633 |
return 1; |
| 634 |
} |
| 635 |
|
| 636 |
/* Create the buffers */ |
| 637 |
for (i = 0; i < data->buffer_count; ++i) { |
| 638 |
AudioQueueAllocateBuffer(data->queue, data->buffer_size, &data->buffers[i]); |
| 639 |
AudioQueueEnqueueBuffer(data->queue, data->buffers[i], 0, NULL); |
| 640 |
} |
| 641 |
|
| 642 |
AudioQueueStart(data->queue, NULL); |
| 643 |
|
| 644 |
recorder->extra = data; |
| 645 |
|
| 646 |
/* No thread is created. */ |
| 647 |
|
| 648 |
return 0; |
| 649 |
} |
| 650 |
|
| 651 |
static void _aqueue_deallocate_recorder(ALLEGRO_AUDIO_RECORDER *recorder) |
| 652 |
{ |
| 653 |
RECORDER_DATA *data = (RECORDER_DATA *) recorder->extra; |
| 654 |
|
| 655 |
AudioQueueStop(data->queue, true); |
| 656 |
AudioQueueDispose(data->queue, true); |
| 657 |
|
| 658 |
al_free(data->buffers); |
| 659 |
al_free(data); |
| 660 |
} |
| 661 |
|
| 662 |
ALLEGRO_AUDIO_DRIVER _al_kcm_aqueue_driver = { |
| 663 |
"Apple Audio Queues", |
| 664 |
|
| 665 |
_aqueue_open, |
| 666 |
_aqueue_close, |
| 667 |
|
| 668 |
_aqueue_allocate_voice, |
| 669 |
_aqueue_deallocate_voice, |
| 670 |
|
| 671 |
_aqueue_load_voice, |
| 672 |
_aqueue_unload_voice, |
| 673 |
|
| 674 |
_aqueue_start_voice, |
| 675 |
_aqueue_stop_voice, |
| 676 |
|
| 677 |
_aqueue_voice_is_playing, |
| 678 |
|
| 679 |
_aqueue_get_voice_position, |
| 680 |
_aqueue_set_voice_position, |
| 681 |
|
| 682 |
_aqueue_allocate_recorder, |
| 683 |
_aqueue_deallocate_recorder, |
| 684 |
|
| 685 |
_aqueue_get_output_devices, |
| 686 |
}; |
| 687 |
|