ringbuffer.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #include <string.h>
  2. #include <stdio.h>
  3. #include "ringbuffer.h"
  4. #undef NDEBUG
  5. #include <assert.h>
  6. template <typename T> inline T min(T a, T b) { return a<b?a:b; }
  7. CRingBuffer::CRingBuffer() : m_BufferSize(sizeof(m_aBuffer) / sizeof(*m_aBuffer))
  8. {
  9. m_ReadIndex = 0;
  10. m_WriteIndex = 0;
  11. m_Length = 0;
  12. }
  13. bool CRingBuffer::Pop(int16_t *pBuffer, size_t Samples)
  14. {
  15. if(Samples > TotalLength())
  16. return false;
  17. if(m_ReadIndex + Samples > m_BufferSize)
  18. {
  19. size_t TowardsEnd = m_BufferSize - m_ReadIndex;
  20. memcpy(pBuffer, &m_aBuffer[m_ReadIndex], TowardsEnd * sizeof(*m_aBuffer));
  21. m_ReadIndex = 0;
  22. size_t Left = Samples - TowardsEnd;
  23. memcpy(&pBuffer[TowardsEnd], m_aBuffer, Left * sizeof(*m_aBuffer));
  24. m_ReadIndex = Left;
  25. }
  26. else
  27. {
  28. memcpy(pBuffer, &m_aBuffer[m_ReadIndex], Samples * sizeof(*m_aBuffer));
  29. m_ReadIndex += Samples;
  30. }
  31. if(m_ReadIndex == m_BufferSize)
  32. m_ReadIndex = 0;
  33. m_Length -= Samples;
  34. return true;
  35. }
  36. void CRingBuffer::Mix(int16_t *pData, size_t Samples)
  37. {
  38. assert(!(m_WriteIndex + Samples > m_BufferSize));
  39. int16_t *pBuffer = &m_aBuffer[m_WriteIndex];
  40. while(Samples--)
  41. {
  42. int32_t Sample = *pBuffer;
  43. Sample += *pData;
  44. if(Sample > INT16_MAX)
  45. *pBuffer = INT16_MAX;
  46. else if(Sample < INT16_MIN)
  47. *pBuffer = INT16_MIN;
  48. else
  49. *pBuffer = Sample;
  50. pBuffer++;
  51. pData++;
  52. }
  53. }
  54. bool CRingBuffer::Push(int16_t *pData, size_t Samples)
  55. {
  56. if(Samples > CurrentFree())
  57. return false;
  58. // Mix with data in front of us
  59. if(CurrentLength() < TotalLength())
  60. {
  61. //
  62. size_t ToMix = min(Samples, TotalLength() - CurrentLength());
  63. if(m_WriteIndex + ToMix > m_BufferSize)
  64. {
  65. size_t TowardsEnd = m_BufferSize - m_WriteIndex;
  66. Mix(pData, TowardsEnd);
  67. m_WriteIndex = 0;
  68. size_t Left = ToMix - TowardsEnd;
  69. Mix(&pData[TowardsEnd], Left);
  70. m_WriteIndex = Left;
  71. }
  72. else
  73. {
  74. Mix(pData, ToMix);
  75. m_WriteIndex += ToMix;
  76. }
  77. if(m_WriteIndex == m_BufferSize)
  78. m_WriteIndex = 0;
  79. pData += ToMix;
  80. Samples -= ToMix;
  81. }
  82. //
  83. if(!Samples)
  84. return true;
  85. if(m_WriteIndex + Samples > m_BufferSize)
  86. {
  87. size_t TowardsEnd = m_BufferSize - m_WriteIndex;
  88. memcpy(&m_aBuffer[m_WriteIndex], pData, TowardsEnd * sizeof(*m_aBuffer));
  89. m_WriteIndex = 0;
  90. size_t Left = Samples - TowardsEnd;
  91. memcpy(m_aBuffer, &pData[TowardsEnd], Left * sizeof(*m_aBuffer));
  92. m_WriteIndex = Left;
  93. }
  94. else
  95. {
  96. memcpy(&m_aBuffer[m_WriteIndex], pData, Samples * sizeof(*m_aBuffer));
  97. m_WriteIndex += Samples;
  98. }
  99. if(m_WriteIndex == m_BufferSize)
  100. m_WriteIndex = 0;
  101. m_Length += Samples;
  102. return true;
  103. }
  104. size_t CRingBuffer::TotalLength()
  105. {
  106. return m_Length;
  107. }
  108. size_t CRingBuffer::TotalFree()
  109. {
  110. return m_BufferSize - m_Length - 1;
  111. }
  112. size_t CRingBuffer::CurrentLength()
  113. {
  114. return ((ssize_t)m_WriteIndex - (ssize_t)m_ReadIndex) % m_BufferSize;
  115. }
  116. size_t CRingBuffer::CurrentFree()
  117. {
  118. size_t BufferFree = ((ssize_t)m_ReadIndex - (ssize_t)m_WriteIndex) % m_BufferSize;
  119. return (BufferFree ? BufferFree : m_BufferSize) - 1;
  120. }
  121. size_t CRingBuffer::GetReadIndex()
  122. {
  123. return m_ReadIndex;
  124. }
  125. size_t CRingBuffer::GetWriteIndex()
  126. {
  127. return m_WriteIndex;
  128. }
  129. void CRingBuffer::SetWriteIndex(size_t WriteIndex)
  130. {
  131. m_WriteIndex = WriteIndex;
  132. }