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.

49 lines
983B

  1. #include "whatnote-module.hh"
  2. #include <cmath>
  3. namespace rack_plugin_Skylights {
  4. const double SEMITONE = 1.0 / 12.0;
  5. void whatnote_module::step() {
  6. voltage = inputs[0].value;
  7. // its not valid, so don't analyze it
  8. if (voltage < -10.0 || voltage > 10.0) {
  9. octave = -11.0;
  10. return;
  11. }
  12. double y;
  13. double x = modf(voltage, &y); // semitones/cents are fractional part
  14. octave = (int)y + 4; // octage is integer part
  15. // and find semitones in there
  16. if (x < 0.0) {
  17. octave -= 1.0;
  18. x = 1.0 + x;
  19. }
  20. double z;
  21. double w = modf(x / SEMITONE, &z);
  22. semitone = z;
  23. cents = (int)round(w * 100.0);
  24. if (cents == 100) {
  25. semitone = (semitone + 1) % 12;
  26. cents = 0;
  27. }
  28. assert(semitone >= 0);
  29. assert(semitone < 12);
  30. }
  31. whatnote_module::whatnote_module() : Module(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS), octave(0), semitone(0), cents(0), voltage(0) {
  32. }
  33. whatnote_module::~whatnote_module() {
  34. }
  35. } // namespace rack_plugin_Skylights