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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
|
//SuperCollider is under GNU GPL version 3, http://supercollider.sourceforge.net/
//these extensions released under the same license
/*
* AntiAliasingOscillators.cpp
* AntiAliasingOscillators
*
* Created by Nicholas Collins on 07/08/2010.
* Copyright 2010 Nicholas M Collins. All rights reserved.
*
*/
#include "SC_PlugIn.h"
//Juhan Nam, Vesa Valimaki, Jonathan S. Abel, and Julius O. Smith
//Efficient Antialiasing Oscillator Algorithms Using Low-Order Fractional Delay Filters
//IEEE Transactions on Audio, Speech, and Language Processing 18(4) May 2010, pp 773--785
//and
//Vesa Valimaki, Juhan Nam, Julius O. Smith and Jonathan S. Abel
//Alias-Suppressed Oscillators Based on Differentiated Polynomial Waveforms
//IEEE Transactions on Audio, Speech, and Language Processing 18(4) May 2010, pp 786--798
InterfaceTable *ft;
struct BlitB3 : public Unit
{
float phase;
};
struct BlitB3Saw : public Unit
{
float phase_;
//float leakycoefficient_;
float lastoutput_;
float dcoffset_; //compensation for DC offset
};
struct BlitB3Square : public Unit
{
float phase_;
//float leakycoefficient_;
float lastoutput_;
float bipolar_;
};
struct BlitB3Tri : public Unit
{
float phase_;
//float leakycoefficient_;
float lastoutput_;
float lastoutput2_;
float bipolar_;
float scalefactor_;
};
//4th order differentiated polynomial waveform for sawtooth generation
//requires double precision for sufficient accuracy
struct DPW4Saw : public Unit
{
// float phase_;
// float scalefactor_;
// float differentiations_[3];
double phase_;
//double scalefactor_;
double differentiations_[3];
double differentiations2_[2]; //for DPW2 algorithm covering lower frequencies
};
struct DPW3Tri : public Unit
{
// float phase_;
// float differentiations_[2];
double phase_;
double differentiations_[2];
};
extern "C" {
void BlitB3_next(BlitB3 *unit, int inNumSamples);
void BlitB3_Ctor(BlitB3* unit);
void BlitB3Saw_next(BlitB3Saw *unit, int inNumSamples);
void BlitB3Saw_Ctor(BlitB3Saw* unit);
void BlitB3Square_next(BlitB3Square *unit, int inNumSamples);
void BlitB3Square_Ctor(BlitB3Square* unit);
void BlitB3Tri_next(BlitB3Tri *unit, int inNumSamples);
void BlitB3Tri_Ctor(BlitB3Tri* unit);
void DPW4Saw_next(DPW4Saw *unit, int inNumSamples);
void DPW4Saw_Ctor(DPW4Saw* unit);
void DPW3Tri_next(DPW3Tri *unit, int inNumSamples);
void DPW3Tri_Ctor(DPW3Tri* unit);
}
// BlitB3 revised Nathan Ho 2016.
// Phase tracking was modified to prevent freezing with lower frequencies.
void BlitB3_Ctor(BlitB3* unit) {
unit->phase = 0.0f;
SETCALC(BlitB3_next);
BlitB3_next(unit, 1);
// We have to set this to 0 again because the above line disrupts the phase
unit->phase = 0.0f;
}
void BlitB3_next(BlitB3 *unit, int inNumSamples) {
float *out = OUT(0);
float freq = ZIN0(0);
// Clip ludicrous frequencies
if (freq < 0.000001f) {
freq = 0.000001f;
}
// period in samples
float period = SAMPLERATE / freq;
// The phase is in range [0,1).
float phase = fmod(unit->phase, 1.f);
// Scale up to [0,period).
float t = phase * period;
// Temporary variables to save some arithmetic ops
float x, y;
for (int i = 0; i < inNumSamples; i++) {
// 3rd order Lagrange interpolator
if (t >= 4.0f) {
out[i] = 0.0f;
} else if (t >= 3.0f) {
x = 4.0f - t;
out[i] = 0.16666666666667f*x*x*x;
} else if (t >= 2.0f) {
x = t - 2.0;
y = x*x;
out[i] = 0.66666666666666f - y + (0.5f*y*x);
} else if (t >= 1.0f) {
x = t - 2.0;
y = x*x;
out[i] = 0.66666666666666f - y - (0.5f*y*x);
} else {
out[i] = 0.16666666666667f*t*t*t;
}
t += 1.f;
// wrap
if (t >= period) {
t -= period;
}
}
// Divide by period (without dividing)
// t / (SAMPLERATE / freq) = t * freq * SAMPLEDUR
unit->phase = t * freq * SAMPLEDUR;
}
void BlitB3Saw_Ctor( BlitB3Saw* unit ) {
unit->phase_ = 3.0f;
//implies initial period is 5.0f
//unit->leakycoefficient_ = 0.99;
unit->lastoutput_ = 3.0f/5.0f- 0.5f;
unit->dcoffset_ = -1.0f/5.0f;
//float freq = ZIN0(0);
SETCALC(BlitB3Saw_next);
}
//void BlitB3_Dtor(BlitB3 *unit)
//{
//
//
//}
void BlitB3Saw_next( BlitB3Saw *unit, int inNumSamples ) {
//int numSamples = unit->mWorld->mFullRate.mBufLength;
//float *input = IN(0);
float *output = OUT(0);
float dcoffset= unit->dcoffset_;
//unit->period_ = period;
float phase= unit->phase_;
float temp;
float lastoutput = unit->lastoutput_;
float leakycoefficient = ZIN0(1);
float calc;
for (int i=0; i<inNumSamples; ++i) {
phase -= 1.0f;
if (phase>=2.0f) {
calc = dcoffset;
} else if (phase>=1.0f) {
temp= 2.0f-phase;
calc = 0.16666666666667f*temp*temp*temp + dcoffset;
} else if (phase>=0.0f) {
temp= phase*phase;
calc = 0.66666666666666f - temp + (0.5f*temp*phase) + dcoffset;
} else if (phase>= -1.0f) {
temp= phase*phase;
calc = 0.66666666666666f - temp - (0.5f*temp*phase) + dcoffset;
} else if (phase>= - 2.0f) {
temp= 2.0f+phase;
calc = 0.16666666666667f*temp*temp*temp + dcoffset;
} else {
calc= dcoffset; //0.0f;
//recalculate parameters
float freq = ZIN0(0);
if(freq<0.000001f) freq= 0.000001f;
//need period in samples
float period = SAMPLERATE/freq;
if (period<=4.0f) period = 4.0f; //otherwise would have runaway fall off of phase value and incorrect integration
//won't the jump of offset cause discontinuities?
dcoffset= -1.0f/period; //compensation, see eqn (30) in the paper
unit->dcoffset_ = dcoffset;
//printf("hello phase %f period %f dc %f leaky %f \n",phase, period, dcoffset, leakycoefficient);
phase += period; //(period-2.0);
}
calc += (lastoutput*leakycoefficient);
output[i]= calc;
lastoutput = calc;
}
unit->phase_ = phase;
unit->lastoutput_ = lastoutput;
}
void BlitB3Square_Ctor( BlitB3Square* unit ) {
unit->phase_ = 3.0f;
//implies initial period is 10.0f
//unit->leakycoefficient_ = 0.99;
unit->lastoutput_ = -0.5f;
unit->bipolar_ = 1.0f; //start positive, will flip each time
//float freq = ZIN0(0);
SETCALC(BlitB3Square_next);
}
void BlitB3Square_next( BlitB3Square *unit, int inNumSamples ) {
//int numSamples = unit->mWorld->mFullRate.mBufLength;
//float *input = IN(0);
float *output = OUT(0);
float phase= unit->phase_;
float temp;
float lastoutput = unit->lastoutput_;
float leakycoefficient = ZIN0(1);
float calc;
float bipolar = unit->bipolar_;
for (int i=0; i<inNumSamples; ++i) {
phase -= 1.0f;
if (phase>=2.0f) {
calc = 0.0f;
} else if (phase>=1.0f) {
temp= 2.0f-phase;
calc = 0.16666666666667f*temp*temp*temp*bipolar;
} else if (phase>=0.0f) {
temp= phase*phase;
calc = (0.66666666666666f - temp + (0.5f*temp*phase))*bipolar;
} else if (phase>= -1.0f) {
temp= phase*phase;
calc = (0.66666666666666f - temp - (0.5f*temp*phase))*bipolar;
} else if (phase>= - 2.0f) {
temp= 2.0f+phase;
calc = 0.16666666666667f*temp*temp*temp*bipolar;
} else {
calc= 0.0f;
//recalculate parameters
float freq = ZIN0(0);
if(freq<0.000001f) freq= 0.000001f;
//need period in samples
float period = (SAMPLERATE/freq)*0.5f; //always half period because of bipolarism
//no dc offset issues here hopefully
if (period<=1.0f) period = 1.0f; //otherwise would have runaway fall off of phase value and incorrect integration
//printf("hello phase %f period %f dc %f leaky %f \n",phase, period, dcoffset, leakycoefficient);
bipolar *= -1.0f; //swap sign
phase += period; //(period-2.0);
}
calc += (lastoutput*leakycoefficient);
output[i]= calc;
lastoutput = calc;
}
unit->phase_ = phase;
unit->lastoutput_ = lastoutput;
unit->bipolar_ = bipolar;
}
void BlitB3Tri_Ctor( BlitB3Tri* unit ) {
unit->phase_ = 3.0f;
//implies initial period is 20.0f
//unit->leakycoefficient_ = 0.99;
//unit->leakycoefficient2_ = 0.99;
unit->lastoutput_ = -0.5f;
unit->lastoutput2_ = 0.0f;
unit->bipolar_ = 1.0f; //start positive, will flip each time
unit->scalefactor_ = 4.0f/20.0f; //start positive, will flip each time
//float freq = ZIN0(0);
SETCALC(BlitB3Tri_next);
}
void BlitB3Tri_next( BlitB3Tri *unit, int inNumSamples ) {
//int numSamples = unit->mWorld->mFullRate.mBufLength;
//float *input = IN(0);
float *output = OUT(0);
float phase= unit->phase_;
float temp;
float lastoutput = unit->lastoutput_;
float lastoutput2 = unit->lastoutput2_;
float leakycoefficient = ZIN0(1);
float leakycoefficient2 = ZIN0(2);
float calc;
float bipolar = unit->bipolar_;
float scalefactor = unit->scalefactor_;
for (int i=0; i<inNumSamples; ++i) {
phase -= 1.0f;
if (phase>=2.0f) {
calc = 0.0f;
} else if (phase>=1.0f) {
temp= 2.0f-phase;
calc = 0.16666666666667f*temp*temp*temp*bipolar;
} else if (phase>=0.0f) {
temp= phase*phase;
calc = (0.66666666666666f - temp + (0.5f*temp*phase))*bipolar;
} else if (phase>= -1.0f) {
temp= phase*phase;
calc = (0.66666666666666f - temp - (0.5f*temp*phase))*bipolar;
} else if (phase>= - 2.0f) {
temp= 2.0f+phase;
calc = 0.16666666666667f*temp*temp*temp*bipolar;
} else {
calc= 0.0f;
//recalculate parameters
float freq = ZIN0(0);
if(freq<0.000001f) freq= 0.000001f;
//need period in samples
float period = (SAMPLERATE/freq)*0.5f; //always half period because of bipolarism
//no dc offset issues here hopefully
if (period<=1.0) period = 1.0; //otherwise would have runaway fall off of phase value and incorrect integration
//printf("hello phase %f period %f dc %f leaky %f \n",phase, period, dcoffset, leakycoefficient);
//some compensation, pretty uniform, better than having higher frequencies louder as paper has it
scalefactor = 0.25f; //2.0/period;
bipolar *= -1.0f; //swap sign
phase += period; //(period-2.0);
}
calc += (lastoutput*leakycoefficient);
lastoutput = calc;
calc += (lastoutput2*leakycoefficient2);
lastoutput2 = calc;
output[i]= calc*scalefactor;
}
unit->phase_ = phase;
unit->lastoutput_ = lastoutput;
unit->lastoutput2_ = lastoutput2;
unit->bipolar_ = bipolar;
unit->scalefactor_ = scalefactor;
}
/*
void DPW4Saw_Ctor( DPW4Saw* unit ) {
unit->phase_ = 0.0f;
unit->scalefactor_ = 1.0f;
for (int i=0; i<3; ++i)
unit->differentiations_[i] = 0.0f;
SETCALC(DPW4Saw_next);
}
//void BlitB3_Dtor(BlitB3 *unit)
//{
//
//
//}
void DPW4Saw_next( DPW4Saw *unit, int inNumSamples ) {
float *output = OUT(0);
//can change frequency at control rate once per block
float freq = ZIN0(0);
if(freq<0.000001f) freq= 0.000001f;
//need period in samples too
float phasestep = freq/SAMPLERATE;
float period = 1.0f/phasestep;
//4th order
float ampcompensation = 0.0052083333333333f*period*period*period; //basic formula, other formula involves sine
//3rd order
//float ampcompensation = 0.041666666666667*period*period; //basic formula, other formula involves sine
float phase= unit->phase_;
float * differentiations= unit->differentiations_;
float diff1 = differentiations[0];
float diff2 = differentiations[1];
float diff3 = differentiations[2];
float temp, temp2;
//if frequency less than 500Hz, use DPW2, else DPW4
for (int i=0; i<inNumSamples; ++i) {
phase += phasestep;
if (phase >= 1.0f) phase -= 1.0f;
temp= 2.f*phase- 1.f;
temp = temp*temp;
temp = temp*temp - (2.f*temp);
//approximation works for x^2 and one differentiation
//now three differentiations to return it to linear from x^4- 2x^2
// temp2 = temp*period;
// temp = temp2 - diff1;
// diff1 = temp2;
//
// temp2 = temp*period;
// temp = temp2 - diff2;
// diff2= temp2;
//
// temp2 = temp*period;
// temp = temp2 - diff3;
// diff3= temp2;
//
temp2 = temp;
temp = temp2 - diff1;
diff1 = temp2;
temp2 = temp;
temp = temp2 - diff2;
diff2= temp2;
temp2 = temp;
temp = temp2 - diff3;
diff3= temp2;
//output[i]= 0.0052083333333333f*temp; //ampcompensation*temp;
output[i]= ampcompensation*temp;
//output[i]= temp;
}
//printf("hello phase %f period %f\n",phase, period);
unit->phase_ = phase;
differentiations[0] = diff1;
differentiations[1] = diff2;
differentiations[2] = diff3;
}
*/
void DPW4Saw_Ctor( DPW4Saw* unit ) {
unit->phase_ = 0.5;
//unit->scalefactor_ = 1.0;
for (int i=0; i<3; ++i)
unit->differentiations_[i] = 0.0;
for (int i=0; i<2; ++i)
unit->differentiations2_[i] = 0.0;
SETCALC(DPW4Saw_next);
}
//void BlitB3_Dtor(BlitB3 *unit)
//{
//
//
//}
void DPW4Saw_next( DPW4Saw *unit, int inNumSamples ) {
float *output = OUT(0);
//can change frequency at control rate once per block
double freq = ZIN0(0);
if(freq<0.000001) freq= 0.000001;
//need period in samples too
double phasestep = freq/SAMPLERATE;
double period = 1.0/phasestep;
//4th order
double ampcompensation = 0.0052083333333333*period*period*period; //basic formula, other formula involves sine
//if split up normalisation factors, get more artefacts on transients!
double ampcompensation2 = 0.25*period; //basic formula, other formula involves sine
//3rd order
//float ampcompensation = 0.041666666666667*period*period; //basic formula, other formula involves sine
double phase= unit->phase_;
double * differentiations= unit->differentiations_;
double diff1 = differentiations[0];
double diff2 = differentiations[1];
double diff3 = differentiations[2];
//for DPW2
double * differentiations2= unit->differentiations2_;
double diff4 = differentiations2[0];
double diff5 = differentiations2[1];
double temp, temp2;
//if frequency less than 500Hz, use DPW2, else DPW4
// if(freq>600.0) {
//
// for (int i=0; i<inNumSamples; ++i) {
//
// phase += phasestep;
// if (phase > 1.0) phase -= 1.0;
//
// temp= 2.0*phase- 1.0;
//
// temp = temp*temp;
//
// temp = temp*temp - (2.0*temp);
//
// //now three differentiations to return it to linear from x^4- 2x^2
// temp2 = temp;
// temp = temp2 - diff1;
// diff1 = temp2;
//
// temp2 = temp;
// temp = temp2 - diff2;
// diff2= temp2;
//
// temp2 = temp;
// temp = temp2 - diff3;
// diff3= temp2;
//
// //output[i]= 0.0052083333333333f*temp; //ampcompensation*temp;
// output[i]= ampcompensation*temp;
// //output[i]= temp;
// }
//
// } else if (freq>400.0) {
//crossfade region between 400 and 600 Hz, linear for simplicity, could use g *= g
// double g= (freq-400.0)*0.005;
// g *= g;
// double oneminusg = 1.0-g;
//
//must calculate both and crossfade; need to calculate both all the time to avoid click issues from phase mismatch, thankyou Jussi Pekonen
double g;
double oneminusg;
if(freq>600.0) {
g=1.0;
oneminusg = 0.0;
} else if (freq>400.0) {
//crossfade region between 400 and 600 Hz, linear for simplicity, could use g *= g
g= (freq-400.0)*0.005;
g *= g;
oneminusg = 1.0-g;
} else {
g=0.0;
oneminusg = 1.0;
}
for (int i=0; i<inNumSamples; ++i) {
phase += phasestep;
if (phase > 1.0) phase -= 1.0;
temp= 2.0*phase- 1.0;
temp = temp*temp;
//calculate DPW2 here
//delay of one sample required
double dpw2= diff5;
//now one differentiation to return it to linear from x^2
temp2 = temp;
diff5 = temp2 - diff4;
diff4 = temp2;
//output[i]= 0.0052083333333333f*temp; //ampcompensation*temp;
diff5*= ampcompensation2;
temp = temp*temp - (2.0*temp);
//now three differentiations to return it to linear from x^4- 2x^2
temp2 = temp;
temp = temp2 - diff1;
diff1 = temp2;
temp2 = temp;
temp = temp2 - diff2;
diff2= temp2;
temp2 = temp;
temp = temp2 - diff3;
diff3= temp2;
temp *= ampcompensation;
//output[i]= 0.0052083333333333f*temp; //ampcompensation*temp;
output[i]= (g*temp) + (oneminusg*dpw2);
//output[i]= temp;
}
// } else {
//
// //just DPW2
//
// for (int i=0; i<inNumSamples; ++i) {
//
// phase += phasestep;
// if (phase > 1.0) phase -= 1.0;
//
// temp= 2.0*phase- 1.0;
//
// temp = temp*temp;
//
// //now one differentiation to return it to linear from x^2
// temp2 = temp;
// temp = temp2 - diff4;
// diff4 = temp2;
//
// //output[i]= 0.0052083333333333f*temp; //ampcompensation*temp;
// output[i]= diff5;
// //delay of one sample required
// diff5= ampcompensation2*temp;
// //output[i]= temp;
// }
// }
//printf("hello phase %f period %f\n",phase, period);
unit->phase_ = phase;
differentiations[0] = diff1;
differentiations[1] = diff2;
differentiations[2] = diff3;
differentiations2[0] = diff4;
differentiations2[1] = diff5;
}
/*
void DPW3Tri_Ctor( DPW3Tri* unit ) {
unit->phase_ = 0.75f; //want it to start at (0.5-|2*phase-1|), ie zero
for (int i=0; i<2; ++i)
unit->differentiations_[i] = 0.0f;
SETCALC(DPW3Tri_next);
}
void DPW3Tri_next( DPW3Tri *unit, int inNumSamples ) {
float *output = OUT(0);
//can change frequency at control rate once per block
float freq = ZIN0(0);
if(freq<0.000001f) freq= 0.000001f;
//need period in samples too
float phasestep = freq/SAMPLERATE;
float period = 1.0f/phasestep;
//3rd order: 2c for triangle, where c is scale factor for DPW sawtooth
float ampcompensation = 0.083333333333333f*period*period; //basic formula, other formula involves sine
//sine formula, too much sapping of amplitude at low frequencies
//float ampcompensation = 2.0f*sin(pi/period);
//ampcompensation = 1.6449340668482f/(ampcompensation*ampcompensation); //basic formula, other formula involves sine
float phase= unit->phase_;
float * differentiations= unit->differentiations_;
float diff1 = differentiations[0];
float diff2 = differentiations[1];
float temp, temp2;
//if frequency less than 500Hz, use DPW2, else DPW4
for (int i=0; i<inNumSamples; ++i) {
phase += phasestep;
if (phase >= 1.0f) phase -= 1.0f;
temp= 2.f*phase- 1.f;
//absolute value
temp2 = temp>0.0f?(0.5f-temp):(0.5f+temp);
temp = temp2*temp2;
temp = (temp-0.75f)*temp2;
//now two differentiations
temp2 = temp;
temp = temp2 - diff1;
diff1 = temp2;
temp2 = temp;
temp = temp2 - diff2;
diff2= temp2;
output[i]= ampcompensation*temp;
}
//printf("hello phase %f period %f\n",phase, period);
unit->phase_ = phase;
differentiations[0] = diff1;
differentiations[1] = diff2;
}
*/
void DPW3Tri_Ctor( DPW3Tri* unit ) {
unit->phase_ = 0.75; //want it to start at (0.5-|2*phase-1|), ie zero
for (int i=0; i<2; ++i)
unit->differentiations_[i] = 0.0;
SETCALC(DPW3Tri_next);
}
void DPW3Tri_next( DPW3Tri *unit, int inNumSamples ) {
float *output = OUT(0);
//can change frequency at control rate once per block
double freq = ZIN0(0);
if(freq<0.000001) freq= 0.000001;
//need period in samples too
double phasestep = freq/SAMPLERATE;
double period = 1.0/phasestep;
//3rd order: 2c for triangle, where c is scale factor for DPW sawtooth
double ampcompensation = 0.083333333333333*period*period; //basic formula, other formula involves sine
//sine formula, too much sapping of amplitude at low frequencies
//float ampcompensation = 2.0f*sin(pi/period);
//ampcompensation = 1.6449340668482f/(ampcompensation*ampcompensation); //basic formula, other formula involves sine
double phase= unit->phase_;
double * differentiations= unit->differentiations_;
double diff1 = differentiations[0];
double diff2 = differentiations[1];
double temp, temp2;
//if frequency less than 500Hz, use DPW2, else DPW4
for (int i=0; i<inNumSamples; ++i) {
phase += phasestep;
if (phase >= 1.0) phase -= 1.0;
temp= 2.0*phase- 1.0;
//absolute value
temp2 = temp>0.0?(0.5-temp):(0.5+temp);
temp = temp2*temp2;
temp = (temp-0.75)*temp2;
//now two differentiations
temp2 = temp;
temp = temp2 - diff1;
diff1 = temp2;
temp2 = temp;
temp = temp2 - diff2;
diff2= temp2;
output[i]= ampcompensation*temp;
}
//printf("hello phase %f period %f\n",phase, period);
unit->phase_ = phase;
differentiations[0] = diff1;
differentiations[1] = diff2;
}
PluginLoad(AntiAliasingOscillators) {
ft = inTable;
DefineSimpleUnit(BlitB3);
DefineSimpleUnit(BlitB3Saw);
DefineSimpleUnit(BlitB3Square);
DefineSimpleUnit(BlitB3Tri);
DefineSimpleUnit(DPW4Saw);
DefineSimpleUnit(DPW3Tri);
}
|