public void Initialize(Stream stream)
        {
            Contract.Ensures(_converter != null);
            Contract.Ensures(_magicCookie != IntPtr.Zero);
            Contract.Ensures(_divisor > 0);

            try
            {
                var audioFile = new NativeAudioFile(AudioFileType.M4A, stream);

                _inputDescription = audioFile.GetProperty <AudioStreamBasicDescription>(AudioFilePropertyId.DataFormat);
                if (_inputDescription.AudioFormat != AudioFormat.AppleLossless)
                {
                    throw new UnsupportedAudioException(Resources.LosslessSampleDecoderFormatError);
                }

                var outputDescription = InitializeOutputDescription(_inputDescription);

                _divisor = (float)Math.Pow(2, outputDescription.BitsPerChannel - 1);

                _converter = new NativeAudioConverter(ref _inputDescription, ref outputDescription, audioFile);

                _magicCookie = InitializeMagicCookie(audioFile, _converter);
            }
            catch (TypeInitializationException e)
            {
                if (e.InnerException != null && e.InnerException.GetType() == typeof(ExtensionInitializationException))
                {
                    throw e.InnerException;
                }
                throw;
            }
        }
        static IntPtr InitializeMagicCookie(NativeAudioFile audioFile, NativeAudioConverter converter)
        {
            Contract.Requires(audioFile != null);
            Contract.Requires(converter != null);

            uint dataSize;
            uint isWritable;

            AudioFileStatus getStatus = audioFile.GetPropertyInfo(AudioFilePropertyId.MagicCookieData, out dataSize,
                                                                  out isWritable);

            if (getStatus != AudioFileStatus.Ok)
            {
                throw new IOException(string.Format(CultureInfo.CurrentCulture,
                                                    Resources.LosslessSampleDecoderGetCookieInfoError, getStatus));
            }

            if (dataSize == 0)
            {
                return(IntPtr.Zero);
            }

            IntPtr cookie = audioFile.GetProperty(AudioFilePropertyId.MagicCookieData, dataSize);

            AudioConverterStatus setStatus = converter.SetProperty(AudioConverterPropertyId.DecompressionMagicCookie,
                                                                   dataSize, cookie);

            if (setStatus != AudioConverterStatus.Ok)
            {
                throw new IOException(string.Format(CultureInfo.CurrentCulture,
                                                    Resources.LosslessSampleDecoderSetCookieError, setStatus));
            }

            return(cookie);
        }
        internal NativeAudioConverter(ref AudioStreamBasicDescription inputDescription, ref AudioStreamBasicDescription outputDescription, NativeAudioFile audioFile)
        {
            Contract.Requires(audioFile != null);
            Contract.Ensures(_handle != null);
            Contract.Ensures(!_handle.IsClosed);
            Contract.Ensures(_inputCallback != null);
            Contract.Ensures(_audioFile != null);
            Contract.Ensures(_audioFile == audioFile);

            AudioConverterStatus status = SafeNativeMethods.AudioConverterNew(ref inputDescription,
                                                                              ref outputDescription, out _handle);

            if (status != AudioConverterStatus.Ok)
            {
                throw new IOException(string.Format(CultureInfo.CurrentCulture,
                                                    Resources.NativeAudioConverterInitializationError, status));
            }

            _inputCallback = InputCallback;

            _audioFile = audioFile;
        }