summaryrefslogtreecommitdiff
path: root/project/Core/Src/Project/song_info.c
blob: 4d3f23230ccb26f2adc0a8c807009df4356df0f8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
 * song_info.c
 *
 *  Created on: Oct 9, 2025
 *      Author: sowgro
 */

#include "song.h"
#include "activity4.h"
#include <stdint.h>
#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;
}