/* mt32 reverb main, testg.c */

#include <exec/types.h>
#include <intuition/intuition.h>
#include <midi.h>
#include <midibase.h>
#include "midiprog1:reverb/reverb.h" /* modify to indicate where you put reverb.h*/

void *MidiBase = NULL;
void *GfxBase = NULL;
void *IntuitionBase = NULL;

struct Window *Window;
struct IntuiMessage *message;

extern calccheck ();       /* in event.c */
extern senddisplay ();     /* in event.c */

/* midi declarations*/
struct MDest *dest;
struct MRoute *routed;
struct MSource *source=0;
struct MRoute *routes=0;

/* this is the easy way out, I am using predefined arrays, and will
   change the fields that need to be changed as I go along.
   Arrays are rs [], used to reset reverb settings
              data [], data that is sent to change the reverb
              disp [], text to be displayed by the MT-32 */

UBYTE rs [] = { 
   MS_SYSEX,MID_ROLAND,
   0x10,0x16,        /* device id,model id */
   0x12,             /* rq1 command */
   0x10,0x00,0x01,   /* addmsb,addmid,addlsb */
   0x00,0x00,0x00,   /* mode, time, level  */
   0,                /* checksum */
   MS_EOX
};

UBYTE data [] = { 
   MS_SYSEX,MID_ROLAND,
   0x10,0x16,          /* device id,model id */
   0x12,               /* dt1 command */
   0x10,0x00,0x01,     /* addmsb,addmid,addlsb=1 mode,=2 time, =3 level */
   0x00,               /* reverb mode or time or level */
   0,                  /* checksum */
   MS_EOX
};

UBYTE disp [] = { 
   MS_SYSEX,MID_ROLAND,
   0x10,0x16,        /* device id,model id */
   0x12,             /* dt1 command */
   0x20,0x00,0x00,   /* display area address  */
   ' ',' ','R','E','V','E','R','B',' ',' ',
   ' ',' ','R','E','S','E','T',' ',' ',' ',
   0,                /* checksum */
   MS_EOX
};

clean () {
   if (routed) DeleteMRoute (routed);
   if (dest) DeleteMDest (dest);
   if (routes) DeleteMRoute (routes);
   if (source) DeleteMSource (source);
   if (Window) ClearMenuStrip(Window);
   if (Window) CloseWindow (Window);
   if (IntuitionBase) CloseLibrary (IntuitionBase);
   if (GfxBase) CloseLibrary (GfxBase);
   if (MidiBase) CloseLibrary (MidiBase);
}

process () {          /* process until closewindow is received */

   LONG closing = 1L;
   ULONG class;
   UBYTE code;
   APTR address;

   while (closing) {

      Wait(1<<Window->UserPort->mp_SigBit);
      while (message=(struct IntuiMessage *)GetMsg(Window->UserPort)) {

         class = message->Class;
         code = message->Code;
         address = message->IAddress;
         ReplyMsg(message);


         closing = (HandleEvent(class,code,address));

      }
   }
}

/* reset mt-32 reverb settings to all 0s*/
resetrev () {

   calccheck (11L,&rs[0]);
   PutMidiMsg (source,&rs[0]);
}

OpenAll() {   /* opens the necessary libraries */


   IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",0);
      if(IntuitionBase==NULL) {
      clean();
      exit(FALSE);
   }

   GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0);
      if(GfxBase==NULL) {
      clean();
      exit(FALSE);
   }

   MidiBase=OpenLibrary("midi.library",0);

      if(MidiBase==NULL) {
      clean();
      exit(FALSE);
   }
}
make_window() {	

   Window=(struct Window *) OpenWindow(&NewWindowStructure1);
   SetMenuStrip(Window,&MenuList1);
   PrintIText(Window->RPort,&IntuiTextList1,0,0);
}

main() {

   static struct MRouteInfo routesinfo = { -1, -1};
   static struct MRouteInfo routedinfo = { MMF_SYSEX, -1};

   OpenAll();

   if (!(dest = CreateMDest (NULL,NULL))) {
      clean();
      exit(FALSE);
   }

   if (!(routed = MRouteDest ("MidiIn",dest,&routedinfo))) {
      clean();
      exit(FALSE);
   }

   if (!(source = CreateMSource (NULL,NULL))) {
      clean();
      exit(FALSE);
   }

   if (!(routes = MRouteSource (source,"MidiOut",&routesinfo))) {
      clean();
      exit(FALSE);
   }

   make_window();
   resetrev();
   senddisplay (28L);      /* will work only if MT-32 is displaying
                              the main volume and channels info */
   process ();
   clean();
}

