implement export_tune

This commit is contained in:
twobit 2012-08-09 21:41:54 +02:00
parent bc7afe7a2d
commit 136eafed97
2 changed files with 62 additions and 14 deletions

View File

@ -21,26 +21,27 @@ static void fill_buffer(void* userdata, Uint8* stream, int len) {
// this parser is a complete hack // this parser is a complete hack
// put i can't care less right now // put i can't care less right now
static int wave_counter;
static int inst_counter;
static int pattern_counter;
static int parse_tune(const char* filename) { static int parse_tune(const char* filename) {
FILE* file = fopen(filename, "r"); FILE* file = fopen(filename, "r");
if(!file) return -1; if(!file) return -1;
wave_counter = 0;
inst_counter = 0;
pattern_counter = 0;
tune_length = 0;
char wave_names[256][256]; char wave_names[256][256];
memset(wave_names, 0, sizeof(wave_names));
int wave_counter = 0;
char inst_names[256][256]; char inst_names[256][256];
memset(inst_names, 0, sizeof(inst_names));
int inst_counter = 0;
char pattern_names[256][256]; char pattern_names[256][256];
memset(wave_names, 0, sizeof(wave_names));
memset(inst_names, 0, sizeof(inst_names));
memset(pattern_names, 0, sizeof(pattern_names)); memset(pattern_names, 0, sizeof(pattern_names));
int pattern_counter = 0;
tune_length = 0;
int state = 0; int state = 0;
@ -164,6 +165,53 @@ static int parse_tune(const char* filename) {
if(state =! 4) return line_nr; if(state =! 4) return line_nr;
fclose(file); fclose(file);
return 0;
}
static int export_tune(const char* filename) {
FILE* file = fopen(filename, "w");
if(!file) return -1;
fprintf(file, "uint8_t wave_table[][2] = {\n");
for(int i = 0; i < wave_counter; i++) {
fprintf(file, "\t{ %d, %d },\n", wave_table[i][0], wave_table[i][1]);
}
fprintf(file, "};\n\n");
fprintf(file, "synth_instrument_t instruments[] = {\n");
for(int i = 0; i < inst_counter; i++) {
fprintf(file, "\t{ %d, %d, %d, %d },\n",
instruments[i].pulse_width,
instruments[i].pulse_sweep,
instruments[i].decay,
instruments[i].wave_table_pos);
}
fprintf(file, "};\n\n");
fprintf(file, "uint8_t patterns[][pattern_length][2] = {\n");
for(int i = 0; i < pattern_counter; i++) {
fprintf(file, "\t{\n");
for(int j = 0; j < pattern_length; j++) {
fprintf(file, "\t\t{ %d, %d },\n", patterns[i][j][0], patterns[i][j][1]);
}
fprintf(file, "\t},\n");
}
fprintf(file, "};\n\n");
fprintf(file, "uint8_t pattern_table[][channel_count] = {\n");
for(int i = 0; i < tune_length; i++) {
fprintf(file, "\t{ ");
for(int j = 0; j < channel_count; j++) {
fprintf(file, "%d ", pattern_table[i][j]);
}
fprintf(file, "},\n");
}
fprintf(file, "};\n\n");
fprintf(file, "enum { pattern_table_length = %d };\n", tune_length);
fclose(file);
int size = wave_counter * 2 + int size = wave_counter * 2 +
inst_counter * sizeof(synth_instrument_t) + inst_counter * sizeof(synth_instrument_t) +
pattern_counter * pattern_length * 2 + pattern_counter * pattern_length * 2 +
@ -174,8 +222,6 @@ static int parse_tune(const char* filename) {
} }
int main(int argc, char** argv) { int main(int argc, char** argv) {
if(argc != 2) { if(argc != 2) {
printf("usage: %s tunefile\n", argv[0]); printf("usage: %s tunefile\n", argv[0]);
@ -188,6 +234,8 @@ int main(int argc, char** argv) {
fprintf(stderr, "%d: parsing error\n", error); fprintf(stderr, "%d: parsing error\n", error);
return 1; return 1;
} }
puts("exporting...");
export_tune("tune.h");
spec.callback = &fill_buffer; spec.callback = &fill_buffer;

View File

@ -3,8 +3,8 @@
#include "freq_table.h" #include "freq_table.h"
synth_instrument_t instruments[256];
uint8_t wave_table[256][2]; uint8_t wave_table[256][2];
synth_instrument_t instruments[256];
uint8_t patterns[256][pattern_length][2]; uint8_t patterns[256][pattern_length][2];
uint8_t pattern_table[256][channel_count]; uint8_t pattern_table[256][channel_count];
uint8_t tune_length; uint8_t tune_length;