Code cleanup, fix warnings, error checking, add midi2wav to viewers.config


git-svn-id: svn://svn.rockbox.org/rockbox/trunk@6304 a1c6a512-1295-4272-9138-f99709370657
diff --git a/apps/plugins/midi/guspat.c b/apps/plugins/midi/guspat.c
index f0f1eff..675d6ff 100644
--- a/apps/plugins/midi/guspat.c
+++ b/apps/plugins/midi/guspat.c
@@ -94,6 +94,14 @@
 
 	int file = rb->open(filename, O_RDONLY);
 
+	if(file == -1)
+	{
+		char message[50];
+		snprintf(message, 50, "Error opening %s", filename);
+		rb->splash(HZ*2, true, message);
+		return NULL;
+	}
+
 	gp->header=readData(file, 12);
 	gp->gravisid=readData(file, 10);
 	gp->desc=readData(file, 60);
diff --git a/apps/plugins/midi/guspat.h b/apps/plugins/midi/guspat.h
index 5007c4d..75bdb2c 100644
--- a/apps/plugins/midi/guspat.h
+++ b/apps/plugins/midi/guspat.h
@@ -17,7 +17,7 @@
  ****************************************************************************/
 
 //This came from one of the Gravis documents
-const static unsigned int gustable[]=
+static const unsigned int gustable[]=
 {
   8175, 8661, 9177, 9722, 10300, 10913, 11562, 12249, 12978, 13750, 14567, 15433,
   16351, 17323, 18354, 19445, 20601, 21826, 23124, 24499, 25956, 27500, 29135, 30867,
diff --git a/apps/plugins/midi/midifile.c b/apps/plugins/midi/midifile.c
index 08efb73..c786b8e 100644
--- a/apps/plugins/midi/midifile.c
+++ b/apps/plugins/midi/midifile.c
@@ -185,7 +185,6 @@
 	trk->pos = 0;
 	trk->delta = 0;
 
-	int len=0;
 	int numEvents=0;
 
 	int pos = rb->lseek(file, 0, SEEK_CUR);
@@ -269,6 +268,8 @@
     return(value);
 }
 
+
+/*
 //This function should not be needed because we
 //can just release the whole memory buffer at once
 void unloadFile(struct MIDIfile * mf)
@@ -296,10 +297,10 @@
 	}
 	free(mf); 	//Unload the main struct
 }
-
+*/
 void bail(const char * err)
 {
-	printf("\nERROR: %s\n", err);
+	rb->splash(HZ*3, true, err);
 	exit(0);
 }
 
diff --git a/apps/plugins/midi/midiutil.c b/apps/plugins/midi/midiutil.c
index 0ab5a35..41a02ae 100644
--- a/apps/plugins/midi/midiutil.c
+++ b/apps/plugins/midi/midiutil.c
@@ -40,16 +40,18 @@
 #define CTRL_PANNING	10
 #define CHANNEL 	1
 
-
+//Most of these are deprecated.. rampdown is used, maybe one other one too
 #define STATE_ATTACK  		1
 #define STATE_DECAY  		2
 #define STATE_SUSTAIN 		3
 #define STATE_RELEASE 		4
 #define STATE_RAMPDOWN		5
 
+//Loop states
 #define STATE_LOOPING           7
 #define STATE_NONLOOPING	8
 
+//Various bits in the GUS mode byte
 #define LOOP_ENABLED	4
 #define LOOP_PINGPONG   8
 #define LOOP_REVERSE    16
@@ -65,7 +67,8 @@
 unsigned char chPanLeft[16];	//Channel panning
 unsigned char chPanRight[16];
 unsigned char chPat[16];        //Channel patch
-unsigned char chPW[16];        //Channel pitch wheel, MSB
+unsigned char chPW[16];        //Channel pitch wheel, MSB only
+
 
 struct GPatch * gusload(char *);
 struct GPatch * patchSet[128];
@@ -118,8 +121,19 @@
 	int numPatches;
 };
 
-void *my_malloc(int size);
 
+
+void sendEvent(struct Event * ev);
+int tick(struct MIDIfile * mf);
+inline void setPoint(struct SynthObject * so, int pt);
+struct Event * getEvent(struct Track * tr, int evNum);
+int readTwoBytes(int file);
+int readFourBytes(int file);
+int readVarData(int file);
+int midimain(void * filename);
+
+
+//Rick's code
 void *alloc(int size)
 {
 	static char *offset = NULL;
@@ -143,7 +157,7 @@
 	totalSize -= size + 4;
 	return ret;
 }
