| @@ -23,7 +23,7 @@ · 22 unmodified lines |
| 23 | 23 | |
| 24 | 24 | //============================================================================== |
| 25 | 25 | |
| 26 | | −void tSample_init (tSample* const s, uint32_t length) |
| 26 | +void tBuffer_init (tBuffer* const s, uint32_t length) |
| 27 | 27 | { |
| 28 | 28 | s->buff = (float*) leaf_alloc( sizeof(float) * length); |
| 29 | 29 | |
| @@ -33,16 +33,16 @@ void tSample_init (tSample* const s, uint32_t length) · 3 unmodified lines |
| 33 | 33 | s->idx = 0; |
| 34 | 34 | s->mode = RecordOneShot; |
| 35 | 35 | |
| 36 | | − tSample_clear(s); |
| 36 | + tBuffer_clear(s); |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | | −void tSample_free (tSample* const s) |
| 39 | +void tBuffer_free (tBuffer* const s) |
| 40 | 40 | { |
| 41 | 41 | leaf_free(s->buff); |
| 42 | 42 | leaf_free(s); |
| 43 | 43 | } |
| 44 | 44 | |
| 45 | | −void tSample_tick (tSample* const s, float sample) |
| 45 | +void tBuffer_tick (tBuffer* const s, float sample) |
| 46 | 46 | { |
| 47 | 47 | if (s->active == 1) |
| 48 | 48 | { |
| @@ -54,7 +54,7 @@ void tSample_tick (tSample* const s, float sample) · 5 unmodified lines |
| 54 | 54 | { |
| 55 | 55 | if (s->mode == RecordOneShot) |
| 56 | 56 | { |
| 57 | | − tSample_stop(s); |
| 57 | + tBuffer_stop(s); |
| 58 | 58 | } |
| 59 | 59 | else if (s->mode == RecordLoop) |
| 60 | 60 | { |
| @@ -64,7 +64,7 @@ void tSample_tick (tSample* const s, float sample) · 3 unmodified lines |
| 64 | 64 | } |
| 65 | 65 | } |
| 66 | 66 | |
| 67 | | −void tSample_read(tSample* const s, float* buff, uint32_t len) |
| 67 | +void tBuffer_read(tBuffer* const s, float* buff, uint32_t len) |
| 68 | 68 | { |
| 69 | 69 | for (int i = 0; i < s->length; i++) |
| 70 | 70 | { |
| @@ -73,30 +73,30 @@ void tSample_read(tSample* const s, float* buff, uint32_t len) · 2 unmodified lines |
| 73 | 73 | } |
| 74 | 74 | } |
| 75 | 75 | |
| 76 | | −float tSample_get (tSample* const s, int idx) |
| 76 | +float tBuffer_get (tBuffer* const s, int idx) |
| 77 | 77 | { |
| 78 | 78 | if ((idx < 0) || (idx >= s->length)) return 0.f; |
| 79 | 79 | |
| 80 | 80 | return s->buff[idx]; |
| 81 | 81 | } |
| 82 | 82 | |
| 83 | | −void tSample_start(tSample* const s) |
| 83 | +void tBuffer_record(tBuffer* const s) |
| 84 | 84 | { |
| 85 | 85 | s->active = 1; |
| 86 | 86 | s->idx = 0; |
| 87 | 87 | } |
| 88 | 88 | |
| 89 | | −void tSample_stop(tSample* const s) |
| 89 | +void tBuffer_stop(tBuffer* const s) |
| 90 | 90 | { |
| 91 | 91 | s->active = 0; |
| 92 | 92 | } |
| 93 | 93 | |
| 94 | | −void tSample_setRecordMode (tSample* const s, RecordMode mode) |
| 94 | +void tBuffer_setRecordMode (tBuffer* const s, RecordMode mode) |
| 95 | 95 | { |
| 96 | 96 | s->mode = mode; |
| 97 | 97 | } |
| 98 | 98 | |
| 99 | | −void tSample_clear (tSample* const s) |
| 99 | +void tBuffer_clear (tBuffer* const s) |
| 100 | 100 | { |
| 101 | 101 | for (int i = 0; i < s->length; i++) |
| 102 | 102 | { |
| @@ -104,11 +104,9 @@ void tSample_clear (tSample* const s) · 1 unmodified line |
| 104 | 104 | } |
| 105 | 105 | } |
| 106 | 106 | |
| 107 | | −//============================================================================== |
| 108 | | − |
| 109 | | −#define CFX 300 |
| 107 | +//================================tSampler===================================== |
| 110 | 108 | |
| 111 | | −void tSamplePlayer_init (tSamplePlayer* const p, tSample* s) |
| 109 | +void tSampler_init (tSampler* const p, tBuffer* s) |
| 112 | 110 | { |
| 113 | 111 | p->samp = s; |
| 114 | 112 | |
| @@ -121,30 +119,29 @@ void tSamplePlayer_init (tSamplePlayer* const p, tSample* s) · 6 unmodified lines |
| 121 | 119 | |
| 122 | 120 | p->idx = 0.f; |
| 123 | 121 | p->inc = 1.f; |
| 122 | + p->iinc = 1.f; |
| 124 | 123 | p->dir = 1; |
| 125 | 124 | p->flip = 1; |
| 126 | 125 | |
| 127 | 126 | p->mode = Normal; |
| 128 | | − |
| 129 | | − p->g1 = 1.f; |
| 130 | | − p->g2 = 0.f; |
| 127 | + |
| 128 | + p->cfxlen = 300; // default 300 sample crossfade |
| 131 | 129 | } |
| 132 | 130 | |
| 133 | | −void tSamplePlayer_free (tSamplePlayer* const p) |
| 131 | +void tSampler_free (tSampler* const p) |
| 134 | 132 | { |
| 135 | 133 | leaf_free(p->samp); |
| 136 | 134 | leaf_free(p); |
| 137 | 135 | } |
| 138 | 136 | |
| 139 | | −float tSamplePlayer_tick (tSamplePlayer* const p) |
| 137 | +float tSampler_tick (tSampler* const p) |
| 140 | 138 | { |
| 141 | | − if ((p->mode == Normal) && (p->cnt > 0)) p->active = 0; |
| 142 | | − |
| 143 | 139 | if (p->active == 0 || (p->len < 4)) return 0.f; |
| 144 | 140 | |
| 145 | 141 | float sample = 0.f; |
| 146 | 142 | float cfxsample = 0.f; |
| 147 | | − int cfx = CFX; // make this part of class |
| 143 | + int numsamps; |
| 144 | + float g1 = 1.f, g2 = 0.f; |
| 148 | 145 | |
| 149 | 146 | float* buff = p->samp->buff; |
| 150 | 147 | |
| @@ -152,16 +149,16 @@ float tSamplePlayer_tick (tSamplePlayer* const p) · 1 unmodified line |
| 152 | 149 | |
| 153 | 150 | int idx; |
| 154 | 151 | float alpha; |
| 155 | | − |
| 156 | | − if (dir > 0) |
| 157 | | − { |
| 152 | + |
| 153 | + if (dir > 0) |
| 154 | + { |
| 158 | 155 | idx = (int) p->idx; |
| 159 | 156 | alpha = p->idx - idx; |
| 160 | | − } |
| 161 | | − else |
| 162 | | − { |
| 163 | | − idx = (int) (p->idx + 1.f); // we think this is because flooring on int works different when reading backwards |
| 164 | | − alpha = (p->idx+1.f) - idx; |
| 157 | + } |
| 158 | + else |
| 159 | + { |
| 160 | + idx = (int) (p->idx + 1.f); // we think this is because flooring on int works different when reading backwards |
| 161 | + alpha = (p->idx+1.f) - idx; |
| 165 | 162 | } |
| 166 | 163 | |
| 167 | 164 | int32_t start = p->start, end = p->end; |
| @@ -178,10 +175,6 @@ float tSamplePlayer_tick (tSamplePlayer* const p) · 10 unmodified lines |
| 178 | 175 | int i1 = idx-1; |
| 179 | 176 | int i3 = idx+1; |
| 180 | 177 | int i4 = idx+2; |
| 181 | | − |
| 182 | | − if (i1 < start) i1 += p->len; |
| 183 | | − if (i3 > end) i3 -= p->len; |
| 184 | | − if (i4 > end) i4 -= p->len; |
| 185 | 178 | |
| 186 | 179 | sample = LEAF_interpolate_hermite (buff[i1], |
| 187 | 180 | buff[idx], |
| @@ -189,9 +182,11 @@ float tSamplePlayer_tick (tSamplePlayer* const p) · 1 unmodified line |
| 189 | 182 | buff[i4], |
| 190 | 183 | alpha); |
| 191 | 184 | |
| 192 | | − cfx = (end - idx) / p->inc; // num samples to end of loop, use inverse increment |
| 193 | | − |
| 194 | | − if (cfx <= CFX) |
| 185 | + // num samples to end of loop |
| 186 | + numsamps = (dir > 0) ? (end - idx) : (idx - start); |
| 187 | + numsamps *= p->iinc; |
| 188 | + |
| 189 | + if (numsamps <= p->cfxlen) |
| 195 | 190 | { |
| 196 | 191 | // CROSSFADE SAMPLE |
| 197 | 192 | float idxx = p->idx - p->len; |
| @@ -207,9 +202,9 @@ float tSamplePlayer_tick (tSamplePlayer* const p) · 9 unmodified lines |
| 207 | 202 | buff[i4], |
| 208 | 203 | alpha); |
| 209 | 204 | |
| 210 | | − //rprintf("cidx: %d\n", cdx); |
| 205 | + g2 = (float) (p->cfxlen - numsamps) / (float) p->cfxlen; |
| 211 | 206 | } |
| 212 | | − else cfx = CFX; |
| 207 | + else g2 = 0.f; |
| 213 | 208 | } |
| 214 | 209 | else |
| 215 | 210 | { |
| @@ -217,20 +212,16 @@ float tSamplePlayer_tick (tSamplePlayer* const p) · 1 unmodified line |
| 217 | 212 | int i1 = idx+1; |
| 218 | 213 | int i3 = idx-1; |
| 219 | 214 | int i4 = idx-2; |
| 220 | | − |
| 221 | | − if (i1 > end) i1 -= p->len; |
| 222 | | − if (i3 < start) i3 += p->len; |
| 223 | | − if (i4 < start) i4 += p->len; |
| 224 | | − |
| 215 | + |
| 225 | 216 | sample = LEAF_interpolate_hermite (buff[i1], |
| 226 | 217 | buff[idx], |
| 227 | 218 | buff[i3], |
| 228 | 219 | buff[i4], |
| 229 | 220 | 1.0f-alpha); |
| 230 | 221 | |
| 231 | | − cfx = (idx - start) / p->inc; |
| 222 | + numsamps = (idx - start) / p->inc; |
| 232 | 223 | |
| 233 | | − if (cfx <= CFX) |
| 224 | + if (numsamps <= p->cfxlen) |
| 234 | 225 | { |
| 235 | 226 | // CROSSFADE SAMPLE |
| 236 | 227 | float idxx = p->idx + p->len + 1.f; |
| @@ -246,8 +237,10 @@ float tSamplePlayer_tick (tSamplePlayer* const p) · 9 unmodified lines |
| 246 | 237 | buff[i3], |
| 247 | 238 | buff[i4], |
| 248 | 239 | 1.f-alpha); |
| 240 | + |
| 241 | + g2 = (float) (p->cfxlen - numsamps) / (float) p->cfxlen; |
| 249 | 242 | } |
| 250 | | − else cfx = CFX; |
| 243 | + else g2 = 0.f; |
| 251 | 244 | } |
| 252 | 245 | |
| 253 | 246 | p->idx += (dir * p->inc); |
| @@ -257,12 +250,10 @@ float tSamplePlayer_tick (tSamplePlayer* const p) · 3 unmodified lines |
| 257 | 250 | if (idx <= start) |
| 258 | 251 | { |
| 259 | 252 | p->idx += (float)(p->len); |
| 260 | | − p->cnt++; |
| 261 | 253 | } |
| 262 | 254 | else if (idx >= end) |
| 263 | 255 | { |
| 264 | 256 | p->idx -= (float)(p->len); |
| 265 | | − p->cnt++; |
| 266 | 257 | } |
| 267 | 258 | } |
| 268 | 259 | else // == BackAndForth |
| @@ -270,32 +261,37 @@ float tSamplePlayer_tick (tSamplePlayer* const p) · 1 unmodified line |
| 270 | 261 | if ((idx <= start) || (idx >= end)) |
| 271 | 262 | { |
| 272 | 263 | p->dir = -p->dir; |
| 273 | | − p->cnt++; |
| 274 | 264 | } |
| 275 | 265 | } |
| 276 | 266 | |
| 277 | | − p->g2 = (float) (CFX - cfx) / (float) CFX; |
| 278 | | − p->g1 = 1.f - p->g2; |
| 279 | | − |
| 280 | | − sample = sample * p->g1 + cfxsample * p->g2; |
| 267 | + g1 = 1.f - g2; |
| 268 | + sample = sample * g1 + cfxsample * g2; |
| 281 | 269 | |
| 282 | 270 | return sample; |
| 283 | 271 | } |
| 284 | 272 | |
| 285 | | −void tSamplePlayer_setSample (tSamplePlayer* const p, tSample* s) |
| 273 | +void tSampler_setSample (tSampler* const p, tBuffer* s) |
| 286 | 274 | { |
| 287 | 275 | p->samp = s; |
| 288 | 276 | } |
| 289 | 277 | |
| 290 | | −void tSamplePlayer_setMode (tSamplePlayer* const p, Mode mode) |
| 278 | +void tSampler_setMode (tSampler* const p, Mode mode) |
| 291 | 279 | { |
| 292 | 280 | p->mode = mode; |
| 293 | 281 | } |
| 294 | 282 | |
| 295 | | −void tSamplePlayer_play (tSamplePlayer* const p) |
| 283 | +void tSampler_setCrossfadeLength (tSampler* const p, uint32_t length) |
| 284 | +{ |
| 285 | + uint32_t cfxlen = LEAF_clip(0, length, 1000); |
| 286 | + |
| 287 | + //if (cfxlen > p->len) cfxlen = p->len * 0.25f; |
| 288 | + |
| 289 | + p->cfxlen = cfxlen; |
| 290 | +} |
| 291 | + |
| 292 | +void tSampler_play (tSampler* const p) |
| 296 | 293 | { |
| 297 | 294 | p->active = 1; |
| 298 | | − p->cnt = 0; |
| 299 | 295 | |
| 300 | 296 | if (p->dir > 0) |
| 301 | 297 | { |
| @@ -309,13 +305,17 @@ void tSamplePlayer_play (tSamplePlayer* const p) · 7 unmodified lines |
| 309 | 305 | } |
| 310 | 306 | } |
| 311 | 307 | |
| 312 | | −void tSamplePlayer_stop (tSamplePlayer* const p) |
| 308 | +void tSampler_stop (tSampler* const p) |
| 313 | 309 | { |
| 314 | 310 | p->active = 0; |
| 315 | 311 | } |
| 316 | 312 | |
| 317 | | −static void handleStartEndChange(tSamplePlayer* const p) |
| 313 | +static void handleStartEndChange(tSampler* const p) |
| 318 | 314 | { |
| 315 | + p->len = abs(p->end - p->start); |
| 316 | + |
| 317 | + //if (p->len < p->cfxlen) p->cfxlen = p->len * 0.9f; |
| 318 | + |
| 319 | 319 | if (p->start > p->end) |
| 320 | 320 | { |
| 321 | 321 | p->flip = -1; |
| @@ -326,25 +326,21 @@ static void handleStartEndChange(tSamplePlayer* const p) · 4 unmodified lines |
| 326 | 326 | } |
| 327 | 327 | } |
| 328 | 328 | |
| 329 | | −void tSamplePlayer_setStart (tSamplePlayer* const p, int32_t start) |
| 329 | +void tSampler_setStart (tSampler* const p, int32_t start) |
| 330 | 330 | { |
| 331 | 331 | p->start = LEAF_clipInt(0, start, (p->samp->length - 1)); |
| 332 | 332 | |
| 333 | | − p->len = abs(p->end - p->start); |
| 334 | | − |
| 335 | 333 | handleStartEndChange(p); |
| 336 | 334 | } |
| 337 | 335 | |
| 338 | | −void tSamplePlayer_setEnd (tSamplePlayer* const p, int32_t end) |
| 336 | +void tSampler_setEnd (tSampler* const p, int32_t end) |
| 339 | 337 | { |
| 340 | 338 | p->end = LEAF_clipInt(0, end, (p->samp->length - 1)); |
| 341 | | − |
| 342 | | − p->len = abs(p->end - p->start); |
| 343 | 339 | |
| 344 | 340 | handleStartEndChange(p); |
| 345 | 341 | } |
| 346 | 342 | |
| 347 | | −void tSamplePlayer_setRate (tSamplePlayer* const p, float rate) |
| 343 | +void tSampler_setRate (tSampler* const p, float rate) |
| 348 | 344 | { |
| 349 | 345 | if (rate < 0.f) |
| 350 | 346 | { |
| @@ -357,6 +353,7 @@ void tSamplePlayer_setRate (tSamplePlayer* const p, float rate) · 6 unmodified lines |
| 357 | 353 | } |
| 358 | 354 | |
| 359 | 355 | p->inc = LEAF_clip(0.f, rate, 8.0f); |
| 356 | + p->iinc = 1.f / p->inc; |
| 360 | 357 | } |
| 361 | 358 | |
| 362 | 359 | |