summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas White <taw@bitwiz.org.uk>2011-11-15 23:37:25 +0100
committerThomas White <taw@bitwiz.org.uk>2011-11-15 23:37:25 +0100
commit0e78d96eff03491be4711f63fe1681ccefe133de (patch)
tree57e67dc01c04104215f02b5e4d70df4c896cc279
parent80af680e18ea3db3b0e9a2f0da9b02cbba480c1e (diff)
Progress on note extraction
-rw-r--r--maestropond.c130
1 files changed, 77 insertions, 53 deletions
diff --git a/maestropond.c b/maestropond.c
index 5250083..7504ac7 100644
--- a/maestropond.c
+++ b/maestropond.c
@@ -22,6 +22,15 @@
#include <sys/stat.h>
#include <math.h>
+struct music
+{
+ unsigned int n_staves;
+ unsigned int n_perc;
+ unsigned int n_gates;
+ unsigned int lengths[8];
+ unsigned char *gates;
+ unsigned char *notes[8];
+};
static const int bpm[] = {
40, 50, 60, 65, 70, 80, 90, 100, 115,
@@ -71,7 +80,7 @@ out:
}
-static void music_attribute(unsigned char ma, FILE *ofh)
+static void music_attribute(unsigned char ma, int save, FILE *ofh)
{
if ( (ma & 0x7f) == 0x40 ) {
@@ -208,39 +217,32 @@ static void get_note(unsigned char **notes, int *nptrs, int ch, FILE *ofh)
}
-static void interpret_gates(unsigned char *gates, unsigned char **notes,
- int n_gates, FILE *ofh)
+static void interpret_gates(struct music *mus, int stave, int ch, FILE *ofh)
{
- int i;
+ unsigned int i;
int ma = 0;
int nptrs[8];
for ( i=0; i<8; i++ ) nptrs[i] = 0;
fprintf(ofh, "{\n ");
- for ( i=0; i<n_gates; i++ ) {
+ printf("%i gates\n", mus->n_gates);
+ for ( i=0; i<mus->n_gates; i++ ) {
if ( ma ) {
- music_attribute(gates[i], ofh);
+ music_attribute(mus->gates[i], stave, ofh);
ma = 0;
continue;
}
- if ( gates[i] == 0 ) {
+ if ( mus->gates[i] == 0 ) {
ma = 1;
continue;
} else {
- fprintf(ofh, "<");
- if ( gates[i] & 0x1 ) get_note(notes, nptrs, 0, ofh);
- if ( gates[i] & 0x2 ) get_note(notes, nptrs, 1, ofh);
- if ( gates[i] & 0x4 ) get_note(notes, nptrs, 2, ofh);
- if ( gates[i] & 0x8 ) get_note(notes, nptrs, 3, ofh);
- if ( gates[i] & 0x10 ) get_note(notes, nptrs, 4, ofh);
- if ( gates[i] & 0x20 ) get_note(notes, nptrs, 5, ofh);
- if ( gates[i] & 0x40 ) get_note(notes, nptrs, 6, ofh);
- if ( gates[i] & 0x80 ) get_note(notes, nptrs, 7, ofh);
- fprintf(ofh, "> ");
+ if ( mus->gates[i] & 1<<ch ) {
+ get_note(mus->notes, nptrs, ch, ofh);
+ }
}
}
@@ -248,64 +250,57 @@ static void interpret_gates(unsigned char *gates, unsigned char **notes,
}
-static size_t process_music_data(unsigned char *f, size_t ptr, FILE *ofh,
- size_t len, int staff)
+static size_t read_music_data(unsigned char *f, size_t ptr, size_t len,
+ struct music *mus)
{
- 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);
+ mus->n_gates = get_basic_int(f, &ptr);
for ( i=0; i<8; i++ ) {
- lengths[i] = get_basic_int(f, &ptr);
- printf("Channel %i, length %i\n", i+1, lengths[i]);
+ mus->lengths[i] = get_basic_int(f, &ptr);
+ printf("Channel %i, length %i\n", i+1, mus->lengths[i]);
}
- gates = malloc(n_gates);
- if ( gates == NULL ) {
+ mus->gates = malloc(mus->n_gates);
+ if ( mus->gates == NULL ) {
fprintf(stderr, "Failed to allocate gates\n");
}
- for ( i=0; i<n_gates; i++ ) {
- gates[i] = f[ptr++];
+ for ( i=0; i<mus->n_gates; i++ ) {
+ mus->gates[i] = f[ptr++];
}
for ( i=0; i<8; i++ ) {
unsigned int j;
- notes[i] = malloc(lengths[i]);
- if ( notes[i] == NULL ) {
+ mus->notes[i] = malloc(mus->lengths[i]);
+ if ( mus->notes[i] == NULL ) {
fprintf(stderr, "Failed to allocate notes\n");
}
- for ( j=0; j<lengths[i]; j++ ) {
- notes[i][j] = f[ptr++];
+ for ( j=0; j<mus->lengths[i]; j++ ) {
+ mus->notes[i][j] = f[ptr++];
}
}
- interpret_gates(gates, notes, n_gates, ofh);
-
return ptr;
}
-static size_t process_stave_data(unsigned char *f, size_t ptr)
+static size_t process_stave_data(unsigned char *f, size_t ptr, size_t len,
+ struct music *mus)
{
- int n_staves, n_perc;
+ mus->n_staves = 1 + f[ptr++];
+ mus->n_perc = f[ptr++];
- n_staves = 1 + f[ptr++];
- n_perc = f[ptr++];
-
- printf("%i staves, %i percussion\n", n_staves, n_perc);
+ printf("%i staves, %i percussion\n", mus->n_staves, mus->n_perc);
return ptr;
}
-static size_t process_instrument_data(unsigned char *f, size_t ptr)
+static size_t process_instrument_data(unsigned char *f, size_t ptr, size_t len,
+ struct music *mus)
{
int i;
@@ -319,7 +314,8 @@ static size_t process_instrument_data(unsigned char *f, size_t ptr)
}
-static size_t process_volume_data(unsigned char *f, size_t ptr)
+static size_t process_volume_data(unsigned char *f, size_t ptr, size_t len,
+ struct music *mus)
{
int i;
@@ -333,7 +329,8 @@ static size_t process_volume_data(unsigned char *f, size_t ptr)
}
-static size_t process_stereo_data(unsigned char *f, size_t ptr)
+static size_t process_stereo_data(unsigned char *f, size_t ptr, size_t len,
+ struct music *mus)
{
int i;
@@ -347,7 +344,8 @@ static size_t process_stereo_data(unsigned char *f, size_t ptr)
}
-static size_t process_tempo_data(unsigned char *f, size_t ptr)
+static size_t process_tempo_data(unsigned char *f, size_t ptr, size_t len,
+ struct music *mus)
{
int tempo;
@@ -365,6 +363,8 @@ static void convert_file(const char *filename)
FILE *ofh;
unsigned char *f;
size_t r, ptr;
+ unsigned int i;
+ struct music mus;
if ( stat(filename, &statbuf) == -1 ) {
fprintf(stderr, "Couldn't file file '%s'\n", filename);
@@ -419,34 +419,58 @@ static void convert_file(const char *filename)
ptr = 9;
while ( ptr < r ) {
+
switch ( f[ptr++] ) {
case 1 :
- ptr = process_music_data(f, ptr, ofh, r);
+ ptr = read_music_data(f, ptr, r, &mus);
break;
case 2 :
- ptr = process_stave_data(f, ptr);
+ ptr = process_stave_data(f, ptr, r, &mus);
break;
case 3 :
- ptr = process_instrument_data(f, ptr);
+ ptr = process_instrument_data(f, ptr, r, &mus);
break;
case 4 :
- ptr = process_volume_data(f, ptr);
+ ptr = process_volume_data(f, ptr, r, &mus);
break;
case 5 :
- ptr = process_stereo_data(f, ptr);
+ ptr = process_stereo_data(f, ptr, r, &mus);
break;
case 6 :
- ptr = process_tempo_data(f, ptr);
+ ptr = process_tempo_data(f, ptr, r, &mus);
break;
}
}
+
+ fprintf(ofh, "\score {\n");
+ for ( i=0; i<mus.n_staves; i++ ) {
+
+ int j;
+
+ fprintf(ofh, "<<\n\\new Staff = \"%i\" <<\n", i);
+ for ( j=0; j<4; j++ ) { /* FIXME! */
+
+ if ( j == 0 ) {
+ fprintf(ofh, "{\n");
+ } else {
+ fprintf(ofh, " \\\\ {\n");
+ }
+
+ interpret_gates(&mus, i, i*4+j, ofh);
+ fprintf(ofh, "}\n");
+
+ }
+ fprintf(ofh, ">>\n>>\n");
+
+ }
+ fprintf(ofh, "}\n");
}