compositor for 2d glitch effects
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

oscillator.pde 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. class Oscillator {
  2. float speed; //in milliseconds
  3. int spawnMillis;
  4. int pauseMillis;
  5. int beatCount = 0;
  6. int mode;
  7. //int function; //function to be called by bang-oscillators
  8. float position; // between 0 and TWO_PI
  9. float value; // is returned, constrained between "low" and "high"
  10. float low = -1.0;
  11. float high = 1.0;
  12. boolean playing, pause, valInvert;
  13. Oscillator(float bpm_) {
  14. bpm(bpm_);
  15. play();
  16. }
  17. void amplitude(float low_, float high_) {
  18. low = low_;
  19. high = high_;
  20. }
  21. void frequency(float hertz_) {
  22. speed = 1000/hertz_;
  23. }
  24. void bpm(float bpm_) {
  25. speed = int(60 / bpm_ * 1000);
  26. }
  27. void play() {
  28. if (pause) {
  29. float pauseTime = millis() - pauseMillis;
  30. spawnMillis += pauseTime;
  31. pause = false;
  32. } else if (!playing) {
  33. spawnMillis = millis();
  34. }
  35. playing = true;
  36. }
  37. void pause() {
  38. if (playing) {
  39. pauseMillis = millis();
  40. pause = true;
  41. playing = false;
  42. }
  43. }
  44. void stop() {
  45. beatCount = 0;
  46. playing = false;
  47. value = 0;
  48. }
  49. void update() {
  50. if (playing) {
  51. while (millis() - spawnMillis > speed*beatCount) {
  52. beatCount++;
  53. }
  54. float millisToBeat = millis() - spawnMillis - (speed*beatCount);
  55. position = map(-millisToBeat, speed, 0, 0, TWO_PI);
  56. switch(mode) {
  57. case(SINE):
  58. value = sin(position);
  59. break;
  60. case(SAWTOOTH):
  61. value = ((position % TWO_PI)-PI)/PI;
  62. break;
  63. case(SAWTOOTHINVERSE):
  64. value = (((position % TWO_PI)-PI)/PI)*-1;
  65. break;
  66. case(SQUAR):
  67. value = sin(position > PI ? 1 : -1);
  68. break;
  69. case(TRIANG):
  70. value = position-PI < 0 ? ((position % TWO_PI)-PI)/PI : (((position % TWO_PI)-PI)/PI)*-1;
  71. value+=0.5;
  72. value*=2;
  73. if (value == 3.0) value = -1;
  74. break;
  75. case(TAN):
  76. value = tan(position);
  77. break;
  78. case(TANINVERSE):
  79. value = tan(position)*-1;
  80. break;
  81. case(RAMP):
  82. value = sin(position) > 0 ? ((position % TWO_PI)-PI)/PI : 0;
  83. value+=0.5;
  84. value*=2;
  85. if (value == 3.0) value = -1;
  86. break;
  87. case(RAMPINVERSE):
  88. value = sin(position) > 0 ? ((position % TWO_PI)-PI)/PI : 0;
  89. value+=0.5;
  90. value*=2;
  91. if (value == 3.0) value = -1;
  92. value = value*-1;
  93. break;
  94. case(RAMPUPDOWN):
  95. if (sin(position) <= -1.6666666666) value = -1.6666666666;
  96. else if (sin(position) < 1.6666666666 && sin(position) > -1.6666666666) value = sin(position);
  97. else if (sin(position) >= 1.6666666666) value = 1.6666666666;
  98. value *= 1.5;
  99. break;
  100. default:
  101. break;
  102. }
  103. value = constrain(value, -1.0, 1.0);
  104. value = map(value, -1.0, 1.0, low, high);
  105. //value = valInvert? map(value, 1.0, -1.0, low, high) : map(value, -1.0, 1.0, low, high);
  106. }
  107. //fill(0);
  108. //ellipse(inc, height/2+value*height/4, 1, 1);
  109. }
  110. }