#include <shinobi.h>
#include <sg_sd.h>


Uint32 AttachSector( Sint32 size ) {
  Uint32 ret;
  ret = ( size + GDD_FS_SCTSIZE - 1 ) / GDD_FS_SCTSIZE;
  return ret * GDD_FS_SCTSIZE;
}

Sint32 SoundInit(void)
{
    char           *gSndDrvPtr;
    long           gSndDrvSize;
    SDMEMBLK       mem_blk;
    GDFS           fd;
    unsigned long  size;

    /* Load Sound Driver */
    if ( ( fd = gdFsOpen( "manatee.drv", NULL ) ) != NULL )
    {
        {
            gdFsGetFileSize( fd, &gSndDrvSize );
            size = AttachSector( gSndDrvSize );
            gSndDrvPtr = ( char * )syMalloc( size * sizeof( char ) );
            gdFsRead( fd, size / GDD_FS_SCTSIZE, gSndDrvPtr );
        }
        gdFsClose( fd );
    }

    /*                                 */
    /* Initialize sound library module */
    /*                                 */
    sdLibInit( NULL, 0, 0 );
    sdMemBlkCreate( &mem_blk );
    {
        /* Initialize sound driver */
        sdMemBlkSetPrm( mem_blk, gSndDrvPtr, gSndDrvSize, SDD_MEMBLK_SYNC_FUNC, NULL );
        sdDrvInit( mem_blk);
    }

    sdMemBlkDestroy( mem_blk );
    syFree( gSndDrvPtr );
}

void SoundServer(void)
{
	sdSysServer();
}