smallest CoreAudio code to grab audio out on iPhone

will generate & play realtime white noise on iphone.

MyAudio.h

#import 

#define MY_AUDIO_BUFFERS_NUM 4
#define MY_AUDIO_BUFFER_FRAMES 256
#define MY_AUDIO_BUFFER_SIZE (MY_AUDIO_BUFFER_FRAMES * 2 * 2)

@interface MyAudio : NSObject {
    AudioQueueRef queueObject;
    AudioQueueBufferRef audioBuffers[MY_AUDIO_BUFFERS_NUM];
    AudioStreamBasicDescription audioFormat;
}

- (IBAction)setupAudio:(id)sender;
- (IBAction)teardownAudio:(id)sender;

@end

MyAudio.m

#import "MyAudio.h"

@interface MyAudio ()
- (void)playbackCallbackWithAudioQueue:(AudioQueueRef)inAudioQueue
                                buffer:(AudioQueueBufferRef)bufferReference;
@end

static void PlaybackCallback (
                              void *inUserData,
                              AudioQueueRef inAudioQueue,
                              AudioQueueBufferRef bufferReference
                              )
{
    [(MyAudio *)inUserData playbackCallbackWithAudioQueue:inAudioQueue
                                                   buffer:bufferReference];

}

@implementation MyAudio

- (void)playbackCallbackWithAudioQueue:(AudioQueueRef)inAudioQueue
                                buffer:(AudioQueueBufferRef)bufferReference;
{
    NSUInteger numBytes = MY_AUDIO_BUFFER_SIZE;
    NSUInteger bytesPerFrame = audioFormat.mBytesPerFrame;
    NSUInteger channlesPerFrame = audioFormat.mChannelsPerFrame;
    NSUInteger bytesPerSample = bytesPerFrame/channlesPerFrame;
    NSUInteger numFrames = numBytes/bytesPerFrame;
    NSUInteger i=0;

    while (imAudioData + dstAddr) = (AudioSampleType)(rand() / (float)RAND_MAX * 10);
        *(AudioSampleType *)(bufferReference->mAudioData + dstAddr + bytesPerSample) = (AudioSampleType)(rand() / (float)RAND_MAX * 10);
        i++;
    }

    bufferReference->mAudioDataByteSize = numBytes;
    AudioQueueEnqueueBuffer (inAudioQueue,
                             bufferReference,
                             0,
                             NULL);
}

- (IBAction)setupAudio:(id)sender
{
    audioFormat.mFormatID = 1819304813;
    audioFormat.mFormatFlags = 14;
    audioFormat.mSampleRate = 44100.0;
    audioFormat.mChannelsPerFrame = 2;
    audioFormat.mFramesPerPacket = 1;
    audioFormat.mBytesPerFrame =  4;
    audioFormat.mBytesPerPacket = 4;
    audioFormat.mBitsPerChannel = 16;
    audioFormat.mReserved = 0;
    OSStatus ret =  AudioQueueNewOutput (
                         &audioFormat,
                         PlaybackCallback,
                         self, 
                         CFRunLoopGetCurrent (),
                         kCFRunLoopCommonModes,
                         0, // run loop flags
                         &queueObject);

    assert(ret == noErr);
    assert(queueObject != NULL);
    assert(audioFormat.mChannelsPerFrame==2);

    Float32 gain = 1.0;
    AudioQueueSetParameter (queueObject,
                            kAudioQueueParam_Volume,
                            gain);

    NSUInteger bufferIndex;
    for (bufferIndex = 0; bufferIndex < MY_AUDIO_BUFFERS_NUM; ++bufferIndex) {
 AudioQueueAllocateBuffer (queueObject,
                                  MY_AUDIO_BUFFER_SIZE,
                                  &audioBuffers[bufferIndex]);
        audioBuffers[bufferIndex]->mAudioDataByteSize = MY_AUDIO_BUFFER_SIZE;
        AudioQueueEnqueueBuffer(queueObject, audioBuffers[bufferIndex], 0, NULL);
    }

    UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
    AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,
                            sizeof (sessionCategory),
                            &sessionCategory);
    AudioSessionSetActive(true);

    ret =  AudioQueueStart (queueObject,
                            NULL);
    assert(ret == noErr);
}

- (void)teardownAudio
{
    AudioQueueStop (queueObject, YES);
    AudioQueueDispose (queueObject, YES);
}

@end

contact: form / email / +33 6 49 52 84 01