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

	Title :                 Sound Module
	Filename :              sound.c
	Author :                Thomas Szirtes
	Revision :              1.0

	Notes :                 

	Copyright :             (C) SEGA Europe 1998

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

/*                 
 * Include headers 
 */                  

#include <shinobi.h>
#include <sg_sd.h>
#include "sndutls.h"
#include "sound.h"

#ifdef SOUND_ENABLE


/*                   
 * Declare Local Prototypes 
 */

void UserVBlankIn( void );
void SoundInit( void );
void SoundMain( PDS_PERIPHERAL * );
void main( void );



void UserVBlankIn(void) 
{
  /* Call Sound Server */
  sdSysServer();
}


/*--------------------------------------------------------------------------

  void SOUND_Init(void) 

  Parameters:
	None
 
  Returns:
 
  Notes:
 
  --------------------------------------------------------------------------*/
void SOUND_Init(void) 
{
  SDMIDI         midi_handle;
  SDS_MIDI_MES   midi;

  njSetVSyncFunction( UserVBlankIn );

  SoundInitialize( "MANATEE.DRV", "CITYSND.MLT" );

  /* Send Initial Message */
  sdMidiOpenPort( &midi_handle );
  {
    /* SET UP MIDI CHANNEL 0 as Choir effect */
	/* Bank Change */
    sdMidiSetMes( midi_handle, &midi, SDD_MIDI_CTL_CHG, 32, 0 ); 
    sdMidiSendMes( midi_handle, &midi, 0 );
    /* Program Change */
	sdMidiSetMes( midi_handle, &midi, SDD_MIDI_PRG_CHG, 1 );
	sdMidiSendMes( midi_handle, &midi, 0 );
 
    /* SET UP MIDI CHANNEL 1 as Fire effect */
	/* Bank Change */
    sdMidiSetMes( midi_handle, &midi, SDD_MIDI_CTL_CHG+1, 32, 0 ); 
    sdMidiSendMes( midi_handle, &midi, 0 );
    /* Program Change */
	sdMidiSetMes( midi_handle, &midi, SDD_MIDI_PRG_CHG+1, 0 );
	sdMidiSendMes( midi_handle, &midi, 0 );

    /* SET UP MIDI CHANNEL 2 as Thrust effect */
    /* Bank Change */
    sdMidiSetMes( midi_handle, &midi, SDD_MIDI_CTL_CHG+2, 32, 0 ); 
    sdMidiSendMes( midi_handle, &midi, 0 );
    /* Program Change */
	sdMidiSetMes( midi_handle, &midi, SDD_MIDI_PRG_CHG+2, 2 );
	sdMidiSendMes( midi_handle, &midi, 0 );

    /* SET UP MIDI CHANNEL 3 as Shield effect */
	/* Bank Change */
    sdMidiSetMes( midi_handle, &midi, SDD_MIDI_CTL_CHG+3, 32, 0 ); 
    sdMidiSendMes( midi_handle, &midi, 0 );
    /* Program Change */
	sdMidiSetMes( midi_handle, &midi, SDD_MIDI_PRG_CHG+3, 3 );
	sdMidiSendMes( midi_handle, &midi, 0 );

    /* SET UP MIDI CHANNEL 4 as Spark effect */
    /* Bank Change */
    sdMidiSetMes( midi_handle, &midi, SDD_MIDI_CTL_CHG+4, 32, 0 ); 
    sdMidiSendMes( midi_handle, &midi, 0 );
    /* Program Change */
	sdMidiSetMes( midi_handle, &midi, SDD_MIDI_PRG_CHG+4, 4 );
	sdMidiSendMes( midi_handle, &midi, 0 );
  }
  sdMidiClosePort( midi_handle );

}



/*--------------------------------------------------------------------------

  void  SOUND_Fire(void)

  Parameters:
	None
 
  Returns:
 
  Notes:
 
  --------------------------------------------------------------------------*/
void  SOUND_Fire(void)
{
  SDMIDI       midi_handle;
  SDS_MIDI_MES midi;
  Uint8        midi_ch = 0;

  sdMidiOpenPort( &midi_handle );
  {
	sdMidiSetMes( midi_handle, &midi, SDD_MIDI_NOTE_ON, 61, 127 );
	sdMidiSendMes( midi_handle, &midi, 2 );
  }
  sdMidiClosePort( midi_handle );

}


