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 3.4KB

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