Reactivate the mmdevapi audio client and set the event handle on reset
[openal-soft.git] / Alc / alcRing.c
blobf831860fb4063e1ca83756033b6b9f30267b1b60
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.
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.
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
21 #include "config.h"
23 #include <string.h>
24 #include <stdlib.h>
26 #include "alMain.h"
27 #include "compat.h"
30 struct RingBuffer {
31 ALubyte *mem;
33 ALsizei frame_size;
34 ALsizei length;
35 ALint read_pos;
36 ALint write_pos;
38 CRITICAL_SECTION cs;
42 RingBuffer *CreateRingBuffer(ALsizei frame_size, ALsizei length)
44 RingBuffer *ring = calloc(1, sizeof(*ring) + ((length+1) * frame_size));
45 if(ring)
47 ring->mem = (ALubyte*)(ring+1);
49 ring->frame_size = frame_size;
50 ring->length = length+1;
51 ring->read_pos = 0;
52 ring->write_pos = 0;
54 InitializeCriticalSection(&ring->cs);
56 return ring;
59 void DestroyRingBuffer(RingBuffer *ring)
61 if(ring)
63 DeleteCriticalSection(&ring->cs);
64 free(ring);
68 ALsizei RingBufferSize(RingBuffer *ring)
70 ALsizei s;
72 EnterCriticalSection(&ring->cs);
73 s = (ring->write_pos-ring->read_pos+ring->length) % ring->length;
74 LeaveCriticalSection(&ring->cs);
76 return s;
79 void WriteRingBuffer(RingBuffer *ring, const ALubyte *data, ALsizei len)
81 int remain;
83 EnterCriticalSection(&ring->cs);
85 remain = (ring->read_pos-ring->write_pos-1+ring->length) % ring->length;
86 if(remain < len) len = remain;
88 if(len > 0)
90 remain = ring->length - ring->write_pos;
91 if(remain < len)
93 memcpy(ring->mem+(ring->write_pos*ring->frame_size), data,
94 remain*ring->frame_size);
95 memcpy(ring->mem, data+(remain*ring->frame_size),
96 (len-remain)*ring->frame_size);
98 else
99 memcpy(ring->mem+(ring->write_pos*ring->frame_size), data,
100 len*ring->frame_size);
102 ring->write_pos += len;
103 ring->write_pos %= ring->length;
106 LeaveCriticalSection(&ring->cs);
109 void ReadRingBuffer(RingBuffer *ring, ALubyte *data, ALsizei len)
111 int remain;
113 EnterCriticalSection(&ring->cs);
115 remain = ring->length - ring->read_pos;
116 if(remain < len)
118 memcpy(data, ring->mem+(ring->read_pos*ring->frame_size), remain*ring->frame_size);
119 memcpy(data+(remain*ring->frame_size), ring->mem, (len-remain)*ring->frame_size);
121 else
122 memcpy(data, ring->mem+(ring->read_pos*ring->frame_size), len*ring->frame_size);
124 ring->read_pos += len;
125 ring->read_pos %= ring->length;
127 LeaveCriticalSection(&ring->cs);