/*
**
** $VER: audio.c 0.1 (04.03.99)
** Description
**
** (C) Copyright 1999 Paul Hill
**
*/

#include <stdio.h>
#ifdef __PPC__
#include </ADE/os-includeppc/proto/exec.h>
#else
#include <proto/exec.h>
#endif
#include <devices/audio.h>
#include <graphics/gfxbase.h>

#include "swfplayer.h"

static struct MsgPort *AudioPort = NULL;
static struct IOAudio *AudioMsg = NULL;
static UBYTE DeviceOpen = FALSE;

extern struct GfxBase *GfxBase;
static ULONG clock_constant;

BOOL audio_init()
{
#ifdef SOUND
	UBYTE MyChannels [] = {15};

	AudioPort = CreateMsgPort ();
	if (!AudioPort) return FALSE;

	AudioMsg = CreateIORequest (AudioPort, sizeof (struct IOAudio));
	if (!AudioMsg) return FALSE;

	AudioMsg->ioa_Request.io_Message.mn_ReplyPort = AudioPort;
	AudioMsg->ioa_Request.io_Message.mn_Node.ln_Pri = 127;
	AudioMsg->ioa_Request.io_Command = ADCMD_ALLOCATE;
	AudioMsg->ioa_Request.io_Flags = ADIOF_NOWAIT;
	AudioMsg->ioa_AllocKey = 0;
	AudioMsg->ioa_Data = MyChannels;
	AudioMsg->ioa_Length = 1;

	// Open audio device

	DeviceOpen = !OpenDevice ("audio.device", 0, (struct IORequest *)AudioMsg, 0);
	if (!DeviceOpen) return FALSE;

//	ML_InitSCC ();

	TRACE("audio initialised\n");

	if ((GfxBase->DisplayFlags & NTSC) != 0)
		clock_constant = 3579545;   // NTSC
	else
		clock_constant = 3546895;   // PAL


//  if ((c->chip_data = AllocMem (lengths[id], MEMF_CHIP)) == NULL)
#endif
	return TRUE;
}




void audio_trash()
{
#ifdef SOUND
	if (DeviceOpen)
	{
//		ML_ExitSCC ();
		CloseDevice ((struct IORequest *)AudioMsg);
		DeviceOpen = FALSE;
	}

	if (AudioMsg)
	{
		DeleteIORequest (AudioMsg);
		AudioMsg = NULL;
	}

	if (AudioPort)
	{
		DeleteMsgPort (AudioPort);
		AudioPort = NULL;
	}

	TRACE("audio closed\n");
#endif
}



/**********************************************************************/
// Starts a sound in a particular sound channel.
int I_StartSound (
  int id,
  int cnum,
  int vol,
  int sep,
  int pitch,
  int priority )
{
#ifdef SOUND
  struct channel_info *c;

  /* fprintf (stderr, "I_StartSound(%d,%d,%d,%d,%d,%d)\n", id, cnum, vol, sep,
              pitch, priority); */
  I_StopSound (cnum);
  c = &channel_info[cnum];
  c->audio_io->ioa_Request.io_Command = CMD_WRITE;
  c->audio_io->ioa_Request.io_Flags = ADIOF_PERVOL;
  c->audio_io->ioa_Data = &chip_cache_info[cache_chip_data (id)].chip_data[8];
  c->audio_io->ioa_Length = lengths[id] - 8;
  c->audio_io->ioa_Period = period_table[pitch];
  c->audio_io->ioa_Volume = vol << 3;
  c->audio_io->ioa_Cycles = 1;
  BeginIO ((struct IORequest *)c->audio_io);
  c->sound_in_progress = TRUE;
#endif
  return cnum;
}

/**********************************************************************/
// Stops a sound channel.
void I_StopSound(int handle)
{
#ifdef SOUND
  /* fprintf (stderr, "I_StopSound(%d)\n", handle); */
  if (channel_info[handle].sound_in_progress) {
    AbortIO ((struct IORequest *)channel_info[handle].audio_io);
    WaitPort (channel_info[handle].audio_mp);
    GetMsg (channel_info[handle].audio_mp);
    channel_info[handle].sound_in_progress = FALSE;
  }
#endif
}
