Exemple #1
0
        internal static int[] DangerousGetUnderlyingArray(this ImmutableArray <int> array)
        {
            var union = new ArrayUnion();

            union.ImmutableArray = array;
            return(union.MutableArray);
        }
Exemple #2
0
        internal static ImmutableArray <int> DangerousCreateFromUnderlyingArray(ref int[] array)
        {
            var union = new ArrayUnion();

            union.MutableArray = array;
            array = null;
            return(union.ImmutableArray);
        }
Exemple #3
0
        public static void AsIntArray(this byte[] array, Action <int[], int> action)
        {
            if (array == null || array.Length == 0)
            {
                return;
            }

            var union = new ArrayUnion {
                ByteArray = array
            };

            action(union.IntArray, array.Length * sizeof(int));
        }
Exemple #4
0
        public Mp3Writer(Stream stream, int sampleRate, int bitRate, int channels, LAMEPreset preset)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            if (channels != 1 && channels != 2)
            {
                throw new ArgumentException(string.Format("Unsupported number of channels {0}", channels), "channels");
            }

            if (bitRate != 16)
            {
                throw new ArgumentException(string.Format("Unsupported PCM sample size {0}", bitRate), "bitRate");
            }

            if (sampleRate < 8000 || sampleRate > 48000)
            {
                throw new ArgumentException(string.Format("Unsupported Sample Rate {0}", sampleRate), "sampleRate");
            }

            _stream                = stream;
            _sampleRate            = sampleRate;
            _bitRate               = bitRate;
            _channels              = channels;
            _blockAlign            = (short)(channels * (bitRate / 8));
            _averageBytesPerSecond = sampleRate * _blockAlign;
            _preset                = preset;

            _inBuffer  = new ArrayUnion(_averageBytesPerSecond);
            _outBuffer = new byte[sampleRate * 5 / 4 + 7200];

            this._lame = new LibMp3Lame();
            this._lame.InputSampleRate = _sampleRate;
            this._lame.NumChannels     = _channels;
            this._lame.SetPreset(_preset);

            this._lame.InitParams();

            if (_channels == 1)
            {
                _encode = EncodePcm16Mono;
            }
            else
            {
                _encode = EncodePcm16stereo;
            }
        }
Exemple #5
0
        /// <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="quality">LAME quality preset</param>
        /// <param name="id3">Optional ID3 data block</param>
        public LameMP3FileWriter(Stream outStream, WaveFormat format, LAMEPreset quality, ID3TagData id3 = null)
            : 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 = new LibMp3Lame
            {
                InputSampleRate = format.SampleRate,
                NumChannels     = format.Channels
            };

            _lame.SetPreset((int)quality);

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

            _lame.InitParams();
        }