• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (C) 2007 Jean-Marc Valin
2 
3    File: buffer.c
4    This is a very simple ring buffer implementation. It is not thread-safe
5    so you need to do your own locking.
6 
7    Redistribution and use in source and binary forms, with or without
8    modification, are permitted provided that the following conditions are
9    met:
10 
11    1. Redistributions of source code must retain the above copyright notice,
12    this list of conditions and the following disclaimer.
13 
14    2. Redistributions in binary form must reproduce the above copyright
15    notice, this list of conditions and the following disclaimer in the
16    documentation and/or other materials provided with the distribution.
17 
18    3. The name of the author may not be used to endorse or promote products
19    derived from this software without specific prior written permission.
20 
21    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24    DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25    INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29    STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31    POSSIBILITY OF SUCH DAMAGE.
32 */
33 
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
37 
38 
39 #include "os_support.h"
40 #include "arch.h"
41 #include <speex/speex_buffer.h>
42 
43 struct SpeexBuffer_ {
44    char *data;
45    int   size;
46    int   read_ptr;
47    int   write_ptr;
48    int   available;
49 };
50 
speex_buffer_init(int size)51 EXPORT SpeexBuffer *speex_buffer_init(int size)
52 {
53    SpeexBuffer *st = speex_alloc(sizeof(SpeexBuffer));
54    st->data = speex_alloc(size);
55    st->size = size;
56    st->read_ptr = 0;
57    st->write_ptr = 0;
58    st->available = 0;
59    return st;
60 }
61 
speex_buffer_destroy(SpeexBuffer * st)62 EXPORT void speex_buffer_destroy(SpeexBuffer *st)
63 {
64    speex_free(st->data);
65    speex_free(st);
66 }
67 
speex_buffer_write(SpeexBuffer * st,void * _data,int len)68 EXPORT int speex_buffer_write(SpeexBuffer *st, void *_data, int len)
69 {
70    int end;
71    int end1;
72    char *data = _data;
73    if (len > st->size)
74    {
75       data += len-st->size;
76       len = st->size;
77    }
78    end = st->write_ptr + len;
79    end1 = end;
80    if (end1 > st->size)
81       end1 = st->size;
82    SPEEX_COPY(st->data + st->write_ptr, data, end1 - st->write_ptr);
83    if (end > st->size)
84    {
85       end -= st->size;
86       SPEEX_COPY(st->data, data+end1 - st->write_ptr, end);
87    }
88    st->available += len;
89    if (st->available > st->size)
90    {
91       st->available = st->size;
92       st->read_ptr = st->write_ptr;
93    }
94    st->write_ptr += len;
95    if (st->write_ptr > st->size)
96       st->write_ptr -= st->size;
97    return len;
98 }
99 
speex_buffer_writezeros(SpeexBuffer * st,int len)100 EXPORT int speex_buffer_writezeros(SpeexBuffer *st, int len)
101 {
102    /* This is almost the same as for speex_buffer_write() but using
103    SPEEX_MEMSET() instead of SPEEX_COPY(). Update accordingly. */
104    int end;
105    int end1;
106    if (len > st->size)
107    {
108       len = st->size;
109    }
110    end = st->write_ptr + len;
111    end1 = end;
112    if (end1 > st->size)
113       end1 = st->size;
114    SPEEX_MEMSET(st->data + st->write_ptr, 0, end1 - st->write_ptr);
115    if (end > st->size)
116    {
117       end -= st->size;
118       SPEEX_MEMSET(st->data, 0, end);
119    }
120    st->available += len;
121    if (st->available > st->size)
122    {
123       st->available = st->size;
124       st->read_ptr = st->write_ptr;
125    }
126    st->write_ptr += len;
127    if (st->write_ptr > st->size)
128       st->write_ptr -= st->size;
129    return len;
130 }
131 
speex_buffer_read(SpeexBuffer * st,void * _data,int len)132 EXPORT int speex_buffer_read(SpeexBuffer *st, void *_data, int len)
133 {
134    int end, end1;
135    char *data = _data;
136    if (len > st->available)
137    {
138       SPEEX_MEMSET(data+st->available, 0, st->size-st->available);
139       len = st->available;
140    }
141    end = st->read_ptr + len;
142    end1 = end;
143    if (end1 > st->size)
144       end1 = st->size;
145    SPEEX_COPY(data, st->data + st->read_ptr, end1 - st->read_ptr);
146 
147    if (end > st->size)
148    {
149       end -= st->size;
150       SPEEX_COPY(data+end1 - st->read_ptr, st->data, end);
151    }
152    st->available -= len;
153    st->read_ptr += len;
154    if (st->read_ptr > st->size)
155       st->read_ptr -= st->size;
156    return len;
157 }
158 
speex_buffer_get_available(SpeexBuffer * st)159 EXPORT int speex_buffer_get_available(SpeexBuffer *st)
160 {
161    return st->available;
162 }
163 
speex_buffer_resize(SpeexBuffer * st,int len)164 EXPORT int speex_buffer_resize(SpeexBuffer *st, int len)
165 {
166    int old_len = st->size;
167    if (len > old_len)
168    {
169       st->data = speex_realloc(st->data, len);
170       /* FIXME: move data/pointers properly for growing the buffer */
171    } else {
172       /* FIXME: move data/pointers properly for shrinking the buffer */
173       st->data = speex_realloc(st->data, len);
174    }
175    return len;
176 }
177