-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathbuffer.h
236 lines (176 loc) · 6.34 KB
/
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
/* buffer.h
audio lib
aleph
classes for manipulating audio buffers.
*/
#ifndef _ALEPH_BUFFER_H_
#define _ALEPH_BUFFER_H_
#include "fix.h"
#include "fix32.h"
#include "types.h"
//--- audioBuffer
// data+descriptor class.
typedef struct _audioBuffer {
// count of frames
u32 frames;
// pointer to data
volatile fract32 *data;
} audioBuffer;
// ---- bufferTap
// class for creating a "tap" or "head."
// stores position/rate within a buffer.
// supports interpolated read/write.
typedef struct _bufferTap {
// pointer to buf
audioBuffer* buf;
// current index
fix32 idx;
// phase increment
// disabled for now until we have single speed taps working
//fix32 inc;
} bufferTap;
// initialize a (mono) audio buffer at pre-allocated memory.
// provide 2nd pointer for data,
// so it can be placed in an arbitrarily separate memory region.
extern void buffer_init(audioBuffer* buf, volatile fract32* data, u32 frames);
// intialize tap
extern void buffer_tap_init(bufferTap* tap, audioBuffer* buf);
// increment the index in a tap
extern void buffer_tap_next(bufferTap* tap);
// interpolated read
extern fract32 buffer_tap_read(bufferTap* tap);
// interpolated write (erases old contents)
extern void buffer_tap_write(bufferTap* tap, fract32 val);
// interpolated mix (old + new, arbitrary)
extern void buffer_tap_mix(bufferTap* tap, fract32 val, fract32 preLevel);
// interpolated add (new + old (unchanged)
extern void buffer_tap_add(bufferTap* tap, fract32 val);
// set rate
extern void buffer_tap_set_rate(bufferTap *tap, fix16 rate);
// set a different buffer (resets position)
extern void buffer_tap_set_buf(bufferTap* tap, audioBuffer* buf);
// set loop endpoint in seconds
extern void buffer_tap_set_loop(bufferTap* tap, u32 loop);
// synchronize one tap with another at a given offset in seconds.
// useful for delays
extern void buffer_tap_sync(bufferTap* tap, bufferTap* target, fix32 samples);
// set tap position directly
extern void buffer_tap_set_pos(bufferTap* tap, fix32 samples);
//--------------------------------------------------------
//---------------------------------------------------------
//---- non-interpolated taps
// ---- bufferTapN
// class for creating a "tap" or "head."
// stores position/rate within a buffer.
// non-interpolated.
typedef struct _bufferTapN {
// pointer to buf
audioBuffer* buf;
// index to loop at (upper bound)
u32 loop;
// current index
u32 idx;
// phase increment
u32 inc;
// rate divisor
u32 div;
// current divisor count
u32 divCount;
} bufferTapN;
//---- bufferXfadeN
// class for cross-fading between two non-interpolated read taps in a buffer
typedef struct _bufferXfadeN {
// two read tapstap
bufferTapN read[2];
// fade status
/// 0 = tap 0 only
/// 1 = tap 1 only
/// 2 = both taps
u8 satus;
// fade levels
u16 fadeLevel[2];
// fade positions
u16 fadePos[2];
} bufferXfadeN;
//------------------------------------
//----- external functions
// intialize
extern void buffer_tapN_init(bufferTapN* tap, audioBuffer* buf);
// increment the index in a tap
extern void buffer_tapN_next(bufferTapN* tap);
// non-interpolated read
extern fract32 buffer_tapN_read(bufferTapN* tap);
// non-interpolated write (erases old contents)
extern void buffer_tapN_write(bufferTapN* tap, fract32 val);
// non-interpolated mix (old + new, arbitrary)
extern void buffer_tapN_mix(bufferTapN* tap, fract32 val, fract32 preLevel);
// non-interpolated add (new + old (unchanged)
extern void buffer_tapN_add(bufferTapN* tap, fract32 val);
// set rate
//void buffer_tapN_set_rate(bufferTapN *tap, fix16 rate);
// set per-sample increment
extern void buffer_tapN_set_inc(bufferTapN *tap, u32 inc);
// set rate divisor
extern void buffer_tapN_set_div(bufferTapN *tap, u32 div);
// set a different buffer (resets position)
extern void buffer_tapN_set_buf(bufferTapN* tap, audioBuffer* buf);
// set loop endpoint in samples
extern void buffer_tapN_set_loop(bufferTapN* tap, u32 samps);
// synchronize one tap with another at a given offset in seconds.
// useful for delays
extern void buffer_tapN_sync(bufferTapN* tap, bufferTapN* target, u32 samps);
// set tap position directly
extern void buffer_tapN_set_pos(bufferTapN* tap, u32 samp);
// copy all params
extern void buffer_tapN_copy( bufferTapN* src, bufferTapN* dst );
// ---- bufferTap24_8
// class for creating a "tap" or "head."
// stores position/rate within a buffer.
// supports interpolated read/write.
typedef struct _bufferTap24_8 {
// pointer to buf
audioBuffer* buf;
// last index
s32 idx_last;
// current index
s32 idx;
// phase increment
s32 inc;
// loop position
s32 loop;
} bufferTap24_8;
// intialize tap
extern void bufferTap24_8_init(bufferTap24_8* tap, audioBuffer* buf);
// increment the index in a tap
extern void bufferTap24_8_next(bufferTap24_8* tap);
// interpolated read
extern fract32 bufferTap24_8_read(bufferTap24_8* tap);
// interpolated read from arbitrary position
extern fract32 bufferTap24_8_read_from(bufferTap24_8* tap, s32 idx);
// antialiased interpolated read BROKEN
//extern fract32 bufferTap24_8_read_antialias(bufferTap24_8* tap);
// interpolated write (erases old contents)
//extern void buffer24_8_tap_write(BufferTap24_8* tap, fract32 val);
// interpolated mix (old + new, arbitrary)
//extern void buffer24_8_tap_mix(BufferTap24_8* tap, fract32 val, fract32 preLevel);
// interpolated add (new + old (unchanged)
//extern void buffer24_8_tap_add(BufferTap24_8* tap, fract32 val);
// set rate
extern void bufferTap24_8_set_rate(bufferTap24_8 *tap, s32 inc);
// set a different buffer (resets position)
//extern void buffer24_8_tap_set_buf(BufferTap24_8* tap, audioBuffer* buf);
// set loop point in subsamples
extern void bufferTap24_8_set_loop(bufferTap24_8* tap, s32 loop);
// synchronize 24.8 interp tap with an non-interpolated tap at a given offset in subsamples.
extern void bufferTap24_8_syncN(bufferTap24_8* tap, bufferTapN* target, s32 offset);
// set 24.8 interp tap position directly in subsamples
extern void bufferTap24_8_set_pos(bufferTap24_8* tap, s32 idx);
//---------------------------
//---- crossfade
// initialize crossfader
extern void buffer_xfadeN_init(bufferXfadeN* fade, audioBuffer* buf);
// set new position, initiating fade
extern void buffer_xfadeN_set_pos(bufferXfadeN* fade, u32 samps);
// get next value
extern void buffer_xfadeN_next(bufferXfadeN* fade);
#endif // h guard