Add per-user preferences support.
[chromium-blink-merge.git] / third_party / speex / libspeex / buffer.c
blob6cfd5a341368a77b01398c05e92954d3c4c43dd8
1 /* Copyright (C) 2007 Jean-Marc Valin
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.
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are
9 met:
11 1. Redistributions of source code must retain the above copyright notice,
12 this list of conditions and the following disclaimer.
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.
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.
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.
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
39 #include "os_support.h"
40 #include "arch.h"
41 #include <speex/speex_buffer.h>
43 struct SpeexBuffer_ {
44 char *data;
45 int size;
46 int read_ptr;
47 int write_ptr;
48 int available;
51 EXPORT SpeexBuffer *speex_buffer_init(int size)
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;
62 EXPORT void speex_buffer_destroy(SpeexBuffer *st)
64 speex_free(st->data);
65 speex_free(st);
68 EXPORT int speex_buffer_write(SpeexBuffer *st, void *_data, int len)
70 int end;
71 int end1;
72 char *data = _data;
73 if (len > st->size)
75 data += len-st->size;
76 len = st->size;
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)
85 end -= st->size;
86 SPEEX_COPY(st->data, data+end1 - st->write_ptr, end);
88 st->available += len;
89 if (st->available > st->size)
91 st->available = st->size;
92 st->read_ptr = st->write_ptr;
94 st->write_ptr += len;
95 if (st->write_ptr > st->size)
96 st->write_ptr -= st->size;
97 return len;
100 EXPORT int speex_buffer_writezeros(SpeexBuffer *st, int len)
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)
108 len = st->size;
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)
117 end -= st->size;
118 SPEEX_MEMSET(st->data, 0, end);
120 st->available += len;
121 if (st->available > st->size)
123 st->available = st->size;
124 st->read_ptr = st->write_ptr;
126 st->write_ptr += len;
127 if (st->write_ptr > st->size)
128 st->write_ptr -= st->size;
129 return len;
132 EXPORT int speex_buffer_read(SpeexBuffer *st, void *_data, int len)
134 int end, end1;
135 char *data = _data;
136 if (len > st->available)
138 SPEEX_MEMSET(data+st->available, 0, st->size-st->available);
139 len = st->available;
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);
147 if (end > st->size)
149 end -= st->size;
150 SPEEX_COPY(data+end1 - st->read_ptr, st->data, end);
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;
159 EXPORT int speex_buffer_get_available(SpeexBuffer *st)
161 return st->available;
164 EXPORT int speex_buffer_resize(SpeexBuffer *st, int len)
166 int old_len = st->size;
167 if (len > old_len)
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);
175 return len;