Search code examples
iphoneiosaudionormalization

How to programmatically normalize a PCM audio sample on iOS 5?


I am recording a 16bit linear PCM file using AVAudioRecorder, saving it to a CAF file.

Now I want to normalize the audio I recorded. I just cannot find ANY library, either Apple or 3rd party, that lets me do this for the iPhone!


Solution

  • Peak normalization takes this general form, which you'll have a few conversions, optimizations, and error checking to add for a 16 bit signal:

    double* const buffer(...);
    const size_t length(...);
    
    double max(0);
    // find the peak
    for (size_t idx(0); idx < length; ++idx)
      max = std::max(max, buffer[idx]);
    // process
    double mul(1.0/max);
    for (size_t idx(0); idx < length; ++idx)
      buffer[idx] *= mul;