diff options
Diffstat (limited to 'platforms/chibios/drivers/audio_dac.h')
| -rw-r--r-- | platforms/chibios/drivers/audio_dac.h | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/platforms/chibios/drivers/audio_dac.h b/platforms/chibios/drivers/audio_dac.h new file mode 100644 index 000000000..07cd622ea --- /dev/null +++ b/platforms/chibios/drivers/audio_dac.h | |||
| @@ -0,0 +1,126 @@ | |||
| 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 | * Size of the dac_buffer arrays. All must be the same size. | ||
| 28 | */ | ||
| 29 | #define AUDIO_DAC_BUFFER_SIZE 256U | ||
| 30 | |||
| 31 | /** | ||
| 32 | * Highest value allowed sample value. | ||
| 33 | |||
| 34 | * since the DAC is limited to 12 bit, the absolute max is 0xfff = 4095U; | ||
| 35 | * lower values adjust the peak-voltage aka volume down. | ||
| 36 | * adjusting this value has only an effect on a sample-buffer whose values are | ||
| 37 | * are NOT pregenerated - see square-wave | ||
| 38 | */ | ||
| 39 | #ifndef AUDIO_DAC_SAMPLE_MAX | ||
| 40 | # define AUDIO_DAC_SAMPLE_MAX 4095U | ||
| 41 | #endif | ||
| 42 | |||
| 43 | #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) | ||
| 44 | # define AUDIO_DAC_QUALITY_SANE_MINIMUM | ||
| 45 | #endif | ||
| 46 | |||
| 47 | /** | ||
| 48 | * These presets allow you to quickly switch between quality settings for | ||
| 49 | * the DAC. The sample rate and maximum number of simultaneous tones roughly | ||
| 50 | * has an inverse relationship - slightly higher sample rates may be possible. | ||
| 51 | * | ||
| 52 | * NOTE: a high sample-rate results in a higher cpu-load, which might lead to | ||
| 53 | * (audible) discontinuities and/or starve other processes of cpu-time | ||
| 54 | * (like RGB-led back-lighting, ...) | ||
| 55 | */ | ||
| 56 | #ifdef AUDIO_DAC_QUALITY_VERY_LOW | ||
| 57 | # define AUDIO_DAC_SAMPLE_RATE 11025U | ||
| 58 | # define AUDIO_MAX_SIMULTANEOUS_TONES 8 | ||
| 59 | #endif | ||
| 60 | |||
| 61 | #ifdef AUDIO_DAC_QUALITY_LOW | ||
| 62 | # define AUDIO_DAC_SAMPLE_RATE 22050U | ||
| 63 | # define AUDIO_MAX_SIMULTANEOUS_TONES 4 | ||
| 64 | #endif | ||
| 65 | |||
| 66 | #ifdef AUDIO_DAC_QUALITY_HIGH | ||
| 67 | # define AUDIO_DAC_SAMPLE_RATE 44100U | ||
| 68 | # define AUDIO_MAX_SIMULTANEOUS_TONES 2 | ||
| 69 | #endif | ||
| 70 | |||
| 71 | #ifdef AUDIO_DAC_QUALITY_VERY_HIGH | ||
| 72 | # define AUDIO_DAC_SAMPLE_RATE 88200U | ||
| 73 | # define AUDIO_MAX_SIMULTANEOUS_TONES 1 | ||
| 74 | #endif | ||
| 75 | |||
| 76 | #ifdef AUDIO_DAC_QUALITY_SANE_MINIMUM | ||
| 77 | /* a sane-minimum config: with a trade-off between cpu-load and tone-range | ||
| 78 | * | ||
| 79 | * the (currently) highest defined note is NOTE_B8 with 7902Hz; if we now | ||
| 80 | * aim for an even even multiple of the buffer-size, we end up with: | ||
| 81 | * ( roundUptoPow2(highest note / AUDIO_DAC_BUFFER_SIZE) * nyquist-rate * AUDIO_DAC_BUFFER_SIZE) | ||
| 82 | * 7902/256 = 30.867 * 2 * 256 ~= 16384 | ||
| 83 | * which works out (but the 'scope shows some sampling artifacts with lower harmonics :-P) | ||
| 84 | */ | ||
| 85 | # define AUDIO_DAC_SAMPLE_RATE 16384U | ||
| 86 | # define AUDIO_MAX_SIMULTANEOUS_TONES 8 | ||
| 87 | #endif | ||
| 88 | |||
| 89 | /** | ||
| 90 | * Effective bit-rate of the DAC. 44.1khz is the standard for most audio - any | ||
| 91 | * lower will sacrifice perceptible audio quality. Any higher will limit the | ||
| 92 | * number of simultaneous tones. In most situations, a tenth (1/10) of the | ||
| 93 | * sample rate is where notes become unbearable. | ||
| 94 | */ | ||
| 95 | #ifndef AUDIO_DAC_SAMPLE_RATE | ||
| 96 | # define AUDIO_DAC_SAMPLE_RATE 44100U | ||
| 97 | #endif | ||
| 98 | |||
| 99 | /** | ||
| 100 | * The number of tones that can be played simultaneously. If too high a value | ||
| 101 | * is used here, the keyboard will freeze and glitch-out when that many tones | ||
| 102 | * are being played. | ||
| 103 | */ | ||
| 104 | #ifndef AUDIO_MAX_SIMULTANEOUS_TONES | ||
| 105 | # define AUDIO_MAX_SIMULTANEOUS_TONES 2 | ||
| 106 | #endif | ||
| 107 | |||
| 108 | /** | ||
| 109 | * The default value of the DAC when not playing anything. Certain hardware | ||
| 110 | * setups may require a high (AUDIO_DAC_SAMPLE_MAX) or low (0) value here. | ||
| 111 | * Since multiple added sine waves tend to oscillate around the midpoint, | ||
| 112 | * and possibly never/rarely reach either 0 of MAX, 1/2 MAX can be a | ||
| 113 | * reasonable default value. | ||
| 114 | */ | ||
| 115 | #ifndef AUDIO_DAC_OFF_VALUE | ||
| 116 | # define AUDIO_DAC_OFF_VALUE AUDIO_DAC_SAMPLE_MAX / 2 | ||
| 117 | #endif | ||
| 118 | |||
| 119 | #if AUDIO_DAC_OFF_VALUE > AUDIO_DAC_SAMPLE_MAX | ||
| 120 | # error "AUDIO_DAC: OFF_VALUE may not be larger than SAMPLE_MAX" | ||
| 121 | #endif | ||
| 122 | |||
| 123 | /** | ||
| 124 | *user overridable sample generation/processing | ||
| 125 | */ | ||
| 126 | uint16_t dac_value_generate(void); | ||
