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

  1. #include <cmath>
  2. #include "DTMF.hpp"
  3. namespace SynthDevKit {
  4. DTMF::DTMF (uint32_t sampleRate) {
  5. this->sampleRate = sampleRate;
  6. reset();
  7. }
  8. void DTMF::reset ( ) {
  9. step = 0;
  10. lowFreq = 0;
  11. highFreq = 0;
  12. }
  13. void DTMF::setTone (char key) {
  14. switch(key) {
  15. case '1': case '2': case '3': case 'A': lowFreq = 697; break;
  16. case '4': case '5': case '6': case 'B': lowFreq = 770; break;
  17. case '7': case '8': case '9': case 'C': lowFreq = 852; break;
  18. case '*': case '0': case '#': case 'D': lowFreq = 941; break;
  19. default: lowFreq = 0;
  20. }
  21. switch (key) {
  22. case '1': case '4': case '7': case '*': highFreq = 1209; break;
  23. case '2': case '5': case '8': case '0': highFreq = 1336; break;
  24. case '3': case '6': case '9': case '#': highFreq = 1477; break;
  25. case 'A': case 'B': case 'C': case 'D': highFreq = 1633; break;
  26. default: lowFreq = 0;
  27. }
  28. }
  29. float DTMF::stepValue ( ) {
  30. if (lowFreq == 0) {
  31. return 0;
  32. }
  33. double pi_prod_1 = (2.0 * DTMF_PI * lowFreq) / sampleRate;
  34. double pi_prod_2 = (2.0 * DTMF_PI * highFreq) / sampleRate;
  35. float ret = ((128 + (63 * sin(step * pi_prod_1)) + (63 * sin(step * pi_prod_2))) / 25.5) - 5;
  36. step++;
  37. return ret;
  38. }
  39. }