1 /* Copyright (C) 2007 Jean-Marc Valin
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
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.
39 #include "os_support.h"
41 #include <speex/speex_buffer.h>
51 EXPORT SpeexBuffer
*speex_buffer_init(int size
)
53 SpeexBuffer
*st
= speex_alloc(sizeof(SpeexBuffer
));
54 st
->data
= speex_alloc(size
);
62 EXPORT
void speex_buffer_destroy(SpeexBuffer
*st
)
68 EXPORT
int speex_buffer_write(SpeexBuffer
*st
, void *_data
, int len
)
78 end
= st
->write_ptr
+ len
;
82 SPEEX_COPY(st
->data
+ st
->write_ptr
, data
, end1
- st
->write_ptr
);
86 SPEEX_COPY(st
->data
, data
+end1
- st
->write_ptr
, end
);
89 if (st
->available
> st
->size
)
91 st
->available
= st
->size
;
92 st
->read_ptr
= st
->write_ptr
;
95 if (st
->write_ptr
> st
->size
)
96 st
->write_ptr
-= st
->size
;
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. */
110 end
= st
->write_ptr
+ len
;
114 SPEEX_MEMSET(st
->data
+ st
->write_ptr
, 0, end1
- st
->write_ptr
);
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
;
132 EXPORT
int speex_buffer_read(SpeexBuffer
*st
, void *_data
, int len
)
136 if (len
> st
->available
)
138 SPEEX_MEMSET(data
+st
->available
, 0, st
->size
-st
->available
);
141 end
= st
->read_ptr
+ len
;
145 SPEEX_COPY(data
, st
->data
+ st
->read_ptr
, end1
- st
->read_ptr
);
150 SPEEX_COPY(data
+end1
- st
->read_ptr
, st
->data
, end
);
152 st
->available
-= len
;
154 if (st
->read_ptr
> st
->size
)
155 st
->read_ptr
-= st
->size
;
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
;
169 st
->data
= speex_realloc(st
->data
, len
);
170 /* FIXME: move data/pointers properly for growing the buffer */
172 /* FIXME: move data/pointers properly for shrinking the buffer */
173 st
->data
= speex_realloc(st
->data
, len
);