diff -urN midi_prog-/Makefile midi_prog/Makefile --- midi_prog-/Makefile Wed Apr 29 00:00:00 2015 +++ midi_prog/Makefile Mon May 11 00:00:00 2015 @@ -8,8 +8,9 @@ VCD_OBJS = vcd.o in.o filter.o out.o wrt.o util.o VOCO_OBJS = voco.o vco.o wave.o in.o filter.o out.o util.o +MIDTXT_OBJS = midtxt.o rd.o wrt.o util.o -all: $(TARG) vcd voco +all: $(TARG) vcd voco midtxt $(TARG): $(OBJS) $(CC) -o $@ $(OBJS) $(LIB) @@ -20,8 +21,12 @@ voco: $(VOCO_OBJS) $(CC) -o $@ $(VOCO_OBJS) -lm +midtxt: $(MIDTXT_OBJS) + $(CC) -o $@ $(MIDTXT_OBJS) + clean: rm -f $(TARG) $(OBJS) *~ rm -f vcd $(VCD_OBJS) voco $(VOCO_OBJS) + rm -f midtxt $(MIDTXT_OBJS) # EOF diff -urN midi_prog-/midtxt.c midi_prog/midtxt.c --- midi_prog-/midtxt.c Thu Jan 1 09:00:00 1970 +++ midi_prog/midtxt.c Mon May 11 00:00:00 2015 @@ -0,0 +1,357 @@ +#include "util.h" +#include "rd.h" +#include "wrt.h" + +struct tbl{ + int v; + char *s; +}tbl_cmd[] = { + { 8, "off" }, + { 9, "on" }, + { 0xa, "press" }, + { 0xb, "ctl_chg" }, + { 0xc, "prog_num" }, + { 0xd, "ch_press" }, + { 0xe, "pitch" }, + { -1, NULL } +}, tbl_ctl[] = { + { 7, "ch_vol_msb" }, + { 39, "ch_vol_lsb" }, + { 10, "pan_msb" }, + { 42, "pan_lsb" }, + { 100, "rpn_lsb" }, + { 101, "rpn_msb" }, + { 6, "data_entry_msb" }, + { -1, NULL } +}; + +char * +to_str(struct tbl *t, int v) +{ + for(; t->s; t++) if(t->v == v) return t->s; + return NULL; +} + +int +to_v(struct tbl *t, char *s) +{ + for(; t->s; t++) if(strcmp(t->s, s) == 0) return t->v; + return -1; +} + +char * +cmd_str(int same, int hi, int low) +{ + if(same) return "same"; + if(hi == 0xf && low == 0xf) return "meta"; + return to_str(tbl_cmd, hi); +} + +void +cmd_hl(char *s, int *same, int *hi, int *low) +{ + int v; + + *same = 0; + if(strcmp(s, "same") == 0){ + *same = 1; + return; + } + if(strcmp(s, "meta") == 0){ + *hi = 0xf; + *low = 0xf; + return; + } + if((*hi = to_v(tbl_cmd, s)) != -1) return; + sscanf(s, "cmd=%x", &v); + *low = v & 0xf; + *hi = (v >> 4) & 0xf; +} + +char * +ctl_chg_type_str(int type) +{ + return to_str(tbl_ctl, type); +} + +int +ctl_chg_type(char *s) +{ + int type = to_v(tbl_ctl, s); + if(type != -1) return type; + sscanf(s, "type=%d", &type); + return type; +} + +char * +meta_str(int type) +{ + switch(type){ + case 0x51: return "set_tempo"; + default: break; + } + return NULL; +} + +void +cmd_bin_out(int same, int hi, int low) +{ + if(same) return; + set_char((hi<<4) | low); +} + +int +rconv(void) +{ + char buf_cmd[16], buf_type[16], buf_meta[16], buf[256]; + int v, hi, low, same, ch, note, velo, type, v2, n, i, c; + FILE *fp = stdin; + + fscanf(fp, "%s\n", buf); + set_str(buf); + + fscanf(fp, "hdsz=%d\n", &v); + set_int(4, v); + + fscanf(fp, "fmt_type=%d\n", &v); + set_int(2, v); + + fscanf(fp, "track_num=%d\n", &v); + set_int(2, v); + + fscanf(fp, "div4=%d\n", &v); + set_int(2, v); + + fscanf(fp, "%s\n", buf); + set_str(buf); + + fscanf(fp, "trksz=%d\n", &v); + set_int(4, v); + + while(fscanf(fp, "delta=%d ", &v) == 1){ + wrt_delta(v); + + fscanf(fp, "%s", buf_cmd); + cmd_hl(buf_cmd, &same, &hi, &low); + switch(hi){ + case 8: + case 9: + fscanf(fp, " ch=%d note=%d velo=%d\n", &ch, ¬e, &velo); + cmd_bin_out(same, hi, ch); + set_char2_wrt(note, velo); + break; + case 0xa: + fscanf(fp, " ch=%d note=%d press=%d\n", &ch, ¬e, &velo); + cmd_bin_out(same, hi, ch); + set_char2_wrt(note, velo); + break; + case 0xb: + fscanf(fp, " %s ch=%d v=%d\n", buf_type, &ch, &v); + cmd_bin_out(same, hi, ch); + type = ctl_chg_type(buf_type); + set_char2_wrt(type, v); + break; + case 0xc: + case 0xd: + fscanf(fp, " ch=%d v=%d\n", &ch, &v); + cmd_bin_out(same, hi, ch); + set_char(v); + break; + case 0xe: + fscanf(fp, " ch=%d v=%d\n", &ch , &v); + cmd_bin_out(same, hi, ch); + v += 8192; + set_char(v & 0x7f); + v >>= 7; + set_char(v & 0x7f); + break; + case 0xf: + cmd_bin_out(same, hi, low); + switch(low){ + case 0: + fscanf(fp, " type=%d", &type); + set_char(type); + while((c = fgetc(fp)) == ' '){ + fscanf(fp, "%x", &v); + set_char(v); + } + break; + case 1: + case 3: + fscanf(fp, " type=%d v=%d\n", &type, &v); + set_char2_wrt(type, v); + break; + case 2: + fscanf(fp, " type=%d v=%d,%d\n", &type, &v, &v2); + set_char3_wrt(type, v, v2); + break; + case 8: + case 0xa: + case 0xb: + case 0xc: + case 0xe: + fgetc(fp); /* skip \n */ + break; + case 0xf: + fscanf(fp, " %s n=%d", buf_meta, &n); + if(strcmp(buf_meta, "set_tempo") == 0){ + type = 0x51; + fscanf(fp, " tempo=%d\n", &v); + set_char2_wrt(type, n); + set_int(n, v); + }else{ + sscanf(buf_meta, "type=%d", &type); + set_char2_wrt(type, n); + for(i=0; i= 0) return rconv(); + + rd_str(4, buf); + printf("%s\n", buf); + + printf("hdsz=%d\n", rd_int(4)); + printf("fmt_type=%d\n", rd_int(2)); + printf("track_num=%d\n", rd_int(2)); + printf("div4=%d\n", rd_int(2)); + + rd_str(4, buf); + printf("%s\n", buf); + + printf("trksz=%d\n", rd_int(4)); + + while((v = rd_delta()) != EOF){ + printf("delta=%d ", v); + + same = 0; + if((v = rd()) & 0x80){ + hi = (v >> 4) & 0xf; + low = v & 0xf; + }else{ + bk(v); + same = 1; + } + if((cmd = cmd_str(same, hi, low)) == NULL){ + sprintf(buf_cmd, "cmd=%x%x", hi, low); + cmd = buf_cmd; + } + ch = low; + + switch(hi){ + case 8: + case 9: + case 0xa: + note = rd(); + velo = rd(); + printf("%s ch=%d note=%d %s=%d\n", cmd, ch, note, hi == 0xa ? "press" : "velo", velo); + break; + + case 0xb: /* control change */ + type = rd(); + v = rd(); + if((type_str = ctl_chg_type_str(type)) == NULL){ + sprintf(buf_type, "type=%d", type); + type_str = buf_type; + } + printf("%s %s ch=%d v=%d\n", cmd, type_str, ch, v); + break; + + case 0xc: /* program number */ + case 0xd: /* pressure */ + printf("%s ch=%d v=%d\n", cmd, ch, rd()); + break; + + case 0xe: /* pitch wheel change */ + v = rd(); + v |= rd() << 7; + v -= 8192; + printf("%s ch=%d v=%d\n", cmd, ch, v); + break; + + case 0xf: + switch(low){ + case 8: + case 0xa: + case 0xb: + case 0xc: + case 0xe: + break; + default: + type = rd(); + break; + } + + switch(low){ + case 0: + printf("%s type=%d", cmd, type); + do{ + v = rd(); + printf(" %02x", v); + }while(v != 0xf7); + printf("\n"); + break; + case 1: + case 3: + v = rd(); + printf("%s type=%d v=%d\n", cmd, type, v); + break; + case 2: + v = rd(); + printf("%s type=%d v=%d,%d\n", cmd, type, v, rd()); + break; + case 8: + case 0xa: + case 0xb: + case 0xc: + case 0xe: + printf("%s\n", cmd); + break; + case 0xf: /* meta */ + n = rd(); + if((meta = meta_str(type)) == NULL){ + sprintf(buf_meta, "type=%d", type); + meta = buf_meta; + } + switch(type){ + case 0x51: /* set tempo */ + v = rd_int(n); + printf("%s %s n=%d tempo=%d\n", cmd, meta, n, v); + break; + default: + printf("%s %s n=%d", cmd, meta, n); + for(i=0; i 100){ + if(v < 0){ fprintf(stderr, "v=%d", v); exit(1); }