;/*
lc -L -cfist -y -v -j73 make8svx
quit
 * make8svx.c
 */

#include <exec/types.h>
#include <exec/memory.h>
#include <exec/execbase.h>
#include <exec/libraries.h>
#include <dos/dos.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 <clib/icon_protos.h>

#include <pragmas/dos_pragmas.h>
#include <pragmas/exec_pragmas.h>
#include <pragmas/datatypes_pragmas.h>
#include <pragmas/icon_pragmas.h>

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

/* This example is overkill.  It is VERY easy to write an 8SVX file. */
void main (int argc, char **argv)
{
    struct VoiceHeader *vhdr;
    struct DiskObject *dob;
    struct dtTrigger dtt;
    struct dtWrite dtw;
    ULONG samplelength;
    UWORD period = 330;
    UBYTE output[300];
    BYTE *sample;
    LONG i, j;
    Object *o;
    BPTR sfh;
    BPTR dfh;

    if (argc < 2)
    {
	printf ("requires a file name\n");
    }
    else if (DataTypesBase = OpenLibrary ("datatypes.library", 39))
    {
	IconBase = OpenLibrary ("icon.library", 39);

	/* Build the output file name */
	if (argc > 2)
	{
	    strcpy (output, argv[2]);
	}
	else
	{
	    strcpy (output, argv[1]);
	    j = strlen (output) - 1;

	    for (i = j; i >= 0; i--)
	    {
		if (output[i] == '.')
		{
		    output[i] = 0;
		    break;
		}
	    }
	    strcat (output, ".8svx");
	}

	/* Open and read the source raw sound file */
	if (sfh = Open (argv[1], MODE_OLDFILE))
	{
	    Seek (sfh, 0, OFFSET_END);
	    if ((samplelength = Seek (sfh, 0, OFFSET_BEGINNING)) > 0)
	    {
		printf ("length = %ld bytes\n", samplelength);

		if (sample = AllocVec (samplelength + 1, MEMF_CHIP))
		{
		    if (Read (sfh, sample, samplelength) == samplelength)
		    {
			/* Create the sound object */
			if (o = NewDTObject ((APTR) "sound.test",
					     DTA_SourceType, DTST_RAM,
					     DTA_GroupID, GID_SOUND,
					     SDTA_Sample, sample,
					     SDTA_SampleLength, samplelength,
					     SDTA_Volume, 64,
					     SDTA_Period, period,
					     SDTA_Cycles, 1,
					     TAG_DONE))
			{
			    /* Play the sound */
			    printf ("play sound...\n");
			    dtt.MethodID = DTM_TRIGGER;
			    dtt.dtt_Function = STM_PLAY;
			    DoMethodA (o, &dtt);

			    /* Fill out the voice header */
			    GetDTAttrs (o, SDTA_VoiceHeader, &vhdr, TAG_DONE);
			    vhdr->vh_SamplesPerSec = (SysBase->ex_EClockFrequency * 5) / period;
			    vhdr->vh_Compression = CMP_NONE;
			    vhdr->vh_Volume = 64;

			    printf ("write out as 8svx to %s\n", output);
			    if (dfh = Open (output, MODE_NEWFILE))
			    {
				/* Write the file out using the base format (8svx in this case) */
				dtw.MethodID = DTM_WRITE;
				dtw.dtw_GInfo = NULL;
				dtw.dtw_FileHandle = dfh;
				dtw.dtw_Mode = DTWM_RAW;
				dtw.dtw_AttrList = NULL;
				if (DoMethodA (o, &dtw))
				{
				    /* Get the default icon for the file */
				    if ((dob = GetDiskObject ("ENV:Sys/def_8svx")) == NULL)
					dob = GetDefDiskObject (WBPROJECT);

				    /* If we have an icon, then write it out */
				    if (dob)
				    {
					dob->do_DefaultTool = "MultiView";

					PutDiskObject (output, dob);
					FreeDiskObject (dob);
				    }
				}
				else
				{
				    /* Couldn't write file! */
				    printf ("couldn't save as 8svx\n");
				}
				Close (dfh);
			    }

			    /* Get rid of the object */
			    DisposeDTObject (o);

			    /* Once you send the sample to the object, it belongs to it. */
			    sample = NULL;
			}
			else
			{
			    printf ("couldn't create sound object\n");
			}
		    }
		    FreeVec (sample);
		}
	    }
	    Close (sfh);
	}

	CloseLibrary (IconBase);
	CloseLibrary (DataTypesBase);
    }
    else
	printf ("couldn't open datatypes.library V39\n");
}
