logo

qmk_firmware

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

audio.h (10371B)


  1. /* Copyright 2016-2020 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. #include <stdint.h>
  19. #include <stdbool.h>
  20. #include "compiler_support.h"
  21. #include "musical_notes.h"
  22. #include "song_list.h"
  23. #include "voices.h"
  24. #if defined(AUDIO_DRIVER_PWM)
  25. # include "audio_pwm.h"
  26. #elif defined(AUDIO_DRIVER_DAC)
  27. # include "audio_dac.h"
  28. #endif
  29. typedef union audio_config_t {
  30. uint8_t raw;
  31. struct {
  32. bool enable : 1;
  33. bool clicky_enable : 1;
  34. bool valid : 1;
  35. uint8_t reserved : 5;
  36. };
  37. } audio_config_t;
  38. STATIC_ASSERT(sizeof(audio_config_t) == sizeof(uint8_t), "Audio EECONFIG out of spec.");
  39. /*
  40. * a 'musical note' is represented by pitch and duration; a 'musical tone' adds intensity and timbre
  41. * https://en.wikipedia.org/wiki/Musical_tone
  42. * "A musical tone is characterized by its duration, pitch, intensity (or loudness), and timbre (or quality)"
  43. */
  44. typedef struct {
  45. uint16_t time_started; // timestamp the tone/note was started, system time runs with 1ms resolution -> 16bit timer overflows every ~64 seconds, long enough under normal circumstances; but might be too soon for long-duration notes when the note_tempo is set to a very low value
  46. float pitch; // aka frequency, in Hz
  47. uint16_t duration; // in ms, converted from the musical_notes.h unit which has 64parts to a beat, factoring in the current tempo in beats-per-minute
  48. // float intensity; // aka volume [0,1] TODO: not used at the moment; pwm drivers can't handle it
  49. // uint8_t timbre; // range: [0,100] TODO: this currently kept track of globally, should we do this per tone instead?
  50. } musical_tone_t;
  51. // public interface
  52. /**
  53. * @brief Save the current choices to the eeprom
  54. */
  55. void eeconfig_update_audio_current(void);
  56. /**
  57. * @brief one-time initialization called by quantum/quantum.c
  58. * @details usually done lazy, when some tones are to be played
  59. *
  60. * @post audio system (and hardware) initialized and ready to play tones
  61. */
  62. void audio_init(void);
  63. void audio_startup(void);
  64. /**
  65. * @brief en-/disable audio output, save this choice to the eeprom
  66. */
  67. void audio_toggle(void);
  68. /**
  69. * @brief enable audio output, save this choice to the eeprom
  70. */
  71. void audio_on(void);
  72. /**
  73. * @brief disable audio output, save this choice to the eeprom
  74. */
  75. void audio_off(void);
  76. /**
  77. * @brief query the if audio output is enabled
  78. */
  79. bool audio_is_on(void);
  80. /**
  81. * @brief start playback of a tone with the given frequency and duration
  82. *
  83. * @details starts the playback of a given note, which is automatically stopped
  84. * at the the end of its duration = fire&forget
  85. *
  86. * @param[in] pitch frequency of the tone be played
  87. * @param[in] duration in milliseconds, use 'audio_duration_to_ms' to convert
  88. * from the musical_notes.h unit to ms
  89. */
  90. void audio_play_note(float pitch, uint16_t duration);
  91. // TODO: audio_play_note(float pitch, uint16_t duration, float intensity, float timbre);
  92. // audio_play_note_with_instrument ifdef AUDIO_ENABLE_VOICES
  93. /**
  94. * @brief start playback of a tone with the given frequency
  95. *
  96. * @details the 'frequency' is put on-top the internal stack of active tones,
  97. * as a new tone with indefinite duration. this tone is played by
  98. * the hardware until a call to 'audio_stop_tone'.
  99. * should a tone with that frequency already be active, its entry
  100. * is put on the top of said internal stack - so no duplicate
  101. * entries are kept.
  102. * 'hardware_start' is called upon the first note.
  103. *
  104. * @param[in] pitch frequency of the tone be played
  105. */
  106. void audio_play_tone(float pitch);
  107. /**
  108. * @brief stop a given tone/frequency
  109. *
  110. * @details removes a tone matching the given frequency from the internal
  111. * playback stack
  112. * the hardware is stopped in case this was the last/only frequency
  113. * being played.
  114. *
  115. * @param[in] pitch tone/frequency to be stopped
  116. */
  117. void audio_stop_tone(float pitch);
  118. /**
  119. * @brief play a melody
  120. *
  121. * @details starts playback of a melody passed in from a SONG definition - an
  122. * array of {pitch, duration} float-tuples
  123. *
  124. * @param[in] np note-pointer to the SONG array
  125. * @param[in] n_count number of MUSICAL_NOTES of the SONG
  126. * @param[in] n_repeat false for onetime, true for looped playback
  127. */
  128. void audio_play_melody(float (*np)[][2], uint16_t n_count, bool n_repeat);
  129. /**
  130. * @brief play a short tone of a specific frequency to emulate a 'click'
  131. *
  132. * @details constructs a two-note melody (one pause plus a note) and plays it through
  133. * audio_play_melody. very short durations might not quite work due to
  134. * hardware limitations (DAC: added pulses from zero-crossing feature;...)
  135. *
  136. * @param[in] delay in milliseconds, length for the pause before the pulses, can be zero
  137. * @param[in] pitch
  138. * @param[in] duration in milliseconds, length of the 'click'
  139. */
  140. void audio_play_click(uint16_t delay, float pitch, uint16_t duration);
  141. /**
  142. * @brief stops all playback
  143. *
  144. * @details stops playback of both a melody as well as single tones, resetting
  145. * the internal state
  146. */
  147. void audio_stop_all(void);
  148. /**
  149. * @brief query if one/multiple tones are playing
  150. */
  151. bool audio_is_playing_note(void);
  152. /**
  153. * @brief query if a melody/SONG is playing
  154. */
  155. bool audio_is_playing_melody(void);
  156. // These macros are used to allow audio_play_melody to play an array of indeterminate
  157. // length. This works around the limitation of C's sizeof operation on pointers.
  158. // The global float array for the song must be used here.
  159. #define NOTE_ARRAY_SIZE(x) ((int16_t)(sizeof(x) / (sizeof(x[0]))))
  160. /**
  161. * @brief convenience macro, to play a melody/SONG once
  162. */
  163. #define PLAY_SONG(note_array) audio_play_melody(&note_array, NOTE_ARRAY_SIZE((note_array)), false)
  164. // TODO: a 'song' is a melody plus singing/vocals -> PLAY_MELODY
  165. /**
  166. * @brief convenience macro, to play a melody/SONG in a loop, until stopped by 'audio_stop_all'
  167. */
  168. #define PLAY_LOOP(note_array) audio_play_melody(&note_array, NOTE_ARRAY_SIZE((note_array)), true)
  169. // Tone-Multiplexing functions
  170. // this feature only makes sense for hardware setups which can't do proper
  171. // audio-wave synthesis = have no DAC and need to use PWM for tone generation
  172. #ifdef AUDIO_ENABLE_TONE_MULTIPLEXING
  173. # ifndef AUDIO_TONE_MULTIPLEXING_RATE_DEFAULT
  174. # define AUDIO_TONE_MULTIPLEXING_RATE_DEFAULT 0
  175. // 0=off, good starting value is 4; the lower the value the higher the cpu-load
  176. # endif
  177. void audio_set_tone_multiplexing_rate(uint16_t rate);
  178. void audio_enable_tone_multiplexing(void);
  179. void audio_disable_tone_multiplexing(void);
  180. void audio_increase_tone_multiplexing_rate(uint16_t change);
  181. void audio_decrease_tone_multiplexing_rate(uint16_t change);
  182. #endif
  183. // Tempo functions
  184. void audio_set_tempo(uint8_t tempo);
  185. void audio_increase_tempo(uint8_t tempo_change);
  186. void audio_decrease_tempo(uint8_t tempo_change);
  187. // conversion macros, from 64parts-to-a-beat to milliseconds and back
  188. uint16_t audio_duration_to_ms(uint16_t duration_bpm);
  189. uint16_t audio_ms_to_duration(uint16_t duration_ms);
  190. void audio_startup(void);
  191. // hardware interface
  192. // implementation in the driver_avr/arm_* respective parts
  193. void audio_driver_initialize_impl(void);
  194. void audio_driver_start_impl(void);
  195. void audio_driver_stop_impl(void);
  196. /**
  197. * @brief get the number of currently active tones
  198. * @return number, 0=none active
  199. */
  200. uint8_t audio_get_number_of_active_tones(void);
  201. /**
  202. * @brief access to the raw/unprocessed frequency for a specific tone
  203. * @details each active tone has a frequency associated with it, which
  204. * the internal state keeps track of, and is usually influenced
  205. * by various effects
  206. * @param[in] tone_index, ranging from 0 to number_of_active_tones-1, with the
  207. * first being the most recent and each increment yielding the next
  208. * older one
  209. * @return a positive frequency, in Hz; or zero if the tone is a pause
  210. */
  211. float audio_get_frequency(uint8_t tone_index);
  212. /**
  213. * @brief calculate and return the frequency for the requested tone
  214. * @details effects like glissando, vibrato, ... are post-processed onto the
  215. * each active tones 'base'-frequency; this function returns the
  216. * post-processed result.
  217. * @param[in] tone_index, ranging from 0 to number_of_active_tones-1, with the
  218. * first being the most recent and each increment yielding the next
  219. * older one
  220. * @return a positive frequency, in Hz; or zero if the tone is a pause
  221. */
  222. float audio_get_processed_frequency(uint8_t tone_index);
  223. /**
  224. * @brief update audio internal state: currently playing and active tones,...
  225. * @details This function is intended to be called by the audio-hardware
  226. * specific implementation on a somewhat regular basis while a SONG
  227. * or notes (pitch+duration) are playing to 'advance' the internal
  228. * state (current playing notes, position in the melody, ...)
  229. *
  230. * @return true if something changed in the currently active tones, which the
  231. * hardware might need to react to
  232. */
  233. bool audio_update_state(void);
  234. // legacy and back-warts compatibility stuff
  235. #define is_audio_on() audio_is_on()
  236. #define is_playing_notes() audio_is_playing_melody()
  237. #define is_playing_note() audio_is_playing_note()
  238. #define stop_all_notes() audio_stop_all()
  239. #define stop_note(f) audio_stop_tone(f)
  240. #define play_note(f, v) audio_play_tone(f)
  241. #define set_timbre(t) voice_set_timbre(t)
  242. #define set_tempo(t) audio_set_tempo(t)
  243. #define increase_tempo(t) audio_increase_tempo(t)
  244. #define decrease_tempo(t) audio_decrease_tempo(t)
  245. // vibrato functions are not used in any keyboards
  246. void audio_on_user(void);
  247. void audio_off_user(void);