driver_chibios_dac.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* Copyright 2019 Jack Humbert
  2. * Copyright 2020 JohSchneider
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #pragma once
  18. #ifndef A4
  19. # define A4 PAL_LINE(GPIOA, 4)
  20. #endif
  21. #ifndef A5
  22. # define A5 PAL_LINE(GPIOA, 5)
  23. #endif
  24. /**
  25. * Size of the dac_buffer arrays. All must be the same size.
  26. */
  27. #define AUDIO_DAC_BUFFER_SIZE 256U
  28. /**
  29. * Highest value allowed sample value.
  30. * since the DAC is limited to 12 bit, the absolute max is 0xfff = 4095U;
  31. * lower values adjust the peak-voltage aka volume down.
  32. * adjusting this value has only an effect on a sample-buffer whose values are
  33. * are NOT pregenerated - see square-wave
  34. */
  35. #ifndef AUDIO_DAC_SAMPLE_MAX
  36. # define AUDIO_DAC_SAMPLE_MAX 4095U
  37. #endif
  38. #if !defined(AUDIO_DAC_SAMPLE_RATE) && !defined(AUDIO_MAX_SIMULTANEOUS_TONES) && !defined(AUDIO_DAC_QUALITY_VERY_LOW) && !defined(AUDIO_DAC_QUALITY_LOW) && !defined(AUDIO_DAC_QUALITY_HIGH) && !defined(AUDIO_DAC_QUALITY_VERY_HIGH)
  39. # define AUDIO_DAC_QUALITY_SANE_MINIMUM
  40. #endif
  41. /**
  42. * These presets allow you to quickly switch between quality settings for
  43. * the DAC. The sample rate and maximum number of simultaneous tones roughly
  44. * has an inverse relationship - slightly higher sample rates may be possible.
  45. *
  46. * NOTE: a high sample-rate results in a higher cpu-load, which might lead to
  47. * (audible) discontinuities and/or starve other processes of cpu-time
  48. * (like RGB-led back-lighting, ...)
  49. */
  50. #ifdef AUDIO_DAC_QUALITY_VERY_LOW
  51. # define AUDIO_DAC_SAMPLE_RATE 11025U
  52. # define AUDIO_MAX_SIMULTANEOUS_TONES 8
  53. #endif
  54. #ifdef AUDIO_DAC_QUALITY_LOW
  55. # define AUDIO_DAC_SAMPLE_RATE 22050U
  56. # define AUDIO_MAX_SIMULTANEOUS_TONES 4
  57. #endif
  58. #ifdef AUDIO_DAC_QUALITY_HIGH
  59. # define AUDIO_DAC_SAMPLE_RATE 44100U
  60. # define AUDIO_MAX_SIMULTANEOUS_TONES 2
  61. #endif
  62. #ifdef AUDIO_DAC_QUALITY_VERY_HIGH
  63. # define AUDIO_DAC_SAMPLE_RATE 88200U
  64. # define AUDIO_MAX_SIMULTANEOUS_TONES 1
  65. #endif
  66. #ifdef AUDIO_DAC_QUALITY_SANE_MINIMUM
  67. /* a sane-minimum config: with a trade-off between cpu-load and tone-range
  68. *
  69. * the (currently) highest defined note is NOTE_B8 with 7902Hz; if we now
  70. * aim for an even even multiple of the buffer-size, we end up with:
  71. * ( roundUptoPow2(highest note / AUDIO_DAC_BUFFER_SIZE) * nyquist-rate * AUDIO_DAC_BUFFER_SIZE)
  72. * 7902/256 = 30.867 * 2 * 256 ~= 16384
  73. * which works out (but the 'scope shows some sampling artifacts with lower harmonics :-P)
  74. */
  75. # define AUDIO_DAC_SAMPLE_RATE 16384U
  76. # define AUDIO_MAX_SIMULTANEOUS_TONES 8
  77. #endif
  78. /**
  79. * Effective bit-rate of the DAC. 44.1khz is the standard for most audio - any
  80. * lower will sacrifice perceptible audio quality. Any higher will limit the
  81. * number of simultaneous tones. In most situations, a tenth (1/10) of the
  82. * sample rate is where notes become unbearable.
  83. */
  84. #ifndef AUDIO_DAC_SAMPLE_RATE
  85. # define AUDIO_DAC_SAMPLE_RATE 44100U
  86. #endif
  87. /**
  88. * The number of tones that can be played simultaneously. If too high a value
  89. * is used here, the keyboard will freeze and glitch-out when that many tones
  90. * are being played.
  91. */
  92. #ifndef AUDIO_MAX_SIMULTANEOUS_TONES
  93. # define AUDIO_MAX_SIMULTANEOUS_TONES 2
  94. #endif
  95. /**
  96. * The default value of the DAC when not playing anything. Certain hardware
  97. * setups may require a high (AUDIO_DAC_SAMPLE_MAX) or low (0) value here.
  98. * Since multiple added sine waves tend to oscillate around the midpoint,
  99. * and possibly never/rarely reach either 0 of MAX, 1/2 MAX can be a
  100. * reasonable default value.
  101. */
  102. #ifndef AUDIO_DAC_OFF_VALUE
  103. # define AUDIO_DAC_OFF_VALUE AUDIO_DAC_SAMPLE_MAX / 2
  104. #endif
  105. #if AUDIO_DAC_OFF_VALUE > AUDIO_DAC_SAMPLE_MAX
  106. # error "AUDIO_DAC: OFF_VALUE may not be larger than SAMPLE_MAX"
  107. #endif
  108. /**
  109. *user overridable sample generation/processing
  110. */
  111. uint16_t dac_value_generate(void);