Combines two short (Int16) buffers.
Example #1
0
        public void TestFlushAfterShift()
        {
            short[] buffer1 = { 1, 2, 3, 4, 5 };
            short[] buffer2 = { 6, 7, 8 };
            short[] tmp = new short[10];

            CombinedBuffer buffer = new CombinedBuffer(buffer1, 5, buffer2, 3);
            buffer.Shift(6);

            for (int i = 0; i < 10; i++)
            {
                
                Assert.AreEqual(0, tmp[i]);
            }

            buffer.Flush(tmp);
            Assert.AreEqual(6, buffer.Offset);

            for (int i = 0; i < 2; i++)
            {
                Assert.AreEqual(7 + i, tmp[i]);
            }
            for (int i = 2; i < 10; i++)
            {
                Assert.AreEqual(0, tmp[i]);
            }
        }
Example #2
0
        public void Consume(short[] input, int length)
        {
            // Special case, just pre-filling the buffer
            if (m_buffer_offset + length < m_frame_size)
            {
                Array.Copy(input, 0, m_buffer, m_buffer_offset, length);
                m_buffer_offset += length;
                return;
            }

            // Apply FFT on the available data
            CombinedBuffer combined_buffer = new CombinedBuffer(m_buffer, m_buffer_offset, input, length);

            while (combined_buffer.Size >= m_frame_size)
            {
                combined_buffer.Read(m_input, 0, m_frame_size);
                m_lib.ComputeFrame(m_input, m_frame.Data);

                m_consumer.Consume(m_frame);
                combined_buffer.Shift(m_increment);
            }

            // Copy the remaining input data to the internal buffer
            combined_buffer.Flush(m_buffer);

            m_buffer_offset = combined_buffer.Size;
        }
Example #3
0
        public void Consume(short[] input, int length)
        {
            // Special case, just pre-filling the buffer
            if (m_buffer_offset + length < m_frame_size)
            {
                Array.Copy(input, 0, m_buffer, m_buffer_offset, length);
                m_buffer_offset += length;
                return;
            }

            // Apply FFT on the available data
            CombinedBuffer combined_buffer = new CombinedBuffer(m_buffer, m_buffer_offset, input, length);

            while (combined_buffer.Size >= m_frame_size)
            {
                combined_buffer.Read(m_input, 0, m_frame_size);
                m_lib.ComputeFrame(m_input, m_frame.Data);

                m_consumer.Consume(m_frame);
                combined_buffer.Shift(m_increment);
            }

            // Copy the remaining input data to the internal buffer
            combined_buffer.Flush(m_buffer);

            m_buffer_offset = combined_buffer.Size;
        }
Example #4
0
 public void TestSize()
 {
     short[] buffer1 = { 1, 2, 3, 4, 5 };
     short[] buffer2 = { 6, 7, 8 };
     CombinedBuffer buffer = new CombinedBuffer(buffer1, 5, buffer2, 3);
     Assert.AreEqual(8, buffer.Size);
     buffer.Shift(1);
     Assert.AreEqual(7, buffer.Size);
 }
Example #5
0
 public void TestAccessElements()
 {
     short[] buffer1 = { 1, 2, 3, 4, 5 };
     short[] buffer2 = { 6, 7, 8 };
     CombinedBuffer buffer = new CombinedBuffer(buffer1, 5, buffer2, 3);
     for (int i = 0; i < 8; i++)
     {
         Assert.AreEqual(1 + i, buffer[i]);
     }
     buffer.Shift(1);
     Assert.AreEqual(1, buffer.Offset);
     for (int i = 0; i < 7; i++)
     {
         Assert.AreEqual(2 + i, buffer[i]);
     }
     buffer.Shift(5);
     Assert.AreEqual(6, buffer.Offset);
     for (int i = 0; i < 2; i++)
     {
         Assert.AreEqual(7 + i, buffer[i]);
     }
 }
Example #6
0
        public void TestReadAfterShift()
        {
            short[] buffer1 = { 1, 2, 3, 4, 5 };
            short[] buffer2 = { 6, 7, 8 };
            short[] tmp = new short[10];

            CombinedBuffer buffer = new CombinedBuffer(buffer1, 5, buffer2, 3);

            // Offset 2
            buffer.Shift(2);

            int size = buffer.Read(tmp, 0, 4);
            Assert.AreEqual(2, buffer.Offset);
            Assert.AreEqual(4, size);

            for (int i = 0; i < size; i++)
            {
                Assert.AreEqual(3 + i, tmp[i]);
            }

            // Offset 4
            buffer.Shift(2);

            size = buffer.Read(tmp, 0, 4);
            Assert.AreEqual(4, buffer.Offset);
            Assert.AreEqual(4, size);

            for (int i = 0; i < size; i++)
            {
                Assert.AreEqual(5 + i, tmp[i]);
            }

            // Offset 6
            buffer.Shift(2);

            size = buffer.Read(tmp, 0, 4);
            Assert.AreEqual(6, buffer.Offset);
            Assert.AreEqual(2, size);

            for (int i = 0; i < size; i++)
            {
                Assert.AreEqual(7 + i, tmp[i]);
            }
        }
Example #7
0
        public void TestRead()
        {
            short[] buffer1 = { 1, 2, 3, 4, 5 };
            short[] buffer2 = { 6, 7, 8 };
            short[] tmp = new short[10];

            CombinedBuffer buffer = new CombinedBuffer(buffer1, 5, buffer2, 3);

            for (int i = 0; i < 10; i++)
            {
                Assert.AreEqual(0, tmp[i]);
            }

            // Read from first part
            int size = buffer.Read(tmp, 0, 4);
            Assert.AreEqual(0, buffer.Offset);
            Assert.AreEqual(4, size);

            for (int i = 0; i < size; i++)
            {
                Assert.AreEqual(1 + i, tmp[i]);
            }

            // Read from both parts
            size = buffer.Read(tmp, 3, 4);
            Assert.AreEqual(0, buffer.Offset);
            Assert.AreEqual(4, size);

            for (int i = 0; i < size; i++)
            {
                Assert.AreEqual(4 + i, tmp[i]);
            }

            // Read at split point
            size = buffer.Read(tmp, 5, 4);
            Assert.AreEqual(0, buffer.Offset);
            Assert.AreEqual(3, size);

            for (int i = 0; i < size; i++)
            {
                Assert.AreEqual(6 + i, tmp[i]);
            }

            // Read from last parts
            size = buffer.Read(tmp, 6, 4);
            Assert.AreEqual(0, buffer.Offset);
            Assert.AreEqual(2, size);

            for (int i = 0; i < size; i++)
            {
                Assert.AreEqual(7 + i, tmp[i]);
            }
        }