buffer.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* Copyright (C) 2007 Jean-Marc Valin
  2. File: buffer.c
  3. This is a very simple ring buffer implementation. It is not thread-safe
  4. so you need to do your own locking.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions are
  7. met:
  8. 1. Redistributions of source code must retain the above copyright notice,
  9. this list of conditions and the following disclaimer.
  10. 2. Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions and the following disclaimer in the
  12. documentation and/or other materials provided with the distribution.
  13. 3. The name of the author may not be used to endorse or promote products
  14. derived from this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  16. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  19. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  23. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  24. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifdef HAVE_CONFIG_H
  28. #include "config.h"
  29. #endif
  30. #include "os_support.h"
  31. #include "arch.h"
  32. #include "speex/speex_buffer.h"
  33. struct SpeexBuffer_ {
  34. char *data;
  35. int size;
  36. int read_ptr;
  37. int write_ptr;
  38. int available;
  39. };
  40. EXPORT SpeexBuffer *speex_buffer_init(int size)
  41. {
  42. SpeexBuffer *st = speex_alloc(sizeof(SpeexBuffer));
  43. st->data = speex_alloc(size);
  44. st->size = size;
  45. st->read_ptr = 0;
  46. st->write_ptr = 0;
  47. st->available = 0;
  48. return st;
  49. }
  50. EXPORT void speex_buffer_destroy(SpeexBuffer *st)
  51. {
  52. speex_free(st->data);
  53. speex_free(st);
  54. }
  55. EXPORT int speex_buffer_write(SpeexBuffer *st, void *_data, int len)
  56. {
  57. int end;
  58. int end1;
  59. char *data = _data;
  60. if (len > st->size)
  61. {
  62. data += len-st->size;
  63. len = st->size;
  64. }
  65. end = st->write_ptr + len;
  66. end1 = end;
  67. if (end1 > st->size)
  68. end1 = st->size;
  69. SPEEX_COPY(st->data + st->write_ptr, data, end1 - st->write_ptr);
  70. if (end > st->size)
  71. {
  72. end -= st->size;
  73. SPEEX_COPY(st->data, data+end1 - st->write_ptr, end);
  74. }
  75. st->available += len;
  76. if (st->available > st->size)
  77. {
  78. st->available = st->size;
  79. st->read_ptr = st->write_ptr;
  80. }
  81. st->write_ptr += len;
  82. if (st->write_ptr > st->size)
  83. st->write_ptr -= st->size;
  84. return len;
  85. }
  86. EXPORT int speex_buffer_writezeros(SpeexBuffer *st, int len)
  87. {
  88. /* This is almost the same as for speex_buffer_write() but using
  89. SPEEX_MEMSET() instead of SPEEX_COPY(). Update accordingly. */
  90. int end;
  91. int end1;
  92. if (len > st->size)
  93. {
  94. len = st->size;
  95. }
  96. end = st->write_ptr + len;
  97. end1 = end;
  98. if (end1 > st->size)
  99. end1 = st->size;
  100. SPEEX_MEMSET(st->data + st->write_ptr, 0, end1 - st->write_ptr);
  101. if (end > st->size)
  102. {
  103. end -= st->size;
  104. SPEEX_MEMSET(st->data, 0, end);
  105. }
  106. st->available += len;
  107. if (st->available > st->size)
  108. {
  109. st->available = st->size;
  110. st->read_ptr = st->write_ptr;
  111. }
  112. st->write_ptr += len;
  113. if (st->write_ptr > st->size)
  114. st->write_ptr -= st->size;
  115. return len;
  116. }
  117. EXPORT int speex_buffer_read(SpeexBuffer *st, void *_data, int len)
  118. {
  119. int end, end1;
  120. char *data = _data;
  121. if (len > st->available)
  122. {
  123. SPEEX_MEMSET(data+st->available, 0, len - st->available);
  124. len = st->available;
  125. }
  126. end = st->read_ptr + len;
  127. end1 = end;
  128. if (end1 > st->size)
  129. end1 = st->size;
  130. SPEEX_COPY(data, st->data + st->read_ptr, end1 - st->read_ptr);
  131. if (end > st->size)
  132. {
  133. end -= st->size;
  134. SPEEX_COPY(data+end1 - st->read_ptr, st->data, end);
  135. }
  136. st->available -= len;
  137. st->read_ptr += len;
  138. if (st->read_ptr > st->size)
  139. st->read_ptr -= st->size;
  140. return len;
  141. }
  142. EXPORT int speex_buffer_get_available(SpeexBuffer *st)
  143. {
  144. return st->available;
  145. }
  146. EXPORT int speex_buffer_resize(SpeexBuffer *st, int len)
  147. {
  148. int old_len = st->size;
  149. if (len > old_len)
  150. {
  151. st->data = speex_realloc(st->data, len);
  152. /* FIXME: move data/pointers properly for growing the buffer */
  153. } else {
  154. /* FIXME: move data/pointers properly for shrinking the buffer */
  155. st->data = speex_realloc(st->data, len);
  156. }
  157. return len;
  158. }