alcRing.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. * OpenAL cross platform audio library
  3. * Copyright (C) 1999-2007 by authors.
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public
  15. * License along with this library; if not, write to the
  16. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17. * Boston, MA 02111-1307, USA.
  18. * Or go to http://www.gnu.org/copyleft/lgpl.html
  19. */
  20. #include "config.h"
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include "alMain.h"
  24. struct RingBuffer {
  25. ALubyte *mem;
  26. ALsizei frame_size;
  27. ALsizei length;
  28. ALint read_pos;
  29. ALint write_pos;
  30. CRITICAL_SECTION cs;
  31. };
  32. RingBuffer *CreateRingBuffer(ALsizei frame_size, ALsizei length)
  33. {
  34. RingBuffer *ring = calloc(1, sizeof(*ring));
  35. if(ring)
  36. {
  37. ring->frame_size = frame_size;
  38. ring->length = length+1;
  39. ring->write_pos = 1;
  40. ring->mem = malloc(ring->length * ring->frame_size);
  41. if(!ring->mem)
  42. {
  43. free(ring);
  44. ring = NULL;
  45. }
  46. InitializeCriticalSection(&ring->cs);
  47. }
  48. return ring;
  49. }
  50. void DestroyRingBuffer(RingBuffer *ring)
  51. {
  52. if(ring)
  53. {
  54. DeleteCriticalSection(&ring->cs);
  55. free(ring->mem);
  56. free(ring);
  57. }
  58. }
  59. ALsizei RingBufferSize(RingBuffer *ring)
  60. {
  61. ALsizei s;
  62. EnterCriticalSection(&ring->cs);
  63. s = (ring->write_pos-ring->read_pos-1+ring->length) % ring->length;
  64. LeaveCriticalSection(&ring->cs);
  65. return s;
  66. }
  67. void WriteRingBuffer(RingBuffer *ring, const ALubyte *data, ALsizei len)
  68. {
  69. int remain;
  70. EnterCriticalSection(&ring->cs);
  71. remain = (ring->read_pos-ring->write_pos+ring->length) % ring->length;
  72. if(remain < len) len = remain;
  73. if(len > 0)
  74. {
  75. remain = ring->length - ring->write_pos;
  76. if(remain < len)
  77. {
  78. memcpy(ring->mem+(ring->write_pos*ring->frame_size), data,
  79. remain*ring->frame_size);
  80. memcpy(ring->mem, data+(remain*ring->frame_size),
  81. (len-remain)*ring->frame_size);
  82. }
  83. else
  84. memcpy(ring->mem+(ring->write_pos*ring->frame_size), data,
  85. len*ring->frame_size);
  86. ring->write_pos += len;
  87. ring->write_pos %= ring->length;
  88. }
  89. LeaveCriticalSection(&ring->cs);
  90. }
  91. void ReadRingBuffer(RingBuffer *ring, ALubyte *data, ALsizei len)
  92. {
  93. int remain;
  94. EnterCriticalSection(&ring->cs);
  95. remain = ring->length - ring->read_pos;
  96. if(remain < len)
  97. {
  98. memcpy(data, ring->mem+(ring->read_pos*ring->frame_size), remain*ring->frame_size);
  99. memcpy(data+(remain*ring->frame_size), ring->mem, (len-remain)*ring->frame_size);
  100. }
  101. else
  102. memcpy(data, ring->mem+(ring->read_pos*ring->frame_size), len*ring->frame_size);
  103. ring->read_pos += len;
  104. ring->read_pos %= ring->length;
  105. LeaveCriticalSection(&ring->cs);
  106. }