diff options
Diffstat (limited to 'project/Core/Src/Project')
| -rw-r--r-- | project/Core/Src/Project/parser.c | 6 | ||||
| -rw-r--r-- | project/Core/Src/Project/tone.c | 54 |
2 files changed, 34 insertions, 26 deletions
diff --git a/project/Core/Src/Project/parser.c b/project/Core/Src/Project/parser.c index 55a8a53..82d0ce7 100644 --- a/project/Core/Src/Project/parser.c +++ b/project/Core/Src/Project/parser.c @@ -55,7 +55,11 @@ void parser_play_loop() { case NOTE_OFF_EVENT: remove_tone(curEvent.key_number); break; case NOTE_ON_EVENT: - add_tone(curEvent.key_number, curEvent.value); break; + if (curEvent.value != 0) { + add_tone(curEvent.key_number, curEvent.value); break; + } else { + remove_tone(curEvent.key_number); break; + } } tracks[i].curEventIndex++; } diff --git a/project/Core/Src/Project/tone.c b/project/Core/Src/Project/tone.c index 7adea60..f43127b 100644 --- a/project/Core/Src/Project/tone.c +++ b/project/Core/Src/Project/tone.c @@ -9,7 +9,7 @@ #include "tone.h" #define CPU_SPEED (80000000.0) -#define MAX_TONES (1) +#define MAX_TONES (10) static float notes[] = { /* Octave -1 */ 8.18, 8.66, 9.18, 9.73, 10.30, 10.92, 11.56, 12.25, 12.98, 13.75, 14.57, 13.44, @@ -31,7 +31,8 @@ typedef struct { static uint32_t counter = 0; -static tone_info tones[MAX_TONES] = {{0,0}}; +static tone_info NULL_TONE = {0,0}; +static tone_info tones[MAX_TONES]; /* You will want to integrate this with your existing one but it is here for standalone testing @@ -42,6 +43,12 @@ static tone_info tones[MAX_TONES] = {{0,0}}; uint32_t get_tick_speed (); +void tones_init() { + for (int i = 0; i < MAX_TONES; i++) { + tones[i] = NULL_TONE; + } +} + // Convert from hertz to ticks uint32_t hertz_to_systicks (float hertz) { @@ -63,39 +70,36 @@ void reset_counter () { // Add a tone to the tones array uint8_t add_tone (uint8_t note, uint8_t velocity) { - tones [0] = (tone_info){hertz_to_systicks (notes [note]), velocity}; + for (int i = 0; i < MAX_TONES; i++) { + if (tones[i].note == NULL_TONE.note) { + tones[i] = (tone_info){note, velocity}; + return 1; + } + } return 0; } // Remove a tone from the tones array uint8_t remove_tone (uint8_t note) { - tones [0].note = hertz_to_systicks (notes [note]); - tones [0].power = 0; + for (int i = 0; i < MAX_TONES; i++) { + if (tones[i].note == note) { + tones[i] = NULL_TONE; + return 1; + } + } return 0; } // Play any tones in the array void play_tones () { -// int power_sum = 0; -// for (int i = 0; i < MAX_TONES; i++) { -// uint32_t rollover = tones[i].note; -//// printf("%i\r\n", tones[i].note); -// if ((systick_get_count () % rollover) < (rollover >> 1)) { -// power_sum += 500; -// } -// } -// DAC_Set_Value(power_sum); -// printf("called %i\r\n", power_sum); - - uint32_t rollover = tones[0].note; - if ((systick_get_count () % rollover) < (rollover >> 1)) { - DAC_Set_Value(tones[0].power * 10); -// printf ("1"); - } - else { - DAC_Set_Value(0); -// printf ("0"); - } + int power_sum = 0; + for (int i = 0; i < MAX_TONES; i++) { + uint32_t rollover = hertz_to_systicks (notes [tones[i].note]); + if ((systick_get_count () % rollover) < (rollover >> 1)) { + power_sum += tones[i].power * 10; + } + } + DAC_Set_Value(power_sum); } |
