# Raw audio processing (/en/realtime-media/video/build/customize-audio-processing/stream-raw-audio/macos)

> For AI agents: see the complete documentation index at [llms.txt](/llms.txt).

In some use-cases, raw audio captured through the microphone must be processed to enhance the user experience or achieve the desired functionality Video SDK enables you to pre-process and post-process the captured audio for implementation of custom playback effects.

      
  
      
  
      
    This article shows you how to pre-process and post-process collected raw audio data.

    ## Understand the tech [#understand-the-tech-2]

    For use-cases that require self-processing of audio data, Agora Video SDK provides raw data processing functionality. You can perform pre-processing to modify the captured audio signal before sending the data to the encoder, or post-process data to modify the received audio signal after sending the data to the decoder.

    To implement processing of raw audio data in your app, take the following steps.

    * Register an instance of the audio frame observer before joining a channel.
    * Set the format of audio frames captured by each callback.
    * Implement callbacks in the frame observers to process raw audio data.
    * Unregister the frame observers before you leave a channel.

    The following figure shows the basic processing of raw audio data:

    **Process raw audio**

    ![Raw Audio Processing](https://assets-docs.agora.io/images/video-sdk/process-raw-audio.svg)

    ## Prerequisites [#prerequisites-2]

    Ensure that you have implemented the [SDK quickstart](/en/realtime-media/video/get-started-sdk) in your project.

    ## Implement raw audio processing [#implement-raw-audio-processing-2]

    Follow these steps to implement raw audio data processing functionality in your app:

    1. Before joining a channel, call `setAudioFrameDelegate` to set the audio frame delegate.
    2. Call `setRecordingAudioFrameParameters`, `setPlaybackAudioFrameParameters`, and `setMixedAudioFrameParameters` to configure the audio frame format.
    3. Implement `onRecordAudioFrame`, `onPlaybackAudioFrame`, `onPlaybackAudioFrameBeforeMixing`, and `onMixedAudioFrame` callbacks. These callbacks receive and process audio frames. If the return value of these callbacks is `false`, it indicates that the processing of the audio frames is invalid.

    Refer to the following sample code to implement this logic:

    ```swift

    class RawAudioDataMain: BaseViewController
    {
      var localVideo = Bundle.loadVideoView(type: .local, audioOnly: true)
      var remoteVideo = Bundle.loadVideoView(type: .remote, audioOnly: true)

      @IBOutlet weak var container: AGEVideoContainer!
      // Define the agoraKit variable
      var agoraKit: AgoraRtcEngineKit!

      // ...
      // Initialize agoraKit and register the corresponding callbacks
      agoraKit = AgoraRtcEngineKit.sharedEngine(with: config, delegate: self)

      // Set the audio frame delegate.
      // You need to implement the AgoraAudioFrameDelegate protocol in this method
      agoraKit.setAudioFrameDelegate(self)

      // Configure the audio frames captured by each callback
      agoraKit.setRecordingAudioFrameParametersWithSampleRate(44100, channel: 1, mode: .readWrite, samplesPerCall: 4410)
      agoraKit.setMixedAudioFrameParametersWithSampleRate(44100, samplesPerCall: 4410)
      agoraKit.setPlaybackAudioFrameParametersWithSampleRate(44100, channel: 1, mode: .readWrite, samplesPerCall: 4410)

      // ...

      // In the current class, implement an extension for the AgoraAudioFrameDelegate protocol
      extension RawAudioDataMain: AgoraAudioFrameDelegate {

        // Implement the onRecordAudioFrame callback
        func onRecordAudioFrame(_ frame: AgoraAudioFrame, channelId: String) -> Bool {
          return true
        }
        // Implement the onPlaybackAudioFrame callback
        func onPlaybackAudioFrame(_ frame: AgoraAudioFrame, channelId: String) -> Bool {
          return true
        }
        // Implement the onMixedAudioFrame callback
        func onMixedAudioFrame(_ frame: AgoraAudioFrame, channelId: String) -> Bool {
          return true
        }
        // Implement the onPlaybackAudioFrameBeforeMixing callback
      func onPlaybackAudioFrame(beforeMixing frame: AgoraAudioFrame, channelId: String, uid: UInt) -> Bool {
          return true
        }
      }
    }
    ```

    <CalloutContainer type="info">
      <CalloutDescription>
        Video SDK uses a synchronous callback mechanism for processing raw audio data. When you save or rewrite data using the callbacks, consider the following best practices:

        * To ensure continuity of the audio stream, do not block the SDK thread by processing data directly in the callback function. Instead, make a deep copy of the received audio data and transfer the copied data to another thread for processing.
        * If you choose to process the audio data synchronously within the callback function, you must strictly control the processing time. For example, if the callback function is triggered every 10 milliseconds, then the processing time within the callback must be less than 10 milliseconds to prevent delays or interruptions in the audio stream.
      </CalloutDescription>
    </CalloutContainer>

    ## Reference [#reference-2]

    This section contains content that completes the information on this page, or points you to documentation that explains other aspects to this product.

    * [Audio module](../../core-concepts.mdx)

    ### Sample project [#sample-project-2]

    Agora provides an open-source example project [RawAudioData](https://github.com/AgoraIO/API-Examples/blob/main/iOS/APIExample/APIExample/Examples/Advanced/RawAudioData/RawAudioData.swift) for your reference. Download or view the project for a more detailed example.

    ### API reference [#api-reference-2]

    * [`setAudioFrameDelegate`](https://api-ref.agora.io/en/video-sdk/macos/4.x/documentation/agorartckit/agorartcenginekit/setaudioframedelegate\(_:\))
    * [`setRecordingAudioFrameParametersWithSampleRate`](https://api-ref.agora.io/en/video-sdk/macos/4.x/documentation/agorartckit/agorartcenginekit/setrecordingaudioframeparameterswithsamplerate\(_\:channel\:mode\:samplespercall:\))
    * [`setPlaybackAudioFrameParametersWithSampleRate`](https://api-ref.agora.io/en/video-sdk/macos/4.x/documentation/agorartckit/agorartcenginekit/setplaybackaudioframeparameterswithsamplerate\(_\:channel\:mode\:samplespercall:\))
    * [`setMixedAudioFrameParametersWithSampleRate`](https://api-ref.agora.io/en/video-sdk/macos/4.x/documentation/agorartckit/agorartcenginekit/setmixedaudioframeparameterswithsamplerate\(_\:channel\:samplespercall:\))
    * [`onRecordAudioFrame`](https://api-ref.agora.io/en/video-sdk/macos/4.x/documentation/agorartckit/agoraaudioframedelegate/onrecordaudioframe\(_\:channelid:\))
    * [`onPlaybackAudioFrame`](https://api-ref.agora.io/en/video-sdk/macos/4.x/documentation/agorartckit/agoraaudioframedelegate/onplaybackaudioframe\(_\:channelid:\))
    * [`onPlaybackAudioframeBeforeMixing`](https://api-ref.agora.io/en/video-sdk/macos/4.x/documentation/agorartckit/agoraaudioframedelegate/onplaybackaudioframe\(beforemixing\:channelid\:uid:\))
    * [`onMixedAudioFrame`](https://api-ref.agora.io/en/video-sdk/macos/4.x/documentation/agorartckit/agoraaudioframedelegate/onmixedaudioframe\(_\:channelid:\))

    
  
      
  
      
  
      
  
      
  
