audio.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include <stdint.h>
  2. #include <stdbool.h>
  3. #include <avr/io.h>
  4. #include <util/delay.h>
  5. #include "musical_notes.h"
  6. #include "song_list.h"
  7. #include "voices.h"
  8. #ifndef AUDIO_H
  9. #define AUDIO_H
  10. // Largely untested PWM audio mode (doesn't sound as good)
  11. // #define PWM_AUDIO
  12. // #define VIBRATO_ENABLE
  13. // Enable vibrato strength/amplitude - slows down ISR too much
  14. // #define VIBRATO_STRENGTH_ENABLE
  15. typedef union {
  16. uint8_t raw;
  17. struct {
  18. bool enable :1;
  19. uint8_t level :7;
  20. };
  21. } audio_config_t;
  22. bool is_audio_on(void);
  23. void audio_toggle(void);
  24. void audio_on(void);
  25. void audio_off(void);
  26. // Vibrato rate functions
  27. #ifdef VIBRATO_ENABLE
  28. void set_vibrato_rate(float rate);
  29. void increase_vibrato_rate(float change);
  30. void decrease_vibrato_rate(float change);
  31. #ifdef VIBRATO_STRENGTH_ENABLE
  32. void set_vibrato_strength(float strength);
  33. void increase_vibrato_strength(float change);
  34. void decrease_vibrato_strength(float change);
  35. #endif
  36. #endif
  37. // Polyphony functions
  38. void set_polyphony_rate(float rate);
  39. void enable_polyphony(void);
  40. void disable_polyphony(void);
  41. void increase_polyphony_rate(float change);
  42. void decrease_polyphony_rate(float change);
  43. void set_timbre(float timbre);
  44. void set_tempo(uint8_t tempo);
  45. void increase_tempo(uint8_t tempo_change);
  46. void decrease_tempo(uint8_t tempo_change);
  47. void audio_init(void);
  48. #ifdef PWM_AUDIO
  49. void play_sample(uint8_t * s, uint16_t l, bool r);
  50. #endif
  51. void play_note(float freq, int vol);
  52. void stop_note(float freq);
  53. void stop_all_notes(void);
  54. void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat, float n_rest);
  55. #define SCALE (int8_t []){ 0 + (12*0), 2 + (12*0), 4 + (12*0), 5 + (12*0), 7 + (12*0), 9 + (12*0), 11 + (12*0), \
  56. 0 + (12*1), 2 + (12*1), 4 + (12*1), 5 + (12*1), 7 + (12*1), 9 + (12*1), 11 + (12*1), \
  57. 0 + (12*2), 2 + (12*2), 4 + (12*2), 5 + (12*2), 7 + (12*2), 9 + (12*2), 11 + (12*2), \
  58. 0 + (12*3), 2 + (12*3), 4 + (12*3), 5 + (12*3), 7 + (12*3), 9 + (12*3), 11 + (12*3), \
  59. 0 + (12*4), 2 + (12*4), 4 + (12*4), 5 + (12*4), 7 + (12*4), 9 + (12*4), 11 + (12*4), }
  60. // These macros are used to allow play_notes to play an array of indeterminate
  61. // length. This works around the limitation of C's sizeof operation on pointers.
  62. // The global float array for the song must be used here.
  63. #define NOTE_ARRAY_SIZE(x) ((int16_t)(sizeof(x) / (sizeof(x[0]))))
  64. #define PLAY_NOTE_ARRAY(note_array, note_repeat, note_rest_style) play_notes(&note_array, NOTE_ARRAY_SIZE((note_array)), (note_repeat), (note_rest_style));
  65. bool is_playing_notes(void);
  66. void play_goodbye_tone(void);
  67. void play_startup_tone(void);
  68. void audio_on_user(void);
  69. void play_music_scale(void);
  70. #endif