/// <summary>
        /// Write data into the buffer
        /// </summary>
        /// <param name="m_pInBuffer"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public int WriteBuffer(T[] m_pInBuffer, int count)
        {
            count = Math.Min(count, m_bufSize);

            var startPos   = m_availBuf != m_bufSize ? m_availBuf : m_begBufOffset;
            var pass1Count = Math.Min(count, m_bufSize - startPos);
            var pass2Count = count - pass1Count;

            PitchDsp.CopyBuffer(m_pInBuffer, 0, m_buffer, startPos, pass1Count);

            if (pass2Count > 0)
            {
                PitchDsp.CopyBuffer(m_pInBuffer, pass1Count, m_buffer, 0, pass2Count);
            }

            if (pass2Count == 0)
            {
                // did not wrap around
                if (m_availBuf != m_bufSize)
                {
                    m_availBuf += count;   // have never wrapped around
                }
                else
                {
                    m_begBufOffset += count;
                    m_startPos     += count;
                }
            }
            else
            {
                // wrapped around
                if (m_availBuf != m_bufSize)
                {
                    m_startPos += pass2Count;  // first time wrap-around
                }
                else
                {
                    m_startPos += count;
                }

                m_begBufOffset = pass2Count;
                m_availBuf     = m_bufSize;
            }

            return(count);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Write data into the buffer
        /// </summary>
        /// <param name="m_pInBuffer"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public int WriteBuffer(T[] m_pInBuffer, int count)
        {
            count = Math.Min(count, bufSize);

            var startPos   = availBuf != bufSize ? availBuf : begBufOffset;
            var pass1Count = Math.Min(count, bufSize - startPos);
            var pass2Count = count - pass1Count;

            PitchDsp.CopyBuffer(m_pInBuffer, 0, buffer, startPos, pass1Count);

            if (pass2Count > 0)
            {
                PitchDsp.CopyBuffer(m_pInBuffer, pass1Count, buffer, 0, pass2Count);
            }

            if (pass2Count == 0)
            {
                // did not wrap around
                if (availBuf != bufSize)
                {
                    availBuf += count;   // have never wrapped around
                }
                else
                {
                    begBufOffset  += count;
                    startPosition += count;
                }
            }
            else
            {
                // wrapped around
                if (availBuf != bufSize)
                {
                    startPosition += pass2Count;  // first time wrap-around
                }
                else
                {
                    startPosition += count;
                }

                begBufOffset = pass2Count;
                availBuf     = bufSize;
            }

            return(count);
        }
        /// <summary>
        /// Read from the buffer
        /// </summary>
        /// <param name="outBuffer"></param>
        /// <param name="startRead"></param>
        /// <param name="readCount"></param>
        /// <returns></returns>
        public bool ReadBuffer(T[] outBuffer, long startRead, int readCount)
        {
            var endRead  = (int)(startRead + readCount);
            var endAvail = (int)(m_startPos + m_availBuf);

            if (startRead < m_startPos || endRead > endAvail)
            {
                return(false);
            }

            var startReadPos  = (int)(((startRead - m_startPos) + m_begBufOffset) % m_bufSize);
            var block1Samples = Math.Min(readCount, m_bufSize - startReadPos);
            var block2Samples = readCount - block1Samples;

            PitchDsp.CopyBuffer(m_buffer, startReadPos, outBuffer, 0, block1Samples);

            if (block2Samples > 0)
            {
                PitchDsp.CopyBuffer(m_buffer, 0, outBuffer, block1Samples, block2Samples);
            }

            return(true);
        }