/*--------------------------------------------------------------------------

  void  SOUND_Ahh(void)

  Parameters:
	None
 
  Returns:
 
  Notes:
 
  --------------------------------------------------------------------------*/
void  SOUND_Ahh(void)
{
  SDMIDI       midi_handle;
  SDS_MIDI_MES midi;
  Uint8        midi_ch = 0;

  sdMidiOpenPort( &midi_handle );
  {
	sdMidiSetMes( midi_handle, &midi, SDD_MIDI_NOTE_ON+1, 61, 127 );
	sdMidiSendMes( midi_handle, &midi, 2 );
  }
  sdMidiClosePort( midi_handle );

}


/*--------------------------------------------------------------------------

  void  SOUND_Boom(void)

  Parameters:
	None
 
  Returns:
 
  Notes:
 
  --------------------------------------------------------------------------*/
void  SOUND_Boom(void)
{
  SDMIDI       midi_handle;
  SDS_MIDI_MES midi;
  Uint8        midi_ch = 0;

  sdMidiOpenPort( &midi_handle );
  {
	sdMidiSetMes( midi_handle, &midi, SDD_MIDI_NOTE_ON+3, 61, 127 );
	sdMidiSendMes( midi_handle, &midi, 2 );
  }
  sdMidiClosePort( midi_handle );

}




/*--------------------------------------------------------------------------

  void  SOUND_ShipOn(void)

  Parameters:
	None
 
  Returns:
 
  Notes:
 
  --------------------------------------------------------------------------*/
static int	latch	=	0;

void  SOUND_ShipOn(void)
{
  SDMIDI       midi_handle;
  SDS_MIDI_MES midi;
  Uint8        midi_ch = 0;

  if (latch!=0) return;

  sdMidiOpenPort( &midi_handle );
  {
	sdMidiSetMes( midi_handle, &midi, SDD_MIDI_NOTE_ON+2, 61, 127 );
	sdMidiSendMes( midi_handle, &midi, 2 );
  }
  sdMidiClosePort( midi_handle );

  latch	=	1;

}


/*--------------------------------------------------------------------------

  void  SOUND_ShipOff(void)

  Parameters:
	None
 
  Returns:
 
  Notes:
 
  --------------------------------------------------------------------------*/
void  SOUND_ShipOff(void)
{
  SDMIDI       midi_handle;
  SDS_MIDI_MES midi;
  Uint8        midi_ch = 0;

  if (latch==0) return;

  sdMidiOpenPort( &midi_handle );
  {
      sdMidiSetMes( midi_handle, &midi, SDD_MIDI_NOTE_OFF + 2, 61 );
      sdMidiSendMes( midi_handle, &midi, 0 );
  }
  sdMidiClosePort( midi_handle );

  latch	=	0;
}


/*--------------------------------------------------------------------------

  void  SOUND_ShipOff(void)

  Parameters:
	None
 
  Returns:
 
  Notes:
 
  --------------------------------------------------------------------------*/
static int	latch2	=	0;

void  SOUND_ShieldOn(void)
{
  SDMIDI       midi_handle;
  SDS_MIDI_MES midi;
  Uint8        midi_ch = 0;

  if (latch2!=0) return;

  sdMidiOpenPort( &midi_handle );
  {
	sdMidiSetMes( midi_handle, &midi, SDD_MIDI_NOTE_ON+4, 61, 127 );
	sdMidiSendMes( midi_handle, &midi, 2 );
  }
  sdMidiClosePort( midi_handle );

  latch2	=	1;
}


/*--------------------------------------------------------------------------

  void  SOUND_ShieldOff(void)

  Parameters:
	None
 
  Returns:
 
  Notes:
 
  --------------------------------------------------------------------------*/
void  SOUND_ShieldOff(void)
{
  SDMIDI       midi_handle;
  SDS_MIDI_MES midi;
  Uint8        midi_ch = 0;

  if (latch2==0) return;

  sdMidiOpenPort( &midi_handle );
  {
      sdMidiSetMes( midi_handle, &midi, SDD_MIDI_NOTE_OFF + 4, 61 );
      sdMidiSendMes( midi_handle, &midi, 0 );
  }
  sdMidiClosePort( midi_handle );

  latch2	=	0;
}


#endif