Mercurial > fife-parpg
comparison ext/openal-soft/Alc/alcRing.c @ 0:4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
author | mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Sun, 29 Jun 2008 18:44:17 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4a0efb7baf70 |
---|---|
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 | |
21 #include "config.h" | |
22 | |
23 #include <string.h> | |
24 #include <stdlib.h> | |
25 | |
26 #include "alMain.h" | |
27 | |
28 | |
29 struct RingBuffer { | |
30 ALubyte *mem; | |
31 | |
32 ALsizei frame_size; | |
33 ALsizei length; | |
34 ALint read_pos; | |
35 ALint write_pos; | |
36 | |
37 CRITICAL_SECTION cs; | |
38 }; | |
39 | |
40 | |
41 RingBuffer *CreateRingBuffer(ALsizei frame_size, ALsizei length) | |
42 { | |
43 RingBuffer *ring = calloc(1, sizeof(*ring)); | |
44 if(ring) | |
45 { | |
46 ring->frame_size = frame_size; | |
47 ring->length = length+1; | |
48 ring->write_pos = 1; | |
49 ring->mem = malloc((length+1)*frame_size); | |
50 if(!ring->mem) | |
51 { | |
52 free(ring); | |
53 ring = NULL; | |
54 } | |
55 | |
56 InitializeCriticalSection(&ring->cs); | |
57 } | |
58 return ring; | |
59 } | |
60 | |
61 void DestroyRingBuffer(RingBuffer *ring) | |
62 { | |
63 if(ring) | |
64 { | |
65 DeleteCriticalSection(&ring->cs); | |
66 free(ring->mem); | |
67 free(ring); | |
68 } | |
69 } | |
70 | |
71 ALsizei RingBufferSize(RingBuffer *ring) | |
72 { | |
73 return (ring->write_pos-ring->read_pos-1+ring->length) % ring->length; | |
74 } | |
75 | |
76 void WriteRingBuffer(RingBuffer *ring, const ALubyte *data, ALsizei len) | |
77 { | |
78 int remain = ring->length - ring->write_pos; | |
79 | |
80 EnterCriticalSection(&ring->cs); | |
81 | |
82 if((ring->read_pos-ring->write_pos+ring->length)%ring->length < len) | |
83 ring->read_pos = (ring->write_pos+len) % ring->length; | |
84 | |
85 if(remain < len) | |
86 { | |
87 memcpy(ring->mem+(ring->write_pos*ring->frame_size), data, remain*ring->frame_size); | |
88 memcpy(ring->mem, data+(remain*ring->frame_size), (len-remain)*ring->frame_size); | |
89 } | |
90 else | |
91 memcpy(ring->mem+(ring->write_pos*ring->frame_size), data, len*ring->frame_size); | |
92 | |
93 ring->write_pos += len; | |
94 ring->write_pos %= ring->length; | |
95 | |
96 LeaveCriticalSection(&ring->cs); | |
97 } | |
98 | |
99 void ReadRingBuffer(RingBuffer *ring, ALubyte *data, ALsizei len) | |
100 { | |
101 int remain = ring->length - ring->read_pos; | |
102 | |
103 EnterCriticalSection(&ring->cs); | |
104 | |
105 if(remain < len) | |
106 { | |
107 memcpy(data, ring->mem+(ring->read_pos*ring->frame_size), remain*ring->frame_size); | |
108 memcpy(data+(remain*ring->frame_size), ring->mem, (len-remain)*ring->frame_size); | |
109 } | |
110 else | |
111 memcpy(data, ring->mem+(ring->read_pos*ring->frame_size), len*ring->frame_size); | |
112 | |
113 ring->read_pos += len; | |
114 ring->read_pos %= ring->length; | |
115 | |
116 LeaveCriticalSection(&ring->cs); | |
117 } | |
118 |