LMMS
Loading...
Searching...
No Matches
buffer.h
Go to the documentation of this file.
1/* Calf DSP Library
2 * Buffer abstractions.
3 *
4 * Copyright (C) 2007 Krzysztof Foltman
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301 USA
20 */
21#ifndef __BUFFER_H
22#define __BUFFER_H
23
24namespace dsp {
25
27template<int N> inline int wrap_around(int a) {
28 return (a >= N) ? a - N : a;
29}
30
31// provide fast specializations for powers of 2
32template<> inline int wrap_around<2>(int a) { return a & 1; }
33template<> inline int wrap_around<4>(int a) { return a & 3; }
34template<> inline int wrap_around<8>(int a) { return a & 7; }
35template<> inline int wrap_around<16>(int a) { return a & 15; }
36template<> inline int wrap_around<32>(int a) { return a & 31; }
37template<> inline int wrap_around<64>(int a) { return a & 63; }
38template<> inline int wrap_around<128>(int a) { return a & 127; }
39template<> inline int wrap_around<256>(int a) { return a & 255; }
40template<> inline int wrap_around<512>(int a) { return a & 511; }
41template<> inline int wrap_around<1024>(int a) { return a & 1023; }
42template<> inline int wrap_around<2048>(int a) { return a & 2047; }
43template<> inline int wrap_around<4096>(int a) { return a & 4095; }
44template<> inline int wrap_around<8192>(int a) { return a & 8191; }
45template<> inline int wrap_around<16384>(int a) { return a & 16383; }
46template<> inline int wrap_around<32768>(int a) { return a & 32767; }
47template<> inline int wrap_around<65536>(int a) { return a & 65535; }
48
49template<class Buf, class T>
50void fill(Buf &buf, T value) {
51 T* data = buf.data();
52 int size = buf.size();
53 for (int i=0; i<size; i++)
54 *data++ = value;
55}
56
57template<class T>
58void fill(T *data, int size, T value) {
59 for (int i=0; i<size; i++)
60 *data++ = value;
61}
62
63template<class T, class U>
64void copy(T *dest, U *src, int size, T scale = 1, T add = 0) {
65 for (int i=0; i<size; i++)
66 *dest++ = (*src++) * scale + add;
67}
68
69template<class T>
71 enum {
73 bps = sizeof(T)*8
74 };
75};
76
77template<class T>
79 enum {
81 bps = sizeof(T)*8
82 };
83};
84
85template<int N, class T = float>
87public:
88 typedef T data_type;
89 enum { buffer_size = N };
90 inline int size() { return N; }
91};
92
93template<int N, class T = float>
95 T *buf;
96public:
97 mem_fixed_size_buffer(T ubuf[N]) { buf = ubuf; }
98 void set_data(T buf[N]) { this->buf = buf; }
99 inline T* data() { return buf; }
100 inline const T* data() const { return buf; }
101 inline T& operator[](int pos) { return buf[pos]; }
102 inline const T& operator[](int pos) const { return buf[pos]; }
103};
104
105template<int N, class T = float>
106class auto_buffer: public fixed_size_buffer<N, T> {
107 T buf[N];
108public:
109 T* data() const { return buf; }
110 inline T& operator[](int pos) { return buf[pos]; }
111 inline const T& operator[](int pos) const { return buf[pos]; }
112};
113
114template<class T = float>
116 T *buf;
118 bool owns;
119public:
120 dynamic_buffer() { owns = false; }
121 dynamic_buffer(T *_buf, int _buf_size, bool _own)
122 : buf(_buf), buf_size(_buf_size), owns(_own) {
123 }
124 dynamic_buffer(int _size) {
125 buf = new T[_size];
126 buf_size = _size;
127 owns = true;
128 }
129 inline T* data() { return buf; }
130 inline const T* data() const { return buf; }
131 inline int size() { return buf_size; }
132 void resize(int new_size, bool fill_with_zeros = false) {
133 T *new_buf = new T[new_size];
134 memcpy(new_buf, buf, std::min(buf_size, new_size));
135 if (fill_with_zeros && buf_size < new_size)
136 dsp::zero(new_buf + buf_size, new_size - buf_size);
137 if (owns)
138 delete []buf;
139 buf = new_buf;
140 buf_size = new_size;
141 owns = true;
142 }
143 inline T& operator[](int pos) { return buf[pos]; }
144 inline const T& operator[](int pos) const { return buf[pos]; }
146 if (owns)
147 delete []buf;
148 }
149};
150
151template<class T, class U>
152void copy_buf(T &dest_buf, const U &src_buf, T scale = 1, T add = 0) {
153 typedef typename T::data_type data_type;
154 data_type *dest = dest_buf.data();
155 const data_type *src = src_buf.data();
156 int size = src->size();
157 for (int i=0; i<size; i++)
158 *dest++ = (*src++) * scale + add;
159}
160
161template<class T>
163};
164
166template<int N, class T>
168 int inc_wrap(int pos) const {
169 return wrap_around<T::size>(pos+1);
170 }
171
172 int pos_diff(int pos1, int pos2) const {
173 int pos = pos1 - pos2;
174 if (pos < 0) pos += T::size;
175 return pos;
176 }
177};
178
180template<class B>
181class circular_buffer: public B {
182 typedef typename B::data_type data_type;
183 typedef class buffer_traits<B> traits;
185 int rpos, wpos;
187 clear();
188 }
189 void clear() {
190 rpos = 0;
191 wpos = 0;
192 }
193 inline void put(data_type data) {
194 buffer[wpos] = data;
195 wpos = traits::inc_wrap(wpos);
196 }
197 inline bool empty() {
198 return rpos == wpos;
199 }
200 inline bool full() {
201 return rpos == traits::inc_wrap(wpos);
202 }
203 inline const data_type& get() {
204 int oldrpos = rpos;
205 rpos = traits::inc_wrap(rpos);
206 return buffer[oldrpos];
207 }
208 inline int get_rbytes() {
209 return traits::pos_diff(wpos, rpos);
210 }
211 inline int get_wbytes() {
212 if (full()) return 0;
213 return traits::pos_diff(rpos, wpos);
214 }
215};
216
218template<int N, class T = float>
219class mono_auto_buffer: public auto_buffer<N, T> {
220};
221
223template<int N, class T = float>
224class stereo_auto_buffer: public auto_buffer<N, stereo_sample<T> > {
225};
226
227};
228
229#endif
#define copy(x)
Definition ADnoteParameters.cpp:1011
uint8_t a
Definition Spc_Cpu.h:141
Definition buffer.h:106
T & operator[](int pos)
Definition buffer.h:110
T buf[N]
Definition buffer.h:107
const T & operator[](int pos) const
Definition buffer.h:111
T * data() const
Definition buffer.h:109
const data_type & get()
Definition buffer.h:203
void clear()
Definition buffer.h:189
int get_wbytes()
Definition buffer.h:211
int rpos
Definition buffer.h:185
B::data_type data_type
Definition buffer.h:182
int wpos
Definition buffer.h:185
bool full()
Definition buffer.h:200
bool empty()
Definition buffer.h:197
class buffer_traits< B > traits
Definition buffer.h:183
circular_buffer()
Definition buffer.h:186
int get_rbytes()
Definition buffer.h:208
B buffer
Definition buffer.h:184
void put(data_type data)
Definition buffer.h:193
void resize(int new_size, bool fill_with_zeros=false)
Definition buffer.h:132
bool owns
Definition buffer.h:118
~dynamic_buffer()
Definition buffer.h:145
T & operator[](int pos)
Definition buffer.h:143
T * data()
Definition buffer.h:129
int buf_size
Definition buffer.h:117
const T & operator[](int pos) const
Definition buffer.h:144
dynamic_buffer()
Definition buffer.h:120
T * buf
Definition buffer.h:116
const T * data() const
Definition buffer.h:130
dynamic_buffer(T *_buf, int _buf_size, bool _own)
Definition buffer.h:121
dynamic_buffer(int _size)
Definition buffer.h:124
int size()
Definition buffer.h:131
Definition buffer.h:86
@ buffer_size
Definition buffer.h:89
int size()
Definition buffer.h:90
T data_type
Definition buffer.h:88
T & operator[](int pos)
Definition buffer.h:101
T * buf
Definition buffer.h:95
void set_data(T buf[N])
Definition buffer.h:98
T * data()
Definition buffer.h:99
const T & operator[](int pos) const
Definition buffer.h:102
const T * data() const
Definition buffer.h:100
mem_fixed_size_buffer(T ubuf[N])
Definition buffer.h:97
this is useless for now
Definition buffer.h:219
this is useless for now
Definition buffer.h:224
register unsigned i
Definition inflate.c:1575
#define U(x)
Definition fmopl.c:132
static PuglViewHint int value
Definition pugl.h:1708
Definition audio_fx.h:36
int wrap_around< 512 >(int a)
Definition buffer.h:40
int wrap_around< 16 >(int a)
Definition buffer.h:35
int wrap_around< 16384 >(int a)
Definition buffer.h:45
void zero(float &v)
Set a float to zero.
Definition primitives.h:41
int wrap_around< 128 >(int a)
Definition buffer.h:38
int wrap_around< 8192 >(int a)
Definition buffer.h:44
int wrap_around< 4 >(int a)
Definition buffer.h:33
T sine_table< T, N, Multiplier >::data[N+1]
Definition primitives.h:442
int wrap_around(int a)
decrease by N if >= N (useful for circular buffers)
Definition buffer.h:27
void copy_buf(T &dest_buf, const U &src_buf, T scale=1, T add=0)
Definition buffer.h:152
int wrap_around< 2048 >(int a)
Definition buffer.h:42
int wrap_around< 8 >(int a)
Definition buffer.h:34
int wrap_around< 32768 >(int a)
Definition buffer.h:46
int wrap_around< 1024 >(int a)
Definition buffer.h:41
int wrap_around< 64 >(int a)
Definition buffer.h:37
int wrap_around< 256 >(int a)
Definition buffer.h:39
void fill(Buf &buf, T value)
Definition buffer.h:50
int wrap_around< 65536 >(int a)
Definition buffer.h:47
int wrap_around< 32 >(int a)
Definition buffer.h:36
int wrap_around< 4096 >(int a)
Definition buffer.h:43
int wrap_around< 2 >(int a)
Definition buffer.h:32
#define N
Definition nseel-cfunc.c:36
int inc_wrap(int pos) const
Definition buffer.h:168
int pos_diff(int pos1, int pos2) const
Definition buffer.h:172
Definition buffer.h:162
Definition buffer.h:70
@ channels
Definition buffer.h:72
@ bps
Definition buffer.h:73
Definition primitives.h:83
memcpy(hh, h, RAND_HEAD_LEN)
ulg size
Definition extract.c:2350