public void Initialize(Stream stream, AudioInfo audioInfo, MetadataDictionary metadata, SettingsDictionary settings)
        {
            _encoder = new NativeVorbisEncoder();
            _output  = stream;

            // Load the external gain filter:
            ExportFactory <ISampleFilter> sampleFilterFactory =
                ExtensionProvider.GetFactories <ISampleFilter>("Name", "ReplayGain").SingleOrDefault();

            if (sampleFilterFactory == null)
            {
                throw new ExtensionInitializationException(Resources.SampleEncoderReplayGainFilterError);
            }
            _replayGainFilterLifetime = sampleFilterFactory.CreateExport();
            _replayGainFilterLifetime.Value.Initialize(metadata, settings);

            _oggStream = IntializeOggStream(settings);
            _buffer    = new byte[4096];

            if (!string.IsNullOrEmpty(settings["BitRate"]))
            {
                ConfigureEncoderForBitRate(settings, audioInfo, _encoder);
            }
            else
            {
                ConfigureEncoderForQuality(settings, audioInfo, _encoder);
            }

            WriteHeader(metadata, stream);
        }
        static void ConfigureEncoderForQuality(SettingsDictionary settings, AudioInfo audioInfo, NativeVorbisEncoder encoder)
        {
            Contract.Requires(settings != null);
            Contract.Requires(audioInfo != null);
            Contract.Requires(encoder != null);

            if (!string.IsNullOrEmpty(settings["ControlMode"]) &&
                string.Compare(settings["ControlMode"], "Variable", StringComparison.OrdinalIgnoreCase) != 0)
            {
                throw new InvalidSettingException(string.Format(CultureInfo.CurrentCulture,
                                                                Resources.SampleEncoderBadQualityControlMode, settings["ControlMode"]));
            }

            // Set the quality if specified, otherwise select "5":
            float quality;

            if (string.IsNullOrEmpty(settings["VBRQuality"]))
            {
                quality = 5;
            }
            else if (!float.TryParse(settings["VBRQuality"], out quality) || quality < -1 || quality > 10)
            {
                throw new InvalidSettingException(string.Format(CultureInfo.CurrentCulture,
                                                                Resources.SampleEncoderBadVbrQuality, settings["VBRQuality"]));
            }

            encoder.Initialize(audioInfo.Channels, audioInfo.SampleRate, quality / 10);
        }
        static void ConfigureEncoderForBitRate(SettingsDictionary settings, AudioInfo audioInfo, NativeVorbisEncoder encoder)
        {
            Contract.Requires(settings != null);
            Contract.Requires(audioInfo != null);
            Contract.Requires(encoder != null);

            int bitRate;

            if (!int.TryParse(settings["BitRate"], out bitRate) || bitRate < 32 || bitRate > 500)
            {
                throw new InvalidSettingException(string.Format(CultureInfo.CurrentCulture,
                                                                Resources.SampleEncoderBadBitRate, settings["BitRate"]));
            }

            if (string.IsNullOrEmpty(settings["ControlMode"]) ||
                string.Compare(settings["ControlMode"], "Variable", StringComparison.OrdinalIgnoreCase) == 0)
            {
                // Configure the nominal bitrate, but then disable bitrate management:
                encoder.SetupManaged(audioInfo.Channels, audioInfo.SampleRate, -1, bitRate * 1000, -1);
                encoder.Control(0x15, IntPtr.Zero); // OV_ECTL_RATEMANAGE2_SET
                encoder.SetupInitialize();
            }
            else if (string.Compare(settings["ControlMode"], "Average", StringComparison.OrdinalIgnoreCase) == 0)
            {
                encoder.Initialize(audioInfo.Channels, audioInfo.SampleRate, -1, bitRate * 1000, -1);
            }
            else if (string.Compare(settings["ControlMode"], "Constant", StringComparison.OrdinalIgnoreCase) == 0)
            {
                encoder.Initialize(audioInfo.Channels, audioInfo.SampleRate, bitRate, bitRate * 1000, bitRate);
            }
            else
            {
                throw new InvalidSettingException(string.Format(CultureInfo.CurrentCulture,
                                                                Resources.SampleEncoderBadBitRateControlMode, settings["ControlMode"]));
            }
        }