| @@ -119,6 +119,8 @@ void tSamplePlayer_init (tSamplePlayer* const p, tSample* s) · 118 unmodified lines |
| 119 | | 119 | |
| 120 | p->idx = 0.f; | 120 | p->idx = 0.f; |
| 121 | p->inc = 1.f; | 121 | p->inc = 1.f; |
| | 122 | p->dir = 1; |
| | 123 | p->flip = 0; |
| 122 | | 124 | |
| 123 | p->mode = Normal; | 125 | p->mode = Normal; |
| 124 | | 126 | |
| @@ -133,52 +135,110 @@ void tSamplePlayer_free (tSamplePlayer* const p) · 8 unmodified lines |
| 133 | | 135 | |
| 134 | float tSamplePlayer_tick (tSamplePlayer* const p) | 136 | float tSamplePlayer_tick (tSamplePlayer* const p) |
| 135 | { | 137 | { |
| 136 | if (p->active == 0 || (p->len < 4)) return 0.f; | 138 | if ((p->mode == Normal) && (p->cnt > 0)) p->active = 0; |
| | 139 | |
| | 140 | if (p->active == 0 || (p->len < 4)) return 0.f; |
| 137 | | 141 | |
| 138 | float sample = 0.f; | 142 | float sample = 0.f; |
| 139 | | 143 | |
| 140 | float* buff = p->samp->buff; | 144 | float* buff = p->samp->buff; |
| 141 | | 145 | |
| 142 | int idx = (int) p->idx; | 146 | int idx = (int) p->idx; |
| 143 | float alpha = p->idx - idx; | | |
| 144 | | | |
| 145 | int i1 = idx-1; | | |
| 146 | int i3 = idx+1; | | |
| 147 | int i4 = idx+2; | | |
| 148 | | 147 | |
| 149 | if (i1 < p->start) i1 += p->len; | 148 | // Check dir (direction bit) to interpolate properly |
| 150 | if (i3 > p->end) i3 -= p->len; | 149 | if (p->dir > 0) |
| 151 | if (i4 > p->end) i4 -= p->len; | 150 | { |
| | 151 | // FORWARD |
| | 152 | float alpha = p->idx - idx; |
| | 153 | |
| | 154 | int i1 = idx-1; |
| | 155 | int i3 = idx+1; |
| | 156 | int i4 = idx+2; |
| | 157 | |
| | 158 | if (i1 < p->start) i1 += p->len; |
| | 159 | if (i3 > p->end) i3 -= p->len; |
| | 160 | if (i4 > p->end) i4 -= p->len; |
| | 161 | |
| | 162 | sample = LEAF_interpolate_hermite (buff[i1], |
| | 163 | buff[idx], |
| | 164 | buff[i3], |
| | 165 | buff[i4], |
| | 166 | alpha); |
| | 167 | } |
| | 168 | else |
| | 169 | { |
| | 170 | // REVERSE |
| | 171 | float alpha = 1.0f - (p->idx - idx); |
| | 172 | |
| | 173 | int i1 = idx+1; |
| | 174 | int i3 = idx-1; |
| | 175 | int i4 = idx-2; |
| | 176 | |
| | 177 | if (i1 > p->start) i1 -= p->len; |
| | 178 | if (i3 < p->end) i3 += p->len; |
| | 179 | if (i4 < p->end) i4 += p->len; |
| | 180 | |
| | 181 | sample = LEAF_interpolate_hermite (buff[i1], |
| | 182 | buff[idx], |
| | 183 | buff[i3], |
| | 184 | buff[i4], |
| | 185 | alpha); |
| | 186 | } |
| 152 | | 187 | |
| 153 | sample = LEAF_interpolate_hermite (buff[i1], | | |
| 154 | buff[idx], | | |
| 155 | buff[i3], | | |
| 156 | buff[i4], | | |
| 157 | alpha); | | |
| 158 | | 188 | |
| 159 | p->idx += (p->dir * p->inc); | 189 | p->idx += (p->dir * p->inc); |
| 160 | | 190 | |
| 161 | if (p->mode == Normal) | 191 | if (p->mode != BackAndForth) |
| 162 | { | 192 | { |
| 163 | // FIX THIS: NORMAL MODE NOT IMPLEMENTED YET | 193 | // Check flip bit to change loop test |
| 164 | if ((p->dir == 1) && (idx > p->end)) | 194 | if (p->flip > 0) |
| 165 | { | 195 | { |
| 166 | p->active = 0; | 196 | if (idx <= p->end) |
| | 197 | { |
| | 198 | p->idx += (float)(p->len); |
| | 199 | p->cnt++; |
| | 200 | } |
| | 201 | else if (idx >= p->start) |
| | 202 | { |
| | 203 | p->idx -= (float)(p->len); |
| | 204 | p->cnt++; |
| | 205 | } |
| | 206 | } |
| | 207 | else |
| | 208 | { |
| | 209 | if (idx <= p->start) |
| | 210 | { |
| | 211 | p->idx += (float)(p->len); |
| | 212 | p->cnt++; |
| | 213 | } |
| | 214 | else if (idx >= p->end) |
| | 215 | { |
| | 216 | p->idx -= (float)(p->len); |
| | 217 | p->cnt++; |
| | 218 | } |
| 167 | } | 219 | } |
| 168 | } | 220 | } |
| 169 | else if (p->mode == Loop) | 221 | else // BackAndForth |
| 170 | { | | |
| 171 | if (idx < p->start) p->idx += (float)(p->len); | | |
| 172 | | | |
| 173 | if (idx > p->end) p->idx -= (float)(p->len); | | |
| 174 | } | | |
| 175 | else if (p->mode == BackAndForth) | | |
| 176 | { | 222 | { |
| 177 | if ((idx < p->start) || (idx > p->end)) | 223 | if (p->flip > 0) |
| 178 | { | 224 | { |
| 179 | p->dir = -p->dir; | 225 | if ((idx < p->start) || (idx > p->end)) |
| 180 | p->idx += (2*p->inc); | 226 | { |
| | 227 | p->dir = -p->dir; |
| | 228 | p->idx += (2*p->inc); |
| | 229 | p->cnt++; |
| | 230 | } |
| 181 | } | 231 | } |
| | 232 | else |
| | 233 | { |
| | 234 | if ((idx > p->start) || (idx < p->end)) |
| | 235 | { |
| | 236 | p->dir = -p->dir; |
| | 237 | p->idx += (2*p->inc); |
| | 238 | p->cnt++; |
| | 239 | } |
| | 240 | } |
| | 241 | |
| 182 | } | 242 | } |
| 183 | | 243 | |
| 184 | | 244 | |
| @@ -198,6 +258,7 @@ void tSamplePlayer_setMode (tSamplePlayer* const p, Mode mode) · 13 unmodified lines |
| 198 | void tSamplePlayer_play (tSamplePlayer* const p) | 258 | void tSamplePlayer_play (tSamplePlayer* const p) |
| 199 | { | 259 | { |
| 200 | p->active = 1; | 260 | p->active = 1; |
| | 261 | p->cnt = 0; |
| 201 | } | 262 | } |
| 202 | | 263 | |
| 203 | void tSamplePlayer_stop (tSamplePlayer* const p) | 264 | void tSamplePlayer_stop (tSamplePlayer* const p) |
| @@ -205,14 +266,27 @@ void tSamplePlayer_stop (tSamplePlayer* const p) · 1 unmodified line |
| 205 | p->active = 0; | 266 | p->active = 0; |
| 206 | } | 267 | } |
| 207 | | 268 | |
| | 269 | static void handleStartEndChange(tSamplePlayer* const p) |
| | 270 | { |
| | 271 | if (p->start > p->end) |
| | 272 | { |
| | 273 | p->dir = -1; |
| | 274 | p->flip = 1; |
| | 275 | } |
| | 276 | else |
| | 277 | { |
| | 278 | p->dir = 1; |
| | 279 | p->flip = 0; |
| | 280 | } |
| | 281 | } |
| | 282 | |
| 208 | void tSamplePlayer_setStart (tSamplePlayer* const p, int32_t start) | 283 | void tSamplePlayer_setStart (tSamplePlayer* const p, int32_t start) |
| 209 | { | 284 | { |
| 210 | p->start = LEAF_clipInt(0, start, (p->samp->length - 1)); | 285 | p->start = LEAF_clipInt(0, start, (p->samp->length - 1)); |
| 211 | | 286 | |
| 212 | p->len = abs(p->end - p->start); | 287 | p->len = abs(p->end - p->start); |
| 213 | | 288 | |
| 214 | if (p->start > p->end) p->dir = -1; | 289 | handleStartEndChange(p); |
| 215 | else p->dir = 1; | | |
| 216 | } | 290 | } |
| 217 | | 291 | |
| 218 | void tSamplePlayer_setEnd (tSamplePlayer* const p, int32_t end) | 292 | void tSamplePlayer_setEnd (tSamplePlayer* const p, int32_t end) |
| @@ -221,8 +295,7 @@ void tSamplePlayer_setEnd (tSamplePlayer* const p, int32_t end) · 2 unmodified lines |
| 221 | | 295 | |
| 222 | p->len = abs(p->end - p->start); | 296 | p->len = abs(p->end - p->start); |
| 223 | | 297 | |
| 224 | if (p->start > p->end) p->dir = -1; | 298 | handleStartEndChange(p); |
| 225 | else p->dir = 1; | | |
| 226 | } | 299 | } |
| 227 | | 300 | |
| 228 | void tSamplePlayer_setRate (tSamplePlayer* const p, float rate) | 301 | void tSamplePlayer_setRate (tSamplePlayer* const p, float rate) |