0

We are trying to play an 5.1 (6 channels) AAC audio file using AUGraph. We also tried using AVAudioEngine. We have connected to 5.1 surround sound output device (Sony Speakers). The file is played as stereo file. All the channels are played in Left and Right front speakers.

  • we are setting the file’s AudioStreamBasicDescription (which is 6 channels) to the file unit as input and output format.

      //creating file Unit.
    
      AudioComponentDescription fileDesc;
      fileDesc.componentType = kAudioUnitType_Generator;
      fileDesc.componentSubType = kAudioUnitSubType_AudioFilePlayer;
      fileDesc.componentManufacturer = kAudioUnitManufacturer_Apple;
      fileDesc.componentFlags = 0;
      fileDesc.componentFlagsMask = 0;
    
      status = AUGraphAddNode(_audioGraph,&fileDesc,&filePlayerNode);
      status = AUGraphNodeInfo(_audioGraph,filePlayerNode,NULL,&filePlayerUnit);
    
      //creating output Unit.
    
      AudioComponentDescription outputDesc;
      outputDesc.componentType = kAudioUnitType_Output;
      outputDesc.componentSubType = kAudioUnitSubType_DefaultOutput;
      outputDesc.componentManufacturer = kAudioUnitManufacturer_Apple;
      outputDesc.componentFlags = 0;
      outputDesc.componentFlagsMask = 0;
    
      status = AUGraphAddNode(_audioGraph,&outputDesc,&outputNode);
      status = AUGraphNodeInfo(_audioGraph,outputNode,NULL,&outputUnit);
    
      OSStatus error = AudioFileOpenURL(audioFileURL, kAudioFileReadPermission, 0, &_audioInputFileId);
    
      //get file's streamFormat.
    
      UInt32 dataSize = sizeof(AudioStreamBasicDescription); 
    
      AudioFileGetProperty(_audioInputFileId, kAudioFilePropertyDataFormat, &dataSize, &_fileStreamDescription);
    
      UInt32 propertySize = sizeof(_fileStreamDescription);
    
      //set input and output format to fileUnit.
    
      OSStatus err = AudioUnitSetProperty(filePlayerUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &_fileStreamDescription, propertySize);
    
      OSStatus err = AudioUnitSetProperty(filePlayerUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 0, &_fileStreamDescription, propertySize);
    
      //set input and output format to outputUnit.
    
      OSStatus err = AudioUnitSetProperty(outputUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &_fileStreamDescription, propertySize);
    
      OSStatus err = AudioUnitSetProperty(outputUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 0, &_fileStreamDescription, propertySize);
    

Similarly for the outputUnit we set the input and output format to that of the _fileStreamDescription.

  • we registered for render callback for file unit. It shows number of buffers as 2 and each buffer has 1 channel, indicates stereo data.

    AudioUnitAddRenderNotify(filePlayerUnit, fileUnitRenderCallback, (__bridge void*)self);
    
     static OSStatus fileUnitRenderCallback(void *  inRefCon,AudioUnitRenderActionFlags * ioActionFlags, const AudioTimeStamp *inTimeStamp,UInt32 inBusNumber, UInt32  inNumberFrames, AudioBufferList *ioData){}
    
  • we registered for render callback for output unit. It shows number of buffers as 1 and each buffer has 2 channel. Indicates stereo data.

       AudioUnitAddRenderNotify(outputUnit, outputUnitRenderCallback, (__bridge void*)self);
    
       static OSStatus outputUnitRenderCallback(void *  inRefCon,AudioUnitRenderActionFlags * ioActionFlags, const AudioTimeStamp *inTimeStamp,UInt32 inBusNumber, UInt32  inNumberFrames, AudioBufferList *ioData){}
    
  • We will not be able to use Apple’s built-in players like AVPlayer. Because, we need to connect some of the audio units to apply Audio-Effects.

Could you please suggest on how we can achieve 5.1 playback support.

  • I don't see you setting the audio unit's output device (`kAudioOutputUnitProperty_CurrentDevice`). Are you selecting the device as the default output somewhere else? Have you confirmed that you can manually read 6 channel data from the file and that you can manually output 6 channel data to the AudioUnit (with out all the `AUGraph` machinery)? – Rhythmic Fistman Oct 04 '22 at 08:49
  • @RhythmicFistman. Yes we do select the device as default output device later. And sorry I did not get the second part of your question. But I have confirmed that, we can pass 6 channel data to the audioUnit which will output the same by setting input and output formats to that of 6 channels, but not sure how we can read 6 channel data from the file. In my sample I used kAudioUnitSubType_AudioFilePlayer as a fileUnit, but this fileUnit callback will show stereo data, when I tried playing 6 channel (.aac) audio file. – Babitha shetty k Oct 04 '22 at 10:26

0 Answers0