/// <summary>Create MP3FileWriter to write to supplied stream</summary> /// <param name="outStream">Stream to write encoded data to</param> /// <param name="format">Input WaveFormat</param> /// <param name="config">LAME configuration</param> public LameMP3FileWriter(Stream outStream, WaveFormat format, LameConfig config) : base() { if (format == null) { throw new ArgumentNullException("format"); } // check for unsupported wave formats if (format.Channels != 1 && format.Channels != 2) { throw new ArgumentException($"Unsupported number of channels {format.Channels}", "format"); } if (format.Encoding != WaveFormatEncoding.Pcm && format.Encoding != WaveFormatEncoding.IeeeFloat) { throw new ArgumentException($"Unsupported encoding format {format.Encoding}", "format"); } if (format.Encoding == WaveFormatEncoding.Pcm && format.BitsPerSample != 16) { throw new ArgumentException($"Unsupported PCM sample size {format.BitsPerSample}", "format"); } if (format.Encoding == WaveFormatEncoding.IeeeFloat && format.BitsPerSample != 32) { throw new ArgumentException($"Unsupported Float sample size {format.BitsPerSample}", "format"); } if (format.SampleRate < 8000 || format.SampleRate > 48000) { throw new ArgumentException($"Unsupported Sample Rate {format.SampleRate}", "format"); } // select encoder function that matches data format if (format.Encoding == WaveFormatEncoding.Pcm) { if (format.Channels == 1) { _encode = Encode_pcm_16_mono; } else { _encode = Encode_pcm_16_stereo; } } else { if (format.Channels == 1) { _encode = Encode_float_mono; } else { _encode = Encode_float_stereo; } } // Set base properties _inputFormat = format; _outStream = outStream ?? throw new ArgumentNullException("outStream"); _disposeOutput = false; // Allocate buffers based on sample rate _inBuffer = new ArrayUnion(format.AverageBytesPerSecond); _outBuffer = new byte[format.SampleRate * 5 / 4 + 7200]; // Initialize lame library _lame = config.ConfigureDLL(format); if (config.ID3 != null) { ApplyID3Tag(config.ID3); } _lame.InitParams(); }
/// <summary>Create MP3FileWriter to write to a file on disk</summary> /// <param name="outFileName">Name of file to create</param> /// <param name="format">Input WaveFormat</param> /// <param name="config">LAME configuration</param> public LameMP3FileWriter(string outFileName, WaveFormat format, LameConfig config) : this(File.Create(outFileName), format, config) { _disposeOutput = true; }