Don't pass the device to HRTF methods
[openal-soft.git] / Alc / alcRing.c
blob9b5d82146c75f13c5cfd6b8139f7b13c529fa05f
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 "threads.h"
28 #include "compat.h"
31 struct RingBuffer {
32 ALubyte *mem;
34 ALsizei frame_size;
35 ALsizei length;
36 ALint read_pos;
37 ALint write_pos;
39 almtx_t mtx;
43 RingBuffer *CreateRingBuffer(ALsizei frame_size, ALsizei length)
45 RingBuffer *ring = calloc(1, sizeof(*ring) + ((length+1) * frame_size));
46 if(ring)
48 ring->mem = (ALubyte*)(ring+1);
50 ring->frame_size = frame_size;
51 ring->length = length+1;
52 ring->read_pos = 0;
53 ring->write_pos = 0;
55 almtx_init(&ring->mtx, almtx_plain);
57 return ring;
60 void DestroyRingBuffer(RingBuffer *ring)
62 if(ring)
64 almtx_destroy(&ring->mtx);
65 free(ring);
69 ALsizei RingBufferSize(RingBuffer *ring)
71 ALsizei s;
73 almtx_lock(&ring->mtx);
74 s = (ring->write_pos-ring->read_pos+ring->length) % ring->length;
75 almtx_unlock(&ring->mtx);
77 return s;
80 void WriteRingBuffer(RingBuffer *ring, const ALubyte *data, ALsizei len)
82 int remain;
84 almtx_lock(&ring->mtx);
86 remain = (ring->read_pos-ring->write_pos-1+ring->length) % ring->length;
87 if(remain < len) len = remain;
89 if(len > 0)
91 remain = ring->length - ring->write_pos;
92 if(remain < len)
94 memcpy(ring->mem+(ring->write_pos*ring->frame_size), data,
95 remain*ring->frame_size);
96 memcpy(ring->mem, data+(remain*ring->frame_size),
97 (len-remain)*ring->frame_size);
99 else
100 memcpy(ring->mem+(ring->write_pos*ring->frame_size), data,
101 len*ring->frame_size);
103 ring->write_pos += len;
104 ring->write_pos %= ring->length;
107 almtx_unlock(&ring->mtx);
110 void ReadRingBuffer(RingBuffer *ring, ALubyte *data, ALsizei len)
112 int remain;
114 almtx_lock(&ring->mtx);
116 remain = ring->length - ring->read_pos;
117 if(remain < len)
119 memcpy(data, ring->mem+(ring->read_pos*ring->frame_size), remain*ring->frame_size);
120 memcpy(data+(remain*ring->frame_size), ring->mem, (len-remain)*ring->frame_size);
122 else
123 memcpy(data, ring->mem+(ring->read_pos*ring->frame_size), len*ring->frame_size);
125 ring->read_pos += len;
126 ring->read_pos %= ring->length;
128 almtx_unlock(&ring->mtx);