diff options
author | Thomas White <taw@bitwiz.org.uk> | 2011-11-12 22:24:26 +0100 |
---|---|---|
committer | Thomas White <taw@bitwiz.org.uk> | 2011-11-12 22:24:26 +0100 |
commit | 13056b602ac05a47bdf9e04a991da158f5f4d556 (patch) | |
tree | 1b80a5b3faf014fc5d74ce77f6c4288537cd4552 | |
parent | 6bb9c7c400a928afb541d18818a05eb8139f6532 (diff) |
Read notes
-rw-r--r-- | maestropond.c | 145 |
1 files changed, 141 insertions, 4 deletions
diff --git a/maestropond.c b/maestropond.c index bfe3a44..3b9792f 100644 --- a/maestropond.c +++ b/maestropond.c @@ -70,11 +70,138 @@ out: } +static void music_attribute(unsigned char ma) +{ + if ( (ma & 0x7f) == 0x40 ) { + + printf("Warning: reserved gate type.\n"); + + } else if ( (ma & 0x3f) == 0x20 ) { + + printf("Bar line\n"); + + } else if ( (ma & 0x1f) == 0x10 ) { + + printf("Bar line\n"); + + } else if ( (ma & 0xf) == 0x8 ) { + + printf("Octave shift\n"); + + } else if ( (ma & 0x7) == 0x4 ) { + + int st; + + st = 1 + ((ma & 0xc0) >> 6); + if ( ma & 0x10 ) { + printf("Slur on (stave %i)\n", st); + } else { + printf("Slur off (stave %i)\n", st); + } + + } else if ( (ma & 0x3) == 0x2 ) { + + int ct, st; + + ct = (ma & 0x18) >> 3; + switch ( ct ) { + + case 0 : + printf("Treble clef:"); + break; + + case 1 : + printf("Alto clef:"); + break; + + case 2 : + printf("Tenor clef:"); + break; + + case 3 : + printf("Bass clef:"); + break; + } + + st = (ma & 0x60) >> 5; + printf(" stave %i\n", st); + + } 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); + } +} + + + +static void get_note(unsigned char **notes, int *nptrs, int ch) +{ + unsigned char n1, n2; + int rest = 0; + + n1 = notes[ch][nptrs[ch]++]; + + if ( n1 & 0xf8 ) { + n2 = notes[ch][nptrs[ch]++]; + } else { + rest = 1; + } + + if ( rest ) { + printf("rest %i\n", n1); + } else { + printf("note %i:%i\n", n1, n2); + } +} + + +static void interpret_gates(unsigned char *gates, unsigned char **notes, + int n_gates) +{ + int i; + int ma = 0; + int nptrs[8]; + + for ( i=0; i<8; i++ ) nptrs[i] = 0; + + for ( i=0; i<n_gates; i++ ) { + + if ( ma ) { + music_attribute(gates[i]); + ma = 0; + continue; + } + + if ( gates[i] == 0 ) { + ma = 1; + continue; + } else { + + if ( gates[i] & 0x1 ) get_note(notes, nptrs, 0); + if ( gates[i] & 0x2 ) get_note(notes, nptrs, 1); + if ( gates[i] & 0x4 ) get_note(notes, nptrs, 2); + if ( gates[i] & 0x8 ) get_note(notes, nptrs, 3); + if ( gates[i] & 0x10 ) get_note(notes, nptrs, 4); + if ( gates[i] & 0x20 ) get_note(notes, nptrs, 5); + if ( gates[i] & 0x40 ) get_note(notes, nptrs, 6); + if ( gates[i] & 0x80 ) get_note(notes, nptrs, 7); + + } + } +} + + static size_t process_music_data(unsigned char *f, size_t ptr, size_t len) { unsigned int n_gates; unsigned int i; unsigned int lengths[8]; + unsigned char *gates; + unsigned char *notes[8]; n_gates = get_basic_int(f, &ptr); printf("%i gates\n", n_gates); @@ -84,19 +211,29 @@ static size_t process_music_data(unsigned char *f, size_t ptr, size_t len) printf("Channel %i, length %i\n", i+1, lengths[i]); } + gates = malloc(n_gates); + if ( gates == NULL ) { + fprintf(stderr, "Failed to allocate gates\n"); + } for ( i=0; i<n_gates; i++ ) { - unsigned char gd; - gd = f[ptr++]; + gates[i] = f[ptr++]; } for ( i=0; i<8; i++ ) { + unsigned int j; + + notes[i] = malloc(lengths[i]); + if ( notes[i] == NULL ) { + fprintf(stderr, "Failed to allocate notes\n"); + } for ( j=0; j<lengths[i]; j++ ) { - unsigned char d; - d = f[ptr++]; + notes[i][j] = f[ptr++]; } } + interpret_gates(gates, notes, n_gates); + return ptr; } |