#include "shinobi.h"
#include "onmempcm.h"

void cb( mpHn *hn ) {
  Sint32 time;
  Sint32 v, ang;
  Float  t;

  time = syTmrDiffCount( hn->LFOStart, syTmrGetCount() );
  t = time * 0.00000128F;
  ang = NJM_RAD_ANG( t );

  v = ( Sint32 )( njSin( ang * 50 * njExp( -t / 3 ) ) * njExp( -t ) * 15.0F );
  mpSetVol( hn, v, 0 );
}

void main() {
  mpHn *hn;
  Sint32 port[]  = { 0, 1 };
  char   *name[] = { "SAMPLE_L.P08", "SAMPLE_R.P08" };
  PDS_PERIPHERAL *pad;

  njSetBorderColor( 0x00000000 );
  sbInitSystem( NJD_RESOLUTION_320x240_NTSCNI, NJD_FRAMEBUFFER_MODE_RGB565, 1 );
  njInitVertexBuffer( 0x50000, 0, 0x50000, 0, 0 );
  njInitPrint( NULL, 0, NJD_TEXFMT_ARGB_1555 );

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

  mpInit();

  hn = mpOpen( name, 2 );
  if ( hn == NULL ) PrgStop();
  mpSetPrm( hn, 44100, 8 );
  mpSetPort( hn, port, 2 );
  mpSetBaseVol( hn, 0x70 );
  mpSetLFOCallback( hn, cb );

  mpPlay( hn, 0 );
  mpSetLoopStart( hn, 824000 );
  mpSetLoop( hn, true );

  while( -1 ) {
    pad = pdGetPeripheral( PDD_PORT_A0 );

    if ( pad->press & PDD_DGT_ST ) {
      mpSetLFO( hn, true );
    }

    njPrint( NJM_LOCATION( 3, 3 ), "On Memory PCM Stream Sample 2" );
    njPrint( NJM_LOCATION( 3, 5 ), "Press START!" );
    SoundLibVer( 4, 8 );
    SoundDrvVer( 4, 9 );
    SoundDrvStat( 4, 10 );
    mpTask();
    sdSysServer();
    njWaitVSync();
  }
}
