home *** CD-ROM | disk | FTP | other *** search
/ The Best of Mecomp Multimedia 2 / MECOMP-CD-II.iso / amiga / datatypes / mididt / source / playaudio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-10-04  |  5.5 KB  |  210 lines

  1. /*
  2. ** $PROJECT: playaudio
  3. **
  4. ** $VER: playaudio.c 1.0 (11.9.97)
  5. **
  6. ** Copyright 1997 by Martin Gierich.
  7. ** Inspired by playsound.c and examinedt.c written by David N. Junod.
  8. **
  9. ** Plays sound samples and midi music using datatypes (CLI only).
  10. ** sound.datatype V40 or better is recommended for detecting end of sample
  11. ** playback, otherwise you have to stop it using CTRL-C.
  12. **
  13. ** Calling template: NAME/A,VOLUME/N,CYCLES/N,REPEAT/S,IMMEDIATE/S,CLUSTER/K
  14. ** NAME - name of file to play
  15. ** VOLUME - main playback volume
  16. ** CYCLES - number of repeats
  17. ** REPEAT - infinite repeats (does not work with sound.datatype V39/40)
  18. ** IMMEDIATE - immediately start playing without sending STM_PLAY (does not
  19. **             work with sound.datatype V39/40).
  20. ** CLUSTER - name of CAMD cluster for midi playback
  21. **
  22. ** $TABSIZE: 3
  23. */
  24.  
  25. #include <string.h>
  26. #include <exec/types.h>
  27. #include <datatypes/datatypesclass.h>
  28. #include <datatypes/soundclass.h>
  29. #include <datatypes/midiclass.h>
  30.  
  31. #include <proto/dos.h>
  32. #include <proto/exec.h>
  33. #include <proto/datatypes.h>
  34.  
  35. /*****************************************************************************/
  36.  
  37. /* Prototype */
  38. int playfile(ULONG options[]);
  39.  
  40. /* Disable CTRL-C handling */
  41. #ifdef __SASC
  42. void __regargs __chkabort(void) { }
  43. void __regargs _CXBRK(void) { }
  44. #endif
  45.  
  46. /*****************************************************************************/
  47.  
  48. /* Define command line arguments for ReadArgs() */
  49. #define TEMPLATE "NAME/A,VOLUME/N,CYCLES/N,REPEAT/S,IMMEDIATE/S,CLUSTER/K"
  50. enum
  51. {
  52.     OPT_NAME,
  53.     OPT_VOL,
  54.     OPT_CYC,
  55.     OPT_REP,
  56.     OPT_IMM,
  57.     OPT_CLU,
  58.     NUM_OPTS
  59. };
  60.  
  61. /*****************************************************************************/
  62.  
  63. /* The main routine analyses the file and calls playfile() */
  64. int main (int argc, char **argv)
  65. {
  66.     /* Argument parsing variables */
  67.     ULONG options[NUM_OPTS];
  68.     struct RDArgs *rdargs;
  69.  
  70.     /* Datatype variables */
  71.     struct DataTypeHeader *dth;
  72.     struct DataType *dtn;
  73.     BPTR lock;
  74.     ULONG groupid;
  75.  
  76.     /* Parse arguments */
  77.     memset (options, 0, sizeof(options));
  78.     if (rdargs = ReadArgs(TEMPLATE, (LONG *)options, NULL))
  79.     {
  80.  
  81.         if (lock = Lock((STRPTR) options[OPT_NAME], ACCESS_READ))
  82.         {
  83.             /* Determine the DataType of the file */
  84.             if (dtn = ObtainDataTypeA(DTST_FILE, (APTR) lock, NULL))
  85.             {
  86.                 /* Get the DataType header and group ID*/
  87.                 dth = dtn->dtn_Header;
  88.                 groupid = dth->dth_GroupID;
  89.  
  90.                 /* Give some informations */
  91.                 Printf("Information on: %s\n", options[OPT_NAME]);
  92.                 Printf("   Description: %s\n", dth->dth_Name);
  93.                 Printf("     Base Name: %s\n", dth->dth_BaseName);
  94.                 Printf("          Type: %s\n", GetDTString((dth->dth_Flags & DTF_TYPE_MASK) + DTMSG_TYPE_OFFSET));
  95.                 Printf("         Group: %s\n", GetDTString(groupid));
  96.                 Printf("            ID: 0x%lx\n\n", dth->dth_ID);
  97.  
  98.                 /* Release the DataType */
  99.                 ReleaseDataType (dtn);
  100.  
  101.                 /* Now check for the correct DataType before playing file */
  102.                 if (groupid==GID_SOUND || groupid==GID_MUSIC)
  103.                     playfile(options);
  104.                 else
  105.                     Printf("Error: Need sound or music file.\n");
  106.             }
  107.             else
  108.                 PrintFault(IoErr (), "Error");
  109.             UnLock(lock);
  110.         }
  111.         else
  112.             PrintFault(IoErr (), "Error");
  113.  
  114.     /* Free the allocated memory after ReadArgs */
  115.     FreeArgs (rdargs);
  116.     }
  117.     else
  118.         /* Show the failure message */
  119.         PrintFault(IoErr (), "Error");
  120.  
  121.     return(0);
  122. }
  123.  
  124.  
  125.  
  126. /* This routine plays the sound or midi data from a file */
  127. int playfile(ULONG options[])
  128. {
  129.     /* Object variables */
  130.     STRPTR name=NULL;
  131.     STRPTR objname=NULL;
  132.     STRPTR title=NULL;
  133.     ULONG  sig;
  134.     Object *obj;
  135.  
  136.     /* Clear signals to be used, so we can recognize end of playback */
  137.     SetSignal(0L, SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_F);
  138.  
  139.     /* Open the sound or midi object */
  140.     if (obj = NewDTObject (
  141.         /* Set the source file name */
  142.         (APTR) options[OPT_NAME],
  143.         /* Say that the source is a file */
  144.         DTA_SourceType,    DTST_FILE,
  145.         /* Set attributes from the commandline */
  146.         DTA_Repeat,            options[OPT_REP],
  147.         DTA_Immediate,        options[OPT_IMM],
  148.         SDTA_Volume,        options[OPT_VOL] ? *(LONG*) options[OPT_VOL] : 64,
  149.         SDTA_Cycles,        options[OPT_CYC] ? *(LONG*) options[OPT_CYC] : 1,
  150.         options[OPT_CLU] ? MDTA_Cluster : DTA_Dummy, (APTR) options[OPT_CLU],
  151.         /* We want to be notified when the sound stops playing, so
  152.          * we provide a signal task and a signal (CTRL-F) */
  153.         SDTA_SignalTask,    (ULONG) FindTask (NULL),
  154.         SDTA_SignalBit,    (ULONG) SIGBREAKF_CTRL_F,
  155.         /* No more attributes */
  156.         TAG_DONE))
  157.     {
  158.  
  159.         /* Get information about the object */
  160.         if (GetDTAttrs (obj,
  161.             DTA_ObjName,(ULONG) &objname,
  162.             DTA_Name,    (ULONG) &name,
  163.             DTA_Title,    (ULONG) &title,
  164.             TAG_DONE))
  165.         {
  166.             Printf("       ObjName: %s\n", objname ? objname : (STRPTR)"[none]");
  167.             Printf("          Name: %s\n", name ? name : (STRPTR)"[none]");
  168.             Printf("         Title: %s\n", title ? title : (STRPTR)"[none]");
  169.         }
  170.  
  171.         /* Start playing the sound, if it has not started immediately */
  172.         if (!options[OPT_IMM])
  173.             DoMethod(obj, DTM_TRIGGER, NULL, STM_PLAY, NULL);
  174.         Printf("\nPlaying sound...\n");
  175.  
  176.         /* Wait till the datatype tells us, that it is all over */
  177.         sig=Wait(SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_F);
  178.  
  179.         /* Was it the end, or a user break ? */
  180.         if(sig & SIGBREAKF_CTRL_C)
  181.             Printf("...stopped\n");
  182.         else
  183.             Printf("...done\n");
  184.  
  185.         /* Get rid of the object */
  186.         DisposeDTObject(obj);
  187.  
  188.         /* All OK */
  189.         return(FALSE);
  190.     }
  191.     else
  192.     {
  193.         LONG errnum = IoErr ();
  194.         UBYTE errbuff[80];
  195.  
  196.         /* Creating a object failed, so output a message */
  197.         if (errnum >= DTERROR_UNKNOWN_DATATYPE)
  198.             Printf(GetDTString(errnum), options[OPT_NAME]);
  199.         else
  200.         {
  201.             Fault(errnum, NULL, errbuff, sizeof (errbuff));
  202.             Printf("%s");
  203.         }
  204.         Printf("\nError %ld.\n", errnum);
  205.  
  206.         /* Report error */
  207.         return(TRUE);
  208.     }
  209. }
  210.