home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 519b.lha / WaveMaker_v1.2 / setup.c < prev    next >
C/C++ Source or Header  |  1991-06-09  |  9KB  |  361 lines

  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <exec/types.h>
  4. #include <exec/memory.h>
  5. #include <devices/audio.h>
  6. #include <proto/dos.h>
  7. #include <proto/intuition.h>
  8. #include <graphics/gfxbase.h>
  9. #include <graphics/gfx.h>
  10.  
  11. /* define logicals for ErrExit */
  12.  
  13. #define SUCCESS         0
  14. #define INTUITION_FAIL  1
  15. #define GRAPHICS_FAIL   2
  16. #define PORTPN_FAIL     3
  17. #define PORTP0_FAIL     4
  18. #define AUDIOALLOC_FAIL 5
  19. #define AUDIO_FAIL      6
  20. #define USERSOUND_FAIL  7
  21. #define REFSOUND_FAIL   8
  22. #define SCREEN_FAIL     9
  23. #define WINDOW_FAIL    10
  24. #define NBR_IOA_STRUCTS 9
  25.  
  26. /*  Miscellaneous logicals */
  27.  
  28. #define INTUITION_REV 0
  29. #define GRAPHICS_REV 0
  30.  
  31. extern struct IntuitionBase *IntuitionBase;
  32. extern struct GfxBase *GfxBase;
  33. extern struct Window *Window;
  34. extern struct Screen *Screen;
  35. extern struct ViewPort *ViewPort;
  36. extern struct RastPort *rport;
  37. extern struct IOAudio *ioa,*audio,*finish[4],*audchan[4];
  38. extern struct Border brdi1;
  39. extern struct IntuiText textamp;
  40. extern struct Gadget select8;
  41.  
  42. extern SHORT sinewave[8][513];
  43. extern BYTE *usersound,*refsound;
  44. extern char textbuf[6];
  45. extern LONG amps[8],refamps[8];
  46. extern USHORT userwave[513][2],refwave[513][2],selwave[513][2];
  47.  
  48. extern BYTE sunit;
  49.  
  50. /**************************************************************/     
  51. VOID OpenLibs()
  52.     
  53.  
  54. /* Open all required libraries */
  55.  
  56. {
  57.  
  58. extern VOID ErrExit();
  59.  
  60. /*  ==Open Intuition... */
  61.  
  62.        IntuitionBase = (struct IntuitionBase *)
  63.               OpenLibrary("intuition.library",INTUITION_REV);
  64.        if(IntuitionBase == NULL) 
  65.       ErrExit(INTUITION_FAIL);
  66.  
  67. /*  ==Open Graphics...  */
  68.         GfxBase = (struct GfxBase *)
  69.           OpenLibrary("graphics.library",GRAPHICS_REV);
  70.     if(GfxBase == NULL)
  71.             ErrExit(GRAPHICS_FAIL);
  72.  
  73. }  
  74.         
  75. /*******************************************************************/
  76. VOID WaveCleanup()
  77. /* ======Close down the window, the screen, and the libraries====== */
  78. {
  79.         int i;
  80.  
  81.     ClearMenuStrip(Window);
  82.     CloseWindow(Window);  
  83.       CloseScreen(Screen);
  84.     FreeMem(refsound,516);
  85.     FreeMem(usersound,516);
  86.     for (i=0; i<4; i++) CloseDevice(audchan[i]);
  87.     CloseLibrary(GfxBase);
  88.       CloseLibrary(IntuitionBase);
  89.       exit(NULL);
  90. }
  91.  
  92. /******************************************************************/
  93. VOID ErrExit(SHORT errno)
  94.  
  95. /*  This routine gets called if a fatal error occurs.  It ends    */
  96. /*  the program gracefully by closing all of the resources opened */
  97. /*  to that point.                                                */
  98.  
  99. {
  100.         int i;
  101.     char *ErrMess[]={"Successful Completion\n",
  102.              "Unable to open Intuition.library\n",
  103.                  "Unable to open Graphics.library\n",
  104.              "Unable to allocate port PN\n",
  105.              "Unable to allocate port P0\n",
  106.              "Unable to allocate audio memory\n",
  107.              "Unable to open audio device\n",
  108.              "Unable to allocate usersound\n",
  109.              "Unable to allocate refsound\n",
  110.              "Unable to open Screen\n",
  111.              "Unable to open Window\n"};
  112.     
  113.     printf(ErrMess[errno]);    
  114.     
  115.     switch(errno)            /* Yes, the fall-through is deliberate */
  116.     {
  117.     case SUCCESS:
  118.         ClearMenuStrip(Window);
  119.         CloseWindow(Window);
  120.     case WINDOW_FAIL:
  121.         CloseScreen(Screen);
  122.     case SCREEN_FAIL:
  123.         FreeMem(refsound,516);
  124.     case REFSOUND_FAIL:
  125.         FreeMem(usersound,516);
  126.     case USERSOUND_FAIL:
  127.         for (i=0; i<4; i++) CloseDevice(audchan[i]);
  128.     case AUDIOALLOC_FAIL:
  129.     case AUDIO_FAIL:
  130.     case PORTP0_FAIL:
  131.     case PORTPN_FAIL:
  132.         CloseLibrary(GfxBase);
  133.     case GRAPHICS_FAIL:
  134.         CloseLibrary(IntuitionBase);
  135.     case INTUITION_FAIL:
  136.         break;
  137.     }
  138.     Exit(FALSE);
  139. }
  140.  
  141. /*************************************************************/
  142. VOID MakeDisplay()
  143.  
  144. /*  Opens the screen and window, and creates the initial display  */
  145.  
  146. {    
  147.     VOID DrawBoxes(),MakePalette();
  148.         struct Screen *WaveScreen();
  149.         struct Window *MakeWindow();
  150.         
  151.     Screen = WaveScreen();
  152.     if (Screen == NULL) ErrExit(SCREEN_FAIL);
  153.     
  154.     Window = MakeWindow();
  155.     if (Window == NULL) ErrExit(WINDOW_FAIL);
  156.     
  157.     MakePalette(Window);
  158.     
  159.     DrawBoxes();
  160. }
  161.  
  162. /*************************************************************/
  163. VOID DrawBoxes()
  164.  
  165. /*  Draws the waveform display area and the boxes around the */
  166. /*  numerical display                                        */
  167. {
  168.     SHORT i;
  169.     static SHORT boxx[8]={34,108,182,256,330,404,478,552};
  170.     
  171. /*  Draw waveform display area  */
  172.     rport = Window->RPort;
  173.     SetAPen(rport,5);
  174.     RectFill(rport,40,14,601,109);
  175.     SetAPen(rport,2);
  176.     RectFill(rport,44,12,597,111);
  177.     SetAPen(rport,1);
  178.     RectFill(rport,64,14,577,109);
  179.     SetAPen(rport,3);
  180.     RectFill(rport,64,61,577,61);
  181.  
  182. /*  Draw boxes for digital display of amplitudes   */
  183.     for (i=0;i<8;++i)
  184.     {
  185.         DrawBorder(rport,&brdi1,boxx[i],188);
  186.         sprintf(textbuf,"%5d",amps[i]);
  187.         PrintIText(rport,&textamp,boxx[i],188);
  188.     }
  189. }
  190.  
  191. /****************************************************************/
  192. VOID InitArrays()    
  193. /*  Initialize data arrays for waveform displays  */
  194. {
  195.         SHORT i;
  196.     
  197.     for (i=0;i<513;++i)
  198.     {
  199.         userwave[i][0]=refwave[i][0]=selwave[i][0]=i;
  200.         userwave[i][1]=refwave[i][1]=selwave[i][1]=0;
  201.     }
  202. }
  203.  
  204. /***************************************************************/
  205. VOID MakePalette(Window)
  206. struct Window *Window;
  207.  
  208. /*  Sets the color palette  */
  209.  
  210. {
  211.     ViewPort = (struct ViewPort *)ViewPortAddress(Window);
  212.  
  213. /*  ==Set palette...  */
  214.  
  215.      SetRGB4(ViewPort, 0,  8,  5,  2);    /* Medium brown */
  216.     SetRGB4(ViewPort, 1, 15, 12,  9);    /* Ivory        */
  217.     SetRGB4(ViewPort, 2,  5,  1,  0);    /* Brown        */
  218.     SetRGB4(ViewPort, 3,  4,  4,  6);    /* dark  blue   */
  219.     SetRGB4(ViewPort, 4, 11,  0,  0);    /* Red          */
  220.     SetRGB4(ViewPort, 5, 13,  7,  0);    /* Orange       */
  221.     SetRGB4(ViewPort, 6,  3,  6,  1);    /* Green        */
  222.     SetRGB4(ViewPort, 7,  0,  0,  0);    /* Black        */    
  223.    return;
  224. }
  225.  
  226. /*****************************************************************/
  227. struct Screen *WaveScreen()
  228.  
  229. {
  230. /*  ==Open a custom screen...  */
  231.  
  232.        struct NewScreen NewScreen =
  233.        {    0, 0, 640, 200,      /* LeftEdge, TopEdge, Width, Height */
  234.         3, 0x00, 0x01,       /* Depth, DetailPen, BlockPen       */
  235.         HIRES,CUSTOMSCREEN,  /* ViewModes, Type                  */
  236.         NULL,"WaveScreen",NULL,/* Font, DefaultTitle, Gadgets    */
  237.         NULL                 /* CustomBitMap                     */
  238.     };
  239.   
  240.        return(OpenScreen(&NewScreen));
  241. }
  242.  
  243. /******************************************************************/
  244. struct Window *MakeWindow()
  245.  
  246. /*  This function initializes the NewWindow structure and opens 
  247.     the window */
  248.  
  249. {
  250.     struct NewWindow NewWindow;
  251.      ULONG flags;
  252.     USHORT iflags;
  253.     
  254. /*  ==Open the window...  */
  255.  
  256.        flags=SMART_REFRESH|WINDOWSIZING|WINDOWDRAG|WINDOWCLOSE;
  257.        iflags=CLOSEWINDOW|REFRESHWINDOW|GADGETDOWN|GADGETUP|RAWKEY|
  258.         MENUPICK;
  259.         NewWindow.LeftEdge=0;
  260.         NewWindow.TopEdge=0;
  261.         NewWindow.Width=640;
  262.         NewWindow.Height=200;
  263.         NewWindow.DetailPen=0x00;
  264.         NewWindow.BlockPen=0x01;
  265.         NewWindow.Flags=flags;
  266.         NewWindow.IDCMPFlags=iflags;
  267.         NewWindow.FirstGadget=&select8;
  268.         NewWindow.CheckMark=NULL;
  269.        NewWindow.Title="WaveMaker 1.2";
  270.          NewWindow.Screen=Screen;
  271.         NewWindow.BitMap=NULL;
  272.         NewWindow.MinWidth=16;
  273.         NewWindow.MinHeight=16;
  274.         NewWindow.MaxWidth=640;
  275.         NewWindow.MaxHeight=200;
  276.         NewWindow.Type=CUSTOMSCREEN;
  277.     
  278.         return(OpenWindow(&NewWindow));
  279.     
  280. }
  281.  
  282. /*********************************************************************/
  283. VOID MakeSin()
  284.  
  285. /*  Initialize the sine wave arrays for all eight frequencies.        */
  286. /*  This saves a lot of time later.                                   */
  287.  
  288. {
  289.     SHORT i,j,k;
  290.     DOUBLE step,stepi,sinstep;
  291.     
  292.     step = 6.283185/512.0;
  293.     for (i=0; i<513; ++i)
  294.         {
  295.               stepi = i*step;
  296.               sinstep = sin(stepi);
  297.               sinewave[0][i]=sinstep*0x7FFF;
  298. /*              sinewave[0][i]=sin(i*step)*0x7FFF;  */
  299.         }
  300.     for (j=1; j<8; ++j)
  301.     {
  302.         for (i=0; i<513; ++i)
  303.         {
  304.             k=((j+1)*i)%512;
  305.             sinewave[j][i]=sinewave[0][k];
  306.         }
  307.     }
  308. }
  309.  
  310. /****************************************************************/
  311. VOID OpenAudio()
  312.  
  313. /*  Alloc and open the audio device and initialize the structures */
  314.  
  315. {
  316.         struct MsgPort *CreatePort();
  317.     char *portstring[]={"aud0","aud1","aud2","aud3"};
  318.     SHORT i;
  319.     static UBYTE chanbits[4]={1,2,4,8},sunit;
  320.     
  321. /*  allocate memory for IOAudio structures   */
  322.  
  323.     ioa=(struct IOAudio *)AllocMem((NBR_IOA_STRUCTS*sizeof(*ioa)),
  324.         MEMF_PUBLIC|MEMF_CLEAR);
  325.     if (ioa==NULL) ErrExit(AUDIOALLOC_FAIL);
  326.     
  327. /*  set the IOAudio structure pointers  */
  328.  
  329.         for (i=0;i<4;i++)
  330.         {
  331.           audchan[i] = &ioa[i];
  332.       finish[i] = &ioa[4+i];
  333.         };
  334.     
  335. /* open the audio devices   */
  336.  
  337.         for (i=0; i<4; i++)
  338.         {
  339.            audchan[i]->ioa_Request.io_Message.mn_Node.ln_Pri=10;
  340.        audchan[i]->ioa_Data=&chanbits[i];
  341.        audchan[i]->ioa_Length=(ULONG)sizeof(sunit);
  342.        audchan[i]->ioa_Request.io_Message.mn_ReplyPort=
  343.                    CreatePort(portstring[i],0);
  344.        if (audchan[i]->ioa_Request.io_Message.mn_ReplyPort==NULL)
  345.          ErrExit(PORTP0_FAIL);
  346.        if((OpenDevice(AUDIONAME,0,audchan[i],0))!=NULL) 
  347.                  ErrExit(AUDIO_FAIL);
  348.        audchan[i]->ioa_Request.io_Command=CMD_WRITE;
  349.            audchan[i]->ioa_Request.io_Flags=IOF_QUICK|ADIOF_PERVOL;
  350.        audchan[i]->ioa_Cycles=0;
  351.        audchan[i]->ioa_Length=128;
  352.            *finish[i] = *audchan[i];
  353.            finish[i]->ioa_Request.io_Flags = IOF_QUICK;
  354.        finish[i]->ioa_Request.io_Command = ADCMD_FINISH;
  355.     }
  356.     if((usersound = (BYTE *)AllocMem(516, MEMF_CHIP))==0) 
  357.         ErrExit(USERSOUND_FAIL);
  358.     if((refsound = (BYTE *)AllocMem(516, MEMF_CHIP))==0)
  359.         ErrExit(REFSOUND_FAIL);
  360. }
  361.