audio.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. void audio_toggle(void);
  23. void audio_on(void);
  24. void audio_off(void);
  25. // Vibrato rate functions
  26. #ifdef VIBRATO_ENABLE
  27. void set_vibrato_rate(float rate);
  28. void increase_vibrato_rate(float change);
  29. void decrease_vibrato_rate(float change);
  30. #ifdef VIBRATO_STRENGTH_ENABLE
  31. void set_vibrato_strength(float strength);
  32. void increase_vibrato_strength(float change);
  33. void decrease_vibrato_strength(float change);
  34. #endif
  35. #endif
  36. // Polyphony functions
  37. void set_polyphony_rate(float rate);
  38. void enable_polyphony();
  39. void disable_polyphony();
  40. void increase_polyphony_rate(float change);
  41. void decrease_polyphony_rate(float change);
  42. void set_timbre(float timbre);
  43. void set_tempo(float tempo);
  44. void increase_tempo(uint8_t tempo_change);
  45. void decrease_tempo(uint8_t tempo_change);
  46. void audio_init();
  47. #ifdef PWM_AUDIO
  48. void play_sample(uint8_t * s, uint16_t l, bool r);
  49. #endif
  50. void play_note(float freq, int vol);
  51. void stop_note(float freq);
  52. void stop_all_notes(void);
  53. void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat, float n_rest);
  54. #define SCALE (int []){ 0 + (12*0), 2 + (12*0), 4 + (12*0), 5 + (12*0), 7 + (12*0), 9 + (12*0), 11 + (12*0), \
  55. 0 + (12*1), 2 + (12*1), 4 + (12*1), 5 + (12*1), 7 + (12*1), 9 + (12*1), 11 + (12*1), \
  56. 0 + (12*2), 2 + (12*2), 4 + (12*2), 5 + (12*2), 7 + (12*2), 9 + (12*2), 11 + (12*2), \
  57. 0 + (12*3), 2 + (12*3), 4 + (12*3), 5 + (12*3), 7 + (12*3), 9 + (12*3), 11 + (12*3), \
  58. 0 + (12*4), 2 + (12*4), 4 + (12*4), 5 + (12*4), 7 + (12*4), 9 + (12*4), 11 + (12*4), }
  59. // These macros are used to allow play_notes to play an array of indeterminate
  60. // length. This works around the limitation of C's sizeof operation on pointers.
  61. // The global float array for the song must be used here.
  62. #define NOTE_ARRAY_SIZE(x) ((int16_t)(sizeof(x) / (sizeof(x[0]))))
  63. #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));
  64. void play_goodbye_tone(void);
  65. void play_startup_tone(void);
  66. #endif