home *** CD-ROM | disk | FTP | other *** search
- /*
- ** $PROJECT: playaudio
- **
- ** $VER: playaudio.c 1.0 (11.9.97)
- **
- ** Copyright 1997 by Martin Gierich.
- ** Inspired by playsound.c and examinedt.c written by David N. Junod.
- **
- ** Plays sound samples and midi music using datatypes (CLI only).
- ** sound.datatype V40 or better is recommended for detecting end of sample
- ** playback, otherwise you have to stop it using CTRL-C.
- **
- ** Calling template: NAME/A,VOLUME/N,CYCLES/N,REPEAT/S,IMMEDIATE/S,CLUSTER/K
- ** NAME - name of file to play
- ** VOLUME - main playback volume
- ** CYCLES - number of repeats
- ** REPEAT - infinite repeats (does not work with sound.datatype V39/40)
- ** IMMEDIATE - immediately start playing without sending STM_PLAY (does not
- ** work with sound.datatype V39/40).
- ** CLUSTER - name of CAMD cluster for midi playback
- **
- ** $TABSIZE: 3
- */
-
- #include <string.h>
- #include <exec/types.h>
- #include <datatypes/datatypesclass.h>
- #include <datatypes/soundclass.h>
- #include <datatypes/midiclass.h>
-
- #include <proto/dos.h>
- #include <proto/exec.h>
- #include <proto/datatypes.h>
-
- /*****************************************************************************/
-
- /* Prototype */
- int playfile(ULONG options[]);
-
- /* Disable CTRL-C handling */
- #ifdef __SASC
- void __regargs __chkabort(void) { }
- void __regargs _CXBRK(void) { }
- #endif
-
- /*****************************************************************************/
-
- /* Define command line arguments for ReadArgs() */
- #define TEMPLATE "NAME/A,VOLUME/N,CYCLES/N,REPEAT/S,IMMEDIATE/S,CLUSTER/K"
- enum
- {
- OPT_NAME,
- OPT_VOL,
- OPT_CYC,
- OPT_REP,
- OPT_IMM,
- OPT_CLU,
- NUM_OPTS
- };
-
- /*****************************************************************************/
-
- /* The main routine analyses the file and calls playfile() */
- int main (int argc, char **argv)
- {
- /* Argument parsing variables */
- ULONG options[NUM_OPTS];
- struct RDArgs *rdargs;
-
- /* Datatype variables */
- struct DataTypeHeader *dth;
- struct DataType *dtn;
- BPTR lock;
- ULONG groupid;
-
- /* Parse arguments */
- memset (options, 0, sizeof(options));
- if (rdargs = ReadArgs(TEMPLATE, (LONG *)options, NULL))
- {
-
- if (lock = Lock((STRPTR) options[OPT_NAME], ACCESS_READ))
- {
- /* Determine the DataType of the file */
- if (dtn = ObtainDataTypeA(DTST_FILE, (APTR) lock, NULL))
- {
- /* Get the DataType header and group ID*/
- dth = dtn->dtn_Header;
- groupid = dth->dth_GroupID;
-
- /* Give some informations */
- Printf("Information on: %s\n", options[OPT_NAME]);
- Printf(" Description: %s\n", dth->dth_Name);
- Printf(" Base Name: %s\n", dth->dth_BaseName);
- Printf(" Type: %s\n", GetDTString((dth->dth_Flags & DTF_TYPE_MASK) + DTMSG_TYPE_OFFSET));
- Printf(" Group: %s\n", GetDTString(groupid));
- Printf(" ID: 0x%lx\n\n", dth->dth_ID);
-
- /* Release the DataType */
- ReleaseDataType (dtn);
-
- /* Now check for the correct DataType before playing file */
- if (groupid==GID_SOUND || groupid==GID_MUSIC)
- playfile(options);
- else
- Printf("Error: Need sound or music file.\n");
- }
- else
- PrintFault(IoErr (), "Error");
- UnLock(lock);
- }
- else
- PrintFault(IoErr (), "Error");
-
- /* Free the allocated memory after ReadArgs */
- FreeArgs (rdargs);
- }
- else
- /* Show the failure message */
- PrintFault(IoErr (), "Error");
-
- return(0);
- }
-
-
-
- /* This routine plays the sound or midi data from a file */
- int playfile(ULONG options[])
- {
- /* Object variables */
- STRPTR name=NULL;
- STRPTR objname=NULL;
- STRPTR title=NULL;
- ULONG sig;
- Object *obj;
-
- /* Clear signals to be used, so we can recognize end of playback */
- SetSignal(0L, SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_F);
-
- /* Open the sound or midi object */
- if (obj = NewDTObject (
- /* Set the source file name */
- (APTR) options[OPT_NAME],
- /* Say that the source is a file */
- DTA_SourceType, DTST_FILE,
- /* Set attributes from the commandline */
- DTA_Repeat, options[OPT_REP],
- DTA_Immediate, options[OPT_IMM],
- SDTA_Volume, options[OPT_VOL] ? *(LONG*) options[OPT_VOL] : 64,
- SDTA_Cycles, options[OPT_CYC] ? *(LONG*) options[OPT_CYC] : 1,
- options[OPT_CLU] ? MDTA_Cluster : DTA_Dummy, (APTR) options[OPT_CLU],
- /* We want to be notified when the sound stops playing, so
- * we provide a signal task and a signal (CTRL-F) */
- SDTA_SignalTask, (ULONG) FindTask (NULL),
- SDTA_SignalBit, (ULONG) SIGBREAKF_CTRL_F,
- /* No more attributes */
- TAG_DONE))
- {
-
- /* Get information about the object */
- if (GetDTAttrs (obj,
- DTA_ObjName,(ULONG) &objname,
- DTA_Name, (ULONG) &name,
- DTA_Title, (ULONG) &title,
- TAG_DONE))
- {
- Printf(" ObjName: %s\n", objname ? objname : (STRPTR)"[none]");
- Printf(" Name: %s\n", name ? name : (STRPTR)"[none]");
- Printf(" Title: %s\n", title ? title : (STRPTR)"[none]");
- }
-
- /* Start playing the sound, if it has not started immediately */
- if (!options[OPT_IMM])
- DoMethod(obj, DTM_TRIGGER, NULL, STM_PLAY, NULL);
- Printf("\nPlaying sound...\n");
-
- /* Wait till the datatype tells us, that it is all over */
- sig=Wait(SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_F);
-
- /* Was it the end, or a user break ? */
- if(sig & SIGBREAKF_CTRL_C)
- Printf("...stopped\n");
- else
- Printf("...done\n");
-
- /* Get rid of the object */
- DisposeDTObject(obj);
-
- /* All OK */
- return(FALSE);
- }
- else
- {
- LONG errnum = IoErr ();
- UBYTE errbuff[80];
-
- /* Creating a object failed, so output a message */
- if (errnum >= DTERROR_UNKNOWN_DATATYPE)
- Printf(GetDTString(errnum), options[OPT_NAME]);
- else
- {
- Fault(errnum, NULL, errbuff, sizeof (errbuff));
- Printf("%s");
- }
- Printf("\nError %ld.\n", errnum);
-
- /* Report error */
- return(TRUE);
- }
- }
-