| @@ -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 | s->buff = (float*) leaf_alloc( sizeof(float) * length); | 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 | s->idx = 0; | 33 | s->idx = 0; |
| 34 | s->mode = RecordOneShot; | 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 | leaf_free(s->buff); | 41 | leaf_free(s->buff); |
| 42 | leaf_free(s); | 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 | if (s->active == 1) | 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 | if (s->mode == RecordOneShot) | 55 | if (s->mode == RecordOneShot) |
| 56 | { | 56 | { |
| 57 | tSample_stop(s); | 57 | tBuffer_stop(s); |
| 58 | } | 58 | } |
| 59 | else if (s->mode == RecordLoop) | 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 | for (int i = 0; i < s->length; i++) | 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 | if ((idx < 0) || (idx >= s->length)) return 0.f; | 78 | if ((idx < 0) || (idx >= s->length)) return 0.f; |
| 79 | | 79 | |
| 80 | return s->buff[idx]; | 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 | s->active = 1; | 85 | s->active = 1; |
| 86 | s->idx = 0; | 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 | s->active = 0; | 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 | s->mode = mode; | 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 | for (int i = 0; i < s->length; i++) | 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 | //============================================================================== | 107 | //================================tSampler===================================== |
| 108 | | | |
| 109 | #define CFX 300 | | |
| 110 | | 108 | |
| 111 | void tSamplePlayer_init (tSamplePlayer* const p, tSample* s) | 109 | void tSampler_init (tSampler* const p, tBuffer* s) |
| 112 | { | 110 | { |
| 113 | p->samp = s; | 111 | p->samp = s; |
| 114 | | 112 | |
| @@ -121,30 +119,29 @@ void tSamplePlayer_init (tSamplePlayer* const p, tSample* s) · 6 unmodified lines |
| 121 | | 119 | |
| 122 | p->idx = 0.f; | 120 | p->idx = 0.f; |
| 123 | p->inc = 1.f; | 121 | p->inc = 1.f; |
| | 122 | p->iinc = 1.f; |
| 124 | p->dir = 1; | 123 | p->dir = 1; |
| 125 | p->flip = 1; | 124 | p->flip = 1; |
| 126 | | 125 | |
| 127 | p->mode = Normal; | 126 | p->mode = Normal; |
| 128 | | 127 | |
| 129 | p->g1 = 1.f; | 128 | p->cfxlen = 300; // default 300 sample crossfade |
| 130 | p->g2 = 0.f; | | |
| 131 | } | 129 | } |
| 132 | | 130 | |
| 133 | void tSamplePlayer_free (tSamplePlayer* const p) | 131 | void tSampler_free (tSampler* const p) |
| 134 | { | 132 | { |
| 135 | leaf_free(p->samp); | 133 | leaf_free(p->samp); |
| 136 | leaf_free(p); | 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 | if (p->active == 0 || (p->len < 4)) return 0.f; | 139 | if (p->active == 0 || (p->len < 4)) return 0.f; |
| 144 | | 140 | |
| 145 | float sample = 0.f; | 141 | float sample = 0.f; |
| 146 | float cfxsample = 0.f; | 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 | float* buff = p->samp->buff; | 146 | float* buff = p->samp->buff; |
| 150 | | 147 | |
| @@ -152,16 +149,16 @@ float tSamplePlayer_tick (tSamplePlayer* const p) · 1 unmodified line |
| 152 | | 149 | |
| 153 | int idx; | 150 | int idx; |
| 154 | float alpha; | 151 | float alpha; |
| 155 | | 152 | |
| 156 | if (dir > 0) | 153 | if (dir > 0) |
| 157 | { | 154 | { |
| 158 | idx = (int) p->idx; | 155 | idx = (int) p->idx; |
| 159 | alpha = p->idx - idx; | 156 | alpha = p->idx - idx; |
| 160 | } | 157 | } |
| 161 | else | 158 | else |
| 162 | { | 159 | { |
| 163 | idx = (int) (p->idx + 1.f); // we think this is because flooring on int works different when reading backwards | 160 | 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; | 161 | alpha = (p->idx+1.f) - idx; |
| 165 | } | 162 | } |
| 166 | | 163 | |
| 167 | int32_t start = p->start, end = p->end; | 164 | int32_t start = p->start, end = p->end; |
| @@ -178,10 +175,6 @@ float tSamplePlayer_tick (tSamplePlayer* const p) · 10 unmodified lines |
| 178 | int i1 = idx-1; | 175 | int i1 = idx-1; |
| 179 | int i3 = idx+1; | 176 | int i3 = idx+1; |
| 180 | int i4 = idx+2; | 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 | sample = LEAF_interpolate_hermite (buff[i1], | 179 | sample = LEAF_interpolate_hermite (buff[i1], |
| 187 | buff[idx], | 180 | buff[idx], |
| @@ -189,9 +182,11 @@ float tSamplePlayer_tick (tSamplePlayer* const p) · 1 unmodified line |
| 189 | buff[i4], | 182 | buff[i4], |
| 190 | alpha); | 183 | alpha); |
| 191 | | 184 | |
| 192 | cfx = (end - idx) / p->inc; // num samples to end of loop, use inverse increment | 185 | // num samples to end of loop |
| 193 | | 186 | numsamps = (dir > 0) ? (end - idx) : (idx - start); |
| 194 | if (cfx <= CFX) | 187 | numsamps *= p->iinc; |
| | 188 | |
| | 189 | if (numsamps <= p->cfxlen) |
| 195 | { | 190 | { |
| 196 | // CROSSFADE SAMPLE | 191 | // CROSSFADE SAMPLE |
| 197 | float idxx = p->idx - p->len; | 192 | float idxx = p->idx - p->len; |
| @@ -207,9 +202,9 @@ float tSamplePlayer_tick (tSamplePlayer* const p) · 9 unmodified lines |
| 207 | buff[i4], | 202 | buff[i4], |
| 208 | alpha); | 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 | else | 209 | else |
| 215 | { | 210 | { |
| @@ -217,20 +212,16 @@ float tSamplePlayer_tick (tSamplePlayer* const p) · 1 unmodified line |
| 217 | int i1 = idx+1; | 212 | int i1 = idx+1; |
| 218 | int i3 = idx-1; | 213 | int i3 = idx-1; |
| 219 | int i4 = idx-2; | 214 | int i4 = idx-2; |
| 220 | | 215 | |
| 221 | if (i1 > end) i1 -= p->len; | | |
| 222 | if (i3 < start) i3 += p->len; | | |
| 223 | if (i4 < start) i4 += p->len; | | |
| 224 | | | |
| 225 | sample = LEAF_interpolate_hermite (buff[i1], | 216 | sample = LEAF_interpolate_hermite (buff[i1], |
| 226 | buff[idx], | 217 | buff[idx], |
| 227 | buff[i3], | 218 | buff[i3], |
| 228 | buff[i4], | 219 | buff[i4], |
| 229 | 1.0f-alpha); | 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 | // CROSSFADE SAMPLE | 226 | // CROSSFADE SAMPLE |
| 236 | float idxx = p->idx + p->len + 1.f; | 227 | float idxx = p->idx + p->len + 1.f; |
| @@ -246,8 +237,10 @@ float tSamplePlayer_tick (tSamplePlayer* const p) · 9 unmodified lines |
| 246 | buff[i3], | 237 | buff[i3], |
| 247 | buff[i4], | 238 | buff[i4], |
| 248 | 1.f-alpha); | 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 | p->idx += (dir * p->inc); | 246 | p->idx += (dir * p->inc); |
| @@ -257,12 +250,10 @@ float tSamplePlayer_tick (tSamplePlayer* const p) · 3 unmodified lines |
| 257 | if (idx <= start) | 250 | if (idx <= start) |
| 258 | { | 251 | { |
| 259 | p->idx += (float)(p->len); | 252 | p->idx += (float)(p->len); |
| 260 | p->cnt++; | | |
| 261 | } | 253 | } |
| 262 | else if (idx >= end) | 254 | else if (idx >= end) |
| 263 | { | 255 | { |
| 264 | p->idx -= (float)(p->len); | 256 | p->idx -= (float)(p->len); |
| 265 | p->cnt++; | | |
| 266 | } | 257 | } |
| 267 | } | 258 | } |
| 268 | else // == BackAndForth | 259 | else // == BackAndForth |
| @@ -270,32 +261,37 @@ float tSamplePlayer_tick (tSamplePlayer* const p) · 1 unmodified line |
| 270 | if ((idx <= start) || (idx >= end)) | 261 | if ((idx <= start) || (idx >= end)) |
| 271 | { | 262 | { |
| 272 | p->dir = -p->dir; | 263 | p->dir = -p->dir; |
| 273 | p->cnt++; | | |
| 274 | } | 264 | } |
| 275 | } | 265 | } |
| 276 | | 266 | |
| 277 | p->g2 = (float) (CFX - cfx) / (float) CFX; | 267 | g1 = 1.f - g2; |
| 278 | p->g1 = 1.f - p->g2; | 268 | sample = sample * g1 + cfxsample * g2; |
| 279 | | | |
| 280 | sample = sample * p->g1 + cfxsample * p->g2; | | |
| 281 | | 269 | |
| 282 | return sample; | 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 | p->samp = s; | 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 | p->mode = mode; | 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 | p->active = 1; | 294 | p->active = 1; |
| 298 | p->cnt = 0; | | |
| 299 | | 295 | |
| 300 | if (p->dir > 0) | 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 | p->active = 0; | 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 | if (p->start > p->end) | 319 | if (p->start > p->end) |
| 320 | { | 320 | { |
| 321 | p->flip = -1; | 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 | p->start = LEAF_clipInt(0, start, (p->samp->length - 1)); | 331 | p->start = LEAF_clipInt(0, start, (p->samp->length - 1)); |
| 332 | | 332 | |
| 333 | p->len = abs(p->end - p->start); | | |
| 334 | | | |
| 335 | handleStartEndChange(p); | 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 | p->end = LEAF_clipInt(0, end, (p->samp->length - 1)); | 338 | p->end = LEAF_clipInt(0, end, (p->samp->length - 1)); |
| 341 | | | |
| 342 | p->len = abs(p->end - p->start); | | |
| 343 | | 339 | |
| 344 | handleStartEndChange(p); | 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 | if (rate < 0.f) | 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 | p->inc = LEAF_clip(0.f, rate, 8.0f); | 355 | p->inc = LEAF_clip(0.f, rate, 8.0f); |
| | 356 | p->iinc = 1.f / p->inc; |
| 360 | } | 357 | } |
| 361 | | 358 | |
| 362 | | 359 | |