alcRing.c 3.2 KB

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