audio.h 10 KB

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