home *** CD-ROM | disk | FTP | other *** search
/ Aminet 10 / aminetcdnumber101996.iso / Aminet / dev / src / wavetools_dev.lha / wavetools / DadMon / DadMon.c next >
Encoding:
C/C++ Source or Header  |  1995-11-15  |  8.2 KB  |  285 lines

  1. /*
  2. ** Dad Monitor
  3. */
  4.  
  5. char version_string[] = "$VER: DadMon 0.17 (95.11.15)";
  6.  
  7. #include <intuition/intuitionbase.h>
  8. #include <exec/nodes.h>
  9. #include <exec/tasks.h>
  10. #include <libraries/dos.h>
  11. #include <exec/types.h>
  12. #include <exec/memory.h>
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <devices/dad_audio.h>
  16.  
  17. /*
  18. ** Global Variables
  19. */
  20. LONG FREQUENCY,INPUTGAIN,WIN_LEFT, WIN_TOP, WIN_WIDTH, WIN_HEIGHT, UPDATE;
  21. char *filename;    /* global filename pointer pointer, ugly but...*/
  22.  
  23.  
  24. struct IOStdReq *create_dadaudio() {
  25.  
  26.     LONG error;
  27.     struct MsgPort *dadport;
  28.     struct IOStdReq *dadmsg;
  29.  
  30.     if ((dadport = (struct MsgPort *)CreatePort(0,0)) == NULL)
  31.         exit(1);
  32.     if ((dadmsg = (struct IOStdReq *)CreateExtIO(dadport,sizeof(struct IOStdReq))) == NULL)
  33.         exit(1);
  34.     if ((error = OpenDevice(DAD_DEVICENAME,0,(struct IOStdReq *)dadmsg,0)) != 0)
  35.         exit(1);
  36.     return(dadmsg);
  37. }
  38.  
  39.  
  40.  
  41.  
  42. void Monitor(void){
  43.     struct IntuiMessage *message;
  44.     struct UserPort *winport;
  45.     UWORD l_sample=0,r_sample=0;
  46.         int class;
  47.     UWORD *dad_dma_buffer;
  48.     ULONG dad_dma_length=0;
  49.  
  50.     FILE *filehandle=NULL;
  51.     char *chipmembuf;
  52.         UWORD *long_aligned_buffer;
  53.         ULONG address_temp;
  54.     ULONG dad_max_buffer_size = 8084;    /* this number is from monitoring the device */
  55.     struct IOStdReq *dadio=NULL;
  56.         int buffer_size = 64000;
  57.     int QUIT = 0;                  /* exit flag */
  58.  
  59.     /*
  60.     ** Maybe MEMF_24BITDMA is good too?
  61.     ** It should be for reading but I want to write buffer to disk. ie.chipmem?
  62.     */
  63.     if ((chipmembuf = (char *)AllocMem(buffer_size,MEMF_CHIP|MEMF_CLEAR)) == NULL)
  64.         QUIT = 1;
  65.     address_temp = (LONG)chipmembuf + 1;    /* add one long word so next operation stays within memblock */
  66.     address_temp &=    0xFFFFFFFC;        /* truncate address to nearest longword */
  67.     long_aligned_buffer = (UWORD *)address_temp;
  68.  
  69.         if ((winport = (struct UserPort *)OpenFace(WIN_LEFT, WIN_TOP, WIN_WIDTH, WIN_HEIGHT)) == NULL)
  70.             QUIT = 1;         /* open the window  and get a messageport ,wital we use it to quit sampling!! */
  71.  
  72.     if ((dadio = create_dadaudio()) == NULL)
  73.         QUIT = 1;            /* open dad_audio.device */
  74.  
  75.      /*
  76.      ** WARNING no Fail open test
  77.      */
  78.     if (filename) {
  79.         if ((filehandle = (FILE *)Open(filename,MODE_NEWFILE)) == NULL)
  80.             QUIT = 1;
  81.     }
  82.  
  83.     /* setup hardware */
  84.         if (QUIT == 0){
  85.  
  86.     /* NOTE:
  87.     **    I dont know how much of this stuff is needed
  88.     **    maybe only INIT2 and only once
  89.     **    mainly I put it all here to mimic what wavetools does
  90.     **    as seen by devmon.
  91.     */
  92.         dadio->io_Data    = (void *)(DADF_SETFLAG | DADF_INIT);        /* reset hardware        */
  93.               dadio->io_Length  = 0;
  94.         dadio->io_Offset  = 0;
  95.         dadio->io_Command = DADCMD_INIT2;
  96.         dadio->io_Flags   = 0;
  97.         DoIO((struct IOStdReq *)dadio);
  98.  
  99.         dadio->io_Data    = 0x0;
  100.               dadio->io_Length  = 0;
  101.         dadio->io_Offset  = 0;
  102.         dadio->io_Command = DADCMD_INIT1;
  103.         dadio->io_Flags   = 0;
  104.         DoIO((struct IOStdReq *)dadio);            /* io_Actual is some address but I dont use it here */
  105.  
  106.         dadio->io_Data    = (void *)(DADF_SETFLAG | DADF_INIT);        /* reset hardware again        */
  107.               dadio->io_Length  = 0;
  108.         dadio->io_Offset  = 0;
  109.         dadio->io_Command = DADCMD_INIT2;
  110.         dadio->io_Flags   = IOF_QUICK;
  111.         DoIO((struct IOStdReq *)dadio);
  112.     /*
  113.     ** these are definitly needed if you are going to sample
  114.     ** unless you fancy relying on standard values :)
  115.     */
  116.  
  117.         dadio->io_Data    = (void *)0x0;        /* Damping on line out */
  118.               dadio->io_Length  = 0;
  119.         dadio->io_Offset  = 0;
  120.         dadio->io_Command = DADCMD_OUTPUTDAMP;
  121.         dadio->io_Flags   = IOF_QUICK;
  122.         DoIO((struct IOStdReq *)dadio);
  123.  
  124.         dadio->io_Data    = (void *)INPUTGAIN;        /* Gain on line in    */
  125.               dadio->io_Length  = 0;
  126.         dadio->io_Offset  = 0;
  127.         dadio->io_Command = DADCMD_INPUTGAIN;
  128.         dadio->io_Flags   = IOF_QUICK;
  129.         DoIO((struct IOStdReq *)dadio);
  130.  
  131.         dadio->io_Data    = (void *)FREQUENCY;        /* Replay frequency    */
  132.               dadio->io_Length  = 0;
  133.         dadio->io_Offset  = 0;
  134.         dadio->io_Command = DADCMD_REPLAYFREQ;
  135.         dadio->io_Flags   = IOF_QUICK;
  136.         DoIO((struct IOStdReq *)dadio);
  137.  
  138.         dadio->io_Data    = (void *)FREQUENCY;        /* Sampling frequency    */
  139.               dadio->io_Length  = 0;
  140.         dadio->io_Offset  = 0;
  141.         dadio->io_Command = DADCMD_SAMPLEFREQ;
  142.         dadio->io_Flags   = IOF_QUICK;
  143.         DoIO((struct IOStdReq *)dadio);
  144.  
  145.         dadio->io_Data    = 0x0;            /* you dont have to give an address to this */
  146.               dadio->io_Length  = 0;
  147.         dadio->io_Offset  = DAD_BUFFER_SETUP;
  148.         dadio->io_Command = DADCMD_BUFFER;
  149.         dadio->io_Flags   = IOF_QUICK;
  150.         DoIO((struct IOStdReq *)dadio);
  151.                 dad_dma_length = dadio->io_Actual;        /* maximum buffer length returned ie. the length of internal buffers*/
  152.  
  153.         }
  154.  
  155.     while (QUIT == 0) {
  156.  
  157.         dadio->io_Data    = long_aligned_buffer;
  158.            dadio->io_Length  = dad_dma_length;        /* When we did SET_BUFFER (above) we got a return value */
  159.         dadio->io_Offset  = -1;                /* it works with Offset=0, but it returns with io_Offset = -1 so why not use -1*/
  160.         dadio->io_Command = CMD_READ;
  161.         dadio->io_Flags   = IOF_QUICK;
  162.         SendIO((struct IOStdReq *)dadio);        /* read into chipmem buffer    */
  163.                 WaitIO(dadio);                    /* wait for io to finish     */
  164.  
  165.         /*
  166.         ** Dump Chip buffer to disk,
  167.         ** length = last number of bytes sampled into buffer. ie. io_Actual.
  168.         */
  169.         if(filehandle != NULL){
  170.             Write(filehandle,long_aligned_buffer,dadio->io_Actual);
  171.         }
  172.         else {
  173.             l_sample = *(WORD *)long_aligned_buffer;
  174.             r_sample = *(WORD *)(long_aligned_buffer+1);
  175.             MoveFace(l_sample,r_sample);
  176.         }
  177.  
  178.         dadio->io_Data    = 0x0;        /* relative adressing        */
  179.            dadio->io_Length  = dadio->io_Actual;    /* rewind the buffer to start     */
  180.         dadio->io_Offset  = DAD_BUFFER_SWITCH;    /* swap buffers on card        */
  181.         dadio->io_Command = DADCMD_BUFFER;      /* buffer command         */
  182.         dadio->io_Flags   = 0;
  183.         DoIO((struct IOStdReq *)dadio);
  184.         dad_dma_length = dadio->io_Actual;    /* buffer command always returns next readbuffers length */
  185.  
  186.                while (message = (struct IntuiMessage *)GetMsg(winport)) {
  187.             if ((class = message->Class) == CLOSEWINDOW) {
  188.                 ReplyMsg((struct Msg *)message);
  189.                 QUIT=1;        /* close window to quit*/
  190.             }
  191.             else if (class == NEWSIZE) {
  192.                 ReplyMsg((struct Message *)message);
  193.                 FreshFace();
  194.             }
  195.             else if (class == CHANGEWINDOW)
  196.                 ReplyMsg((struct Msg *)message);
  197.         }/* while */
  198.  
  199.  
  200.     } /* while */
  201.  
  202.     /*
  203.     ** Free resources
  204.     */
  205.     if (winport) CloseFace();        /* window stuff */
  206.     if (chipmembuf) FreeMem(chipmembuf,buffer_size);
  207.     if(filehandle) Close(filehandle);
  208.     if (dadio) {
  209.         WaitIO(dadio);    /* wait till all is clear */
  210.         CloseDevice((struct IORequest *)dadio);
  211.         DeleteExtIO((struct IORequest *)dadio);
  212.     }
  213.  
  214. }
  215.  
  216.  
  217. /*
  218. ** Template string and variables used for command line arguments.
  219. */
  220. #define TEMPLATE "FILE/M,F=FREQUENCY/N,G=INPUTGAIN/N,L=LEFT/N,T=TOP/N,W=WIDTH/N,H=HEIGHT/N,UPDATE/N"
  221.  
  222.  
  223. int main(int argc, char *argv[]) {
  224.     struct Task *thistask;
  225.     struct RdArgs *ra;
  226.     LONG args[8] = { 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L };
  227.       struct WBStartup *argmsg;
  228.       struct WBArg *wb_arg;
  229.     char programname[20];
  230.  
  231.  
  232.         /*thistask = (struct Task *)FindTask(NULL);*/
  233.       /*GetProgramName (programname, 19);*/
  234.         if (argc == 0) {
  235.             exit(0);    /* bailout if launched from WB */
  236.     }else {
  237.           if (ra = (struct RdArgs *)ReadArgs(TEMPLATE, args, NULL)) {
  238.  
  239.             filename   = (args[0] ? *(char **)args[0] : NULL);
  240.             FREQUENCY  = (args[1] ? *(LONG *)args[1] : 44100);
  241.             INPUTGAIN  = (args[2] ? *(LONG *)args[2] : 0);
  242.             WIN_LEFT   = (args[3] ? *(LONG *)args[3] : 0);
  243.             WIN_TOP    = (args[4] ? *(LONG *)args[4] : 0);
  244.             WIN_WIDTH  = (args[5] ? *(LONG *)args[5] : 130);
  245.             WIN_HEIGHT = (args[6] ? *(LONG *)args[6] : 100);
  246.             UPDATE     = (args[7] ? *(LONG *)args[7] : 1);
  247.             if (UPDATE < 1)            /* A value of 0 would be unacceptable.*/
  248.                 UPDATE = 1;
  249.         } else {
  250.             /*
  251.             ** IF nothing was given, does RdArgs still allocate?
  252.             ** set defaults here just in case.
  253.             */
  254.             filename = NULL;
  255.             FREQUENCY  = 44100;
  256.             INPUTGAIN  = 0;
  257.             WIN_LEFT   = 0;
  258.             WIN_TOP    = 0;
  259.             WIN_WIDTH  = 130;
  260.             WIN_HEIGHT = 100;
  261.             UPDATE     = 1;
  262.         }
  263.         /*
  264.         ** Dump values to stdout.
  265.         */
  266.  
  267.         printf("%s\n",version_string);
  268.         if (filename) printf("Output    : %s\n",filename);
  269.         printf("Frequency : %i\n",FREQUENCY);
  270.         printf("Inputgain : %i\n",INPUTGAIN);
  271.         printf("Win Left  : %i\n",WIN_LEFT);
  272.         printf("Win Top   : %i\n",WIN_TOP);
  273.         printf("Win Width : %i\n",WIN_WIDTH);
  274.         printf("Win Height: %i\n",WIN_HEIGHT);
  275.         printf("Update    : %i\n",UPDATE);
  276.  
  277.             /*
  278.             ** Do the monitoring stuff
  279.             */
  280.         Monitor();
  281.         if(ra) FreeArgs(ra);    /* dump args */
  282.         }
  283.  
  284. }
  285.