/* Copyright (C) 1986 by M. J. Shannon, Jr.
** Permission to distribute for non-commercial uses granted as long as this
** notice is retained.  Violators will be prosecuted.
*/

#include	<stdio.h>
#include	"mpu/mpu.h"

void getavoice();

unsigned char dump1voice[] =
{
	0xF0,	/* System Exclusive */
	0x43,	/* ID: Yamaha */
	0x20,	/* substatus w/channel 0 */
	0x03,	/* format (1 voice) */
	0xF7	/* EOX */
};

unsigned char read_buf[256];	/* a buffer to read into */

int
main(argc, argv, envp)
int argc;
char **argv;
char **envp;
{
	char *vers_string;

	/* reset the MPU */
	mpu_reset();
	/* get the firmware version/revision */
	vers_string = mpu_id();
	/* show it to the user */
	printf("MPU: %s\n", vers_string);
	/* enable various options */
	mpu_put(SWE_HEXCL);
	mpu_put(SWE_DATAINSTOP);
	mpu_put(SWE_TIME);
	/* and get patches until the user gives up */
	for (;;)
		getavoice();
	/* return success to DOS */
	return (0);
}

void
getavoice()
{
	int length;
	FILE *fp;
	char s[256];

	/* Tell the DX-100 to dump a voice */
	mpu_sexcl(dump1voice);
	/* But it just echoes the message, so ignore it */
	length = mpu_read(&read_buf[0]);
getfile:;
	/* ask for a filename to store the patch into */
	printf("File name:");
	gets(s);
	/* to get out, just hit CR */
	if (s[0] == '\0')
		exit(0);
	/* create the file for binary i/o */
	fp = fopen(s, "wb");
	/* complain if we couldn't */
	if (!fp)
	{
		printf("Couldn't create <%s>!\n", s);
		goto getfile;
	}
	/* prompt the user */
	printf("Press voice selector now.\n");
	/* read until we get something */
	while ((length = mpu_read(&read_buf[0])) == 0)
		/* tell user we didn't get anything */
		printf("Waiting...\n");
	/* tell user how much we got */
	printf("Got %d bytes.\n", length);
	/* write it out */
	fwrite(read_buf, length, 1, fp);
	/* close the file */
	fclose(fp);
	/* and confirm to user */
	printf("Written to <%s>!\n", s);
}
