qmk_firmware

QMK firmware for my keyboards (Corne, Sweep Ferris) and trackball (Ploopy Adept)
Log | Files | Refs | Submodules | LICENSE

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