From 06ba9e05c166c48a739683766f09c4abf242fb81 Mon Sep 17 00:00:00 2001 From: sowgro Date: Fri, 17 Oct 2025 22:01:03 -0400 Subject: Complete project --- project/Core/Inc/Activities/activity4.h | 8 +++++ project/Core/Inc/Homework/endian_converters.h | 5 +-- project/Core/Inc/Project/song_info.h | 19 ++++++++++ project/Core/Src/Activties/activity4.c | 13 ++----- project/Core/Src/Activties/activity8.txt | 2 +- project/Core/Src/Homework/endian_converters.c | 4 +++ project/Core/Src/Project/project.c | 18 +++++++++- project/Core/Src/Project/song_info.c | 50 +++++++++++++++++++++++++++ 8 files changed, 105 insertions(+), 14 deletions(-) create mode 100644 project/Core/Inc/Project/song_info.h create mode 100644 project/Core/Src/Project/song_info.c (limited to 'project/Core') diff --git a/project/Core/Inc/Activities/activity4.h b/project/Core/Inc/Activities/activity4.h index 6a1a93f..e3b677a 100644 --- a/project/Core/Inc/Activities/activity4.h +++ b/project/Core/Inc/Activities/activity4.h @@ -8,6 +8,14 @@ #ifndef INC_ACTIVITIES_ACTIVITY4_H_ #define INC_ACTIVITIES_ACTIVITY4_H_ +typedef struct { + char chunk_type[4]; + uint32_t length; + uint16_t format; + uint16_t ntrcks; + uint16_t division; +} header_t; + void activity4_run(); #endif /* INC_ACTIVITIES_ACTIVITY4_H_ */ diff --git a/project/Core/Inc/Homework/endian_converters.h b/project/Core/Inc/Homework/endian_converters.h index 27603ea..a4d959c 100644 --- a/project/Core/Inc/Homework/endian_converters.h +++ b/project/Core/Inc/Homework/endian_converters.h @@ -8,7 +8,8 @@ #ifndef INC_HOMEWORK_ENDIAN_CONVERTERS_H_ #define INC_HOMEWORK_ENDIAN_CONVERTERS_H_ -uint16_t convert_to_uint16 (uint8_t* p_value); -uint32_t convert_to_uint32 (uint8_t* p_value); +uint16_t convert_to_uint16(uint8_t* p_value); +uint32_t convert_to_uint24(uint8_t* p_value); +uint32_t convert_to_uint32(uint8_t* p_value); #endif /* INC_HOMEWORK_ENDIAN_CONVERTERS_H_ */ diff --git a/project/Core/Inc/Project/song_info.h b/project/Core/Inc/Project/song_info.h new file mode 100644 index 0000000..fbc5bc2 --- /dev/null +++ b/project/Core/Inc/Project/song_info.h @@ -0,0 +1,19 @@ +/* + * song_info.h + * + * Created on: Oct 15, 2025 + * Author: sowgro + */ + +#ifndef INC_PROJECT_SONG_INFO_H_ +#define INC_PROJECT_SONG_INFO_H_ + +typedef struct { + char *title; + char *copyright; + int tempo; +} song_info_t; + +song_info_t get_song_info(unsigned char *p_song); + +#endif /* INC_PROJECT_SONG_INFO_H_ */ diff --git a/project/Core/Src/Activties/activity4.c b/project/Core/Src/Activties/activity4.c index 44653e1..f3bd673 100644 --- a/project/Core/Src/Activties/activity4.c +++ b/project/Core/Src/Activties/activity4.c @@ -9,19 +9,12 @@ #include #include #include - -typedef struct { - char chunk_type[4]; - uint32_t length; - uint16_t format; - uint16_t ntrcks; - uint16_t division; -} header; +#include "activity4.h" void activity4_run() { - header* p_header; + header_t* p_header; - p_header = (header *) (get_song(0).p_song); + p_header = (header_t *) (get_song(0).p_song); p_header->length = convert_to_uint32((uint8_t*) &p_header->length); p_header->format = convert_to_uint16((uint8_t*) &p_header->format); diff --git a/project/Core/Src/Activties/activity8.txt b/project/Core/Src/Activties/activity8.txt index 0951319..2f85183 100644 --- a/project/Core/Src/Activties/activity8.txt +++ b/project/Core/Src/Activties/activity8.txt @@ -1,4 +1,4 @@ -parse song 8 for the tempo, title and copyright +parse song 0 for the tempo, title and copyright title: Twinkle Twinkle Little Star copyright: Jim Paterson diff --git a/project/Core/Src/Homework/endian_converters.c b/project/Core/Src/Homework/endian_converters.c index 1dbb5f7..724306c 100644 --- a/project/Core/Src/Homework/endian_converters.c +++ b/project/Core/Src/Homework/endian_converters.c @@ -11,6 +11,10 @@ uint16_t convert_to_uint16 (uint8_t* p_value) { return (p_value[0] << 8) + p_value[1]; } +uint32_t convert_to_uint24(uint8_t* p_value) { + return (p_value[0] << 16) + (p_value[1] << 8) + p_value[2]; +} + uint32_t convert_to_uint32 (uint8_t* p_value) { return (p_value[0] << 24) + (p_value[1] << 16) + (p_value[2] << 8) + p_value[3]; } diff --git a/project/Core/Src/Project/project.c b/project/Core/Src/Project/project.c index 379f380..1311219 100644 --- a/project/Core/Src/Project/project.c +++ b/project/Core/Src/Project/project.c @@ -11,6 +11,8 @@ #include #include "hw4.h" #include +#include "song_info.h" +#include "song.h" #define PLAYING_STATE (1) #define PAUSED_STATE (2) @@ -80,7 +82,21 @@ void help() { } void next() { - // TODO + static int current_song = -1; + current_song++; + if (current_song > 4) + current_song = 0; + + void *song = get_song(current_song).p_song; + song_info_t song_info = get_song_info(song); + + printf("Song #%i\r\n", current_song + 1); + if (song_info.title) + printf("Title: %s\r\n", song_info.title); + if (song_info.copyright) + printf("Copyright: %s\r\n", song_info.copyright); + if (song_info.tempo) + printf("Tempo: %i\r\n", song_info.tempo); } void play() { diff --git a/project/Core/Src/Project/song_info.c b/project/Core/Src/Project/song_info.c new file mode 100644 index 0000000..4d3f232 --- /dev/null +++ b/project/Core/Src/Project/song_info.c @@ -0,0 +1,50 @@ +/* + * song_info.c + * + * Created on: Oct 9, 2025 + * Author: sowgro + */ + +#include "song.h" +#include "activity4.h" +#include +#include "song_info.h" +#include "endian_converters.h" + +song_info_t get_song_info(uint8_t *p_song) { + song_info_t ret = {0, 0, 0}; +// header_t *header = (header_t *) p_song; + p_song += sizeof(header_t) - 2; // move pointer past header + p_song += 4; // move past MTrk label + + uint32_t MTrk_len = convert_to_uint32(p_song); // read in size of MTrk + + for (uint8_t *p_end = p_song + MTrk_len; p_song != p_end; p_song++) { + // FF 02 - copyright + if (convert_to_uint16(p_song) == 0xFF02) { + p_song += sizeof(uint16_t); + uint8_t ev_len = *(uint8_t *) p_song; + p_song += sizeof(ev_len); + ret.copyright = (char *) p_song; + ret.copyright[ev_len] = 0; + } + + // FF 03 - title + if (convert_to_uint16(p_song) == 0xFF03) { + p_song += sizeof(uint16_t); + uint8_t ev_len = *(uint8_t *) p_song; + p_song += sizeof(ev_len); + ret.title = (char *) p_song; + ret.title[ev_len] = 0; + } + + // FF 51 - tempo + if (convert_to_uint16(p_song) == 0xFF51) { + p_song += sizeof(uint16_t); + p_song += sizeof(uint8_t); // skip length, always 03 + ret.tempo = convert_to_uint24(p_song); + } + } + + return ret; +} -- cgit v1.2.3