| @@ -6,13 +6,11 @@ · 5 unmodified lines |
| 6 | 6 | |
| 7 | 7 | #if _WIN32 || _WIN64 |
| 8 | 8 | |
| 9 | | −#include "..\Inc\leaf-tables.h" |
| 10 | 9 | #include "..\Inc\leaf-oscillators.h" |
| 11 | 10 | #include "..\leaf.h" |
| 12 | 11 | |
| 13 | 12 | #else |
| 14 | 13 | |
| 15 | | −#include "../Inc/leaf-tables.h" |
| 16 | 14 | #include "../Inc/leaf-oscillators.h" |
| 17 | 15 | #include "../leaf.h" |
| 18 | 16 | |
| @@ -51,7 +49,10 @@ void tCycle_setFreq(tCycle* const cy, float freq) · 32 unmodified lines |
| 51 | 49 | { |
| 52 | 50 | _tCycle* c = *cy; |
| 53 | 51 | |
| 54 | | − c->freq = freq; |
| 52 | + if (freq < 0.0f) c->freq = 0.0f; |
| 53 | + else if (freq > 20480.0f) c->freq = 20480.0f; |
| 54 | + else c->freq = freq; |
| 55 | + |
| 55 | 56 | c->inc = freq * leaf.invSampleRate; |
| 56 | 57 | } |
| 57 | 58 | |
| @@ -109,6 +110,7 @@ void tTriangle_initToPool (tTriangle* const cy, tMempool* const mp) · 51 unmodified lines |
| 109 | 110 | |
| 110 | 111 | c->inc = 0.0f; |
| 111 | 112 | c->phase = 0.0f; |
| 113 | + tTriangle_setFreq(cy, 220); |
| 112 | 114 | } |
| 113 | 115 | |
| 114 | 116 | void tTriangle_freeFromPool (tTriangle* const cy, tMempool* const mp) |
| @@ -119,16 +121,22 @@ void tTriangle_freeFromPool (tTriangle* const cy, tMempool* const mp) · 4 unmodified lines |
| 119 | 121 | mpool_free(c, m); |
| 120 | 122 | } |
| 121 | 123 | |
| 122 | | −int tTriangle_setFreq(tTriangle* const cy, float freq) |
| 124 | +void tTriangle_setFreq(tTriangle* const cy, float freq) |
| 123 | 125 | { |
| 124 | 126 | _tTriangle* c = *cy; |
| 125 | 127 | |
| 126 | | − if (freq < 0.0f) freq = 0.0f; |
| 128 | + if (freq < 0.0f) c->freq = 0.0f; |
| 129 | + else if (freq > 20480.0f) c->freq = 20480.0f; |
| 130 | + else c->freq = freq; |
| 127 | 131 | |
| 128 | | − c->freq = freq; |
| 129 | | − c->inc = freq * leaf.invSampleRate; |
| 132 | + c->inc = c->freq * leaf.invSampleRate; |
| 130 | 133 | |
| 131 | | − return 0; |
| 134 | + c->w = c->freq * INV_20; |
| 135 | + for (c->oct = 0; c->w > 2.0f; c->oct++) |
| 136 | + { |
| 137 | + c->w = 0.5f * c->w; |
| 138 | + } |
| 139 | + c->w = 2.0f - c->w; |
| 132 | 140 | } |
| 133 | 141 | |
| 134 | 142 | |
| @@ -141,70 +149,12 @@ float tTriangle_tick(tTriangle* const cy) · 6 unmodified lines |
| 141 | 149 | while (c->phase >= 1.0f) c->phase -= 1.0f; |
| 142 | 150 | |
| 143 | 151 | float out = 0.0f; |
| 144 | | − float w; |
| 145 | 152 | |
| 146 | 153 | int idx = (int)(c->phase * TRI_TABLE_SIZE); |
| 147 | 154 | |
| 148 | 155 | // Wavetable synthesis |
| 149 | | − |
| 150 | | − if (c->freq <= 20.0f) |
| 151 | | − { |
| 152 | | − out = __leaf_table_triangle[T20][idx]; |
| 153 | | − } |
| 154 | | − else if (c->freq <= 40.0f) |
| 155 | | − { |
| 156 | | − w = ((40.0f - c->freq) * INV_20); |
| 157 | | − out = (__leaf_table_triangle[T20][idx] * w) + (__leaf_table_triangle[T40][idx] * (1.0f - w)); |
| 158 | | − } |
| 159 | | − else if (c->freq <= 80.0f) |
| 160 | | − { |
| 161 | | − w = ((80.0f - c->freq) * INV_40); |
| 162 | | − out = (__leaf_table_triangle[T40][idx] * w) + (__leaf_table_triangle[T80][idx] * (1.0f - w)); |
| 163 | | − } |
| 164 | | − else if (c->freq <= 160.0f) |
| 165 | | − { |
| 166 | | − w = ((160.0f - c->freq) * INV_80); |
| 167 | | − out = (__leaf_table_triangle[T80][idx] * w) + (__leaf_table_triangle[T160][idx] * (1.0f - w)); |
| 168 | | − } |
| 169 | | − else if (c->freq <= 320.0f) |
| 170 | | − { |
| 171 | | − w = ((320.0f - c->freq) * INV_160); |
| 172 | | − out = (__leaf_table_triangle[T160][idx] * w) + (__leaf_table_triangle[T320][idx] * (1.0f - w)); |
| 173 | | − } |
| 174 | | − else if (c->freq <= 640.0f) |
| 175 | | − { |
| 176 | | − w = ((640.0f - c->freq) * INV_320); |
| 177 | | − out = (__leaf_table_triangle[T320][idx] * w) + (__leaf_table_triangle[T640][idx] * (1.0f - w)); |
| 178 | | − } |
| 179 | | − else if (c->freq <= 1280.0f) |
| 180 | | − { |
| 181 | | − w = ((1280.0f - c->freq) * INV_640); |
| 182 | | − out = (__leaf_table_triangle[T640][idx] * w) + (__leaf_table_triangle[T1280][idx] * (1.0f - w)); |
| 183 | | − } |
| 184 | | − else if (c->freq <= 2560.0f) |
| 185 | | − { |
| 186 | | − w = ((2560.0f - c->freq) * INV_1280); |
| 187 | | − out = (__leaf_table_triangle[T1280][idx] * w) + (__leaf_table_triangle[T2560][idx] * (1.0f - w)); |
| 188 | | − } |
| 189 | | − else if (c->freq <= 5120.0f) |
| 190 | | − { |
| 191 | | − w = ((5120.0f - c->freq) * INV_2560); |
| 192 | | − out = (__leaf_table_triangle[T2560][idx] * w) + (__leaf_table_triangle[T5120][idx] * (1.0f - w)); |
| 193 | | − } |
| 194 | | − else if (c->freq <= 10240.0f) |
| 195 | | − { |
| 196 | | − w = ((10240.0f - c->freq) * INV_5120); |
| 197 | | − out = (__leaf_table_triangle[T5120][idx] * w) + (__leaf_table_triangle[T10240][idx] * (1.0f - w)); |
| 198 | | − } |
| 199 | | − else if (c->freq <= 20480.0f) |
| 200 | | − { |
| 201 | | − w = ((20480.0f - c->freq) * INV_10240); |
| 202 | | − out = (__leaf_table_triangle[T10240][idx] * w) + (__leaf_table_triangle[T20480][idx] * (1.0f - w)); |
| 203 | | − } |
| 204 | | − else |
| 205 | | − { |
| 206 | | − out = __leaf_table_triangle[T20480][idx]; |
| 207 | | − } |
| 156 | + out = __leaf_table_triangle[c->oct+1][idx] + |
| 157 | + (__leaf_table_triangle[c->oct][idx] - __leaf_table_triangle[c->oct+1][idx]) * c->w; |
| 208 | 158 | |
| 209 | 159 | return out; |
| 210 | 160 | } |
| @@ -235,6 +185,7 @@ void tSquare_initToPool (tSquare* const cy, tMempool* const mp) · 24 unmodified lines |
| 235 | 185 | |
| 236 | 186 | c->inc = 0.0f; |
| 237 | 187 | c->phase = 0.0f; |
| 188 | + tSquare_setFreq(cy, 220); |
| 238 | 189 | } |
| 239 | 190 | |
| 240 | 191 | void tSquare_freeFromPool(tSquare* const cy, tMempool* const mp) |
| @@ -245,16 +196,22 @@ void tSquare_freeFromPool(tSquare* const cy, tMempool* const mp) · 4 unmodified lines |
| 245 | 196 | mpool_free(c, m); |
| 246 | 197 | } |
| 247 | 198 | |
| 248 | | −int tSquare_setFreq(tSquare* const cy, float freq) |
| 199 | +void tSquare_setFreq(tSquare* const cy, float freq) |
| 249 | 200 | { |
| 250 | 201 | _tSquare* c = *cy; |
| 251 | 202 | |
| 252 | | − if (freq < 0.0f) freq = 0.0f; |
| 203 | + if (freq < 0.0f) c->freq = 0.0f; |
| 204 | + else if (freq > 20480.0f) c->freq = 20480.0f; |
| 205 | + else c->freq = freq; |
| 253 | 206 | |
| 254 | | − c->freq = freq; |
| 255 | | − c->inc = freq * leaf.invSampleRate; |
| 207 | + c->inc = c->freq * leaf.invSampleRate; |
| 256 | 208 | |
| 257 | | − return 0; |
| 209 | + c->w = c->freq * INV_20; |
| 210 | + for (c->oct = 0; c->w > 2.0f; c->oct++) |
| 211 | + { |
| 212 | + c->w = 0.5f * c->w; |
| 213 | + } |
| 214 | + c->w = 2.0f - c->w; |
| 258 | 215 | } |
| 259 | 216 | |
| 260 | 217 | float tSquare_tick(tSquare* const cy) |
| @@ -266,69 +223,12 @@ float tSquare_tick(tSquare* const cy) · 5 unmodified lines |
| 266 | 223 | while (c->phase >= 1.0f) c->phase -= 1.0f; |
| 267 | 224 | |
| 268 | 225 | float out = 0.0f; |
| 269 | | − float w = 0.0f; |
| 226 | + |
| 270 | 227 | int idx = (int)(c->phase * TRI_TABLE_SIZE); |
| 271 | 228 | |
| 272 | 229 | // Wavetable synthesis |
| 273 | | − |
| 274 | | − if (c->freq <= 20.0f) |
| 275 | | − { |
| 276 | | − out = __leaf_table_squarewave[T20][idx]; |
| 277 | | − } |
| 278 | | − else if (c->freq <= 40.0f) |
| 279 | | − { |
| 280 | | − w = ((40.0f - c->freq) * INV_20); |
| 281 | | − out = (__leaf_table_squarewave[T20][idx] * w) + (__leaf_table_squarewave[T40][idx] * (1.0f - w)); |
| 282 | | − } |
| 283 | | − else if (c->freq <= 80.0f) |
| 284 | | − { |
| 285 | | − w = ((80.0f - c->freq) * INV_40); |
| 286 | | − out = (__leaf_table_squarewave[T40][idx] * w) + (__leaf_table_squarewave[T80][idx] * (1.0f - w)); |
| 287 | | − } |
| 288 | | − else if (c->freq <= 160.0f) |
| 289 | | − { |
| 290 | | − w = ((160.0f - c->freq) * INV_80); |
| 291 | | − out = (__leaf_table_squarewave[T80][idx] * w) + (__leaf_table_squarewave[T160][idx] * (1.0f - w)); |
| 292 | | − } |
| 293 | | − else if (c->freq <= 320.0f) |
| 294 | | − { |
| 295 | | − w = ((320.0f - c->freq) * INV_160); |
| 296 | | − out = (__leaf_table_squarewave[T160][idx] * w) + (__leaf_table_squarewave[T320][idx] * (1.0f - w)); |
| 297 | | − } |
| 298 | | − else if (c->freq <= 640.0f) |
| 299 | | − { |
| 300 | | − w = ((640.0f - c->freq) * INV_320); |
| 301 | | − out = (__leaf_table_squarewave[T320][idx] * w) + (__leaf_table_squarewave[T640][idx] * (1.0f - w)); |
| 302 | | − } |
| 303 | | − else if (c->freq <= 1280.0f) |
| 304 | | − { |
| 305 | | − w = ((1280.0f - c->freq) * INV_640); |
| 306 | | − out = (__leaf_table_squarewave[T640][idx] * w) + (__leaf_table_squarewave[T1280][idx] * (1.0f - w)); |
| 307 | | − } |
| 308 | | − else if (c->freq <= 2560.0f) |
| 309 | | − { |
| 310 | | − w = ((2560.0f - c->freq) * INV_1280); |
| 311 | | − out = (__leaf_table_squarewave[T1280][idx] * w) + (__leaf_table_squarewave[T2560][idx] * (1.0f - w)); |
| 312 | | − } |
| 313 | | − else if (c->freq <= 5120.0f) |
| 314 | | − { |
| 315 | | − w = ((5120.0f - c->freq) * INV_2560); |
| 316 | | − out = (__leaf_table_squarewave[T2560][idx] * w) + (__leaf_table_squarewave[T5120][idx] * (1.0f - w)); |
| 317 | | − } |
| 318 | | − else if (c->freq <= 10240.0f) |
| 319 | | − { |
| 320 | | − w = ((10240.0f - c->freq) * INV_5120); |
| 321 | | − out = (__leaf_table_squarewave[T5120][idx] * w) + (__leaf_table_squarewave[T10240][idx] * (1.0f - w)); |
| 322 | | − } |
| 323 | | − else if (c->freq <= 20480.0f) |
| 324 | | − { |
| 325 | | − w = ((20480.0f - c->freq) * INV_10240); |
| 326 | | − out = (__leaf_table_squarewave[T10240][idx] * w) + (__leaf_table_squarewave[T20480][idx] * (1.0f - w)); |
| 327 | | − } |
| 328 | | − else |
| 329 | | − { |
| 330 | | − out = __leaf_table_squarewave[T20480][idx]; |
| 331 | | − } |
| 230 | + out = __leaf_table_squarewave[c->oct+1][idx] + |
| 231 | + (__leaf_table_squarewave[c->oct][idx] - __leaf_table_squarewave[c->oct+1][idx]) * c->w; |
| 332 | 232 | |
| 333 | 233 | return out; |
| 334 | 234 | } |
| @@ -359,6 +259,7 @@ void tSawtooth_initToPool (tSawtooth* const cy, tMempool* const mp) · 24 unmodified lines |
| 359 | 259 | |
| 360 | 260 | c->inc = 0.0f; |
| 361 | 261 | c->phase = 0.0f; |
| 262 | + tSawtooth_setFreq(cy, 220); |
| 362 | 263 | } |
| 363 | 264 | |
| 364 | 265 | void tSawtooth_freeFromPool (tSawtooth* const cy, tMempool* const mp) |
| @@ -369,16 +270,22 @@ void tSawtooth_freeFromPool (tSawtooth* const cy, tMempool* const mp) · 4 unmodified lines |
| 369 | 270 | mpool_free(c, m); |
| 370 | 271 | } |
| 371 | 272 | |
| 372 | | −int tSawtooth_setFreq(tSawtooth* const cy, float freq) |
| 273 | +void tSawtooth_setFreq(tSawtooth* const cy, float freq) |
| 373 | 274 | { |
| 374 | 275 | _tSawtooth* c = *cy; |
| 375 | 276 | |
| 376 | | − if (freq < 0.0f) freq = 0.0f; |
| 277 | + if (freq < 0.0f) c->freq = 0.0f; |
| 278 | + else if (freq > 20480.0f) c->freq = 20480.0f; |
| 279 | + else c->freq = freq; |
| 377 | 280 | |
| 378 | | − c->freq = freq; |
| 379 | | − c->inc = freq * leaf.invSampleRate; |
| 281 | + c->inc = c->freq * leaf.invSampleRate; |
| 380 | 282 | |
| 381 | | − return 0; |
| 283 | + c->w = c->freq * INV_20; |
| 284 | + for (c->oct = 0; c->w > 2.0f; c->oct++) |
| 285 | + { |
| 286 | + c->w = 0.5f * c->w; |
| 287 | + } |
| 288 | + c->w = 2.0f - c->w; |
| 382 | 289 | } |
| 383 | 290 | |
| 384 | 291 | float tSawtooth_tick(tSawtooth* const cy) |
| @@ -390,70 +297,12 @@ float tSawtooth_tick(tSawtooth* const cy) · 5 unmodified lines |
| 390 | 297 | while (c->phase >= 1.0f) c->phase -= 1.0f; |
| 391 | 298 | |
| 392 | 299 | float out = 0.0f; |
| 393 | | − float w; |
| 394 | 300 | |
| 395 | 301 | int idx = (int)(c->phase * TRI_TABLE_SIZE); |
| 396 | 302 | |
| 397 | 303 | // Wavetable synthesis |
| 398 | | − |
| 399 | | − if (c->freq <= 20.0f) |
| 400 | | − { |
| 401 | | − out = __leaf_table_sawtooth[T20][idx]; |
| 402 | | − } |
| 403 | | − else if (c->freq <= 40.0f) |
| 404 | | − { |
| 405 | | − w = ((40.0f - c->freq) * INV_20); |
| 406 | | − out = (__leaf_table_sawtooth[T20][idx] * w) + (__leaf_table_sawtooth[T40][idx] * (1.0f - w)); |
| 407 | | − } |
| 408 | | − else if (c->freq <= 80.0f) |
| 409 | | − { |
| 410 | | − w = ((80.0f - c->freq) * INV_40); |
| 411 | | − out = (__leaf_table_sawtooth[T40][idx] * w) + (__leaf_table_sawtooth[T80][idx] * (1.0f - w)); |
| 412 | | − } |
| 413 | | − else if (c->freq <= 160.0f) |
| 414 | | − { |
| 415 | | − w = ((160.0f - c->freq) * INV_80); |
| 416 | | − out = (__leaf_table_sawtooth[T80][idx] * w) + (__leaf_table_sawtooth[T160][idx] * (1.0f - w)); |
| 417 | | − } |
| 418 | | − else if (c->freq <= 320.0f) |
| 419 | | − { |
| 420 | | − w = ((320.0f - c->freq) * INV_160); |
| 421 | | − out = (__leaf_table_sawtooth[T160][idx] * w) + (__leaf_table_sawtooth[T320][idx] * (1.0f - w)); |
| 422 | | − } |
| 423 | | − else if (c->freq <= 640.0f) |
| 424 | | − { |
| 425 | | − w = ((640.0f - c->freq) * INV_320); |
| 426 | | − out = (__leaf_table_sawtooth[T320][idx] * w) + (__leaf_table_sawtooth[T640][idx] * (1.0f - w)); |
| 427 | | − } |
| 428 | | − else if (c->freq <= 1280.0f) |
| 429 | | − { |
| 430 | | − w = ((1280.0f - c->freq) * INV_640); |
| 431 | | − out = (__leaf_table_sawtooth[T640][idx] * w) + (__leaf_table_sawtooth[T1280][idx] * (1.0f - w)); |
| 432 | | − } |
| 433 | | − else if (c->freq <= 2560.0f) |
| 434 | | − { |
| 435 | | − w = ((2560.0f - c->freq) * INV_1280); |
| 436 | | − out = (__leaf_table_sawtooth[T1280][idx] * w) + (__leaf_table_sawtooth[T2560][idx] * (1.0f - w)); |
| 437 | | − } |
| 438 | | − else if (c->freq <= 5120.0f) |
| 439 | | − { |
| 440 | | − w = ((5120.0f - c->freq) * INV_2560); |
| 441 | | − out = (__leaf_table_sawtooth[T2560][idx] * w) + (__leaf_table_sawtooth[T5120][idx] * (1.0f - w)); |
| 442 | | − } |
| 443 | | − else if (c->freq <= 10240.0f) |
| 444 | | − { |
| 445 | | − w = ((10240.0f - c->freq) * INV_5120); |
| 446 | | − out = (__leaf_table_sawtooth[T5120][idx] * w) + (__leaf_table_sawtooth[T10240][idx] * (1.0f - w)); |
| 447 | | − } |
| 448 | | − else if (c->freq <= 20480.0f) |
| 449 | | − { |
| 450 | | − w = ((20480.0f - c->freq) * INV_10240); |
| 451 | | − out = (__leaf_table_sawtooth[T10240][idx] * w) + (__leaf_table_sawtooth[T20480][idx] * (1.0f - w)); |
| 452 | | − } |
| 453 | | − else |
| 454 | | − { |
| 455 | | − out = __leaf_table_sawtooth[T20480][idx]; |
| 456 | | − } |
| 304 | + out = __leaf_table_sawtooth[c->oct+1][idx] + |
| 305 | + (__leaf_table_sawtooth[c->oct][idx] - __leaf_table_sawtooth[c->oct+1][idx]) * c->w; |
| 457 | 306 | |
| 458 | 307 | return out; |
| 459 | 308 | } |
| @@ -501,16 +350,15 @@ void tPhasor_freeFromPool(tPhasor* const ph, tMempool* const mp) · 41 unmodified lines |
| 501 | 350 | mpool_free(p, m); |
| 502 | 351 | } |
| 503 | 352 | |
| 504 | | −int tPhasor_setFreq(tPhasor* const ph, float freq) |
| 353 | +void tPhasor_setFreq(tPhasor* const ph, float freq) |
| 505 | 354 | { |
| 506 | 355 | _tPhasor* p = *ph; |
| 507 | 356 | |
| 508 | | − if (freq < 0.0f) freq = 0.0f; |
| 357 | + if (freq < 0.0f) p->freq = 0.0f; |
| 358 | + else if (freq > 20480.0f) p->freq = 20480.0f; |
| 359 | + else p->freq = freq; |
| 509 | 360 | |
| 510 | | − p->freq = freq; |
| 511 | 361 | p->inc = freq * leaf.invSampleRate; |
| 512 | | − |
| 513 | | − return 0; |
| 514 | 362 | } |
| 515 | 363 | |
| 516 | 364 | float tPhasor_tick(tPhasor* const ph) |