-
+/*
 void *ralloc(char *offset, int len)
 {
 	int size;
@@ -175,18 +189,13 @@
 
 	return ret;
 }
-
+*/
 
 void * allocate(int size)
 {
 	return alloc(size);
 }
 
-void sendEvent(struct Event * ev);
-int tick(struct MIDIfile * mf);
-inline void setPoint(struct SynthObject * so, int pt);
-struct Event * getEvent(struct Track * tr, int evNum);
-
 unsigned char readChar(int file)
 {
 	char buf[2];
@@ -211,11 +220,9 @@
 	return size+1 == rb->lseek(fd, 0, SEEK_CUR);
 }
 
-void printf(char *fmt, ...) {}
+void printf(char *fmt, ...) {fmt=fmt; }
 
-//#define my_malloc(a) malloc(a)
-
-
+/*
 void *audio_bufferbase;
 void *audio_bufferpointer;
 unsigned int audio_buffer_free;
@@ -252,11 +259,8 @@
     audio_bufferpointer = pointer;
     audio_buffer_free = audio_bufferpointer - audio_bufferbase;
 }
-
+*/
 void exit(int code)
 {
+	code = code; //Stub function, kill warning for now
 }
-
-void free(void * ptr)
-{
-}
\ No newline at end of file
diff --git a/apps/plugins/midi/sequencer.c b/apps/plugins/midi/sequencer.c
index fb98fda..f7c6f30 100644
--- a/apps/plugins/midi/sequencer.c
+++ b/apps/plugins/midi/sequencer.c
@@ -242,7 +242,7 @@
 int tick(struct MIDIfile * mf)
 {
 	if(mf==NULL)
-		return;
+		return 0;
 
 	int a=0;
 	int tracksAdv=0;
diff --git a/apps/plugins/midi/synth.c b/apps/plugins/midi/synth.c
index 0d07ed9..86cb434 100644
--- a/apps/plugins/midi/synth.c
+++ b/apps/plugins/midi/synth.c
@@ -49,7 +49,7 @@
 
 //Filename is the name of the config file
 //The MIDI file should have been loaded at this point
-void initSynth(struct MIDIfile * mf, char * filename, char * drumConfig)
+int initSynth(struct MIDIfile * mf, char * filename, char * drumConfig)
 {
 	char patchUsed[128];
 	char drumUsed[128];
@@ -92,8 +92,8 @@
 		if(mf->tracks[a] == NULL)
 		{
 			printf("\nNULL TRACK !!!");
-			exit(1);
-			return;
+			rb->splash(HZ*2, true, "Null Track in loader.");
+			return -1;
 		}
 
 		for(ts=0; ts<mf->tracks[a]->numEvents; ts++)
@@ -112,29 +112,43 @@
 	}
 
 	int file = rb->open(filename, O_RDONLY);
+	if(file == -1)
+	{
+		rb->splash(HZ*2, true, "Bad patch config.\nDid you install the patchset?");
+		return -1;
+	}
 
-	char name[30];
-	char fn[30];
+	char name[40];
+	char fn[40];
 
 	//Scan our config file and load the right patches as needed
 	int c = 0;
-	rb->snprintf(name, 30, "");
+	rb->snprintf(name, 40, "");
 	for(a=0; a<128; a++)
 	{
 		while(readChar(file)!=' ' && !eof(file));
 		readTextBlock(file, name);
 
-		rb->snprintf(fn, 30, "/patchset/%s.pat", name);
+		rb->snprintf(fn, 40, "/.rockbox/patchset/%s.pat", name);
 		printf("\nLOADING: <%s> ", fn);
+
 		if(patchUsed[a]==1)
 			patchSet[a]=gusload(fn);
 
+//		if(patchSet[a] == NULL)
+//			return -1;
+
 		while((c != '\n'))
 			c = readChar(file);
 	}
 	rb->close(file);
 
 	file = rb->open(drumConfig, O_RDONLY);
+	if(file == -1)
+	{
+		rb->splash(HZ*2, true, "Bad drum config.\nDid you install the patchset?");
+		return -1;
+	}
 
 	//Scan our config file and load the drum data
 	int idx=0;
@@ -143,7 +157,7 @@
 	{
 		readTextBlock(file, number);
 		readTextBlock(file, name);
-		rb->snprintf(fn, 30, "/patchset/%s.pat", name);
+		rb->snprintf(fn, 40, "/.rockbox/patchset/%s.pat", name);
 
 		idx = rb->atoi(number);
 		if(idx == 0)
@@ -151,13 +165,15 @@
 
 		if(drumUsed[idx]==1)
 			drumSet[idx]=gusload(fn);
+
+//		if(drumSet[idx] == NULL)
+//			return -1;
+
 		while((c != '\n') && (c != 255) && (!eof(file)))
-		{
-			printf("loop");
 			c = readChar(file);
-		}
 	}
 	rb->close(file);
+	return 0;
 }
 
 
