summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas White <taw@bitwiz.org.uk>2011-11-14 23:15:14 +0100
committerThomas White <taw@bitwiz.org.uk>2011-11-14 23:15:14 +0100
commit80af680e18ea3db3b0e9a2f0da9b02cbba480c1e (patch)
treeb83c1847481a9711fb27ba4c7ca5d6d0aac5dfef
parentd71d1f504b983336063e5700987764cc705e4b75 (diff)
Note handling
-rw-r--r--Makefile2
-rw-r--r--maestropond.c124
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 <getopt.h>
#include <sys/types.h>
#include <sys/stat.h>
+#include <math.h>
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<n_gates; i++ ) {
if ( ma ) {
- music_attribute(gates[i]);
+ music_attribute(gates[i], ofh);
ma = 0;
continue;
}
@@ -181,21 +231,25 @@ static void interpret_gates(unsigned char *gates, unsigned char **notes,
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);
+ 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, "> ");
}
}
+ 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 :