From 49ce677d26d938db08a7c6994d3cf0ae92a3d1c2 Mon Sep 17 00:00:00 2001 From: Thomas White Date: Fri, 18 Nov 2011 09:00:45 +0100 Subject: Restructuring --- Makefile | 2 +- maestropond.c | 201 +++++++++++++++++++++++++++++++++------------------------- 2 files changed, 117 insertions(+), 86 deletions(-) diff --git a/Makefile b/Makefile index 11d40f5..5bb6003 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ maestropond: ${OBJS} gcc -g ${OBJS} -o maestropond -lm maestropond.o: maestropond.c - gcc -g -W -Wall -c maestropond.c -o maestropond.o + gcc -g -W -Wall -Wno-unused-parameter -c maestropond.c -o maestropond.o clean: rm -f ${OBJS} maestropond diff --git a/maestropond.c b/maestropond.c index 7504ac7..fe575bd 100644 --- a/maestropond.c +++ b/maestropond.c @@ -22,10 +22,54 @@ #include #include +enum note_tone +{ + NOTE_A, + NOTE_B, + NOTE_C, + NOTE_D, + NOTE_E, + NOTE_F, + NOTE_G, + NOTE_SILENCE +}; + +enum note_semitone +{ + SEMI_SHARPSHARP, + SEMI_SHARP, + SEMI_FLAT, + SEMI_FLATFLAT +}; + +struct note +{ + enum note_tone nt; + enum note_semitone st; + int octave; +}; + +struct chord +{ + int n_notes; + struct note notes[8]; + int length; +}; + +struct stave +{ + int n_chords; + int max_chords; + struct chord *chords; +}; + struct music { unsigned int n_staves; unsigned int n_perc; + struct stave staves[8]; + + /* Maestro music data extracted from file */ unsigned int n_gates; unsigned int lengths[8]; unsigned char *gates; @@ -80,7 +124,7 @@ out: } -static void music_attribute(unsigned char ma, int save, FILE *ofh) +static void music_attribute(unsigned char ma, struct music *mus) { if ( (ma & 0x7f) == 0x40 ) { @@ -88,7 +132,7 @@ static void music_attribute(unsigned char ma, int save, FILE *ofh) } else if ( (ma & 0x3f) == 0x20 ) { - fprintf(ofh, "|\n ");; + //fprintf(ofh, "|\n "); } else if ( (ma & 0x1f) == 0x10 ) { @@ -114,19 +158,19 @@ static void music_attribute(unsigned char ma, int save, FILE *ofh) switch ( ct ) { case 0 : - fprintf(ofh, "\\clef \"treble\"\n"); + //fprintf(ofh, "\\clef \"treble\"\n"); break; case 1 : - fprintf(ofh, "\\clef \"treble\"\n"); + //fprintf(ofh, "\\clef \"treble\"\n"); break; case 2 : - fprintf(ofh, "\\clef \"treble\"\n"); + //fprintf(ofh, "\\clef \"treble\"\n"); break; case 3 : - fprintf(ofh, "\\clef \"treble\"\n"); + //fprintf(ofh, "\\clef \"treble\"\n"); break; } @@ -142,65 +186,21 @@ static void music_attribute(unsigned char ma, int save, FILE *ofh) tn = 1 + ((ma & 0x1e) >> 1); td = 1 + ((ma & 0xe0) >> 5); - 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''''"; - + //fprintf(ofh, "\\time %i/%i\n", tn, td); } - - return "?"; } - -static void get_note(unsigned char **notes, int *nptrs, int ch, FILE *ofh) +static struct note get_note(unsigned char *notes, int *nptr) { unsigned char n1, n2; int rest = 0; + struct note n; - n1 = notes[ch][nptrs[ch]++]; + n1 = notes[*nptr++]; if ( n1 & 0xf8 ) { - n2 = notes[ch][nptrs[ch]++]; + n2 = notes[*nptr++]; } else { rest = 1; } @@ -208,16 +208,54 @@ static void get_note(unsigned char **notes, int *nptrs, int ch, FILE *ofh) if ( rest ) { printf(" %i", n1); } else { - int pos, acc, len; + int pos, acc; pos = (n1 & 0xf8) >> 3; acc = n2 & 0x07; - len = (n2 & 0xe0) >> 5; - fprintf(ofh, "%s%i ", note_letter(pos, acc), (int)pow(2, len)); + n.nt = NOTE_A; + /* FIXME: Convert and store */ } + + return n; } -static void interpret_gates(struct music *mus, int stave, int ch, FILE *ofh) +static int find_max_length(unsigned char g, unsigned char **notes, int *nptrs) +{ + int ch; + int lmax = 100; + + for ( ch=0; ch<8; ch++ ) { + + int len; + + if ( !(g & 1<> 5; + if ( len < lmax ) lmax = len; + + } + + if ( lmax == 100 ) { + fprintf(stderr, "No note length in gate!\n"); + } + + return lmax; +} + + +static void find_note_with_length(unsigned char *notes, int *nptr, + struct chord *c, int ls) +{ + int len; + + len = (notes[*nptr] & 0xe0) >> 5; + if ( len != ls ) return; + + c->notes[c->n_notes++] = get_note(notes, nptr); +} + + +static void interpret_gates(struct music *mus) { unsigned int i; int ma = 0; @@ -225,12 +263,10 @@ static void interpret_gates(struct music *mus, int stave, int ch, FILE *ofh) for ( i=0; i<8; i++ ) nptrs[i] = 0; - fprintf(ofh, "{\n "); - printf("%i gates\n", mus->n_gates); for ( i=0; in_gates; i++ ) { if ( ma ) { - music_attribute(mus->gates[i], stave, ofh); + music_attribute(mus->gates[i], mus); ma = 0; continue; } @@ -240,13 +276,23 @@ static void interpret_gates(struct music *mus, int stave, int ch, FILE *ofh) continue; } else { - if ( mus->gates[i] & 1<notes, nptrs, ch, ofh); + int l, j; + struct chord c; + + l = find_max_length(mus->gates[i], mus->notes, nptrs); + + /* Find the notes of this length on this stave */ + c.n_notes = 0; + c.length = l; + for ( j=0; j<4; j++ ) { + if ( !(mus->gates[i] & (1<notes[j], + &nptrs[j], &c, l); } + } } - fprintf(ofh, "\n}\n"); } @@ -449,28 +495,13 @@ static void convert_file(const char *filename) } } - fprintf(ofh, "\score {\n"); for ( i=0; i>\n>>\n"); - + mus.staves[i].chords = NULL; + mus.staves[i].max_chords = 0; + mus.staves[i].n_chords = 0; } - fprintf(ofh, "}\n"); + + interpret_gates(&mus); } -- cgit v1.2.3