예제 #1
0
        /// <summary>Create MP3FileWriter to write to supplied stream</summary>
        /// <param name="outStream">Stream to write encoded data to</param>
        /// <param name="format">Input Genode.Audio.WavFormat</param>
        /// <param name="bitRate">Output bit rate in kbps</param>
        /// <param name="id3">Optional ID3 data block</param>
        public LameMP3FileWriter(Stream outStream, Cgen.Audio.WavFormat format, int bitRate, int channels = 2, int sampleRate = 44100, ID3TagData id3 = null)
            : base()
        {
            // sanity check
            if (outStream == null)
            {
                throw new ArgumentNullException("outStream");
            }

            // select encoder function that matches data format
            if (format == Cgen.Audio.WavFormat.PCM)
            {
                if (channels == 1)
                {
                    _encode = encode_pcm_16_mono;
                }
                else
                {
                    _encode = encode_pcm_16_stereo;
                }
            }
            else
            {
                if (channels == 1)
                {
                    _encode = encode_float_mono;
                }
                else
                {
                    _encode = encode_float_stereo;
                }
            }

            // Set base properties
            this.inputFormat   = format;
            this.outStream     = outStream;
            this.disposeOutput = false;

            // Allocate buffers based on sample rate
            this.channels  = channels;
            this.inBuffer  = new ArrayUnion(sampleRate * channels * 2);
            this.outBuffer = new byte[sampleRate * 5 / 4 + 7200];

            // Initialize lame library
            this._lame = new Lame.Wrapper.LibMp3Lame();

            this._lame.InputSampleRate = sampleRate;
            this._lame.NumChannels     = channels;

            this._lame.BitRate = bitRate;

            if (id3 != null)
            {
                ApplyID3Tag(id3);
            }

            this._lame.InitParams();
        }
예제 #2
0
        // Close LAME instance and output stream on dispose
        /// <summary>Dispose of object</summary>
        /// <param name="final">True if called from destructor, false otherwise</param>
        protected override void Dispose(bool final)
        {
            if (_lame != null && outStream != null)
            {
                Flush();
            }

            if (_lame != null)
            {
                _lame.Dispose();
                _lame = null;
            }

            if (outStream != null && disposeOutput)
            {
                outStream.Dispose();
                outStream = null;
            }

            base.Dispose(final);
        }