ugens.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include "ugens.h"
  2. #include "math.h"
  3. //--------------------------------------------
  4. ugen::Sine::Sine(float freq)
  5. {
  6. setFreq(freq);
  7. tbl = Tables::sine();
  8. }
  9. sample ugen::Sine::operator()(void)
  10. {
  11. return tick();
  12. }
  13. //--------------------------------------------
  14. ugen::Tri::Tri(float freq)
  15. {
  16. setFreq(freq);
  17. tbl = Tables::tri();
  18. }
  19. sample ugen::Tri::operator()(void)
  20. {
  21. return tick();
  22. }
  23. //--------------------------------------------
  24. ugen::Saw::Saw(float freq)
  25. {
  26. setFreq(freq);
  27. tbl = Tables::saw();
  28. }
  29. sample ugen::Saw::operator()(void)
  30. {
  31. return tick();
  32. }
  33. //--------------------------------------------
  34. ugen::ReverseSaw::ReverseSaw(float freq)
  35. {
  36. setFreq(freq);
  37. tbl = Tables::saw();
  38. }
  39. sample ugen::ReverseSaw::operator()(void)
  40. {
  41. /* This is a neat little trick. It simply reverses the indexing of the saw
  42. * table to give the reverse saw.
  43. * The change in indexing uses bitwise operations so it is very fast!
  44. */
  45. sample outValue = *(tbl + (~current_phase & Tables::phase_mask()) );
  46. current_phase += phase_inc;
  47. current_phase = current_phase & Tables::phase_mask();
  48. return outValue;
  49. }
  50. //--------------------------------------------
  51. ugen::Square::Square(float freq)
  52. {
  53. setFreq(freq);
  54. // Note. This is optimized. See table.cpp for details.
  55. #ifdef SQUAREWAVE_OPTIMIZE_MEMORY
  56. tbl = NULL;
  57. half_length = Tables::length()/2;
  58. #else
  59. tbl = Tables::square();
  60. #endif
  61. }
  62. sample ugen::Square::operator()(void)
  63. {
  64. #ifdef SQUAREWAVE_OPTIMIZE_MEMORY
  65. sample outValue = ( current_phase <= half_length ) ? (TABLE_DC+TABLE_PEAK) : (TABLE_DC-TABLE_PEAK);
  66. current_phase += phase_inc;
  67. current_phase = current_phase & Tables::phase_mask();
  68. return outValue;
  69. #else
  70. return tick();
  71. #endif
  72. }