3

I am porting an audio library to iOS allowing to play audio streams fed from callbacks. The user provides a callback returning raw PCM data, and I need to have this data be played. Moreover, the library must be able to play multiple streams at once.

I figured I would need to use AVFoundation, but it seems like AVAudioPlayer does not support streamed audio buffers, and all the streaming documentation I could find used data coming directly from the network. What is the API I should use here?

Thanks in advance!

By the way, I am not using the Apple libraries through Swift or Objective-C. However I assume everything is exposed still, so an example in Swift would be greatly appreciated anyway!

Moxinilian
  • 31
  • 1
  • 3
  • 1
    https://developer.apple.com/library/archive/documentation/MusicAudio/Conceptual/AudioQueueProgrammingGuide/Introduction/Introduction.html – nakano531 Jun 23 '18 at 09:02
  • 1
    `AVAudioEngine` + `AVAudioPlayerNode` let's you play audio from buffers. Here's an example that uses a single player, with audio buffers taken from the microphone: https://stackoverflow.com/a/43670810/22147 – Rhythmic Fistman Jun 24 '18 at 07:00
  • 1
    Thank you to all of you, Audio Queues are exactly what I needed! – Moxinilian Jun 26 '18 at 10:44

1 Answers1

1

You need to initialise:

  1. The Audio Session to use input audio unit and output.

    -(SInt32) audioSessionInitialization:(SInt32)preferred_sample_rate {
    
        // - - - - - - Audio Session initialization
        NSError *audioSessionError = nil;
        session = [AVAudioSession sharedInstance];
    
        // disable AVAudioSession
        [session setActive:NO error:&audioSessionError];
    
        // set category - (PlayAndRecord to use input and output session AudioUnits)
       [session setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:&audioSessionError];
    
       double preferredSampleRate = 441000;
       [session setPreferredSampleRate:preferredSampleRate error:&audioSessionError];
    
       // enable AVAudioSession
       [session setActive:YES error:&audioSessionError];
    
    
       // Configure notification for device output change (speakers/headphones)
       [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(routeChange:)
                                             name:AVAudioSessionRouteChangeNotification
                                           object:nil];
    
    
       // - - - - - - Create audio engine
       [self audioEngineInitialization];
    
       return [session sampleRate];
     }
    
  2. The Audio Engine

    -(void) audioEngineInitialization{
    
        engine = [[AVAudioEngine alloc] init];
        inputNode = [engine inputNode];
        outputNode = [engine outputNode];
    
        [engine connect:inputNode to:outputNode format:[inputNode inputFormatForBus:0]];
    
    
        AudioStreamBasicDescription asbd_player;
        asbd_player.mSampleRate        = session.sampleRate;
        asbd_player.mFormatID            = kAudioFormatLinearPCM;
        asbd_player.mFormatFlags        = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
        asbd_player.mFramesPerPacket    = 1;
        asbd_player.mChannelsPerFrame    = 2;
        asbd_player.mBitsPerChannel    = 16;
        asbd_player.mBytesPerPacket    = 4;
        asbd_player.mBytesPerFrame        = 4;
    
        OSStatus status;
        status = AudioUnitSetProperty(inputNode.audioUnit,
                                  kAudioUnitProperty_StreamFormat,
                                  kAudioUnitScope_Input,
                                  0,
                                  &asbd_player,
                                  sizeof(asbd_player));
    
    
        // Add the render callback for the ioUnit: for playing
        AURenderCallbackStruct callbackStruct;
        callbackStruct.inputProc = engineInputCallback; ///CALLBACK///
        callbackStruct.inputProcRefCon = (__bridge void *)(self);
        status = AudioUnitSetProperty(inputNode.audioUnit,
                                  kAudioUnitProperty_SetRenderCallback,
                                  kAudioUnitScope_Input,//Global
                                  kOutputBus,
                                  &callbackStruct,
                                  sizeof(callbackStruct));
    
        [engine prepare];
    }
    
  3. The Audio Engine callback

    static OSStatus engineInputCallback(void *inRefCon,
                                 AudioUnitRenderActionFlags *ioActionFlags,
                                 const AudioTimeStamp *inTimeStamp,
                                 UInt32 inBusNumber,
                                 UInt32 inNumberFrames,
                                 AudioBufferList *ioData)
    {
    
        // the reference to the audio controller where you get the stream data
        MyAudioController *ac = (__bridge MyAudioController *)(inRefCon);
    
        // in practice we will only ever have 1 buffer, since audio format is mono
        for (int i = 0; i < ioData->mNumberBuffers; i++) { 
            AudioBuffer buffer = ioData->mBuffers[i];
    
            // copy stream buffer data to output buffer
            UInt32 size = min(buffer.mDataByteSize, ac.playbackBuffer.mDataByteSize); 
            memcpy(buffer.mData, ac.streamBuffer.mData, size);
            buffer.mDataByteSize = size; // indicate how much data we wrote in the buffer
        }
    
        return noErr;
    }
    
vibroto
  • 188
  • 1
  • 5