-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathbuffer16.h
232 lines (176 loc) · 6.6 KB
/
buffer16.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
/* buffer16.h
audio lib
aleph
classes for manipulating audio buffer16s.
*/
#ifndef _ALEPH_BUFFER16_H_
#define _ALEPH_BUFFER16_H_
#include "fix.h"
#include "fix32.h"
#include "types.h"
//--- audioBuffer16
// data+descriptor class.
typedef struct _audioBuffer16 {
// count of frames
u32 frames;
// pointer to data
volatile fract16 *data;
} audioBuffer16;
// ---- buffer16Tap
// class for creating a "tap" or "head."
// stores position within a buffer16.
// supports interpolated read/write.
typedef struct _buffer16Tap {
// pointer to buf
audioBuffer16* buf;
// current index
fix32 idx;
} buffer16Tap;
// initialize a (mono) audio buffer16 at pre-allocated memory.
// provide 2nd pointer for data,
// so it can be placed in an arbitrarily separate memory region.
extern void buffer16_init(audioBuffer16* buf, fract16* data, u32 frames);
// intialize tap
extern void buffer16_tap_init(buffer16Tap* tap, audioBuffer16* buf);
// increment the index in a tap
extern void buffer16_tap_next(buffer16Tap* tap);
// interpolated read
extern fract16 buffer16_tap_read(buffer16Tap* tap);
// interpolated write (erases old contents)
extern void buffer16_tap_write(buffer16Tap* tap, fract16 val);
// interpolated mix (old + new, arbitrary)
extern void buffer16_tap_mix(buffer16Tap* tap, fract16 val, fract16 preLevel);
// interpolated add (new + old (unchanged)
extern void buffer16_tap_add(buffer16Tap* tap, fract16 val);
// set rate
extern void buffer16_tap_set_rate(buffer16Tap *tap, fix16 rate);
// set a different buffer16 (resets position)
extern void buffer16_tap_set_buf(buffer16Tap* tap, audioBuffer16* buf);
// set loop endpoint in seconds
extern void buffer16_tap_set_loop(buffer16Tap* tap, u32 loop);
// synchronize one tap with another at a given offset in seconds.
// useful for delays
extern void buffer16_tap_sync(buffer16Tap* tap, buffer16Tap* target, fix32 samples);
// set tap position directly
extern void buffer16_tap_set_pos(buffer16Tap* tap, fix32 samples);
//--------------------------------------------------------
//---------------------------------------------------------
//---- non-interpolated taps
// ---- buffer16TapN
// class for creating a "tap" or "head."
// stores position/rate within a buffer16.
// non-interpolated.
typedef struct _buffer16TapN {
// pointer to buf
audioBuffer16* 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;
} buffer16TapN;
//---- buffer16XfadeN
// class for cross-fading between two non-interpolated read taps in a buffer16
typedef struct _buffer16XfadeN {
// two read tapstap
buffer16TapN 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];
} buffer16XfadeN;
//------------------------------------
//----- external functions
// intialize
extern void buffer16_tapN_init(buffer16TapN* tap, audioBuffer16* buf);
// increment the index in a tap
extern void buffer16_tapN_next(buffer16TapN* tap);
// non-interpolated read
extern fract16 buffer16_tapN_read(buffer16TapN* tap);
// non-interpolated write (erases old contents)
extern void buffer16_tapN_write(buffer16TapN* tap, fract16 val);
// non-interpolated mix (old + new, arbitrary)
extern void buffer16_tapN_mix(buffer16TapN* tap, fract16 val, fract16 preLevel);
// non-interpolated add (new + old (unchanged)
extern void buffer16_tapN_add(buffer16TapN* tap, fract16 val);
// set rate
//void buffer16_tapN_set_rate(buffer16TapN *tap, fix16 rate);
// set per-sample increment
extern void buffer16_tapN_set_inc(buffer16TapN *tap, u32 inc);
// set rate divisor
extern void buffer16_tapN_set_div(buffer16TapN *tap, u32 div);
// set a different buffer16 (resets position)
extern void buffer16_tapN_set_buf(buffer16TapN* tap, audioBuffer16* buf);
// set loop endpoint in samples
extern void buffer16_tapN_set_loop(buffer16TapN* tap, u32 samps);
// synchronize one tap with another at a given offset in seconds.
// useful for delays
extern void buffer16_tapN_sync(buffer16TapN* tap, buffer16TapN* target, u32 samps);
// set tap position directly
extern void buffer16_tapN_set_pos(buffer16TapN* tap, u32 samp);
// copy all params
extern void buffer16_tapN_copy( buffer16TapN* src, buffer16TapN* dst );
// ---- buffer16Tap24_8
// class for creating a "tap" or "head."
// stores position/rate within a buffer16.
// supports interpolated read/write.
typedef struct _buffer16Tap24_8 {
// pointer to buf
audioBuffer16* buf;
// last index
s32 idx_last;
s16 samp_last;
// current index
s32 idx;
// phase increment
s32 inc;
// loop position
s32 loop;
} buffer16Tap24_8;
// intialize tap
extern void buffer16Tap24_8_init(buffer16Tap24_8* tap, audioBuffer16* buf);
// increment the index in a tap
extern void buffer16Tap24_8_next(buffer16Tap24_8* tap);
// set speed
extern void buffer16Tap24_8_set_inc(buffer16Tap24_8 *tap, u32 inc);
// interpolating write
extern void buffer16Tap24_8_write(buffer16Tap24_8* tap, fract16 val);
// interpolating write mixing old signal
void buffer16Tap24_8_mix(buffer16Tap24_8* tap, fract16 samp, fract16 preLevel);
// interpoilating write adding old signal
void buffer16Tap24_8_add(buffer16Tap24_8* tap, fract16 samp);
// interpolated read
extern fract16 buffer16Tap24_8_read(buffer16Tap24_8* tap);
// interpolated read from arbitrary position
extern fract16 buffer16Tap24_8_read_from(buffer16Tap24_8* tap, s32 idx);
// set rate
extern void buffer16Tap24_8_set_rate(buffer16Tap24_8 *tap, s32 inc);
// set a different buffer16 (resets position)
//extern void buffer1624_8_tap_set_buf(Buffer16Tap24_8* tap, audioBuffer16* buf);
// set loop point in subsamples
extern void buffer16Tap24_8_set_loop(buffer16Tap24_8* tap, s32 loop);
// synchronize 24.8 interp tap with an non-interpolated tap at a given offset in subsamples.
extern void buffer16Tap24_8_syncN(buffer16Tap24_8* tap, buffer16TapN* target, s32 offset);
extern void buffer16Tap24_8_sync(buffer16Tap24_8* tap, buffer16Tap24_8* target, s32 offset_subsamples);
// set 24.8 interp tap position directly in subsamples
extern void buffer16Tap24_8_set_pos(buffer16Tap24_8* tap, s32 idx);
//---------------------------
//---- crossfade
// initialize crossfader
extern void buffer16_xfadeN_init(buffer16XfadeN* fade, audioBuffer16* buf);
// set new position, initiating fade
extern void buffer16_xfadeN_set_pos(buffer16XfadeN* fade, u32 samps);
// get next value
extern void buffer16_xfadeN_next(buffer16XfadeN* fade);
extern void buffer16Tap24_8_copy( buffer16Tap24_8* src, buffer16Tap24_8* dst );
#endif // h guard