audio.h 10 KB

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