diff --git a/apps/plugins/midi2wav.c b/apps/plugins/midi2wav.c
index f836853..0b1659b 100644
--- a/apps/plugins/midi2wav.c
+++ b/apps/plugins/midi2wav.c
@@ -19,13 +19,17 @@
 #define SAMPLE_RATE 48000
 #define MAX_VOICES 100
 
-/*	This is for writing to the DSP directly from the Simulator
+/*
+#if defined(SIMULATOR)
+//	This is for writing to the DSP directly from the Simulator
 #include <stdio.h>
 #include <stdlib.h>
 #include <linux/soundcard.h>
 #include <sys/ioctl.h>
+#endif
 */
 
+
 #include "../../plugin.h"
 #include "midi/midiutil.c"
 #include "midi/guspat.h"
@@ -47,23 +51,56 @@
 
 
 
+
 enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
 {
-    TEST_PLUGIN_API(api);
-    (void)parameter;
-    rb = api;
-    rb->splash(HZ*2, true, "MIDI");
-    midimain();
-    rb->splash(HZ*2, true, "FINISHED PLAYING");
-    return PLUGIN_OK;
+	TEST_PLUGIN_API(api);
+	rb = api;
+    	TEST_PLUGIN_API(api);
+    	(void)parameter;
+    	rb = api;
+
+    	if(parameter == NULL)
+    	{
+    		rb->splash(HZ*2, true, " Play .MID file ");
+    		return PLUGIN_OK;
+    	}
+    	rb->splash(HZ, true, parameter);
+    	if(midimain(parameter) == -1)
+    	{
+    		return PLUGIN_ERROR;
+    	}
+    	rb->splash(HZ*3, true, "FINISHED PLAYING");
+    	return PLUGIN_OK;
 }
 
 
-int midimain()
+int midimain(void * filename)
 {
-    	rb->splash(HZ*2, true, "OPENED DSP");
+
+	printf("\nHello.\n");
+
+	rb->splash(HZ/5, true, "LOADING MIDI");
+
+	struct MIDIfile * mf = loadFile(filename);
+	long bpm, nsmp, l;
+
+	int bp=0;
+
+	rb->splash(HZ/5, true, "LOADING PATCHES");
+	if (initSynth(mf, "/.rockbox/patchset/patchset.cfg", "/.rockbox/patchset/drums.cfg") == -1)
+	{
+		return -1;
+	}
+
 	fd=rb->open("/dsp.raw", O_WRONLY|O_CREAT);
+
 /*
+//This lets you hear the music through the sound card if you are on Simulator
+//Make a symlink, archos/dsp.raw and make it point to /dev/dsp or whatever
+//your sound device is.
+
+#if defined(SIMULATOR)
         int arg, status;
         int bit, samp, ch;
 
@@ -82,24 +119,14 @@
         status = ioctl(fd, SOUND_PCM_WRITE_RATE, &arg);
         status = ioctl(fd, SOUND_PCM_READ_RATE, &arg);
         samp=arg;
+#endif
 */
 
-	printf("\nHello.\n");
-//	initSound();	//Open the computer's sound card
-	int a=0;
 
-	rb->splash(HZ*2, true, "LOADING MIDI");
+	rb->splash(HZ/5, true, "  START PLAYING  ");
 
-	struct MIDIfile * mf = loadFile("/test.mid");
 
-	rb->splash(HZ*2, true, "LOADED MIDI");
-	long bpm, nsmp, l;
 
-	int bp=0;
-
-	rb->splash(HZ*2, true, "LOADING PATCHES");
-	initSynth(mf, "/iriver2.cfg", "/drums.cfg");	//Initialize the MIDI syntehsizer
-	rb->splash(HZ*2, true, "START PLAYING");
 
 	signed char buf[3000];
 
diff --git a/apps/plugins/viewers.config b/apps/plugins/viewers.config
index dcb7ca1..9b30e20 100644
--- a/apps/plugins/viewers.config
+++ b/apps/plugins/viewers.config
@@ -17,3 +17,4 @@
 wv,wv2wav.rock, 00 00 00 00 00 00
 m3u,iriverify.rock,00 00 00 00 00 00
 mpc,mpc2wav.rock, 00 00 00 00 00 00
+mid,midi2wav.rock, 01 20 30 40 50 60