After loading the saved pattern and track, I tried to play it but my program got crashed.
Below is my code.
#include “main_app.h”
void SaveFile(TUniSynthMusic music, char path) {
printf(“Save file to: %sn”, path);
FILE *file = fopen(path, "wb");
if (!file) {
printf("Unable to open file for writing: %sn", path);
return;
}
// Write the basic TUniSynthMusic structure
fwrite(music, sizeof(TUniSynthMusic), 1, file);
// Write the patterns
for (int i = 0; i < MUSIC_MAX_PATTERNS; i++) {
fwrite(&music->patterns[i], sizeof(TUniSynthPattern), 1, file);
}
// Write the tracks and their pattern pointers
for (int i = 0; i < MUSIC_MAX_TRACKS; i++) {
fwrite(&music->tracks[i], sizeof(TUniSynthMusicTrack), 1, file);
for (int j = 0; j < MUSIC_MAX_PATTERNS_PER_TRACK; j++) {
if (music->tracks[i].patternPtrs[j] != NULL) {
int index = music->tracks[i].patternPtrs[j] - music->patterns;
fwrite(&index, sizeof(int), 1, file);
} else {
int index = -1;
fwrite(&index, sizeof(int), 1, file);
}
}
}
fclose(file);
}
void LoadFile(TUniSynthMusic music, char path) {
printf(“Load file from: %sn”, path);
FILE *file = fopen(path, "rb");
if (!file) {
printf("Unable to open file for reading: %sn", path);
return;
}
// Read the basic TUniSynthMusic structure
fread(music, sizeof(TUniSynthMusic), 1, file);
// Read the patterns
for (int i = 0; i < MUSIC_MAX_PATTERNS; i++) {
fread(&music->patterns[i], sizeof(TUniSynthPattern), 1, file);
}
// Read the tracks and their pattern pointers
for (int i = 0; i < MUSIC_MAX_TRACKS; i++) {
fread(&music->tracks[i], sizeof(TUniSynthMusicTrack), 1, file);
for (int j = 0; j < MUSIC_MAX_PATTERNS_PER_TRACK; j++) {
int index;
fread(&index, sizeof(int), 1, file);
if (index != -1) {
music->tracks[i].patternPtrs[j] = &music->patterns[index];
} else {
music->tracks[i].patternPtrs[j] = NULL;
}
}
}
fclose(file);
}
Any idea or suggestions from the experts.
New contributor
Lui Sengi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.