Mercurial > audio-send
diff Alc/alcRing.c @ 0:f9476ff7637e
initial forking of open-al to create multiple listeners
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Tue, 25 Oct 2011 13:02:31 -0700 |
parents | |
children |
line wrap: on
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/Alc/alcRing.c Tue Oct 25 13:02:31 2011 -0700 1.3 @@ -0,0 +1,127 @@ 1.4 +/** 1.5 + * OpenAL cross platform audio library 1.6 + * Copyright (C) 1999-2007 by authors. 1.7 + * This library is free software; you can redistribute it and/or 1.8 + * modify it under the terms of the GNU Library General Public 1.9 + * License as published by the Free Software Foundation; either 1.10 + * version 2 of the License, or (at your option) any later version. 1.11 + * 1.12 + * This library is distributed in the hope that it will be useful, 1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of 1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 1.15 + * Library General Public License for more details. 1.16 + * 1.17 + * You should have received a copy of the GNU Library General Public 1.18 + * License along with this library; if not, write to the 1.19 + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 1.20 + * Boston, MA 02111-1307, USA. 1.21 + * Or go to http://www.gnu.org/copyleft/lgpl.html 1.22 + */ 1.23 + 1.24 +#include "config.h" 1.25 + 1.26 +#include <string.h> 1.27 +#include <stdlib.h> 1.28 + 1.29 +#include "alMain.h" 1.30 + 1.31 + 1.32 +struct RingBuffer { 1.33 + ALubyte *mem; 1.34 + 1.35 + ALsizei frame_size; 1.36 + ALsizei length; 1.37 + ALint read_pos; 1.38 + ALint write_pos; 1.39 + 1.40 + CRITICAL_SECTION cs; 1.41 +}; 1.42 + 1.43 + 1.44 +RingBuffer *CreateRingBuffer(ALsizei frame_size, ALsizei length) 1.45 +{ 1.46 + RingBuffer *ring = calloc(1, sizeof(*ring) + ((length+1) * frame_size)); 1.47 + if(ring) 1.48 + { 1.49 + ring->mem = (ALubyte*)(ring+1); 1.50 + 1.51 + ring->frame_size = frame_size; 1.52 + ring->length = length+1; 1.53 + ring->read_pos = 0; 1.54 + ring->write_pos = 0; 1.55 + 1.56 + InitializeCriticalSection(&ring->cs); 1.57 + } 1.58 + return ring; 1.59 +} 1.60 + 1.61 +void DestroyRingBuffer(RingBuffer *ring) 1.62 +{ 1.63 + if(ring) 1.64 + { 1.65 + DeleteCriticalSection(&ring->cs); 1.66 + free(ring); 1.67 + } 1.68 +} 1.69 + 1.70 +ALsizei RingBufferSize(RingBuffer *ring) 1.71 +{ 1.72 + ALsizei s; 1.73 + 1.74 + EnterCriticalSection(&ring->cs); 1.75 + s = (ring->write_pos-ring->read_pos+ring->length) % ring->length; 1.76 + LeaveCriticalSection(&ring->cs); 1.77 + 1.78 + return s; 1.79 +} 1.80 + 1.81 +void WriteRingBuffer(RingBuffer *ring, const ALubyte *data, ALsizei len) 1.82 +{ 1.83 + int remain; 1.84 + 1.85 + EnterCriticalSection(&ring->cs); 1.86 + 1.87 + remain = (ring->read_pos-ring->write_pos-1+ring->length) % ring->length; 1.88 + if(remain < len) len = remain; 1.89 + 1.90 + if(len > 0) 1.91 + { 1.92 + remain = ring->length - ring->write_pos; 1.93 + if(remain < len) 1.94 + { 1.95 + memcpy(ring->mem+(ring->write_pos*ring->frame_size), data, 1.96 + remain*ring->frame_size); 1.97 + memcpy(ring->mem, data+(remain*ring->frame_size), 1.98 + (len-remain)*ring->frame_size); 1.99 + } 1.100 + else 1.101 + memcpy(ring->mem+(ring->write_pos*ring->frame_size), data, 1.102 + len*ring->frame_size); 1.103 + 1.104 + ring->write_pos += len; 1.105 + ring->write_pos %= ring->length; 1.106 + } 1.107 + 1.108 + LeaveCriticalSection(&ring->cs); 1.109 +} 1.110 + 1.111 +void ReadRingBuffer(RingBuffer *ring, ALubyte *data, ALsizei len) 1.112 +{ 1.113 + int remain; 1.114 + 1.115 + EnterCriticalSection(&ring->cs); 1.116 + 1.117 + remain = ring->length - ring->read_pos; 1.118 + if(remain < len) 1.119 + { 1.120 + memcpy(data, ring->mem+(ring->read_pos*ring->frame_size), remain*ring->frame_size); 1.121 + memcpy(data+(remain*ring->frame_size), ring->mem, (len-remain)*ring->frame_size); 1.122 + } 1.123 + else 1.124 + memcpy(data, ring->mem+(ring->read_pos*ring->frame_size), len*ring->frame_size); 1.125 + 1.126 + ring->read_pos += len; 1.127 + ring->read_pos %= ring->length; 1.128 + 1.129 + LeaveCriticalSection(&ring->cs); 1.130 +}