Esempio n. 1
0
        /// <summary>
        /// Copies both channels of audio samples in integer format.
        /// </summary>
        /// <remarks>
        /// The samples are floating-point values normalized within -1.0 and 1.0.
        /// </remarks>
        /// <param name="leftDestination">The destination for the left channel.</param>
        /// <param name="rightDestination">The destination for the right channel.</param>
        /// <param name="bitsPerSample">The # of bits per sample.</param>
        /// <exception cref="ObjectDisposedException">This <see cref="SampleBuffer"/> has been disposed.</exception>
        /// <exception cref="InvalidOperationException">Thrown if the Channels property does not equal 2.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="bitsPerSample"/> is out of range.</exception>
        public void CopyTo(Span <int> leftDestination, Span <int> rightDestination, int bitsPerSample)
        {
            if (_isDisposed)
            {
                throw new ObjectDisposedException(GetType().ToString());
            }
            if (_buffer == null)
            {
                return;
            }

            if (Channels != 2)
            {
                throw new InvalidOperationException("Not a 2-channel SampleBuffer.");
            }

            if (IsInterleaved)
            {
                Span <float> leftBuffer  = stackalloc float[Frames];
                Span <float> rightBuffer = stackalloc float[Frames];
                SampleProcessor.DeInterleave(_buffer.Memory.Span.Slice(0, Frames * 2), leftBuffer, rightBuffer);
                SampleProcessor.Convert(leftBuffer, leftDestination, bitsPerSample);
                SampleProcessor.Convert(rightBuffer, rightDestination, bitsPerSample);
            }
            else
            {
                SampleProcessor.Convert(_buffer.Memory.Span.Slice(0, Frames), leftDestination, bitsPerSample);
                SampleProcessor.Convert(_buffer.Memory.Span.Slice(Frames, Frames), rightDestination, bitsPerSample);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Copies both channels of audio samples in normalized floating-point format.
        /// </summary>
        /// <remarks>
        /// The samples are floating-point values normalized within -1.0 and 1.0.
        /// </remarks>
        /// <param name="leftDestination">The destination for the left channel.</param>
        /// <param name="rightDestination">The destination for the right channel.</param>
        /// <exception cref="ObjectDisposedException">This <see cref="SampleBuffer"/> has been disposed.</exception>
        /// <exception cref="InvalidOperationException">Thrown if the Channels property does not equal 2.</exception>
        public void CopyTo(Span <float> leftDestination, Span <float> rightDestination)
        {
            if (_isDisposed)
            {
                throw new ObjectDisposedException(GetType().ToString());
            }
            if (_buffer == null)
            {
                return;
            }

            if (Channels != 2)
            {
                throw new InvalidOperationException("Not a 2-channel SampleBuffer.");
            }

            if (IsInterleaved)
            {
                SampleProcessor.DeInterleave(
                    _buffer.Memory.Span.Slice(0, Frames * 2),
                    leftDestination,
                    rightDestination);
            }
            else
            {
                _buffer.Memory.Span.Slice(0, Frames).CopyTo(leftDestination);
                _buffer.Memory.Span.Slice(Frames, Frames).CopyTo(rightDestination);
            }
        }