/*
 *	DXFRIEND
 *
 *	A program to help you have conversations with your DX7.
 *
 *	- pick up 4K voice dumps from disk
 *	- pick up garbled voice dumps from the disk
 *	- spew them at the DX7 (sysinfo available && internal protect off)
 */
#include <stdio.h>
#include <osbind.h>
#include "midi.h"

/* External declarations */
extern char *get_filename();		/* from fileio.c */

/* Miscellaneous interesting static functions */
extern char *numerosuffix();		/* from misc.c */

char voicebuf[4096];		/* The voice dump buffer */

main(argc, argv)
char **argv;
{
	register int ch;

	while (argc >= 2 && argv[1][0] == '-') {
		switch(argv[1][1]) {
		default:
			goto usage;
		}
		argv++; argc--;	/* ugh */
	}
	if (argc > 2) {
usage:		fputs("usage: dxfriend [file]\n", stderr);
		exit(1);
	}

	v_init();			/* initialise screen */
	init_buf(voicebuf);		/* Initialise the voice buffer (honest!) */

	if (argc == 3) {
		load_disk(argv[1], 0);	/* Load undamaged voice */
	}

	while (1) {
		d_print("\
\033pL\033qoad/\033pS\033qave file, \
Load \033pD\033qump file, \
\033pT\033qransmit, \
\033pK\033qeyboard, \
\033pM\033qonitor, \
\033pQ\033quit");

		switch (mgetchar()) {
		case 'L':
		case 'l':		
			load_disk(get_filename(), voicebuf, 0);
			break;
		case 'D':
		case 'd':
			load_disk(get_filename(), voicebuf, 1);
			break;
		case 'S':
		case 's':
			save_disk(get_filename(), voicebuf);
			break;
		case 'T':
		case 't':
			send_dx7(voicebuf);
			break;
		case 'M':
		case 'm':
			view_monitor();
			break;
		case 'N':
		case 'n':
			view_names(voicebuf);
			break;
		case 'K':
		case 'k':
			view_state();
			break;
		case 'Q':
		case 'q':
			quit();
			/* NOTREACHED */
		default:
			bell();	/* Bling! */
		}
	}
}

quit()
{
	v_quit();	/* deinitialise the screen */
	exit(0);
}

/*
 *	Initialise the buffer.
 *	All this takes at present is to fill the names in with spaces.
 */
init_buf(buf)
char *buf;
{
	register int i, j;
	register char *cp;

	for (i=0; i<32; i++) {
		for (j=0, cp=&buf[i*128+118]; j<10; j++, cp++) *cp = ' ';
	}
}
