logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

audio_dac.h (5739B)


  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. * Highest value allowed sample value.
  26. * since the DAC is limited to 12 bit, the absolute max is 0xfff = 4095U;
  27. * lower values adjust the peak-voltage aka volume down.
  28. * adjusting this value has only an effect on a sample-buffer whose values are
  29. * are NOT pregenerated - see square-wave
  30. */
  31. #ifndef AUDIO_DAC_SAMPLE_MAX
  32. # define AUDIO_DAC_SAMPLE_MAX 4095U
  33. #endif
  34. #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)
  35. # define AUDIO_DAC_QUALITY_SANE_MINIMUM
  36. #endif
  37. /**
  38. * These presets allow you to quickly switch between quality settings for
  39. * the DAC. The sample rate and maximum number of simultaneous tones roughly
  40. * has an inverse relationship - slightly higher sample rates may be possible.
  41. *
  42. * NOTE: a high sample-rate results in a higher cpu-load, which might lead to
  43. * (audible) discontinuities and/or starve other processes of cpu-time
  44. * (like RGB-led back-lighting, ...)
  45. */
  46. #ifdef AUDIO_DAC_QUALITY_VERY_LOW
  47. # define AUDIO_DAC_SAMPLE_RATE 11025U
  48. # define AUDIO_MAX_SIMULTANEOUS_TONES 8
  49. #endif
  50. #ifdef AUDIO_DAC_QUALITY_LOW
  51. # define AUDIO_DAC_SAMPLE_RATE 22050U
  52. # define AUDIO_MAX_SIMULTANEOUS_TONES 4
  53. #endif
  54. #ifdef AUDIO_DAC_QUALITY_HIGH
  55. # define AUDIO_DAC_SAMPLE_RATE 44100U
  56. # define AUDIO_MAX_SIMULTANEOUS_TONES 2
  57. #endif
  58. #ifdef AUDIO_DAC_QUALITY_VERY_HIGH
  59. # define AUDIO_DAC_SAMPLE_RATE 88200U
  60. # define AUDIO_MAX_SIMULTANEOUS_TONES 1
  61. #endif
  62. #ifdef AUDIO_DAC_QUALITY_SANE_MINIMUM
  63. /* a sane-minimum config: with a trade-off between cpu-load and tone-range
  64. *
  65. * the (currently) highest defined note is NOTE_B8 with 7902Hz; if we now
  66. * aim for an even even multiple of the buffer-size, we end up with:
  67. * ( roundUptoPow2(highest note / AUDIO_DAC_BUFFER_SIZE) * nyquist-rate * AUDIO_DAC_BUFFER_SIZE)
  68. * 7902/256 = 30.867 * 2 * 256 ~= 16384
  69. * which works out (but the 'scope shows some sampling artifacts with lower harmonics :-P)
  70. */
  71. # define AUDIO_DAC_SAMPLE_RATE 16384U
  72. # define AUDIO_MAX_SIMULTANEOUS_TONES 8
  73. #endif
  74. /**
  75. * Effective bit-rate of the DAC. 44.1khz is the standard for most audio - any
  76. * lower will sacrifice perceptible audio quality. Any higher will limit the
  77. * number of simultaneous tones. In most situations, a tenth (1/10) of the
  78. * sample rate is where notes become unbearable.
  79. */
  80. #ifndef AUDIO_DAC_SAMPLE_RATE
  81. # define AUDIO_DAC_SAMPLE_RATE 44100U
  82. #endif
  83. /**
  84. * Size of the dac_buffer array. This controls the length of the runtime buffer
  85. * which accumulates the data to be sent to the DAC every few milliseconds, and
  86. * it does not need to correspond to the length of the wavetable for the chosen
  87. * waveform defined by AUDIO_DAC_SAMPLE_WAVEFORM_* in the additive DAC driver.
  88. * By default, this is set to be as close to 3.3 ms as possible, giving 300 DAC
  89. * interrupts per second. Any smaller and the interrupt load gets too heavy and
  90. * this results in crackling due to buffer underrun in the additive DAC driver;
  91. * too large and the RAM (additive driver) or flash (basic driver) usage may be
  92. * too high, causing build failures, and matrix scanning is liable to have long
  93. * periodic pauses that delay key presses or releases or fully lose short taps.
  94. * Large buffers also cause notes to take longer to stop after they should from
  95. * music mode or MIDI input.
  96. * This should be a power of 2 for maximum compatibility.
  97. */
  98. #ifndef AUDIO_DAC_BUFFER_SIZE
  99. # if AUDIO_DAC_SAMPLE_RATE < 5100U
  100. # define AUDIO_DAC_BUFFER_SIZE 16U
  101. # elif AUDIO_DAC_SAMPLE_RATE < 9900U
  102. # define AUDIO_DAC_BUFFER_SIZE 32U
  103. # elif AUDIO_DAC_SAMPLE_RATE < 19500U
  104. # define AUDIO_DAC_BUFFER_SIZE 64U
  105. # elif AUDIO_DAC_SAMPLE_RATE < 38700U
  106. # define AUDIO_DAC_BUFFER_SIZE 128U
  107. # else
  108. # define AUDIO_DAC_BUFFER_SIZE 256U
  109. # endif
  110. #endif
  111. /**
  112. * The number of tones that can be played simultaneously. If too high a value
  113. * is used here, the keyboard will freeze and glitch-out when that many tones
  114. * are being played.
  115. */
  116. #ifndef AUDIO_MAX_SIMULTANEOUS_TONES
  117. # define AUDIO_MAX_SIMULTANEOUS_TONES 2
  118. #endif
  119. /**
  120. * The default value of the DAC when not playing anything. Certain hardware
  121. * setups may require a high (AUDIO_DAC_SAMPLE_MAX) or low (0) value here.
  122. * Since multiple added sine waves tend to oscillate around the midpoint,
  123. * and possibly never/rarely reach either 0 of MAX, 1/2 MAX can be a
  124. * reasonable default value.
  125. */
  126. #ifndef AUDIO_DAC_OFF_VALUE
  127. # define AUDIO_DAC_OFF_VALUE AUDIO_DAC_SAMPLE_MAX / 2
  128. #endif
  129. #if AUDIO_DAC_OFF_VALUE > AUDIO_DAC_SAMPLE_MAX
  130. # error "AUDIO_DAC: OFF_VALUE may not be larger than SAMPLE_MAX"
  131. #endif
  132. /**
  133. *user overridable sample generation/processing
  134. */
  135. uint16_t dac_value_generate(void);