annotate Alc/alcRing.c @ 26:56f83c170251

adding hard-to-compile artifacts for other systems
author Robert McIntyre <rlm@mit.edu>
date Sat, 19 Nov 2011 19:17:37 -0700
parents f9476ff7637e
children
rev   line source
rlm@0 1 /**
rlm@0 2 * OpenAL cross platform audio library
rlm@0 3 * Copyright (C) 1999-2007 by authors.
rlm@0 4 * This library is free software; you can redistribute it and/or
rlm@0 5 * modify it under the terms of the GNU Library General Public
rlm@0 6 * License as published by the Free Software Foundation; either
rlm@0 7 * version 2 of the License, or (at your option) any later version.
rlm@0 8 *
rlm@0 9 * This library is distributed in the hope that it will be useful,
rlm@0 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
rlm@0 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
rlm@0 12 * Library General Public License for more details.
rlm@0 13 *
rlm@0 14 * You should have received a copy of the GNU Library General Public
rlm@0 15 * License along with this library; if not, write to the
rlm@0 16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
rlm@0 17 * Boston, MA 02111-1307, USA.
rlm@0 18 * Or go to http://www.gnu.org/copyleft/lgpl.html
rlm@0 19 */
rlm@0 20
rlm@0 21 #include "config.h"
rlm@0 22
rlm@0 23 #include <string.h>
rlm@0 24 #include <stdlib.h>
rlm@0 25
rlm@0 26 #include "alMain.h"
rlm@0 27
rlm@0 28
rlm@0 29 struct RingBuffer {
rlm@0 30 ALubyte *mem;
rlm@0 31
rlm@0 32 ALsizei frame_size;
rlm@0 33 ALsizei length;
rlm@0 34 ALint read_pos;
rlm@0 35 ALint write_pos;
rlm@0 36
rlm@0 37 CRITICAL_SECTION cs;
rlm@0 38 };
rlm@0 39
rlm@0 40
rlm@0 41 RingBuffer *CreateRingBuffer(ALsizei frame_size, ALsizei length)
rlm@0 42 {
rlm@0 43 RingBuffer *ring = calloc(1, sizeof(*ring) + ((length+1) * frame_size));
rlm@0 44 if(ring)
rlm@0 45 {
rlm@0 46 ring->mem = (ALubyte*)(ring+1);
rlm@0 47
rlm@0 48 ring->frame_size = frame_size;
rlm@0 49 ring->length = length+1;
rlm@0 50 ring->read_pos = 0;
rlm@0 51 ring->write_pos = 0;
rlm@0 52
rlm@0 53 InitializeCriticalSection(&ring->cs);
rlm@0 54 }
rlm@0 55 return ring;
rlm@0 56 }
rlm@0 57
rlm@0 58 void DestroyRingBuffer(RingBuffer *ring)
rlm@0 59 {
rlm@0 60 if(ring)
rlm@0 61 {
rlm@0 62 DeleteCriticalSection(&ring->cs);
rlm@0 63 free(ring);
rlm@0 64 }
rlm@0 65 }
rlm@0 66
rlm@0 67 ALsizei RingBufferSize(RingBuffer *ring)
rlm@0 68 {
rlm@0 69 ALsizei s;
rlm@0 70
rlm@0 71 EnterCriticalSection(&ring->cs);
rlm@0 72 s = (ring->write_pos-ring->read_pos+ring->length) % ring->length;
rlm@0 73 LeaveCriticalSection(&ring->cs);
rlm@0 74
rlm@0 75 return s;
rlm@0 76 }
rlm@0 77
rlm@0 78 void WriteRingBuffer(RingBuffer *ring, const ALubyte *data, ALsizei len)
rlm@0 79 {
rlm@0 80 int remain;
rlm@0 81
rlm@0 82 EnterCriticalSection(&ring->cs);
rlm@0 83
rlm@0 84 remain = (ring->read_pos-ring->write_pos-1+ring->length) % ring->length;
rlm@0 85 if(remain < len) len = remain;
rlm@0 86
rlm@0 87 if(len > 0)
rlm@0 88 {
rlm@0 89 remain = ring->length - ring->write_pos;
rlm@0 90 if(remain < len)
rlm@0 91 {
rlm@0 92 memcpy(ring->mem+(ring->write_pos*ring->frame_size), data,
rlm@0 93 remain*ring->frame_size);
rlm@0 94 memcpy(ring->mem, data+(remain*ring->frame_size),
rlm@0 95 (len-remain)*ring->frame_size);
rlm@0 96 }
rlm@0 97 else
rlm@0 98 memcpy(ring->mem+(ring->write_pos*ring->frame_size), data,
rlm@0 99 len*ring->frame_size);
rlm@0 100
rlm@0 101 ring->write_pos += len;
rlm@0 102 ring->write_pos %= ring->length;
rlm@0 103 }
rlm@0 104
rlm@0 105 LeaveCriticalSection(&ring->cs);
rlm@0 106 }
rlm@0 107
rlm@0 108 void ReadRingBuffer(RingBuffer *ring, ALubyte *data, ALsizei len)
rlm@0 109 {
rlm@0 110 int remain;
rlm@0 111
rlm@0 112 EnterCriticalSection(&ring->cs);
rlm@0 113
rlm@0 114 remain = ring->length - ring->read_pos;
rlm@0 115 if(remain < len)
rlm@0 116 {
rlm@0 117 memcpy(data, ring->mem+(ring->read_pos*ring->frame_size), remain*ring->frame_size);
rlm@0 118 memcpy(data+(remain*ring->frame_size), ring->mem, (len-remain)*ring->frame_size);
rlm@0 119 }
rlm@0 120 else
rlm@0 121 memcpy(data, ring->mem+(ring->read_pos*ring->frame_size), len*ring->frame_size);
rlm@0 122
rlm@0 123 ring->read_pos += len;
rlm@0 124 ring->read_pos %= ring->length;
rlm@0 125
rlm@0 126 LeaveCriticalSection(&ring->cs);
rlm@0 127 }