alcRing.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. #include "compat.h"
  25. struct RingBuffer {
  26. ALubyte *mem;
  27. ALsizei frame_size;
  28. ALsizei length;
  29. ALint read_pos;
  30. ALint write_pos;
  31. CRITICAL_SECTION cs;
  32. };
  33. RingBuffer *CreateRingBuffer(ALsizei frame_size, ALsizei length)
  34. {
  35. RingBuffer *ring = calloc(1, sizeof(*ring) + ((length+1) * frame_size));
  36. if(ring)
  37. {
  38. ring->mem = (ALubyte*)(ring+1);
  39. ring->frame_size = frame_size;
  40. ring->length = length+1;
  41. ring->read_pos = 0;
  42. ring->write_pos = 0;
  43. InitializeCriticalSection(&ring->cs);
  44. }
  45. return ring;
  46. }
  47. void DestroyRingBuffer(RingBuffer *ring)
  48. {
  49. if(ring)
  50. {
  51. DeleteCriticalSection(&ring->cs);
  52. free(ring);
  53. }
  54. }
  55. ALsizei RingBufferSize(RingBuffer *ring)
  56. {
  57. ALsizei s;
  58. EnterCriticalSection(&ring->cs);
  59. s = (ring->write_pos-ring->read_pos+ring->length) % ring->length;
  60. LeaveCriticalSection(&ring->cs);
  61. return s;
  62. }
  63. void WriteRingBuffer(RingBuffer *ring, const ALubyte *data, ALsizei len)
  64. {
  65. int remain;
  66. EnterCriticalSection(&ring->cs);
  67. remain = (ring->read_pos-ring->write_pos-1+ring->length) % ring->length;
  68. if(remain < len) len = remain;
  69. if(len > 0)
  70. {
  71. remain = ring->length - ring->write_pos;
  72. if(remain < len)
  73. {
  74. memcpy(ring->mem+(ring->write_pos*ring->frame_size), data,
  75. remain*ring->frame_size);
  76. memcpy(ring->mem, data+(remain*ring->frame_size),
  77. (len-remain)*ring->frame_size);
  78. }
  79. else
  80. memcpy(ring->mem+(ring->write_pos*ring->frame_size), data,
  81. len*ring->frame_size);
  82. ring->write_pos += len;
  83. ring->write_pos %= ring->length;
  84. }
  85. LeaveCriticalSection(&ring->cs);
  86. }
  87. void ReadRingBuffer(RingBuffer *ring, ALubyte *data, ALsizei len)
  88. {
  89. int remain;
  90. EnterCriticalSection(&ring->cs);
  91. remain = ring->length - ring->read_pos;
  92. if(remain < len)
  93. {
  94. memcpy(data, ring->mem+(ring->read_pos*ring->frame_size), remain*ring->frame_size);
  95. memcpy(data+(remain*ring->frame_size), ring->mem, (len-remain)*ring->frame_size);
  96. }
  97. else
  98. memcpy(data, ring->mem+(ring->read_pos*ring->frame_size), len*ring->frame_size);
  99. ring->read_pos += len;
  100. ring->read_pos %= ring->length;
  101. LeaveCriticalSection(&ring->cs);
  102. }