/*
 * $Id: RecordSound.c 1.4 1997/06/11 19:03:28 olsen Exp olsen $
 *
 * :ts=4
 */

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

#include <dos/dosextens.h>

#include <datatypes/datatypes.h>
#include <datatypes/datatypesclass.h>
#include <datatypes/soundclass.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 <string.h>
#include <stdio.h>
#include <stdlib.h>

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

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

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

extern struct ExecBase *SysBase;
extern struct DosLibrary *DOSBase;

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

struct Library *DataTypesBase;

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

struct Library *ConciertoBase;
struct AudioContext *ac;

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

void
do_work(char *name,int seconds,int volume)
{
	BYTE *sample;
	LONG sampleLength;
	LONG frequency;

	Printf("name \"%s\" seconds %ld volume %ld\n",name,seconds,volume);

	frequency = CTO_FindNearestSampleRate(22050); /* Hz */

	sampleLength = frequency * seconds;

	sample = (BYTE *)AllocVec(sampleLength,MEMF_ANY|MEMF_CLEAR|MEMF_PUBLIC);
	if(sample != NULL)
	{
		struct VoiceHeader vh;
		Object *o;

		CTO_StartRecording(ac,
			DSPA_SampleData,			sample,
			DSPA_NumSampleBytes,		sampleLength,
			DSPA_RecordingRate,			frequency,
			DSPA_RecordingSource,		RECORDSRC_Switcher,
			DSPA_LeftInputVolume,		(CTO_VOLUME_Unity * volume) / 15,
			DSPA_RightInputVolume,		(CTO_VOLUME_Unity * volume) / 15,
			DSPA_RecordMonitorVolume,	CTO_VOLUME_Unity,
		TAG_DONE);

		memset(&vh,0,sizeof(vh));

		vh.vh_OneShotHiSamples	= sampleLength;
		vh.vh_SamplesPerSec		= frequency;
		vh.vh_Octaves			= 1;
		vh.vh_Compression		= CMP_NONE;
		vh.vh_Volume			= 0x10000;	/* maximum volume */

		o = NewDTObject(FilePart(name),
			DTA_SourceType,		DTST_RAM,
			DTA_GroupID,		GID_SOUND,
			SDTA_VoiceHeader,	&vh,
			SDTA_Sample,		sample,
			SDTA_SampleLength,	sampleLength,
			SDTA_Volume,		64,
			SDTA_Period,		(5 * SysBase->ex_EClockFrequency) / frequency,
			SDTA_Cycles,		1,
		TAG_DONE);
		if(o != NULL)
		{
			BPTR file;

			file = Open(name,MODE_NEWFILE);
			if(file != NULL)
			{
				DoMethod(o,DTM_WRITE,
					NULL,		/* dtw_GInfo */
					file,		/* dtw_FileHandle */
					DTWM_IFF,	/* dtw_Mode */
					NULL);		/* dtw_AttrList */

				Close(file);
			}

			sample = NULL;
			DisposeDTObject(o);
		}

		FreeVec(sample);
	}
}

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

int
main(int argc,char **argv)
{
	if(argc > 1)
	{
		int seconds = 0,volume = 0;

		if(argc > 2)
			seconds = atoi(argv[2]);

		if(seconds == 0)
			seconds = 5;

		if(argc > 3)
			volume = atoi(argv[3]);

		if(volume < 0)
			volume = 0;
		else if (volume > 15)
			volume = 15;

		if(volume == 0)
			volume = 15;

		DataTypesBase = OpenLibrary("datatypes.library",39);
		if(DataTypesBase != NULL)
		{
			ConciertoBase = OpenLibrary("concierto.library",0);
			if(ConciertoBase != NULL)
			{
				ac = CTO_ObtainAudioContext(0);
				if(ac != NULL)
				{
					do_work(argv[1],seconds,volume);

					CTO_ReleaseAudioContext(ac);
				}

				CloseLibrary(ConciertoBase);
			}

			CloseLibrary(DataTypesBase);
		}
	}
	else
	{
		Printf("usage: recordsound <name> [seconds] [volume]\n");
	}

	return(0);
}
