compositor for 2d shader
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.2KB

11 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //Osci osci, osci_shifted;
  2. final static int SIN = 0; //add 0.25 to phase to get cos
  3. final static int TAN = 1;
  4. final static int LINE = 2;
  5. final static int SQUARE = 3; //switches between -1 and 1, that's all
  6. final static int RAMP = 4; //line for half the sequence up to 1, then stay at 1 for the other half
  7. final static int RAMPUPDOWN = 5; //line up, stay, line down, stay. use this when drawing a square once for x and once for y with a phaseshift of .. what was it.. 0.5 or 0.25. try it
  8. final static int COS = 6;
  9. final static int RANDOM = 7;
  10. final static int INTVAL = 100;
  11. final static int FLOATVAL = 101;
  12. class Osci { //it's a wave generator
  13. float delta;
  14. float beats;
  15. float inc;
  16. boolean playing;
  17. int mode;
  18. int[] modes;
  19. float phaseshift;
  20. float value;
  21. float startFrame; //resets after every playthrough
  22. //color c;
  23. Osci(float beats_, float phaseshift_, int mode_) { //how many beats one cycle takes, the more, the longer.
  24. beats = beats_;
  25. phaseshift = phaseshift_;
  26. inc = phaseshift * TWO_PI;
  27. mode = mode_;
  28. modes = new int[1];
  29. modes[0] = mode;
  30. start();
  31. }
  32. /*
  33. Osci(int[] modes_) { //for random
  34. int[] possibleBeats = {1, 2, 4, 8, 16, 32, 64};
  35. beats = possibleBeats[(int)random(possibleBeats.length)];
  36. modes = modes_;
  37. phaseshift = (random(1) > .8) ? .25 : 0;
  38. inc = phaseshift * TWO_PI;
  39. mode = modes[0];
  40. start();
  41. }*/
  42. void start() { /// start - reset
  43. delta = (8 * TWO_PI / beats) / ((60 * frameRate) / bpm);
  44. playing = true;
  45. startFrame = frameCount;
  46. }
  47. void stop() {
  48. playing = false;
  49. }
  50. void update() {
  51. if (playing) {
  52. inc += delta;
  53. if (inc > beats*16*TWO_PI) inc -= beats*16*TWO_PI;
  54. switch(mode) {
  55. case(SIN):
  56. value = sin(inc);
  57. break;
  58. case(COS):
  59. value = cos(inc);
  60. break;
  61. case(TAN):
  62. value = tan(inc);
  63. break;
  64. case(LINE):
  65. value = ((inc % TWO_PI)-PI)/PI;
  66. //value = ((inc % (TWO_PI-(TWO_PI*phaseshift)))-PI)/PI*1+2*phaseshift;
  67. //println(inc % TWO_PI);
  68. break;
  69. case(SQUARE):
  70. value = sin(inc+(phaseshift*TWO_PI)) > 0 ? 1 : -1;
  71. break;
  72. case(RAMP):
  73. value = sin(inc) > 0 ? ((inc % TWO_PI)-PI)/PI : 0;
  74. break;
  75. case(RAMPUPDOWN):
  76. if (sin(inc) < -0.666666666) {
  77. value = -0.666666666;
  78. } else if (sin(inc) < 0.666666666 && sin(inc) > -0.666666666) {
  79. value = sin(inc);
  80. } else if (sin(inc) < 1 && sin(inc) > 0.666666666) {
  81. value = 0.666666666;
  82. }
  83. value *= 1.5;
  84. break;
  85. default:
  86. break;
  87. }
  88. value = constrain(value, -1, 1);
  89. if (frameCount > startFrame + (((60 * frameRate) / bpm) * beats) /2) {
  90. value = (mode == RANDOM) ? random(-1, 1) : value;
  91. //println("beat hit. that's " + str(frameCount - startFrame) + " frames since the last beat");
  92. startFrame += (((60 * frameRate) / bpm) * beats) /2;
  93. onRepeat();
  94. }
  95. value = map(value, -1, 1, 0, 1);
  96. }
  97. }
  98. void onRepeat() {
  99. //println("beat played..");
  100. //background(c);
  101. }
  102. /*
  103. void display() {
  104. fill(0);
  105. stroke(2);
  106. strokeWeight(3);
  107. point(width/2, ((value + 1) / 2)* height);
  108. }*/
  109. }