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

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