/*
 * $Id: PlaySound.c 1.3 1997/06/03 22:15:11 olsen Exp olsen $
 *
 * :ts=8
 */

#include <exec/memory.h>
#include <exec/execbase.h>

#include <dos/dosextens.h>

#include <datatypes/datatypes.h>
#include <datatypes/datatypesclass.h>
#include <datatypes/soundclass.h>
#include <workbench/workbench.h>

#include <string.h>
#include <stdio.h>

#include <clib/alib_protos.h>
#include <clib/dos_protos.h>
#include <clib/exec_protos.h>
#include <clib/datatypes_protos.h>

#include <pragmas/dos_pragmas.h>
#include <pragmas/exec_sysbase_pragmas.h>
#include <pragmas/datatypes_pragmas.h>

/*****************************************************************************/

#include "libraries/concierto.h"
#include "clib/concierto_protos.h"
#include "pragmas/concierto_pragmas.h"

/*****************************************************************************/

extern struct ExecBase *SysBase;
extern struct Library *DOSBase;

struct Library *DataTypesBase;

struct Library *ConciertoBase;
struct AudioContext *ac;

/*****************************************************************************/

#define	TEMPLATE "NAME/A"
#define	OPT_NAME 0
#define	NUM_OPTS 1

/*****************************************************************************/

void main (int argc, char **argv)
{
    /* Argument parsing variables */
    ULONG options[NUM_OPTS];
    struct RDArgs *rdargs;

    /* Object variables */
    STRPTR name;
    Object *o;

    memset (options, 0, sizeof (options));
    if (rdargs = ReadArgs (TEMPLATE, (LONG *)options, NULL))
    {
        DataTypesBase = OpenLibrary ("datatypes.library", 39);
        ConciertoBase = OpenLibrary("concierto.library",0);
        if(ConciertoBase)
            ac = CTO_ObtainAudioContext(0);

	/* Open DataTypes */
	if (DataTypesBase && ConciertoBase && ac)
	{
	    /* Open the sound object */
	    if (o = NewDTObject ((APTR) options[OPT_NAME],

				/* Say that the source is a file */
				 DTA_SourceType,	DTST_FILE,

				/* We will only accept sound DataTypes */
				 DTA_GroupID,		GID_SOUND,

				/* No more attributes */
				 TAG_DONE))
	    {
	        ULONG period = 0,bytes = 0;
	        UBYTE *sample = NULL;
	        struct VoiceHeader *vhdr = NULL;
	        LONG closest;

		/* Get information about the object */
		if (GetDTAttrs (o,
			DTA_ObjName, (ULONG) &name,
			SDTA_Period, (ULONG) &period,
			SDTA_SampleLength, (ULONG)&bytes,
			SDTA_Sample, (ULONG)&sample,
			SDTA_VoiceHeader, (ULONG)&vhdr,
		TAG_DONE))
		{
		    /* Display any information we obtained */
		    if (name)
			Printf ("     Name: %s\n", name);
		}

		Printf("sample rate = %ld, closest is %ld\n",vhdr->vh_SamplesPerSec,closest = CTO_FindNearestSampleRate(vhdr->vh_SamplesPerSec));

		/* Play the sound */
		CTO_StartPlayback(ac,
			DSPA_SampleData,	sample,
			DSPA_NumSampleBytes,	bytes,
			DSPA_PlaybackRate,	closest,
		TAG_DONE);

		/* Get rid of the object */
		DisposeDTObject (o);
	    }
	    else
	    {
		LONG errnum = IoErr ();
		UBYTE errbuff[80];

		if (errnum >= DTERROR_UNKNOWN_DATATYPE)
		    sprintf (errbuff, GetDTString (errnum), options[OPT_NAME]);
		else
		    Fault (errnum, NULL, errbuff, sizeof (errbuff));
		Printf ("%s\nerror #%ld\n", errbuff, errnum);
	    }
	}

        if(!DataTypesBase)
	    Printf ("couldn't open datatypes.library V39\n");

        CloseLibrary(DataTypesBase);

        if(!ConciertoBase)
            Printf ("couldn't open concierto.library\n");
        else if (!ac)
            Printf ("couldn't access audio unit #0\n");

        if(ac)
            CTO_ReleaseAudioContext(ac);

        CloseLibrary(ConciertoBase);

	/* Free the allocated memory after ReadArgs */
	FreeArgs (rdargs);
    }
    else
    {
	/* Show the failure message */
	PrintFault (IoErr (), NULL);
    }
}
