Stereo recording and audio quality

Hello, I started to set audio stereo recording (both audio and video are recorded) and the audio quality seems to be lower than quality obtained with native camera application (configured for stereo).

Using console to check the log, I found a difference between camera app and mine regarding MXSessionMode (of mediaserverd)

in fact, camera application gives MXSessionMode = SpatialRecording and mine MXSessionMode = VideoRecording

How can I configure capture session to finally have MXSessionMode = SpatialRecording?

Any suggestion? Best regards

Replies

The MXSessionMode you mentioned, specifically "SpatialRecording," is related to spatial audio capture and processing, which provides a more immersive audio experience. Achieving spatial audio recording depends on several factors, including the hardware capabilities of the device, the settings of your capture session, and the audio configuration you use.

To configure your capture session for spatial audio recording, you can follow these steps:

  1. Select the Right Audio Format: Ensure that you are using an audio format that supports spatial audio. You should use Audio Format Settings that allow for multi-channel audio recording. For example, you can use the AVAudioFormat with a channelCount greater than 2 to enable multi-channel audio recording.

    let audioSettings: [String: Any] = [
        AVFormatIDKey: kAudioFormatLinearPCM,
        AVSampleRateKey: 44100.0,
        AVNumberOfChannelsKey: 4, // Set this to the number of audio channels you need (e.g., 4 for spatial audio)
        // Other audio settings...
    ]
    
    let audioFormat = AVAudioFormat(settings: audioSettings)
    
  2. Configure Audio Session: Set up your audio session to enable multi-channel audio recording. You can do this using AVAudioSession and configuring the category and mode properties.

    let audioSession = AVAudioSession.sharedInstance()
    do {
        try audioSession.setCategory(.record, mode: .videoRecording, options: .allowBluetooth)
        try audioSession.setActive(true)
    } catch {
        // Handle audio session configuration error
    }
    
  3. Update Capture Session: Configure your capture session to use the audio format you defined earlier and make sure you're capturing audio along with video.

    let captureSession = AVCaptureSession()
    
    if let audioDevice = AVCaptureDevice.default(for: .audio) {
        do {
            let audioInput = try AVCaptureDeviceInput(device: audioDevice)
            if captureSession.canAddInput(audioInput) {
                captureSession.addInput(audioInput)
            }
        } catch {
            // Handle audio input device error
        }
    }
    
    // Configure video capture input and output...
    
    captureSession.startRunning()
    
  4. Set Audio Output Settings: When configuring your audio output settings (e.g., for a movie file output), make sure you're using the audio format and settings that support spatial audio.

    let audioOutputSettings: [String: Any] = [
        AVFormatIDKey: kAudioFormatMPEG4AAC,
        AVNumberOfChannelsKey: 4, // Match the channel count to the audio format
        // Other audio settings...
    ]
    
    // Set the audio settings when configuring the output...
    audioOutput.setAudioSettings(audioOutputSettings, for: audioConnection)
    
  5. Testing and Validation: Test your app on devices that support spatial audio recording, such as those with multiple microphones. Ensure that you are capturing audio from the correct microphones to achieve the desired spatial audio effect.

By following these steps and configuring your capture session and audio settings appropriately, you should be able to record spatial audio with your iOS app, achieving the desired MXSessionMode of "SpatialRecording" in mediaserverd. Please note that not all iOS devices support spatial audio recording, so you should test on devices with the required hardware capabilities.

Hello I tried to change sample rate and number of channels from audio session by using setPreferredSampleRate and setPreferredInputNumberOfChannels. Does the sample rate at 44,1 kHz really matter or 48 kHz may be supported for spatial recording? Should I use other API since it still fail to have MXSessionMode = SpatialRecording ? Best regards