rlm@0: /** rlm@0: * OpenAL cross platform audio library rlm@0: * Copyright (C) 1999-2007 by authors. rlm@0: * This library is free software; you can redistribute it and/or rlm@0: * modify it under the terms of the GNU Library General Public rlm@0: * License as published by the Free Software Foundation; either rlm@0: * version 2 of the License, or (at your option) any later version. rlm@0: * rlm@0: * This library is distributed in the hope that it will be useful, rlm@0: * but WITHOUT ANY WARRANTY; without even the implied warranty of rlm@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU rlm@0: * Library General Public License for more details. rlm@0: * rlm@0: * You should have received a copy of the GNU Library General Public rlm@0: * License along with this library; if not, write to the rlm@0: * Free Software Foundation, Inc., 59 Temple Place - Suite 330, rlm@0: * Boston, MA 02111-1307, USA. rlm@0: * Or go to http://www.gnu.org/copyleft/lgpl.html rlm@0: */ rlm@0: rlm@0: #include "config.h" rlm@0: rlm@0: #include rlm@0: #include rlm@0: rlm@0: #include "alMain.h" rlm@0: rlm@0: rlm@0: struct RingBuffer { rlm@0: ALubyte *mem; rlm@0: rlm@0: ALsizei frame_size; rlm@0: ALsizei length; rlm@0: ALint read_pos; rlm@0: ALint write_pos; rlm@0: rlm@0: CRITICAL_SECTION cs; rlm@0: }; rlm@0: rlm@0: rlm@0: RingBuffer *CreateRingBuffer(ALsizei frame_size, ALsizei length) rlm@0: { rlm@0: RingBuffer *ring = calloc(1, sizeof(*ring) + ((length+1) * frame_size)); rlm@0: if(ring) rlm@0: { rlm@0: ring->mem = (ALubyte*)(ring+1); rlm@0: rlm@0: ring->frame_size = frame_size; rlm@0: ring->length = length+1; rlm@0: ring->read_pos = 0; rlm@0: ring->write_pos = 0; rlm@0: rlm@0: InitializeCriticalSection(&ring->cs); rlm@0: } rlm@0: return ring; rlm@0: } rlm@0: rlm@0: void DestroyRingBuffer(RingBuffer *ring) rlm@0: { rlm@0: if(ring) rlm@0: { rlm@0: DeleteCriticalSection(&ring->cs); rlm@0: free(ring); rlm@0: } rlm@0: } rlm@0: rlm@0: ALsizei RingBufferSize(RingBuffer *ring) rlm@0: { rlm@0: ALsizei s; rlm@0: rlm@0: EnterCriticalSection(&ring->cs); rlm@0: s = (ring->write_pos-ring->read_pos+ring->length) % ring->length; rlm@0: LeaveCriticalSection(&ring->cs); rlm@0: rlm@0: return s; rlm@0: } rlm@0: rlm@0: void WriteRingBuffer(RingBuffer *ring, const ALubyte *data, ALsizei len) rlm@0: { rlm@0: int remain; rlm@0: rlm@0: EnterCriticalSection(&ring->cs); rlm@0: rlm@0: remain = (ring->read_pos-ring->write_pos-1+ring->length) % ring->length; rlm@0: if(remain < len) len = remain; rlm@0: rlm@0: if(len > 0) rlm@0: { rlm@0: remain = ring->length - ring->write_pos; rlm@0: if(remain < len) rlm@0: { rlm@0: memcpy(ring->mem+(ring->write_pos*ring->frame_size), data, rlm@0: remain*ring->frame_size); rlm@0: memcpy(ring->mem, data+(remain*ring->frame_size), rlm@0: (len-remain)*ring->frame_size); rlm@0: } rlm@0: else rlm@0: memcpy(ring->mem+(ring->write_pos*ring->frame_size), data, rlm@0: len*ring->frame_size); rlm@0: rlm@0: ring->write_pos += len; rlm@0: ring->write_pos %= ring->length; rlm@0: } rlm@0: rlm@0: LeaveCriticalSection(&ring->cs); rlm@0: } rlm@0: rlm@0: void ReadRingBuffer(RingBuffer *ring, ALubyte *data, ALsizei len) rlm@0: { rlm@0: int remain; rlm@0: rlm@0: EnterCriticalSection(&ring->cs); rlm@0: rlm@0: remain = ring->length - ring->read_pos; rlm@0: if(remain < len) rlm@0: { rlm@0: memcpy(data, ring->mem+(ring->read_pos*ring->frame_size), remain*ring->frame_size); rlm@0: memcpy(data+(remain*ring->frame_size), ring->mem, (len-remain)*ring->frame_size); rlm@0: } rlm@0: else rlm@0: memcpy(data, ring->mem+(ring->read_pos*ring->frame_size), len*ring->frame_size); rlm@0: rlm@0: ring->read_pos += len; rlm@0: ring->read_pos %= ring->length; rlm@0: rlm@0: LeaveCriticalSection(&ring->cs); rlm@0: }