-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpx_buffer.h
293 lines (220 loc) · 6.69 KB
/
px_buffer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include "px_globals.h"
#include "px_vector.h"
#include "px_memory.h"
#ifndef PX_BUFFER_H
#define PX_BUFFER_H
/*
Somewhat type generic (double/float) buffer for working with px_. Includes internal array of type generic (void*) px_vector.
//include
#define PX_FLOAT_BUFFER
#include "px_buffer.h"
or:
#define PX_DOUBLE_BUFFER
#include "px_buffer.h"
//init
stack initialize:
px_buffer buffer;
px_buffer_initialize(&buffer, (int)num_samples, (int)num_channels);
heap:
px_buffer* buffer = px_buffer_create( (int)num_samples, (int)num_channels);
// initialize called within create()
free:
px_buffer_destroy(buffer);
// use
get_sample:
for (int i = 0; i < buffer.num_samples; ++i)
{
float leftValue = px_buffer_get_sample(&buffer, 0, i);
float rightValue = px_buffer_get_sample(&buffer, 1, i);
}
for (int channel = 0; i < buffer.num_channels; ++channel)
{
for (int sample = 0; i < buffer.num_samples; ++sample)
{
float value = px_buffer_get_sample(&buffer, channel, i);
}
}
get_pointer:
for (int i = 0; i < buffer.num_samples; ++i)
{
float* left_value = px_buffer_get_pointer(&buffer, 0, i);
float* right_value = px_buffer_get_pointer(&buffer, 1, i);
}
for (int channel = 0; i < buffer.num_channels; ++channel)
{
for (int sample = 0; i < buffer.num_samples; ++sample)
{
float* value = px_buffer_get_pointer(&buffer, channel, i);
}
}
*/
#ifndef MAX_CHANNELS
#define MAX_CHANNELS 4
#endif
#ifdef PX_FLOAT_BUFFER
#define BUFFER_TYPE float
#elif PX_DOUBLE_BUFFER
#define BUFFER_TYPE double
#endif
typedef struct
{
int num_samples;
int num_channels;
BUFFER_TYPE* data[MAX_CHANNELS];
bool is_filled;
} px_buffer;
// --------------------------------------------------------------------------------------------------------
static px_buffer* px_buffer_create(int num_samples, int num_channels);
static void px_buffer_destroy(px_buffer* buffer);
static void px_buffer_initialize(px_buffer* buffer, int num_samples, int num_channels);
static void px_buffer_clear(px_buffer* buffer);
static void px_buffer_set_sample(px_buffer* buffer, int channel, int sample_position, BUFFER_TYPE value);
static BUFFER_TYPE px_buffer_get_sample(px_buffer* buffer, int channel, int sample_position);
static BUFFER_TYPE* px_buffer_get_pointer(px_buffer* buffer, int channel, int sample_position);
static void px_buffer_gain(px_buffer* buffer, BUFFER_TYPE in_gain);
// --------------------------------------------------------------------------------------------------------
static px_buffer* px_buffer_create(int num_samples, int num_channels)
{
px_buffer* buffer = (px_buffer*)px_malloc(sizeof(px_buffer));
px_buffer_initialize(buffer, num_samples, num_channels);
return buffer;
}
static void px_buffer_clear(px_buffer* buffer)
{
if (buffer)
{
for (int channel = 0; channel < buffer->num_channels; ++channel)
{
if (buffer->data[channel])
{
px_free(buffer->data[channel]);
}
}
}
}
static void px_buffer_destroy(px_buffer* buffer)
{
if (buffer)
{
for (int channel =0; channel < buffer->num_channels; ++channel)
{
px_free(buffer->data[channel]);
}
px_free(buffer);
}
}
static void px_buffer_initialize(px_buffer* buffer, int num_samples, int num_channels)
{
assert(buffer);
buffer->num_samples = num_samples;
buffer->num_channels = num_channels;
buffer->is_filled = false;
for (int channel = 0; channel < num_channels; ++channel)
{
buffer->data[channel] = (BUFFER_TYPE*)px_malloc(num_samples * sizeof(BUFFER_TYPE));
assert(buffer->data[channel]);
}
}
static void px_buffer_set_sample(px_buffer* buffer, int channel, int sample_position, BUFFER_TYPE value)
{
assert(buffer);
if ( channel >= buffer->num_channels || sample_position >= buffer->num_samples)
{
printf("OUT OF BUFFER RANGE");
return;
}
buffer->data[channel][sample_position] = value;
}
static BUFFER_TYPE px_buffer_get_sample(px_buffer* buffer, int channel, int sample_position)
{
if ( channel > buffer->num_channels || sample_position > buffer->num_samples)
{
printf("OUT OF BUFFER RANGE");
return 0.f;
}
return buffer->data[channel][sample_position];
}
static BUFFER_TYPE* px_buffer_get_pointer(px_buffer* buffer, int channel, int sample_position)
{
/*
if ( channel > buffer->num_channels || sample_position > buffer->num_samples)
{
printf("OUT OF BUFFER RANGE");
return NULL;
}
BUFFER_TYPE* ptr = (BUFFER_TYPE*)buffer->vector[channel].data[sample_position];
if (ptr)
return ptr;
else
return NULL;
*/
}
static void px_buffer_gain(px_buffer* buffer, BUFFER_TYPE in_gain)
{
/*
assert(buffer);
for (int channel = 0; channel < buffer->num_channels; ++channel)
{
for (int i = 0; i < buffer->num_samples; ++i)
{
BUFFER_TYPE* ptr = (float*)buffer->vector[channel].data[i];
*ptr *= in_gain;
buffer->vector[channel].data[i] = ptr;
}
}*/
}
typedef struct {
float* data;
int head;
int tail;
int max_length;
} px_circular_buffer;
static void px_circular_push(px_circular_buffer* buffer, float value)
{
int next = (buffer->head + 1) % buffer->max_length;
if (next == buffer->tail)
buffer->tail = (buffer->tail + 1) % buffer->max_length;
buffer->data[buffer->head] = value;
buffer->head = next;
}
static float px_circular_pop(px_circular_buffer* buffer)
{
assert(buffer->head != buffer->tail); // Buffer is not empty
float value = buffer->data[buffer->tail];
int next = buffer->tail + 1;
if (next >= buffer->max_length)
next = 0;
buffer->tail = next;
return value;
}
static float px_circular_get_sample(px_circular_buffer* buffer, size_t index)
{
assert(index >= 0 && index < buffer->max_length);
return buffer->data[index % buffer->max_length];
}
static void px_circular_initialize(px_circular_buffer* buffer, int max_length)
{
assert(max_length > 0);
buffer->data = (float*)px_malloc(sizeof(float) * max_length);
memset(buffer->data, 0, sizeof(float) * max_length);
buffer->head = 0;
buffer->tail = 0;
buffer->max_length = max_length;
}
static void px_circular_resize(px_circular_buffer* buffer, int new_size)
{
float* new_data = (float*)px_malloc(sizeof(float) * new_size);
int i = 0;
int j = buffer->head;
while (i < buffer->max_length) {
new_data[i] = buffer->data[j];
j = (j+1) % buffer->max_length;
i++;
}
px_free(buffer->data);
buffer->data = new_data;
buffer->head = 0;
buffer->tail = buffer->max_length-1;
buffer->max_length = new_size;
}
#endif