Esempio n. 1
0
        /// <summary>
        /// Helper function to simplify encoding to MP3
        /// By default, will only be available on Windows 8 and above
        /// </summary>
        /// <param name="inputProvider">Input provider, must be PCM</param>
        /// <param name="outputFile">Output file path, should end with .mp3</param>
        /// <param name="desiredBitRate">Desired bitrate. Use GetEncodeBitrates to find the possibilities for your input type</param>
        public static void EncodeToMp3(IWaveProvider inputProvider, string outputFile, int desiredBitRate = 192000)
        {
            var mediaType = SelectMediaType(AudioFormatGuids.Mp3, inputProvider.WaveFormat, desiredBitRate);

            if (mediaType == null)
            {
                throw new InvalidOperationException("No suitable MP3 encoders available");
            }
            using var encoder = new SharpMediaFoundationEncoder(mediaType);
            encoder.Encode(outputFile, inputProvider);
        }
Esempio n. 2
0
        /// <summary>
        /// Helper function to simplify encoding to AAC
        /// By default, will only be available on Windows 7 and above
        /// </summary>
        /// <param name="inputProvider">Input provider, must be PCM</param>
        /// <param name="outputFile">Output file path, should end with .mp4 (or .aac on Windows 8)</param>
        /// <param name="desiredBitRate">Desired bitrate. Use GetEncodeBitrates to find the possibilities for your input type</param>
        public static void EncodeToAac(IWaveProvider inputProvider, string outputFile, int desiredBitRate = 192000)
        {
            // Information on configuring an AAC media type can be found here:
            // http://msdn.microsoft.com/en-gb/library/windows/desktop/dd742785%28v=vs.85%29.aspx
            var mediaType = SelectMediaType(AudioFormatGuids.Aac, inputProvider.WaveFormat, desiredBitRate);

            if (mediaType == null)
            {
                throw new InvalidOperationException("No suitable AAC encoders available");
            }
            using var encoder = new SharpMediaFoundationEncoder(mediaType);
            // should AAC container have ADTS, or is that just for ADTS?
            // http://www.hydrogenaudio.org/forums/index.php?showtopic=97442
            encoder.Encode(outputFile, inputProvider);
        }