Example #1
0
        static NativeStreamEncoder InitializeEncoder(AudioInfo audioInfo, Stream output)
        {
            Contract.Requires(audioInfo != null);
            Contract.Requires(output != null);
            Contract.Requires(output.CanWrite);
            Contract.Requires(output.CanSeek);

            var result = new NativeStreamEncoder(output);

            result.SetChannels((uint)audioInfo.Channels);
            result.SetBitsPerSample((uint)audioInfo.BitsPerSample);
            result.SetSampleRate((uint)audioInfo.SampleRate);
            result.SetTotalSamplesEstimate((ulong)audioInfo.SampleCount);

            return(result);
        }
Example #2
0
        public void Initialize(Stream stream, AudioInfo audioInfo, MetadataDictionary metadata, SettingsDictionary settings)
        {
            Contract.Ensures(_encoder != null);
            Contract.Ensures(_encoder.GetState() == EncoderState.Ok);
            Contract.Ensures(_multiplier > 0);

            _encoder        = InitializeEncoder(audioInfo, stream);
            _metadataBlocks = new List <NativeMetadataBlock>(3); // Assumes one metadata block, one picture and one seek table
            _multiplier     = (float)Math.Pow(2, audioInfo.BitsPerSample - 1);

            uint compressionLevel;

            if (string.IsNullOrEmpty(settings["CompressionLevel"]))
            {
                compressionLevel = 5;
            }
            else if (!uint.TryParse(settings["CompressionLevel"], out compressionLevel) || compressionLevel > 8)
            {
                throw new InvalidSettingException(string.Format(CultureInfo.CurrentCulture,
                                                                Resources.SampleEncoderBadCompressionLevel, settings["CompressionLevel"]));
            }
            _encoder.SetCompressionLevel(compressionLevel);

            var vorbisCommentBlock = new NativeVorbisCommentBlock();

            foreach (var field in new MetadataToVorbisCommentAdapter(metadata))
            {
                vorbisCommentBlock.Append(field.Key, field.Value);
            }
            _metadataBlocks.Add(vorbisCommentBlock);

            // Add a seek table, unless SeekPointInterval = 0:
            uint seekPointInterval;

            if (string.IsNullOrEmpty(settings["SeekPointInterval"]))
            {
                seekPointInterval = 10;
            }
            else if (!uint.TryParse(settings["SeekPointInterval"], out seekPointInterval))
            {
                throw new InvalidSettingException(string.Format(CultureInfo.CurrentCulture,
                                                                Resources.SampleEncoderBadSeekPointInterval, settings["SeekPointInterval"]));
            }
            if (seekPointInterval > 0)
            {
                _metadataBlocks.Add(new NativeSeekTableBlock(
                                        (int)Math.Ceiling(audioInfo.SampleCount / (double)audioInfo.SampleRate / seekPointInterval),
                                        audioInfo.SampleCount));
            }

            // Add a picture block, if cover art is available:
            if (metadata.CoverArt != null)
            {
                _metadataBlocks.Add(new CoverArtToPictureBlockAdapter(metadata.CoverArt));
            }

            _encoder.SetMetadata(_metadataBlocks);

            EncoderInitStatus initStatus = _encoder.Initialize();

            if (initStatus != EncoderInitStatus.Ok)
            {
                throw new IOException(string.Format(CultureInfo.CurrentCulture,
                                                    Resources.SampleEncoderInitializationError, initStatus));
            }
        }