home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <math.h>
- #include <exec/types.h>
- #include <exec/memory.h>
- #include <devices/audio.h>
- #include <proto/dos.h>
- #include <proto/intuition.h>
- #include <graphics/gfxbase.h>
- #include <graphics/gfx.h>
-
- /* define logicals for ErrExit */
-
- #define SUCCESS 0
- #define INTUITION_FAIL 1
- #define GRAPHICS_FAIL 2
- #define PORTPN_FAIL 3
- #define PORTP0_FAIL 4
- #define AUDIOALLOC_FAIL 5
- #define AUDIO_FAIL 6
- #define USERSOUND_FAIL 7
- #define REFSOUND_FAIL 8
- #define SCREEN_FAIL 9
- #define WINDOW_FAIL 10
- #define NBR_IOA_STRUCTS 9
-
- /* Miscellaneous logicals */
-
- #define INTUITION_REV 0
- #define GRAPHICS_REV 0
-
- extern struct IntuitionBase *IntuitionBase;
- extern struct GfxBase *GfxBase;
- extern struct Window *Window;
- extern struct Screen *Screen;
- extern struct ViewPort *ViewPort;
- extern struct RastPort *rport;
- extern struct IOAudio *ioa,*audio,*finish[4],*audchan[4];
- extern struct Border brdi1;
- extern struct IntuiText textamp;
- extern struct Gadget select8;
-
- extern SHORT sinewave[8][513];
- extern BYTE *usersound,*refsound;
- extern char textbuf[6];
- extern LONG amps[8],refamps[8];
- extern USHORT userwave[513][2],refwave[513][2],selwave[513][2];
-
- extern BYTE sunit;
-
- /**************************************************************/
- VOID OpenLibs()
-
-
- /* Open all required libraries */
-
- {
-
- extern VOID ErrExit();
-
- /* ==Open Intuition... */
-
- IntuitionBase = (struct IntuitionBase *)
- OpenLibrary("intuition.library",INTUITION_REV);
- if(IntuitionBase == NULL)
- ErrExit(INTUITION_FAIL);
-
- /* ==Open Graphics... */
- GfxBase = (struct GfxBase *)
- OpenLibrary("graphics.library",GRAPHICS_REV);
- if(GfxBase == NULL)
- ErrExit(GRAPHICS_FAIL);
-
- }
-
- /*******************************************************************/
- VOID WaveCleanup()
- /* ======Close down the window, the screen, and the libraries====== */
- {
- int i;
-
- ClearMenuStrip(Window);
- CloseWindow(Window);
- CloseScreen(Screen);
- FreeMem(refsound,516);
- FreeMem(usersound,516);
- for (i=0; i<4; i++) CloseDevice(audchan[i]);
- CloseLibrary(GfxBase);
- CloseLibrary(IntuitionBase);
- exit(NULL);
- }
-
- /******************************************************************/
- VOID ErrExit(SHORT errno)
-
- /* This routine gets called if a fatal error occurs. It ends */
- /* the program gracefully by closing all of the resources opened */
- /* to that point. */
-
- {
- int i;
- char *ErrMess[]={"Successful Completion\n",
- "Unable to open Intuition.library\n",
- "Unable to open Graphics.library\n",
- "Unable to allocate port PN\n",
- "Unable to allocate port P0\n",
- "Unable to allocate audio memory\n",
- "Unable to open audio device\n",
- "Unable to allocate usersound\n",
- "Unable to allocate refsound\n",
- "Unable to open Screen\n",
- "Unable to open Window\n"};
-
- printf(ErrMess[errno]);
-
- switch(errno) /* Yes, the fall-through is deliberate */
- {
- case SUCCESS:
- ClearMenuStrip(Window);
- CloseWindow(Window);
- case WINDOW_FAIL:
- CloseScreen(Screen);
- case SCREEN_FAIL:
- FreeMem(refsound,516);
- case REFSOUND_FAIL:
- FreeMem(usersound,516);
- case USERSOUND_FAIL:
- for (i=0; i<4; i++) CloseDevice(audchan[i]);
- case AUDIOALLOC_FAIL:
- case AUDIO_FAIL:
- case PORTP0_FAIL:
- case PORTPN_FAIL:
- CloseLibrary(GfxBase);
- case GRAPHICS_FAIL:
- CloseLibrary(IntuitionBase);
- case INTUITION_FAIL:
- break;
- }
- Exit(FALSE);
- }
-
- /*************************************************************/
- VOID MakeDisplay()
-
- /* Opens the screen and window, and creates the initial display */
-
- {
- VOID DrawBoxes(),MakePalette();
- struct Screen *WaveScreen();
- struct Window *MakeWindow();
-
- Screen = WaveScreen();
- if (Screen == NULL) ErrExit(SCREEN_FAIL);
-
- Window = MakeWindow();
- if (Window == NULL) ErrExit(WINDOW_FAIL);
-
- MakePalette(Window);
-
- DrawBoxes();
- }
-
- /*************************************************************/
- VOID DrawBoxes()
-
- /* Draws the waveform display area and the boxes around the */
- /* numerical display */
- {
- SHORT i;
- static SHORT boxx[8]={34,108,182,256,330,404,478,552};
-
- /* Draw waveform display area */
- rport = Window->RPort;
- SetAPen(rport,5);
- RectFill(rport,40,14,601,109);
- SetAPen(rport,2);
- RectFill(rport,44,12,597,111);
- SetAPen(rport,1);
- RectFill(rport,64,14,577,109);
- SetAPen(rport,3);
- RectFill(rport,64,61,577,61);
-
- /* Draw boxes for digital display of amplitudes */
- for (i=0;i<8;++i)
- {
- DrawBorder(rport,&brdi1,boxx[i],188);
- sprintf(textbuf,"%5d",amps[i]);
- PrintIText(rport,&textamp,boxx[i],188);
- }
- }
-
- /****************************************************************/
- VOID InitArrays()
- /* Initialize data arrays for waveform displays */
- {
- SHORT i;
-
- for (i=0;i<513;++i)
- {
- userwave[i][0]=refwave[i][0]=selwave[i][0]=i;
- userwave[i][1]=refwave[i][1]=selwave[i][1]=0;
- }
- }
-
- /***************************************************************/
- VOID MakePalette(Window)
- struct Window *Window;
-
- /* Sets the color palette */
-
- {
- ViewPort = (struct ViewPort *)ViewPortAddress(Window);
-
- /* ==Set palette... */
-
- SetRGB4(ViewPort, 0, 8, 5, 2); /* Medium brown */
- SetRGB4(ViewPort, 1, 15, 12, 9); /* Ivory */
- SetRGB4(ViewPort, 2, 5, 1, 0); /* Brown */
- SetRGB4(ViewPort, 3, 4, 4, 6); /* dark blue */
- SetRGB4(ViewPort, 4, 11, 0, 0); /* Red */
- SetRGB4(ViewPort, 5, 13, 7, 0); /* Orange */
- SetRGB4(ViewPort, 6, 3, 6, 1); /* Green */
- SetRGB4(ViewPort, 7, 0, 0, 0); /* Black */
- return;
- }
-
- /*****************************************************************/
- struct Screen *WaveScreen()
-
- {
- /* ==Open a custom screen... */
-
- struct NewScreen NewScreen =
- { 0, 0, 640, 200, /* LeftEdge, TopEdge, Width, Height */
- 3, 0x00, 0x01, /* Depth, DetailPen, BlockPen */
- HIRES,CUSTOMSCREEN, /* ViewModes, Type */
- NULL,"WaveScreen",NULL,/* Font, DefaultTitle, Gadgets */
- NULL /* CustomBitMap */
- };
-
- return(OpenScreen(&NewScreen));
- }
-
- /******************************************************************/
- struct Window *MakeWindow()
-
- /* This function initializes the NewWindow structure and opens
- the window */
-
- {
- struct NewWindow NewWindow;
- ULONG flags;
- USHORT iflags;
-
- /* ==Open the window... */
-
- flags=SMART_REFRESH|WINDOWSIZING|WINDOWDRAG|WINDOWCLOSE;
- iflags=CLOSEWINDOW|REFRESHWINDOW|GADGETDOWN|GADGETUP|RAWKEY|
- MENUPICK;
- NewWindow.LeftEdge=0;
- NewWindow.TopEdge=0;
- NewWindow.Width=640;
- NewWindow.Height=200;
- NewWindow.DetailPen=0x00;
- NewWindow.BlockPen=0x01;
- NewWindow.Flags=flags;
- NewWindow.IDCMPFlags=iflags;
- NewWindow.FirstGadget=&select8;
- NewWindow.CheckMark=NULL;
- NewWindow.Title="WaveMaker 1.2";
- NewWindow.Screen=Screen;
- NewWindow.BitMap=NULL;
- NewWindow.MinWidth=16;
- NewWindow.MinHeight=16;
- NewWindow.MaxWidth=640;
- NewWindow.MaxHeight=200;
- NewWindow.Type=CUSTOMSCREEN;
-
- return(OpenWindow(&NewWindow));
-
- }
-
- /*********************************************************************/
- VOID MakeSin()
-
- /* Initialize the sine wave arrays for all eight frequencies. */
- /* This saves a lot of time later. */
-
- {
- SHORT i,j,k;
- DOUBLE step,stepi,sinstep;
-
- step = 6.283185/512.0;
- for (i=0; i<513; ++i)
- {
- stepi = i*step;
- sinstep = sin(stepi);
- sinewave[0][i]=sinstep*0x7FFF;
- /* sinewave[0][i]=sin(i*step)*0x7FFF; */
- }
- for (j=1; j<8; ++j)
- {
- for (i=0; i<513; ++i)
- {
- k=((j+1)*i)%512;
- sinewave[j][i]=sinewave[0][k];
- }
- }
- }
-
- /****************************************************************/
- VOID OpenAudio()
-
- /* Alloc and open the audio device and initialize the structures */
-
- {
- struct MsgPort *CreatePort();
- char *portstring[]={"aud0","aud1","aud2","aud3"};
- SHORT i;
- static UBYTE chanbits[4]={1,2,4,8},sunit;
-
- /* allocate memory for IOAudio structures */
-
- ioa=(struct IOAudio *)AllocMem((NBR_IOA_STRUCTS*sizeof(*ioa)),
- MEMF_PUBLIC|MEMF_CLEAR);
- if (ioa==NULL) ErrExit(AUDIOALLOC_FAIL);
-
- /* set the IOAudio structure pointers */
-
- for (i=0;i<4;i++)
- {
- audchan[i] = &ioa[i];
- finish[i] = &ioa[4+i];
- };
-
- /* open the audio devices */
-
- for (i=0; i<4; i++)
- {
- audchan[i]->ioa_Request.io_Message.mn_Node.ln_Pri=10;
- audchan[i]->ioa_Data=&chanbits[i];
- audchan[i]->ioa_Length=(ULONG)sizeof(sunit);
- audchan[i]->ioa_Request.io_Message.mn_ReplyPort=
- CreatePort(portstring[i],0);
- if (audchan[i]->ioa_Request.io_Message.mn_ReplyPort==NULL)
- ErrExit(PORTP0_FAIL);
- if((OpenDevice(AUDIONAME,0,audchan[i],0))!=NULL)
- ErrExit(AUDIO_FAIL);
- audchan[i]->ioa_Request.io_Command=CMD_WRITE;
- audchan[i]->ioa_Request.io_Flags=IOF_QUICK|ADIOF_PERVOL;
- audchan[i]->ioa_Cycles=0;
- audchan[i]->ioa_Length=128;
- *finish[i] = *audchan[i];
- finish[i]->ioa_Request.io_Flags = IOF_QUICK;
- finish[i]->ioa_Request.io_Command = ADCMD_FINISH;
- }
- if((usersound = (BYTE *)AllocMem(516, MEMF_CHIP))==0)
- ErrExit(USERSOUND_FAIL);
- if((refsound = (BYTE *)AllocMem(516, MEMF_CHIP))==0)
- ErrExit(REFSOUND_FAIL);
- }
-