1 /* Copyright 2016 Jack Humbert
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 // -----------------------------------------------------------------------------
32 float frequency_alt
= 0;
36 float frequencies
[8] = {0, 0, 0, 0, 0, 0, 0, 0};
37 int volumes
[8] = {0, 0, 0, 0, 0, 0, 0, 0};
43 uint16_t sample_length
= 0;
45 bool playing_notes
= false;
46 bool playing_note
= false;
47 float note_frequency
= 0;
48 float note_length
= 0;
49 uint8_t note_tempo
= TEMPO_DEFAULT
;
50 float note_timbre
= TIMBRE_DEFAULT
;
51 uint16_t note_position
= 0;
52 float (*notes_pointer
)[][2];
55 bool note_resting
= false;
57 uint16_t current_note
= 0;
58 uint8_t rest_counter
= 0;
61 float vibrato_counter
= 0;
62 float vibrato_strength
= .5;
63 float vibrato_rate
= 0.125;
66 float polyphony_rate
= 0;
68 static bool audio_initialized
= false;
70 audio_config_t audio_config
;
72 uint16_t envelope_index
= 0;
73 bool glissando
= true;
76 # define STARTUP_SONG SONG(STARTUP_SOUND)
78 float startup_song
[][2] = STARTUP_SONG
;
80 static void gpt_cb8(GPTDriver
*gptp
);
82 #define DAC_BUFFER_SIZE 100
83 #ifndef DAC_SAMPLE_MAX
84 # define DAC_SAMPLE_MAX 65535U
87 #define START_CHANNEL_1() \
88 gptStart(&GPTD6, &gpt6cfg1); \
89 gptStartContinuous(&GPTD6, 2U)
90 #define START_CHANNEL_2() \
91 gptStart(&GPTD7, &gpt7cfg1); \
92 gptStartContinuous(&GPTD7, 2U)
93 #define STOP_CHANNEL_1() gptStopTimer(&GPTD6)
94 #define STOP_CHANNEL_2() gptStopTimer(&GPTD7)
95 #define RESTART_CHANNEL_1() \
98 #define RESTART_CHANNEL_2() \
101 #define UPDATE_CHANNEL_1_FREQ(freq) \
102 gpt6cfg1.frequency = freq * DAC_BUFFER_SIZE; \
104 #define UPDATE_CHANNEL_2_FREQ(freq) \
105 gpt7cfg1.frequency = freq * DAC_BUFFER_SIZE; \
107 #define GET_CHANNEL_1_FREQ (uint16_t)(gpt6cfg1.frequency * DAC_BUFFER_SIZE)
108 #define GET_CHANNEL_2_FREQ (uint16_t)(gpt7cfg1.frequency * DAC_BUFFER_SIZE)
111 * GPT6 configuration.
113 // static const GPTConfig gpt6cfg1 = {
114 // .frequency = 1000000U,
116 // .cr2 = TIM_CR2_MMS_1, /* MMS = 010 = TRGO on Update Event. */
120 GPTConfig gpt6cfg1
= {.frequency
= 440U * DAC_BUFFER_SIZE
,
122 .cr2
= TIM_CR2_MMS_1
, /* MMS = 010 = TRGO on Update Event. */
125 GPTConfig gpt7cfg1
= {.frequency
= 440U * DAC_BUFFER_SIZE
,
127 .cr2
= TIM_CR2_MMS_1
, /* MMS = 010 = TRGO on Update Event. */
130 GPTConfig gpt8cfg1
= {.frequency
= 10,
132 .cr2
= TIM_CR2_MMS_1
, /* MMS = 010 = TRGO on Update Event. */
136 * DAC test buffer (sine wave).
138 // static const dacsample_t dac_buffer[DAC_BUFFER_SIZE] = {
139 // 2047, 2082, 2118, 2154, 2189, 2225, 2260, 2296, 2331, 2367, 2402, 2437,
140 // 2472, 2507, 2542, 2576, 2611, 2645, 2679, 2713, 2747, 2780, 2813, 2846,
141 // 2879, 2912, 2944, 2976, 3008, 3039, 3070, 3101, 3131, 3161, 3191, 3221,
142 // 3250, 3278, 3307, 3335, 3362, 3389, 3416, 3443, 3468, 3494, 3519, 3544,
143 // 3568, 3591, 3615, 3637, 3660, 3681, 3703, 3723, 3744, 3763, 3782, 3801,
144 // 3819, 3837, 3854, 3870, 3886, 3902, 3917, 3931, 3944, 3958, 3970, 3982,
145 // 3993, 4004, 4014, 4024, 4033, 4041, 4049, 4056, 4062, 4068, 4074, 4078,
146 // 4082, 4086, 4089, 4091, 4092, 4093, 4094, 4093, 4092, 4091, 4089, 4086,
147 // 4082, 4078, 4074, 4068, 4062, 4056, 4049, 4041, 4033, 4024, 4014, 4004,
148 // 3993, 3982, 3970, 3958, 3944, 3931, 3917, 3902, 3886, 3870, 3854, 3837,
149 // 3819, 3801, 3782, 3763, 3744, 3723, 3703, 3681, 3660, 3637, 3615, 3591,
150 // 3568, 3544, 3519, 3494, 3468, 3443, 3416, 3389, 3362, 3335, 3307, 3278,
151 // 3250, 3221, 3191, 3161, 3131, 3101, 3070, 3039, 3008, 2976, 2944, 2912,
152 // 2879, 2846, 2813, 2780, 2747, 2713, 2679, 2645, 2611, 2576, 2542, 2507,
153 // 2472, 2437, 2402, 2367, 2331, 2296, 2260, 2225, 2189, 2154, 2118, 2082,
154 // 2047, 2012, 1976, 1940, 1905, 1869, 1834, 1798, 1763, 1727, 1692, 1657,
155 // 1622, 1587, 1552, 1518, 1483, 1449, 1415, 1381, 1347, 1314, 1281, 1248,
156 // 1215, 1182, 1150, 1118, 1086, 1055, 1024, 993, 963, 933, 903, 873,
157 // 844, 816, 787, 759, 732, 705, 678, 651, 626, 600, 575, 550,
158 // 526, 503, 479, 457, 434, 413, 391, 371, 350, 331, 312, 293,
159 // 275, 257, 240, 224, 208, 192, 177, 163, 150, 136, 124, 112,
160 // 101, 90, 80, 70, 61, 53, 45, 38, 32, 26, 20, 16,
161 // 12, 8, 5, 3, 2, 1, 0, 1, 2, 3, 5, 8,
162 // 12, 16, 20, 26, 32, 38, 45, 53, 61, 70, 80, 90,
163 // 101, 112, 124, 136, 150, 163, 177, 192, 208, 224, 240, 257,
164 // 275, 293, 312, 331, 350, 371, 391, 413, 434, 457, 479, 503,
165 // 526, 550, 575, 600, 626, 651, 678, 705, 732, 759, 787, 816,
166 // 844, 873, 903, 933, 963, 993, 1024, 1055, 1086, 1118, 1150, 1182,
167 // 1215, 1248, 1281, 1314, 1347, 1381, 1415, 1449, 1483, 1518, 1552, 1587,
168 // 1622, 1657, 1692, 1727, 1763, 1798, 1834, 1869, 1905, 1940, 1976, 2012
171 // static const dacsample_t dac_buffer_2[DAC_BUFFER_SIZE] = {
172 // 12, 8, 5, 3, 2, 1, 0, 1, 2, 3, 5, 8,
173 // 12, 16, 20, 26, 32, 38, 45, 53, 61, 70, 80, 90,
174 // 101, 112, 124, 136, 150, 163, 177, 192, 208, 224, 240, 257,
175 // 275, 293, 312, 331, 350, 371, 391, 413, 434, 457, 479, 503,
176 // 526, 550, 575, 600, 626, 651, 678, 705, 732, 759, 787, 816,
177 // 844, 873, 903, 933, 963, 993, 1024, 1055, 1086, 1118, 1150, 1182,
178 // 1215, 1248, 1281, 1314, 1347, 1381, 1415, 1449, 1483, 1518, 1552, 1587,
179 // 1622, 1657, 1692, 1727, 1763, 1798, 1834, 1869, 1905, 1940, 1976, 2012,
180 // 2047, 2082, 2118, 2154, 2189, 2225, 2260, 2296, 2331, 2367, 2402, 2437,
181 // 2472, 2507, 2542, 2576, 2611, 2645, 2679, 2713, 2747, 2780, 2813, 2846,
182 // 2879, 2912, 2944, 2976, 3008, 3039, 3070, 3101, 3131, 3161, 3191, 3221,
183 // 3250, 3278, 3307, 3335, 3362, 3389, 3416, 3443, 3468, 3494, 3519, 3544,
184 // 3568, 3591, 3615, 3637, 3660, 3681, 3703, 3723, 3744, 3763, 3782, 3801,
185 // 3819, 3837, 3854, 3870, 3886, 3902, 3917, 3931, 3944, 3958, 3970, 3982,
186 // 3993, 4004, 4014, 4024, 4033, 4041, 4049, 4056, 4062, 4068, 4074, 4078,
187 // 4082, 4086, 4089, 4091, 4092, 4093, 4094, 4093, 4092, 4091, 4089, 4086,
188 // 4082, 4078, 4074, 4068, 4062, 4056, 4049, 4041, 4033, 4024, 4014, 4004,
189 // 3993, 3982, 3970, 3958, 3944, 3931, 3917, 3902, 3886, 3870, 3854, 3837,
190 // 3819, 3801, 3782, 3763, 3744, 3723, 3703, 3681, 3660, 3637, 3615, 3591,
191 // 3568, 3544, 3519, 3494, 3468, 3443, 3416, 3389, 3362, 3335, 3307, 3278,
192 // 3250, 3221, 3191, 3161, 3131, 3101, 3070, 3039, 3008, 2976, 2944, 2912,
193 // 2879, 2846, 2813, 2780, 2747, 2713, 2679, 2645, 2611, 2576, 2542, 2507,
194 // 2472, 2437, 2402, 2367, 2331, 2296, 2260, 2225, 2189, 2154, 2118, 2082,
195 // 2047, 2012, 1976, 1940, 1905, 1869, 1834, 1798, 1763, 1727, 1692, 1657,
196 // 1622, 1587, 1552, 1518, 1483, 1449, 1415, 1381, 1347, 1314, 1281, 1248,
197 // 1215, 1182, 1150, 1118, 1086, 1055, 1024, 993, 963, 933, 903, 873,
198 // 844, 816, 787, 759, 732, 705, 678, 651, 626, 600, 575, 550,
199 // 526, 503, 479, 457, 434, 413, 391, 371, 350, 331, 312, 293,
200 // 275, 257, 240, 224, 208, 192, 177, 163, 150, 136, 124, 112,
201 // 101, 90, 80, 70, 61, 53, 45, 38, 32, 26, 20, 16
205 static const dacsample_t dac_buffer
[DAC_BUFFER_SIZE
] = {
206 // First half is max, second half is 0
207 [0 ... DAC_BUFFER_SIZE
/ 2 - 1] = DAC_SAMPLE_MAX
,
208 [DAC_BUFFER_SIZE
/ 2 ... DAC_BUFFER_SIZE
- 1] = 0,
212 static const dacsample_t dac_buffer_2
[DAC_BUFFER_SIZE
] = {
213 // opposite of dac_buffer above
214 [0 ... DAC_BUFFER_SIZE
/ 2 - 1] = 0,
215 [DAC_BUFFER_SIZE
/ 2 ... DAC_BUFFER_SIZE
- 1] = DAC_SAMPLE_MAX
,
219 * DAC streaming callback.
222 static void end_cb1(DACDriver
*dacp
) {
226 if ((nz
% 1000) == 0) {
227 // palTogglePad(GPIOD, GPIOD_LED3);
232 * DAC error callback.
234 static void error_cb1(DACDriver
*dacp
, dacerror_t err
) {
238 chSysHalt("DAC failure");
241 static const DACConfig dac1cfg1
= {.init
= DAC_SAMPLE_MAX
, .datamode
= DAC_DHRM_12BIT_RIGHT
};
243 static const DACConversionGroup dacgrpcfg1
= {.num_channels
= 1U, .end_cb
= end_cb1
, .error_cb
= error_cb1
, .trigger
= DAC_TRG(0)};
245 static const DACConfig dac1cfg2
= {.init
= DAC_SAMPLE_MAX
, .datamode
= DAC_DHRM_12BIT_RIGHT
};
247 static const DACConversionGroup dacgrpcfg2
= {.num_channels
= 1U, .end_cb
= end_cb1
, .error_cb
= error_cb1
, .trigger
= DAC_TRG(0)};
250 if (audio_initialized
) {
256 if (!eeconfig_is_enabled()) {
259 audio_config
.raw
= eeconfig_read_audio();
261 audio_config
.enable
= true;
262 # ifdef AUDIO_CLICKY_ON
263 audio_config
.clicky_enable
= true;
268 * Starting DAC1 driver, setting up the output pin as analog as suggested
269 * by the Reference Manual.
271 palSetPadMode(GPIOA
, 4, PAL_MODE_INPUT_ANALOG
);
272 palSetPadMode(GPIOA
, 5, PAL_MODE_INPUT_ANALOG
);
273 dacStart(&DACD1
, &dac1cfg1
);
274 dacStart(&DACD2
, &dac1cfg2
);
277 * Starting GPT6/7 driver, it is used for triggering the DAC.
283 * Starting a continuous conversion.
285 dacStartConversion(&DACD1
, &dacgrpcfg1
, (dacsample_t
*)dac_buffer
, DAC_BUFFER_SIZE
);
286 dacStartConversion(&DACD2
, &dacgrpcfg2
, (dacsample_t
*)dac_buffer_2
, DAC_BUFFER_SIZE
);
288 audio_initialized
= true;
290 if (audio_config
.enable
) {
291 PLAY_SONG(startup_song
);
297 void stop_all_notes() {
298 dprintf("audio stop all notes");
300 if (!audio_initialized
) {
305 gptStopTimer(&GPTD6
);
306 gptStopTimer(&GPTD7
);
307 gptStopTimer(&GPTD8
);
309 playing_notes
= false;
310 playing_note
= false;
315 for (uint8_t i
= 0; i
< 8; i
++) {
321 void stop_note(float freq
) {
322 dprintf("audio stop note freq=%d", (int)freq
);
325 if (!audio_initialized
) {
328 for (int i
= 7; i
>= 0; i
--) {
329 if (frequencies
[i
] == freq
) {
332 for (int j
= i
; (j
< 7); j
++) {
333 frequencies
[j
] = frequencies
[j
+ 1];
334 frequencies
[j
+ 1] = 0;
335 volumes
[j
] = volumes
[j
+ 1];
345 if (voice_place
>= voices
) {
351 gptStopTimer(&GPTD8
);
355 playing_note
= false;
360 #ifdef VIBRATO_ENABLE
362 float mod(float a
, int b
) {
363 float r
= fmod(a
, b
);
364 return r
< 0 ? r
+ b
: r
;
367 float vibrato(float average_freq
) {
368 # ifdef VIBRATO_STRENGTH_ENABLE
369 float vibrated_freq
= average_freq
* pow(vibrato_lut
[(int)vibrato_counter
], vibrato_strength
);
371 float vibrated_freq
= average_freq
* vibrato_lut
[(int)vibrato_counter
];
373 vibrato_counter
= mod((vibrato_counter
+ vibrato_rate
* (1.0 + 440.0 / average_freq
)), VIBRATO_LUT_LENGTH
);
374 return vibrated_freq
;
379 static void gpt_cb8(GPTDriver
*gptp
) {
386 if (polyphony_rate
== 0) {
388 if (frequency_alt
!= 0 && frequency_alt
< frequencies
[voices
- 2] && frequency_alt
< frequencies
[voices
- 2] * pow(2, -440 / frequencies
[voices
- 2] / 12 / 2)) {
389 frequency_alt
= frequency_alt
* pow(2, 440 / frequency_alt
/ 12 / 2);
390 } else if (frequency_alt
!= 0 && frequency_alt
> frequencies
[voices
- 2] && frequency_alt
> frequencies
[voices
- 2] * pow(2, 440 / frequencies
[voices
- 2] / 12 / 2)) {
391 frequency_alt
= frequency_alt
* pow(2, -440 / frequency_alt
/ 12 / 2);
393 frequency_alt
= frequencies
[voices
- 2];
396 frequency_alt
= frequencies
[voices
- 2];
399 #ifdef VIBRATO_ENABLE
400 if (vibrato_strength
> 0) {
401 freq_alt
= vibrato(frequency_alt
);
403 freq_alt
= frequency_alt
;
406 freq_alt
= frequency_alt
;
410 if (envelope_index
< 65535) {
414 freq_alt
= voice_envelope(freq_alt
);
416 if (freq_alt
< 30.517578125) {
420 if (GET_CHANNEL_2_FREQ
!= (uint16_t)freq_alt
) {
421 UPDATE_CHANNEL_2_FREQ(freq_alt
);
428 if (polyphony_rate
> 0) {
430 voice_place
%= voices
;
431 if (place
++ > (frequencies
[voice_place
] / polyphony_rate
)) {
432 voice_place
= (voice_place
+ 1) % voices
;
437 #ifdef VIBRATO_ENABLE
438 if (vibrato_strength
> 0) {
439 freq
= vibrato(frequencies
[voice_place
]);
441 freq
= frequencies
[voice_place
];
444 freq
= frequencies
[voice_place
];
448 if (frequency
!= 0 && frequency
< frequencies
[voices
- 1] && frequency
< frequencies
[voices
- 1] * pow(2, -440 / frequencies
[voices
- 1] / 12 / 2)) {
449 frequency
= frequency
* pow(2, 440 / frequency
/ 12 / 2);
450 } else if (frequency
!= 0 && frequency
> frequencies
[voices
- 1] && frequency
> frequencies
[voices
- 1] * pow(2, 440 / frequencies
[voices
- 1] / 12 / 2)) {
451 frequency
= frequency
* pow(2, -440 / frequency
/ 12 / 2);
453 frequency
= frequencies
[voices
- 1];
456 frequency
= frequencies
[voices
- 1];
459 #ifdef VIBRATO_ENABLE
460 if (vibrato_strength
> 0) {
461 freq
= vibrato(frequency
);
470 if (envelope_index
< 65535) {
474 freq
= voice_envelope(freq
);
476 if (freq
< 30.517578125) {
480 if (GET_CHANNEL_1_FREQ
!= (uint16_t)freq
) {
481 UPDATE_CHANNEL_1_FREQ(freq
);
490 if (note_frequency
> 0) {
491 #ifdef VIBRATO_ENABLE
492 if (vibrato_strength
> 0) {
493 freq
= vibrato(note_frequency
);
495 freq
= note_frequency
;
498 freq
= note_frequency
;
501 if (envelope_index
< 65535) {
504 freq
= voice_envelope(freq
);
506 if (GET_CHANNEL_1_FREQ
!= (uint16_t)freq
) {
507 UPDATE_CHANNEL_1_FREQ(freq
);
508 UPDATE_CHANNEL_2_FREQ(freq
);
512 // gptStopTimer(&GPTD6);
513 // gptStopTimer(&GPTD7);
517 bool end_of_note
= false;
518 if (GET_CHANNEL_1_FREQ
> 0) {
520 end_of_note
= (note_position
>= (note_length
* 8 - 1));
522 end_of_note
= (note_position
>= (note_length
* 8));
524 end_of_note
= (note_position
>= (note_length
* 8));
529 if (current_note
>= notes_count
) {
535 // gptStopTimer(&GPTD8);
536 playing_notes
= false;
543 if ((*notes_pointer
)[current_note
][0] == (*notes_pointer
)[current_note
+ 1][0]) {
547 note_frequency
= (*notes_pointer
)[current_note
][0];
551 note_resting
= false;
553 note_frequency
= (*notes_pointer
)[current_note
][0];
554 note_length
= ((*notes_pointer
)[current_note
][1] / 4) * (((float)note_tempo
) / 100);
561 if (!audio_config
.enable
) {
562 playing_notes
= false;
563 playing_note
= false;
567 void play_note(float freq
, int vol
) {
568 dprintf("audio play note freq=%d vol=%d", (int)freq
, vol
);
570 if (!audio_initialized
) {
574 if (audio_config
.enable
&& voices
< 8) {
575 // Cancel notes if notes are playing
585 frequencies
[voices
] = freq
;
586 volumes
[voices
] = vol
;
590 gptStart(&GPTD8
, &gpt8cfg1
);
591 gptStartContinuous(&GPTD8
, 2U);
597 void play_notes(float (*np
)[][2], uint16_t n_count
, bool n_repeat
) {
598 if (!audio_initialized
) {
602 if (audio_config
.enable
) {
603 // Cancel note if a note is playing
608 playing_notes
= true;
611 notes_count
= n_count
;
612 notes_repeat
= n_repeat
;
617 note_frequency
= (*notes_pointer
)[current_note
][0];
618 note_length
= ((*notes_pointer
)[current_note
][1] / 4) * (((float)note_tempo
) / 100);
621 gptStart(&GPTD8
, &gpt8cfg1
);
622 gptStartContinuous(&GPTD8
, 2U);
628 bool is_playing_notes(void) { return playing_notes
; }
630 bool is_audio_on(void) { return (audio_config
.enable
!= 0); }
632 void audio_toggle(void) {
633 audio_config
.enable
^= 1;
634 eeconfig_update_audio(audio_config
.raw
);
635 if (audio_config
.enable
) {
640 void audio_on(void) {
641 audio_config
.enable
= 1;
642 eeconfig_update_audio(audio_config
.raw
);
646 void audio_off(void) {
648 audio_config
.enable
= 0;
649 eeconfig_update_audio(audio_config
.raw
);
652 #ifdef VIBRATO_ENABLE
654 // Vibrato rate functions
656 void set_vibrato_rate(float rate
) { vibrato_rate
= rate
; }
658 void increase_vibrato_rate(float change
) { vibrato_rate
*= change
; }
660 void decrease_vibrato_rate(float change
) { vibrato_rate
/= change
; }
662 # ifdef VIBRATO_STRENGTH_ENABLE
664 void set_vibrato_strength(float strength
) { vibrato_strength
= strength
; }
666 void increase_vibrato_strength(float change
) { vibrato_strength
*= change
; }
668 void decrease_vibrato_strength(float change
) { vibrato_strength
/= change
; }
670 # endif /* VIBRATO_STRENGTH_ENABLE */
672 #endif /* VIBRATO_ENABLE */
674 // Polyphony functions
676 void set_polyphony_rate(float rate
) { polyphony_rate
= rate
; }
678 void enable_polyphony() { polyphony_rate
= 5; }
680 void disable_polyphony() { polyphony_rate
= 0; }
682 void increase_polyphony_rate(float change
) { polyphony_rate
*= change
; }
684 void decrease_polyphony_rate(float change
) { polyphony_rate
/= change
; }
688 void set_timbre(float timbre
) { note_timbre
= timbre
; }
692 void set_tempo(uint8_t tempo
) { note_tempo
= tempo
; }
694 void decrease_tempo(uint8_t tempo_change
) { note_tempo
+= tempo_change
; }
696 void increase_tempo(uint8_t tempo_change
) {
697 if (note_tempo
- tempo_change
< 10) {
700 note_tempo
-= tempo_change
;