From ucsd!ucbvax!agate!helios.ee.lbl.gov!nosc!humu!uhccux!lee Sat Apr  1 05:55:45 PST 1989

>From article <9316@netnews.upenn.edu>, by bradley@dsl.cis.upenn.edu (John Bradley):
" 
" Here's 7 banks of patches for the Kawai K-1.  (the factory presets, and the
" first 6 'sets' from Kawai.  (A101-A105, J101, if that's meaningful)).
" ...

Ah, at last.  Some K1 patches -- thanks.  Below is a utility to
convert these files to glib format.

In looking at these I found a couple of bugs in the K1 editing
portion of glib.  They resulted from inaccuracies in the K1
documentation.  Patches forthcoming.

	Greg, lee@uhccux.uhcc.hawaii.edu
/*
k1split -- convert a Kawai K1 sysex dump
	to glib format files.

Compile: cc -o k1split k1split.c

Usage: k1split <list of names of dump files>
	For each file in the list, two new files are created
	with names created by suffixing to the name of the
	dump file ".sgl" and ".mlt".  The first has the
	single patches, the second the multi patches.

		Greg Lee, lee@uhccux.uhcc.hawaii.edu
		3/30/89
*/

#include <stdio.h>

/* cake flag not used (don't yet know format of cake files) */
int cakeflag = 0;

main(argc, argv)
int argc;
char *argv[];
{	int k;

	for (k = 1; --argc; k++) {
		if (argv[k][0] == '-')
			switch (argv[k][1]) {
				case 'c': cakeflag = 1; break;
				default: break;
			}
		else dump(argv[k]);
	}
}

FILE *fopen(), *fo;
int which = 0, togo = 0, r = 0, rs = 0, dataid = 0;
char foname[80];

dump(name)
char *name;
{	FILE *f;
	int c, next, ign = 0;

	if ( (f = fopen(name, "r")) == NULL ) {
		fprintf(stderr, "no file %s\n", name);
		exit(1);
	}

	if ( (c = getc(f)) == EOF ) {
		fprintf(stderr, "file %s is empty\n", name);
		exit(1);
	}

	if (cakeflag) while ( (c = getc(f)) != EOF && c != 0xf0 ) ;

	while ( (next = getc(f)) != EOF ) {
		if (c == 0xf7) ign = 1;
		else if (c == 0xf0) ign = 8;
		if (ign) ign--;
		else out(c, name);
		c = next;
	}
	if (!ign) out(c, name);

	if (!cakeflag)
	    if (r || togo || which < 2)
		fprintf(stderr, "file %s was incomplete\n",name);
	fclose(f);
}

#define PATDATAID 0xf1
#define TIMDATAID 0xf2
#define TONDATAID 0xf3
#define RSUDATAID 0xf4
#define URPDATAID 0xf5
#define TRADATAID 0xf6

out(c, name)
int c;
char *name;
{	char *suf;
	int i;

	if (togo == 0) {
		if (r == 0) /* next out file */ {
			if (which) fclose(fo);
			which++;
			if (cakeflag && which == 1) which = 2;
			switch (which) {
			    case 1: suf = ".sgl";  rs = 88; r = 64;
					dataid = 0xf3; break;
			    case 2: suf = ".mlt"; rs = 76; r = 32;
					dataid = 0xf1; break;
			    default: rs = 0; break;
			}
			if (cakeflag && which > 4) r = rs = 0;

			if (rs) {
				strcpy(foname, name);
				strcat(foname, suf);
				if ((fo = fopen(foname, "w")) == NULL) {
					fprintf(stderr, "can't open file\n");
					exit(1);
				}
				putc(dataid, fo);
			}
		}
		if (r) /* set togo to recsize */ {
			togo = rs;
			r--;
			/* and pad begin of record */
			/* for (i=0; i<20; i++) putc('\0', fo);*/
		}
	}
	if (togo) {
		/* skip over check sum */
		if (togo > 1) putc(c, fo);
		togo--;
	}
}


