From 80af680e18ea3db3b0e9a2f0da9b02cbba480c1e Mon Sep 17 00:00:00 2001 From: Thomas White Date: Mon, 14 Nov 2011 23:15:14 +0100 Subject: Note handling --- Makefile | 2 +- maestropond.c | 124 ++++++++++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 96 insertions(+), 30 deletions(-) diff --git a/Makefile b/Makefile index facb0e4..11d40f5 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ OBJS=maestropond.o maestropond: ${OBJS} - gcc -g ${OBJS} -o maestropond + gcc -g ${OBJS} -o maestropond -lm maestropond.o: maestropond.c gcc -g -W -Wall -c maestropond.c -o maestropond.o diff --git a/maestropond.c b/maestropond.c index 7fbbfc0..5250083 100644 --- a/maestropond.c +++ b/maestropond.c @@ -20,6 +20,7 @@ #include #include #include +#include static const int bpm[] = { @@ -70,7 +71,7 @@ out: } -static void music_attribute(unsigned char ma) +static void music_attribute(unsigned char ma, FILE *ofh) { if ( (ma & 0x7f) == 0x40 ) { @@ -78,17 +79,13 @@ static void music_attribute(unsigned char ma) } else if ( (ma & 0x3f) == 0x20 ) { - printf("Bar line\n"); + fprintf(ofh, "|\n ");; } else if ( (ma & 0x1f) == 0x10 ) { - printf("Bar line\n"); - - } else if ( (ma & 0xf) == 0x8 ) { - printf("Octave shift\n"); - } else if ( (ma & 0x7) == 0x4 ) { + } else if ( (ma & 0xf) == 0x8 ) { int st; @@ -99,7 +96,8 @@ static void music_attribute(unsigned char ma) printf("Slur off (stave %i)\n", st); } - } else if ( (ma & 0x3) == 0x2 ) { + + } else if ( (ma & 0x7) == 0x4 ) { int ct, st; @@ -107,38 +105,85 @@ static void music_attribute(unsigned char ma) switch ( ct ) { case 0 : - printf("Treble clef:"); + fprintf(ofh, "\\clef \"treble\"\n"); break; case 1 : - printf("Alto clef:"); + fprintf(ofh, "\\clef \"treble\"\n"); break; case 2 : - printf("Tenor clef:"); + fprintf(ofh, "\\clef \"treble\"\n"); break; case 3 : - printf("Bass clef:"); + fprintf(ofh, "\\clef \"treble\"\n"); break; } st = (ma & 0x60) >> 5; printf(" stave %i\n", st); + } else if ( (ma & 0x3) == 0x2 ) { + + } else if ( (ma & 0x1) == 0x1 ) { int tn, td; tn = 1 + ((ma & 0x1e) >> 1); td = 1 + ((ma & 0xe0) >> 5); - printf("Time signature %i/%i\n", tn, td); + fprintf(ofh, "\\time %i/%i\n", tn, td); } } +static const char *note_letter(int pos, int acc) +{ + switch ( pos ) { + + case 0 : return "a"; + case 1 : return "b"; + case 2 : return "c"; + case 4 : return "d"; + case 5 : return "e"; + case 6 : return "f"; + case 7 : return "g"; + case 8 : return "a'"; + case 9 : return "b'"; + case 10 : return "c'"; + case 11 : return "d'"; + case 12 : return "e'"; + case 13 : return "f'"; + case 14 : return "g'"; + case 15 : return "a''"; + + case 16 : return "b''"; + + case 17 : return "c''"; + case 18 : return "d''"; + case 19 : return "e''"; + case 20 : return "f''"; + case 21 : return "g''"; + case 22 : return "a'''"; + case 23 : return "b'''"; + case 24 : return "c'''"; + case 25 : return "d'''"; + case 26 : return "e'''"; + case 27 : return "f'''"; + case 28 : return "g'''"; + case 29 : return "a''''"; + case 30 : return "b''''"; + case 31 : return "c''''"; + + } + + return "?"; +} -static void get_note(unsigned char **notes, int *nptrs, int ch) + + +static void get_note(unsigned char **notes, int *nptrs, int ch, FILE *ofh) { unsigned char n1, n2; int rest = 0; @@ -152,15 +197,19 @@ static void get_note(unsigned char **notes, int *nptrs, int ch) } if ( rest ) { - printf("rest %i\n", n1); + printf(" %i", n1); } else { - printf("note %i:%i\n", n1, n2); + int pos, acc, len; + pos = (n1 & 0xf8) >> 3; + acc = n2 & 0x07; + len = (n2 & 0xe0) >> 5; + fprintf(ofh, "%s%i ", note_letter(pos, acc), (int)pow(2, len)); } } static void interpret_gates(unsigned char *gates, unsigned char **notes, - int n_gates) + int n_gates, FILE *ofh) { int i; int ma = 0; @@ -168,10 +217,11 @@ static void interpret_gates(unsigned char *gates, unsigned char **notes, for ( i=0; i<8; i++ ) nptrs[i] = 0; + fprintf(ofh, "{\n "); for ( i=0; i "); } } + fprintf(ofh, "\n}\n"); } -static size_t process_music_data(unsigned char *f, size_t ptr, size_t len) +static size_t process_music_data(unsigned char *f, size_t ptr, FILE *ofh, + size_t len, int staff) { unsigned int n_gates; unsigned int i; @@ -232,7 +286,7 @@ static size_t process_music_data(unsigned char *f, size_t ptr, size_t len) } } - interpret_gates(gates, notes, n_gates); + interpret_gates(gates, notes, n_gates, ofh); return ptr; } @@ -308,6 +362,7 @@ static void convert_file(const char *filename) { struct stat statbuf; FILE *fh; + FILE *ofh; unsigned char *f; size_t r, ptr; @@ -339,6 +394,17 @@ static void convert_file(const char *filename) } fclose(fh); + ofh = fopen("maestropond.ly", "w"); + if ( ofh == NULL ) { + fprintf(stderr, "Failed to open output file.\n"); + return; + } + fprintf(ofh, "\\version \"2.14.2\"\n"); + fprintf(ofh, "\\header {\n"); + fprintf(ofh, " title = \"%s\"\n", filename); + fprintf(ofh, " composer = \"Unknown\"\n"); + fprintf(ofh, "}\n"); + if ( memcmp(f, "Maestro\n", 8) != 0 ) { fprintf(stderr, "Not a Maestro file.\n"); free(f); @@ -356,7 +422,7 @@ static void convert_file(const char *filename) switch ( f[ptr++] ) { case 1 : - ptr = process_music_data(f, ptr, r); + ptr = process_music_data(f, ptr, ofh, r); break; case 2 : -- cgit v1.2.3