ADC Home > Reference Library > Technical Q&As > Audio > Core Audio >

How to handle audio data with magic cookie information


Q: How do I handle audio data with magic cookies when using an AudioConverter?

A:

Some audio formats have an magic cookie associated with them that are required to decompress audio data. Magic cookies (some times called magic numbers) are information included in audio file headers that are used to describe data formats. When converting audio data you must check to see if the format of the data has a magic cookie. If the audio data format has a magic cookie associated with it, you must need to add this information to an audio converter using AudioConverterSetProperty and kAudioConverterDecompressionMagicCookie to appropriately decompress the Audio File.



Note:
Most data formats do not have magic cookie information, but you must check before converting the data.





Listing 1. Using Magic Cookie information with Audio Converters


   AudioConverterRef *conv; 
   AudioFileID *musicFileID;  
   UInt32    magicCookieSize = 0;

   //...
   // Open a new AudioFile and create a new AudioConverter here.
   // ...


   //Get Magic Cookie info(if exists)  and pass it to converter

   err = AudioFileGetPropertyInfo(*musicFileID,    
                                        kAudioFilePropertyMagicCookieData,
                                        &magicCookieSize,
                                        NULL); 
  
     if (err == noErr)
    {
        void     *magicCookie = calloc (1, magicCookieSize);
        if (magicCookie) 
        {
            err = AudioFileGetProperty (*musicFileID, 
                                  kAudioFilePropertyMagicCookieData, 
                                  &magicCookieSize, 
                                  magicCookie);       
                                           
            // Give the AudioConverter the magic cookie decompression 
            //params if any exist 
            if (err == noErr)
            {
                err = AudioConverterSetProperty(    *conv,       
                     kAudioConverterDecompressionMagicCookie, 
                     magicCookieSize, 
                     magicCookie);
            }
            if (magicCookie) free(magicCookie);
        }
    }else //OK if audio doesn't need magic cookie data
        err = noErr;     //reset error status








[Oct 22, 2003]


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.