#include "SCR.h"
#include "SNDinc.h"

#include "sndevent.h"
#include "sndres.h"
#include "sndplay.h"
#include "sndld_bn.h"
#include "sndld_s.h"
#include "sndld_hy.h"

#ifdef _DLL_COMPILATION_MODE
#include "snddll.h"
#endif

#include "sndini.h" // for getoptions

#ifndef NO_ACP_SOUND

extern SCR_tdst_Link_Table SND_gs_BinResConversionTable;
SndBool g_bForceLoadBanks;


//***************************************************************************
//***************************************************************************
//* File:	sndldhyb.c 
//***************************************************************************
//* Project/Module: CPA project, SND module, Hybrid version 
//***************************************************************************
//* Description: Functions specific to the hybrid version of the SND module
//*
//***************************************************************************
//* Version                      1.0
//* Creation date        09/08/96
//* Revision date
//*
//*				 (c) Ubi Sound Studio 1998
//***************************************************************************
//***************************************************************************


#include "sndld_sh.cxx"





//***************************************************************************
//***************************************************************************
//*																			*
//*																			*
//*						MISCELLANEOUS FUNCTIONS								*
//*																			*
//*																			*
//***************************************************************************
//***************************************************************************



// Link tables containing, for each section name, the generated Binary Ids
//		of dynamically loaded events and resources:

SCR_tdst_Link_Table SND_gs_ExtraBankEventsTable;
SCR_tdst_Link_Table SND_gs_ExtraBankResTable;

#define SND_EXTRA_BANKID	0xFFFE	 


//***************************************************************************
//***************************************************************************
//*																			*
//*																			*
//*						EVENT LOG FUNCTIONS									*
//*																			*
//*																			*
//***************************************************************************
//***************************************************************************

char gs_szEventLogFileName[MAX_PATH]="";

SndBool gs_bLogEvents=FALSE; 



//-----------------------------------------------------------------------------
//  Name : SND_fn_bStartLogEventNames(const char *szFilePath,const char *szMapName)
//-----------------------------------------------------------------------------
//  Description :   This function sets the current log file name, makes sure it is   
//					writable, if necessary, deletes the existing map section section,
//					and saves the section start line.
//-----------------------------------------------------------------------------
//  Input :  Complete log file path, Map section name
//  Output : BOOL
//-----------------------------------------------------------------------------
//  Creation date : 04/02/98            Author : Frédéric Décréau
//-----------------------------------------------------------------------------
//  Modification date :                 Modification Author :
//  Modifications :
//------------------------------------------------------------------------------*/

BOOL SND_fn_bStartLogEventNames(const char *szFilePath,const char *szMapName)
{
	FILE* hFile;

	hFile=fopen(szFilePath,"aw");
	if (hFile==NULL)
	{
		strcpy(gs_szEventLogFileName,"");
		gs_bLogEvents=FALSE;
		return FALSE;
	}

	if (fprintf(hFile,"{SndMapE:%s\n",szMapName)<0)
	{ // Could not write to the file:
		strcpy(gs_szEventLogFileName,"");
		gs_bLogEvents=FALSE;
		fclose(hFile);
		return FALSE;
	}
	strcpy(gs_szEventLogFileName,szFilePath);
	fclose(hFile);
	gs_bLogEvents=TRUE;
	return TRUE;
}



//-----------------------------------------------------------------------------
//  Name : SND_fn_bEndLogEventNames()
//-----------------------------------------------------------------------------
//  Description :   This function opens the current log file,
//					saves the map section end line, and clears the file name.
//-----------------------------------------------------------------------------
//  Input :  void
//  Output : BOOL
//-----------------------------------------------------------------------------
//  Creation date : 04/02/98            Author : Frédéric Décréau
//-----------------------------------------------------------------------------
//  Modification date :                 Modification Author :
//  Modifications :
//------------------------------------------------------------------------------*/

BOOL SND_fn_bEndLogEventNames(void)
{
	FILE* hFile;

	hFile=fopen(gs_szEventLogFileName,"aw");
	if (hFile!=NULL)
	{
		fprintf(hFile,"}");
		fclose(hFile);
	}
	strcpy(gs_szEventLogFileName,"");
	gs_bLogEvents=FALSE;
	return TRUE;
}

//-----------------------------------------------------------------------------
//  Name : SND_fn_bLogSoundLine(const char *szLine)
//-----------------------------------------------------------------------------
//  Description :   This function saves a line in the current log file(adds a carriage return)
//-----------------------------------------------------------------------------
//  Input :  void
//  Output : BOOL
//-----------------------------------------------------------------------------
//  Creation date : 04/02/98            Author : Frédéric Décréau
//-----------------------------------------------------------------------------
//  Modification date :                 Modification Author :
//  Modifications :
//------------------------------------------------------------------------------*/

BOOL SND_fn_bLogSoundLine(const char *szLine)
{
	FILE* hFile;
	if (gs_bLogEvents==FALSE) return FALSE;

	hFile=fopen(gs_szEventLogFileName,"aw");
	if (hFile==NULL)
	{
		strcpy(gs_szEventLogFileName,"");
		gs_bLogEvents=FALSE;
		return FALSE;
	}
	fprintf(hFile,"%s\n",szLine);
	fclose(hFile);
	return FALSE;

}










//***************************************************************************
//***************************************************************************
//*																			*
//*																			*
//*									MAP FUNCTIONS							*
//*																			*
//*																			*
//***************************************************************************
//***************************************************************************


//-----------------------------------------------------------------------------
//  Name : SND_fn_eLoadBinMap
//-----------------------------------------------------------------------------
//  Description :   Script Callback which analyses a SndMapE section and loads the banks 
//					
//-----------------------------------------------------------------------------
//  Input :  usual script callback input params        
//  Output : SCR_tde_Anl_ReturnValue    
//-----------------------------------------------------------------------------
//  Creation date : 04/02/98            Author : Frédéric Décréau
//-----------------------------------------------------------------------------
//  Modification date :                 Modification Author :
//  Modifications :
//------------------------------------------------------------------------------*/


SCR_tde_Anl_ReturnValue SND_fn_eLoadBinMap(SCR_tdst_File_Description *p_fFile, char *szAction, char *szParams[], SCR_tde_Anl_Action cType)
{
	unsigned long iBankId;
    
	switch (cType)
    {
    case SCR_EA_Anl_Entry:
		if (strcmp(szAction,"LoadBank")==0)  
        {
			// First parameter is the binary bank id
			iBankId=atoi(szParams[0]);
			// Load the binary bank:
			SND_fn_bInitBank(iBankId);
		}
	}
        return  SCR_ERV_Anl_NormalReturn;
}


//-----------------------------------------------------------------------------
//  Name : SND_fn_vLoadSoundMap(const char *_szMapName,SndBool _bSingleMap)
//-----------------------------------------------------------------------------
//  Description :   This function loads the banks referenced in the project's
//					SndMapE section. If _bSingleMap is TRUE, the function calls
//					the StartLoadBank and EndLoadBank functions: useful if there
//					are no other banks to load for this level.
//					
//-----------------------------------------------------------------------------
//  Input :  const char * (map name) , SndBool         
//  Output : void  
//-----------------------------------------------------------------------------
//  Creation date : 04/02/98            Author : Frédéric Décréau
//-----------------------------------------------------------------------------
//  Modification date :                 Modification Author :
//  Modifications :
//------------------------------------------------------------------------------*/


void SND_fn_vLoadSoundMap(const char *_szMapName,SndBool _bSingleMap)
{
	char szSectionName[MAX_PATH];

	// Create the section name:
	sprintf(szSectionName,"%s.lcb^SndMapE:%s",SND_fn_czGetProjectTitle(),_szMapName);

	if (_bSingleMap) SND_fn_bStartLoadBanks();

	// Analyze the section:
	SCR_fnp_st_RdL0_AnalyseSection(szSectionName,SCR_CDF_uw_Anl_ForceAnalyse);	

	if (_bSingleMap) SND_fn_bEndLoadBanks();
}





//-----------------------------------------------------------------------------
//  Name : SND_fn_iGetLoadedBankArray(int *paiBankListArray,int iArraySize)
//-----------------------------------------------------------------------------
//  Description :   Fills an array of ints with the list of the loaded banks id's.
//					Returns the number of banks. 
//					The size of the array is an input param. If that size is too small,
//					the array is not filled ( but the required size is returned) 
//-----------------------------------------------------------------------------
//  Input :  int *, int.        
//  Output : int.    
//-----------------------------------------------------------------------------
//  Creation date : 04/02/98            Author : Frédéric Décréau
//-----------------------------------------------------------------------------
//  Modification date :                 Modification Author :
//  Modifications :
//-----------------------------------------------------------------------------

int SND_fn_iGetLoadedBankArray(int *paiBankListArray,int iArraySize)
{
	p_tdstSndBankDescriptorElement pBankDescr;
	int iNbBanks,i;
	int *pArrayElmt;
	
	// Check if the size of the array is big enough. if not, return the required size:
	iNbBanks=SND_fn_iGetNumberOfBanks();
	if (iNbBanks>iArraySize) return iNbBanks;

	
	// Scan the list of loaded banks, and write their id's in the array:
	pArrayElmt=paiBankListArray;
	SNDLST2_M_DynamicForEachElementOf(&SND_g_dBankDescriptionList.pList,pBankDescr,i)
	{
		*pArrayElmt=pBankDescr->iId;
		pArrayElmt++;
	}
	return iNbBanks;
}



//-----------------------------------------------------------------------------
//  Name : SND_fn_eLoadResDisk
//-----------------------------------------------------------------------------
//  Description :   Script Callback for creation of tdstBlockResourceDisk 
//					from SndResourceDisk_<n>(<ResType>)
//-----------------------------------------------------------------------------
//  Input :  usual script callback input params        
//  Output : SCR_tde_Anl_ReturnValue    
//-----------------------------------------------------------------------------
//  Creation date : 04/02/98            Author : Frédéric Décréau
//-----------------------------------------------------------------------------
//  Modification date :                 Modification Author :
//  Modifications :
//------------------------------------------------------------------------------*/


SCR_tde_Anl_ReturnValue SND_fn_eLoadResDisk(SCR_tdst_File_Description *p_fFile, char *szAction, char *szParams[], SCR_tde_Anl_Action cType)
{
    tdstBlockResourceDisk *pResDisk;
	char *pszFileName;
	unsigned long ulResId;
    switch (cType)
    {
	case SCR_EA_Anl_BeginSection:// Creation of resource :
		// Create resource:
		pResDisk=(tdstBlockResourceDisk*)SND_fn_pvMallocSnd(sizeof(tdstBlockResourceDisk));

		SCR_M_RdL0_SetSectionLong(0,0,(unsigned long)pResDisk);

		// Set Engine structure data :

		// Get resource editor Id (file id&section name):
		pszFileName=SCR_M_RdL0_GetFileNameR(0);
		ulResId=SND_fn_ulGetEventGroupId(pszFileName);
		ulResId<<=16;
		ulResId+=atoi(szAction);
		pResDisk->Id=ulResId;

		// Get resource type:
		pResDisk->eType=SND_fn_eGetResTypeFromString(szParams[0]);

		// Set default data for the other fields, according to res type:
		pResDisk->eStorage=TYPE_EXTERNAL;
		pResDisk->ulDataSize=0;
		pResDisk->ulDataOffset=0;
		pResDisk->ucVolume=0;
		pResDisk->eStorage=SAMPLE_PCM;

		switch (pResDisk->eType)
        {
            case TYPE_SAMPLE:
                pResDisk->uRes.stSample=c_stDefaultSample;
                break;
            case TYPE_MIDI:
                pResDisk->uRes.stMidi=c_stDefaultMidi;
                break;
            case TYPE_SEQUENCE:
                pResDisk->uRes.stSequence=c_stDefaultSequence;
                break;
            case TYPE_SWITCH:
                SNDLST2_M_StaticInitAnchor(&pResDisk->uRes.stSwitch);
				pResDisk->uRes.stSwitch.bDynamic=UNINITIALIZED_VALUE;
                break;
            case TYPE_SPLIT:
                pResDisk->uRes.stSplit=c_stDefaultSplit;
                break;
            case TYPE_THEME:
				pResDisk->uRes.stTheme=c_stDefaultTheme;
				break;
			case TYPE_RANDOM:
				pResDisk->uRes.stRandom=c_stDefaultRandom;
				break;
		}
		break; // end case SCR_EA_Anl_BeginSection

	case SCR_EA_Anl_Entry:
		SCR_M_RdL0_GetSectionLong(0,0,tdstBlockResourceDisk *,pResDisk);
//		SND_fn_vAnalyseScriptEntryForResType(pResDisk->eType,szAction,szParams,pResDisk);


		switch (pResDisk->eType)
		{
			case TYPE_SAMPLE:
				SND_fn_vAnalyseScriptEntryForResSample(szAction,szParams,pResDisk);
				break;
			case TYPE_MIDI:
				SND_fn_vAnalyseScriptEntryForResMidi(szAction,szParams,pResDisk);
				break;
			case TYPE_CDAUDIO:
				SND_fn_vAnalyseScriptEntryForResCd(szAction,szParams,pResDisk);
				break;
			case TYPE_SEQUENCE:
				SND_fn_vAnalyseScriptEntryForResSequence(szAction,szParams,pResDisk);
				break;
			case TYPE_SPLIT:
				SND_fn_vAnalyseScriptEntryForResSplit(szAction,szParams,pResDisk);
				break;
			case TYPE_SWITCH:
				SND_fn_vAnalyseScriptEntryForResSwitch(szAction,szParams,pResDisk);
				break;
			case TYPE_THEME:
				SND_fn_vAnalyseScriptEntryForResTheme(szAction,szParams,pResDisk);
				break;
			case TYPE_RANDOM:
				SND_fn_vAnalyseScriptEntryForResRandom(szAction,szParams,pResDisk);
				break;
		}
		break ; // end case SCR_EA_Anl_Entry
	}
	return  SCR_ERV_Anl_NormalReturn;
}








//-----------------------------------------------------------------------------
//  Name : SND_fn_eLoadResM
//-----------------------------------------------------------------------------
//  Description :   Script Callback for  SndResourceM section 
//					
//-----------------------------------------------------------------------------
//  Input :  usual script callback input params        
//  Output : SCR_tde_Anl_ReturnValue    
//-----------------------------------------------------------------------------
//  Creation date : 04/02/98            Author : Frédéric Décréau
//-----------------------------------------------------------------------------
//  Modification date :                 Modification Author :
//  Modifications :
//------------------------------------------------------------------------------*/


SCR_tde_Anl_ReturnValue SND_fn_eLoadResM(SCR_tdst_File_Description *p_fFile, char *szAction, char *szParams[], SCR_tde_Anl_Action cType)
{
	static unsigned long ulNextResBinId=0;
	
	tdstBlockResourceDisk *pResDisk;
	tdstBlockResourceMem *pResMem;
	char *pszFileName,*szSectionName;
	unsigned long ulTemp;
	SCR_tdst_Link_Value   *p_stValue;

	tdstInfoTrackMem *pTrack;
	tdstRangeSplitMem *pRange;
	tdstSwitchElement *pElement;
	tdstThemePart *pPart;
	tdstRandomElement *pRandomElement;

	switch (cType)
	{
	case SCR_EA_Anl_BeginSection:
		
		// Get current resource section name:
		szSectionName=SCR_M_RdL0_GetCompleteSectionNameR(0);
		// Try and find this section in the binary bank list:
		p_stValue=SCR_fnp_st_Link_SearchKey(&SND_gs_BinResConversionTable,(char*)szSectionName);
		if (p_stValue!=NULL)
		{	// Sound event is in a binary bank, try to get a pointer:
			ulTemp=SCR_M_ul_Link_GetValue(p_stValue);
			pResMem=SND_fn_pGetBinRes(ulTemp);
			if ((pResMem==NULL)&& g_bForceLoadBanks) 
			{	// Bank is not loaded: try to load it
				SND_fn_bLoadBinaryBank(HIWORD(ulTemp));
				pResMem=SND_fn_pGetBinRes(ulTemp);
			}
			// Set pointer in section context (even if it's null, meaning bank isn't loaded):
			SCR_M_RdL0_SetSectionLong(0,0,(unsigned long)pResMem);

			//add couple bin-ed ids
			fn_AddEdSectionResource(pResMem->Id,szSectionName);

			// Stop this event's section analysis:
			return  SCR_ERV_Anl_TerminateCurrentSection;
		}
		else 
		{
			// Create BRMem:
			pResMem=(tdstBlockResourceMem*)SND_fn_pvMallocSnd(sizeof(tdstBlockResourceMem));
			pResMem->eType=TYPE_INVALID;
			pResMem->eStorage=SAMPLE_INVALID;
			pResMem->bIsLoaded=FALSE;

			SCR_M_RdL0_SetSectionLong(0,0,(unsigned long)pResMem);

			// Compute resource's Editor Id :
			pszFileName=SCR_M_RdL0_GetFileNameR(0);
			ulTemp=SND_fn_ulGetEventGroupId(pszFileName);
			ulTemp<<=16;
			ulTemp+=atoi(szAction);
			// Set it:
			pResMem->Id=ulTemp;
			SCR_M_RdL0_SetSectionLong(0,1,ulTemp);
			
			// Register this new section in the extra bank's link table:  
			ulTemp=(SND_EXTRA_BANKID<<16)+ulNextResBinId;
			ulNextResBinId++;
			SCR_fnp_st_Link_SetValue(&SND_gs_ExtraBankResTable,
									SCR_M_RdL0_GetCompleteSectionNameR(0),ulTemp);

			//add couple bin-ed ids
			fn_AddEdSectionResource(pResMem->Id,szSectionName);
		}
		break;

	case SCR_EA_Anl_EndSubSection:

        SCR_M_RdL0_GetSectionLong(0,0,tdstBlockResourceMem *,pResMem);
		if (strcmp(szParams[0],"SndResourceDisk")==0)
        {		//  We just finished analysing the BRDisk:

			// Get the BRDisk pointer in the sub-section's context:
			SCR_M_RdL0_GetSectionLong(1,0,tdstBlockResourceDisk *,pResDisk);
			
			// Allocate and initalize the static lists if necessary:
			switch (pResDisk->eType)
			{
			case TYPE_SAMPLE:
			case TYPE_MIDI:
			case TYPE_CDAUDIO:
				break;
			case TYPE_SEQUENCE:
				ulTemp=sizeof(tdstInfoTrackMem)*pResDisk->uRes.stSequence.ulNbElements;
				// Allocate the list (optimized static list=array):
				pTrack=(tdstInfoTrackMem *)SND_fn_pvMallocSnd(ulTemp);
				// update list's parent:
				SNDLST2_M_StaticSetFirstElement(&(pResMem->uRes.stSequence.stResList),pTrack);									
				SNDLST2_M_StaticSetNumberOfElements(&(pResMem->uRes.stSequence.stResList),
							pResDisk->uRes.stSequence.ulNbElements);
				break;
			case TYPE_SPLIT:
				ulTemp=sizeof(tdstRangeSplitMem)*
					pResDisk->uRes.stSplit.ulNbRanges;
				// Allocate the list (optimized static list=array):
				pRange=(tdstRangeSplitMem *)SND_fn_pvMallocSnd(ulTemp);
				// update list's parent:
				SNDLST2_M_StaticSetFirstElement(&(pResMem->uRes.stSplit.stRangeList),pRange);									
				SNDLST2_M_StaticSetNumberOfElements(&(pResMem->uRes.stSplit.stRangeList),
							pResDisk->uRes.stSplit.ulNbRanges);
				break;
			case TYPE_SWITCH:
				ulTemp=sizeof(tdstSwitchElement)*
					pResDisk->uRes.stSwitch.ulNbElements;
				// Allocate the list (optimized static list=array):
				pElement=(tdstSwitchElement *)SND_fn_pvMallocSnd(ulTemp);
				// update list's parent:
				SNDLST2_M_StaticSetFirstElement(&(pResMem->uRes.stSwitch),pElement);									
				SNDLST2_M_StaticSetNumberOfElements(&(pResMem->uRes.stSwitch),
							pResDisk->uRes.stSwitch.ulNbElements);
				break;
			case TYPE_RANDOM:
				ulTemp=sizeof(tdstRandomElement)*
					pResDisk->uRes.stRandom.ulNbElements;
				// Allocate the list (optimized static list=array):
				pRandomElement=(tdstRandomElement *)SND_fn_pvMallocSnd(ulTemp);
				// update list's parent:
				SNDLST2_M_StaticSetFirstElement(&(pResMem->uRes.stRandom),pRandomElement);									
				SNDLST2_M_StaticSetNumberOfElements(&(pResMem->uRes.stRandom),
							pResDisk->uRes.stRandom.ulNbElements);
				break;
			case TYPE_THEME:
				//allocate list of part
				ulTemp=sizeof(tdstThemePart)*
					pResDisk->uRes.stTheme.ulNbParts;
				// Allocate the list (optimized static list=array):
				pPart=(tdstThemePart *)SND_fn_pvMallocSnd(ulTemp);
				// update list's parent:
				SNDLST2_M_StaticSetFirstElement(&(pResMem->uRes.stTheme),pPart);									
				SNDLST2_M_StaticSetNumberOfElements(&(pResMem->uRes.stTheme),
							pResDisk->uRes.stTheme.ulNbParts);

				//pre-init partoutro
				pResMem->uRes.stTheme.pstPartOutro=NULL;
				break;
			case TYPE_INVALID:// Problem!
			default:
				SND_fn_vDisplayError(E_uwSndInvalidTypeOfResource,"");
			}
			// load external data in memory:
			SND_fn_bLoadResScriptSnd(pResDisk,pResMem);
			// Free the disk struct:
			SND_fn_vFreeSnd(pResDisk);
        }
		else if (strcmp(szParams[0],"SndTrackListM")==0)
        {	  // Currently, the subsection does all the work
		}
		break;
    }  // end       switch (cType)
    return  SCR_ERV_Anl_NormalReturn;
}                               


//-----------------------------------------------------------------------------
//  Name : SND_fn_eLoadPolyTrackM
//-----------------------------------------------------------------------------
//  Description :   Script Callback for  SndPolyTrackM section 
//					
//-----------------------------------------------------------------------------
//  Input :  usual script callback input params        
//  Output : SCR_tde_Anl_ReturnValue    
//-----------------------------------------------------------------------------
//  Creation date : 04/02/98            Author : Frédéric Décréau
//-----------------------------------------------------------------------------
//  Modification date :                 Modification Author :
//  Modifications :
//------------------------------------------------------------------------------*/


SCR_tde_Anl_ReturnValue SND_fn_eLoadPolyTrackM(SCR_tdst_File_Description *p_fFile, char *szAction, char *szParams[], SCR_tde_Anl_Action cType)
{
        tdstBlockResourceMem *pResMem;
        tdstInfoTrackList *pTrackList;
        tdstInfoTrackMem *pTrack;

        switch (cType)
        {
			case SCR_EA_Anl_BeginSection:
				// Not ACP_Compliant, but works: get TrackList* from Anchor BRMem:
				SCR_M_RdL0_GetSectionLong(-1,0,tdstBlockResourceMem *,pResMem);
				pTrackList=&(pResMem->uRes.stSequence.stResList);
				SCR_M_RdL0_SetSectionLong(0,0,(unsigned long)pTrackList);
				// Set first track to analyse in child section:
				pTrack=SNDLST2_M_StaticGetFirstElement(pTrackList);
				SCR_M_RdL0_SetContextLong(1,0,(unsigned long)pTrack);
				break;
			case SCR_EA_Anl_EndSubSection:
				// Set next track to analyse in child section:
				SCR_M_RdL0_GetContextLong(1,0,tdstInfoTrackMem *,pTrack);
				pTrack=SNDLST2_M_StaticGetNextElement(pTrack);
				SCR_M_RdL0_SetContextLong(1,0,(unsigned long)pTrack);
				break;
        }
        return  SCR_ERV_Anl_NormalReturn;
}

//-----------------------------------------------------------------------------
//  Name : SND_fn_eLoadTrackM
//-----------------------------------------------------------------------------
//  Description :   Script Callback for  SndTrackM section (sequence)
//					Fills a track struct with its data from script
//-----------------------------------------------------------------------------
//  Input :  usual script callback input params        
//  Output : SCR_tde_Anl_ReturnValue    
//-----------------------------------------------------------------------------
//  Creation date : 04/02/98            Author : Frédéric Décréau
//-----------------------------------------------------------------------------
//  Modification date :                 Modification Author :
//  Modifications :
//------------------------------------------------------------------------------*/

SCR_tde_Anl_ReturnValue SND_fn_eLoadTrackM(SCR_tdst_File_Description *p_fFile, char *szAction, char *szParams[], SCR_tde_Anl_Action cType)
{
        tdstInfoTrackMem *pTrack;
        tdstBlockResourceMem *pResMem;
		SCR_tdst_Cxt_Values* pScriptContext;

        switch (cType)
        {
                case SCR_EA_Anl_BeginSection:
						break;
                case SCR_EA_Anl_Entry:
                    SCR_M_RdL0_GetContextLong(0,0,tdstInfoTrackMem *,pTrack);
					if (strcmp(szAction,"SetXIOFades")==0)
                    {
                            pTrack->ulCrossFade=*((unsigned long*)szParams[0]);
                            pTrack->ulFadeIn=*((unsigned long*)szParams[1]);
                            pTrack->ulFadeOut=*((unsigned long*)szParams[2]);
                    }
                    else if (strcmp(szAction,"LoadResource")==0)
                    {
                        // get pointer to referenced resource: 
						if (SCR_fn_uc_RdL0_GetNumberOfParameters(szParams)==1)//A VIRER
							pScriptContext=SCR_fnp_st_RdL0_AnalyseSection(szParams[0],SCR_CDF_uw_Anl_Normal);
						else pScriptContext=((SCR_tdst_Cxt_Values**)szParams[1])[0];
                        pResMem=(tdstBlockResourceMem*)SCR_M_ul_RdL0_ExtractLongValue(pScriptContext, 0);
                        pTrack->uRes.pstPtr=pResMem;
                    }
        }
        return  SCR_ERV_Anl_NormalReturn;
}


//-----------------------------------------------------------------------------
//  Name : SND_fn_eLoadPolyRangeM
//-----------------------------------------------------------------------------
//  Description :   Script Callback for  SndPolyRangeM section (split)
//					
//-----------------------------------------------------------------------------
//  Input :  usual script callback input params        
//  Output : SCR_tde_Anl_ReturnValue    
//-----------------------------------------------------------------------------
//  Creation date : 04/02/98            Author : Frédéric Décréau
//-----------------------------------------------------------------------------
//  Modification date :                 Modification Author :
//  Modifications :
//------------------------------------------------------------------------------*/



SCR_tde_Anl_ReturnValue SND_fn_eLoadPolyRangeM(SCR_tdst_File_Description *p_fFile, char *szAction, char *szParams[], SCR_tde_Anl_Action cType)
{
        tdstBlockResourceMem *pResMem;
        tdstRangeSplitListMem *pRangeList;
        tdstRangeSplitMem *pRange;

        switch (cType)
        {
            case SCR_EA_Anl_BeginSection:
                // Not ACP_Compliant, but works: get TrackList* from Anchor BRMem:
				SCR_M_RdL0_GetSectionLong(-1,0,tdstBlockResourceMem *,pResMem);
                pRangeList=&(pResMem->uRes.stSplit.stRangeList);
                SCR_M_RdL0_SetSectionLong(0,0,(unsigned long)pRangeList);
				// Set first range to analyse in child section:
				pRange=SNDLST2_M_StaticGetFirstElement(pRangeList);
				SCR_M_RdL0_SetContextLong(1,0,(unsigned long)pRange);
				break;
            case SCR_EA_Anl_EndSubSection:
				// Set next range to analyse in child section:
				SCR_M_RdL0_GetContextLong(1,0,tdstRangeSplitMem *,pRange);
				pRange=SNDLST2_M_StaticGetNextElement(pRange);
				SCR_M_RdL0_SetContextLong(1,0,(unsigned long)pRange);
				break;
        }
        return  SCR_ERV_Anl_NormalReturn;
}


//-----------------------------------------------------------------------------
//  Name : SND_fn_eLoadRangeM
//-----------------------------------------------------------------------------
//  Description :   Script Callback for  SndRangeM section (split)
//					Fills a track struct with its data from script
//-----------------------------------------------------------------------------
//  Input :  usual script callback input params        
//  Output : SCR_tde_Anl_ReturnValue    
//-----------------------------------------------------------------------------
//  Creation date : 04/02/98            Author : Frédéric Décréau
//-----------------------------------------------------------------------------
//  Modification date :                 Modification Author :
//  Modifications :
//------------------------------------------------------------------------------*/

SCR_tde_Anl_ReturnValue SND_fn_eLoadRangeM(SCR_tdst_File_Description *p_fFile, char *szAction, char *szParams[], SCR_tde_Anl_Action cType)
{
        tdstRangeSplitMem *pRange;
        tdstBlockResourceMem *pResMem;
		SCR_tdst_Cxt_Values* pScriptContext;

        switch (cType)
        {
                case SCR_EA_Anl_BeginSection:
						break;
                case SCR_EA_Anl_Entry:
                        SCR_M_RdL0_GetContextLong(0,0,tdstRangeSplitMem*,pRange);
						if (strcmp(szAction,"LoadResource")==0)
                        {
                                // get pointer to referenced resource: 
								if (SCR_fn_uc_RdL0_GetNumberOfParameters(szParams)==1) // A VIRER
									pScriptContext=SCR_fnp_st_RdL0_AnalyseSection(szParams[0],SCR_CDF_uw_Anl_Normal);
								else pScriptContext=((SCR_tdst_Cxt_Values**)szParams[1])[0];
                                pResMem=(tdstBlockResourceMem*)SCR_M_ul_RdL0_ExtractLongValue(pScriptContext, 0);
                                pRange->uRes.pstPtr=pResMem;
                        }
                        if (strcmp(szAction,"SetPitchValue")==0)
                        {
                                pRange->rPitchA=M_DoubleToRealSnd(SCRIPT_DOUBLE(szParams[0]));
                                pRange->rPitchB=M_DoubleToRealSnd(SCRIPT_DOUBLE(szParams[1]));
                                pRange->rPitchC=M_DoubleToRealSnd(SCRIPT_DOUBLE(szParams[2]));
                                pRange->rPitchD=M_DoubleToRealSnd(SCRIPT_DOUBLE(szParams[3]));
                        }
                        if (strcmp(szAction,"SetVolume")==0)
                        {
                                pRange->ucVolume=(unsigned char)(SCRIPT_INT(szParams[0]));
                        }
                        else if (strcmp(szAction,"SetPitchCoef")==0)
                        {
                                pRange->rCoefPitch=M_DoubleToRealSnd(SCRIPT_DOUBLE(szParams[0]));
                        }
                        break;
                case SCR_EA_Anl_EndSection:
                        SCR_M_RdL0_GetContextLong(0,0,tdstRangeSplitMem *,pRange);
						//pRange have been loaded; let's init all the other fields
#ifndef DISABLE_SPLIT
                        SND_fn_vInitRangeSplit(pRange);
#endif
                        break;
        }
        return  SCR_ERV_Anl_NormalReturn;
}


//-----------------------------------------------------------------------------
//  Name : SND_fn_eLoadSwitchListM
//-----------------------------------------------------------------------------
//  Description :   Script Callback for  SndSwitchListM section (switch)
//					
//-----------------------------------------------------------------------------
//  Input :  usual script callback input params        
//  Output : SCR_tde_Anl_ReturnValue    
//-----------------------------------------------------------------------------
//  Creation date : 04/02/98            Author : Frédéric Décréau
//-----------------------------------------------------------------------------
//  Modification date :                 Modification Author :
//  Modifications :
//------------------------------------------------------------------------------*/

SCR_tde_Anl_ReturnValue SND_fn_eLoadSwitchListM(SCR_tdst_File_Description *p_fFile, char *szAction, char *szParams[], SCR_tde_Anl_Action cType)
{
	SCR_tdst_Cxt_Values *pScriptContext;
    tdstBlockResourceMem *pResMem;
    tdstTypeSwitchMem* pSwitchList;
    tdstSwitchElement *pSwitchElement;
	char szTemp[260];

    switch (cType)
    {
    case SCR_EA_Anl_BeginSection:
		// Not ACP_Compliant, but works: get SwitchList* from Anchor BRMem:
		SCR_M_RdL0_GetSectionLong(-1,0,tdstBlockResourceMem *,pResMem);
		pSwitchList=&(pResMem->uRes.stSwitch);
		SCR_M_RdL0_SetSectionLong(0,0,(unsigned long)pSwitchList);
		pSwitchList->bDynamic=FALSE;
		pSwitchList->bMaintainIfSwitched=FALSE;
		pSwitchList->hDefault=(SND_tdxHandleToSoundInfo)NULL;

		// Set first switch element to analyse in child section:
		pSwitchElement=SNDLST2_M_StaticGetFirstElement(pSwitchList);
		SCR_M_RdL0_SetSectionLong(0,1,(unsigned long)pSwitchElement);
		break;

    case SCR_EA_Anl_Entry:
		SCR_M_RdL0_GetSectionLong(0,0,tdstTypeSwitchMem *,pSwitchList);
		if (strcmp(szAction,"SetInfoType")==0)
		{
			pScriptContext=SCR_fnp_st_RdL0_AnalyseSection(szParams[0], SCR_CDF_uw_Anl_Normal);
			pSwitchList->lTypeSwitch=(long)SCR_M_ul_RdL0_ExtractLongValue(pScriptContext,0);
		}
		else if (strcmp(szAction,"SoundInfoType")==0)
		{
			sprintf(szTemp,"%s.sif^%s",SND_fn_czGetProjectTitle(),szParams[0]);
			pScriptContext=SCR_fnp_st_RdL0_AnalyseSection(szTemp, SCR_CDF_uw_Anl_Normal);
			pSwitchList->lTypeSwitch=(long)SCR_M_ul_RdL0_ExtractLongValue(pScriptContext,0);
		}
		else if (strcmp(szAction,"SetDefaultValue")==0)
		{
			pScriptContext=SCR_fnp_st_RdL0_AnalyseSection(szParams[0], SCR_CDF_uw_Anl_Normal);
			pSwitchList->hDefault=(long)SCR_M_ul_RdL0_ExtractLongValue(pScriptContext,0);
		}
		else if (strcmp(szAction,"SetDynamic")==0)
		{
			pSwitchList->bDynamic=(SndBool)((char*)szParams[1])[0];
		}
		else if (strcmp(szAction,"SetMaintainIfSwitched")==0)
		{
			pSwitchList->bMaintainIfSwitched=(SndBool)((char*)szParams[1])[0];
		}
		break;
    case SCR_EA_Anl_EndSubSection:
		// Set next switch element to analyse in child section:
		SCR_M_RdL0_GetSectionLong(0,1,tdstSwitchElement *,pSwitchElement);
		pSwitchElement=SNDLST2_M_StaticGetNextElement(pSwitchElement);
		SCR_M_RdL0_SetSectionLong(0,1,(unsigned long)pSwitchElement);
        break;
	}
    return  SCR_ERV_Anl_NormalReturn;
}


//-----------------------------------------------------------------------------
//  Name : SND_fn_eLoadSwitchElementM
//-----------------------------------------------------------------------------
//  Description :   Script Callback for  SndSwitchElementM section (switch)
//					Fills a SwitchElement struct with its data from script
//-----------------------------------------------------------------------------
//  Input :  usual script callback input params        
//  Output : SCR_tde_Anl_ReturnValue    
//-----------------------------------------------------------------------------
//  Creation date : 04/02/98            Author : Frédéric Décréau
//-----------------------------------------------------------------------------
//  Modification date :                 Modification Author :
//  Modifications :
//------------------------------------------------------------------------------*/


SCR_tde_Anl_ReturnValue SND_fn_eLoadSwitchElementM(SCR_tdst_File_Description *p_fFile, char *szAction, char *szParams[], SCR_tde_Anl_Action cType)
{
	SCR_tdst_Cxt_Values *pScriptContext;
    tdstSwitchElement *pSwitchElement;
    tdstBlockResourceMem *pResMem;
	char szTemp[260];

    switch (cType)
    {
	case SCR_EA_Anl_BeginSection:
		break;
	case SCR_EA_Anl_Entry:
		SCR_M_RdL0_GetSectionLong(-1,1,tdstSwitchElement *,pSwitchElement);
		if (strcmp(szAction,"LoadResource")==0)
		{
			// get pointer to referenced resource: 
			if (SCR_fn_uc_RdL0_GetNumberOfParameters(szParams)==1) // A VIRER
				pScriptContext=SCR_fnp_st_RdL0_AnalyseSection(szParams[0],SCR_CDF_uw_Anl_Normal);
			else pScriptContext=((SCR_tdst_Cxt_Values**)szParams[1])[0];
			pResMem=(tdstBlockResourceMem*)SCR_M_ul_RdL0_ExtractLongValue(pScriptContext, 0);
			// register pointer in switch element:
			pSwitchElement->uRes.pstPtr=pResMem;
		}
		else if (strcmp(szAction,"SetInfoValue")==0)
		{
			pScriptContext=SCR_fnp_st_RdL0_AnalyseSection(szParams[0], SCR_CDF_uw_Anl_Normal);
			pSwitchElement->hIndice=(SND_tdxHandleToSoundInfo)SCR_M_ul_RdL0_ExtractLongValue(pScriptContext,0);
		}
		else if (strcmp(szAction,"SoundInfoValue")==0)
		{
			sprintf(szTemp,"%s.sif^%s",SND_fn_czGetProjectTitle(),szParams[0]);
			pScriptContext=SCR_fnp_st_RdL0_AnalyseSection(szTemp, SCR_CDF_uw_Anl_Normal);
			pSwitchElement->hIndice=(SND_tdxHandleToSoundInfo)SCR_M_ul_RdL0_ExtractLongValue(pScriptContext,0);
		}
    }
    return  SCR_ERV_Anl_NormalReturn;
}

//-----------------------------------------------------------------------------
//  Name : SND_fn_eLoadRandomListM
//-----------------------------------------------------------------------------
//  Description :   Script Callback for  SndRandomListM section (Random)
//					
//-----------------------------------------------------------------------------
//  Input :  usual script callback input params        
//  Output : SCR_tde_Anl_ReturnValue    
//-----------------------------------------------------------------------------
//  Creation date : 04/02/98            Author : Stéphane RONSE
//-----------------------------------------------------------------------------
//  Modification date :                 Modification Author :
//  Modifications :
//------------------------------------------------------------------------------*/

SCR_tde_Anl_ReturnValue SND_fn_eLoadRandomListM(SCR_tdst_File_Description *p_fFile, char *szAction, char *szParams[], SCR_tde_Anl_Action cType)
{
    tdstBlockResourceMem *pResMem;
    tdstTypeRandomMem* pRandomList;
    tdstRandomElement *pRandomElement;

    switch (cType)
    {
    case SCR_EA_Anl_BeginSection:
		// Not ACP_Compliant, but works: get RandomList* from Anchor BRMem:
		SCR_M_RdL0_GetSectionLong(-1,0,tdstBlockResourceMem *,pResMem);
		pRandomList=&(pResMem->uRes.stRandom);
		SCR_M_RdL0_SetSectionLong(0,0,(unsigned long)pRandomList);

		// Set first Random element to analyse in child section:
		pRandomElement=SNDLST2_M_StaticGetFirstElement(pRandomList);
		SCR_M_RdL0_SetSectionLong(0,1,(unsigned long)pRandomElement);
		break;

    case SCR_EA_Anl_Entry:
		SCR_M_RdL0_GetSectionLong(0,0,tdstTypeRandomMem *,pRandomList);

		break;
    case SCR_EA_Anl_EndSubSection:
		// Set next random element to analyse in child section:
		SCR_M_RdL0_GetSectionLong(0,1,tdstRandomElement *,pRandomElement);
		pRandomElement=SNDLST2_M_StaticGetNextElement(pRandomElement);
		SCR_M_RdL0_SetSectionLong(0,1,(unsigned long)pRandomElement);
        break;
	}
    return  SCR_ERV_Anl_NormalReturn;
}


//-----------------------------------------------------------------------------
//  Name : SND_fn_eLoadRandomElementM
//-----------------------------------------------------------------------------
//  Description :   Script Callback for  SndRandomElementM section (Random)
//					Fills a RandomElement struct with its data from script
//-----------------------------------------------------------------------------
//  Input :  usual script callback input params        
//  Output : SCR_tde_Anl_ReturnValue    
//-----------------------------------------------------------------------------
//  Creation date : 04/02/98            Author : Stéphane RONSE
//-----------------------------------------------------------------------------
//  Modification date :                 Modification Author :
//  Modifications :
//------------------------------------------------------------------------------*/


SCR_tde_Anl_ReturnValue SND_fn_eLoadRandomElementM(SCR_tdst_File_Description *p_fFile, char *szAction, char *szParams[], SCR_tde_Anl_Action cType)
{
	SCR_tdst_Cxt_Values *pScriptContext;
    tdstRandomElement *pRandomElement;
    tdstBlockResourceMem *pResMem;

    switch(cType)
    {
	case SCR_EA_Anl_BeginSection:
		break;
	case SCR_EA_Anl_Entry:
		SCR_M_RdL0_GetSectionLong(-1,1,tdstRandomElement *,pRandomElement);
		if (strcmp(szAction,"LoadResource")==0)
		{
			// get pointer to referenced resource: 
			if (SCR_fn_uc_RdL0_GetNumberOfParameters(szParams)==1) // A VIRER
				pScriptContext=SCR_fnp_st_RdL0_AnalyseSection(szParams[0],SCR_CDF_uw_Anl_Normal);
			else pScriptContext=((SCR_tdst_Cxt_Values**)szParams[1])[0];
			pResMem=(tdstBlockResourceMem*)SCR_M_ul_RdL0_ExtractLongValue(pScriptContext, 0);
			// register pointer in Random element:
			pRandomElement->uRes.pstPtr=pResMem;
		}
        else if (strcmp(szAction,"SetProbability")==0)
        {
			pRandomElement->rProb=M_DoubleToRealSnd(SCRIPT_DOUBLE(szParams[0]));
        }
        else if (strcmp(szAction,"SetElementCanBeChoosenTwice")==0)
        {
			pRandomElement->bCanBeChosenTwice=(SndBool)((char*)szParams[1])[0];
        }
    }
    return  SCR_ERV_Anl_NormalReturn;
}


//-----------------------------------------------------------------------------
//  Name : SND_fn_eLoadThemePartOutroM
//-----------------------------------------------------------------------------
//  Description :   Script Callback for  SndThemePartOutroM section 
//					its only function is to update the part's pointer:
//-----------------------------------------------------------------------------
//  Input :  usual script callback input params        
//  Output : SCR_tde_Anl_ReturnValue    
//-----------------------------------------------------------------------------
//  Creation date : 04/11/98            Author : Stephane RONSE
//-----------------------------------------------------------------------------
//  Modification date :                 Modification Author :
//  Modifications :
//------------------------------------------------------------------------------*/
SCR_tde_Anl_ReturnValue SND_fn_eLoadThemePartOutroM(SCR_tdst_File_Description *p_fFile, char *szAction, char *szParams[], SCR_tde_Anl_Action cType)
{
    tdstBlockResourceMem    *pResMem;
    tdstThemePartOutro      *pPart;
	SCR_tdst_Cxt_Values* pScriptContext;
    tdstTypeThemeMem           *pTheme;
        
    switch (cType)
    {
    case SCR_EA_Anl_BeginSection:
		SCR_M_RdL0_GetSectionLong(-1,0,tdstBlockResourceMem *,pResMem);
		pTheme=&(pResMem->uRes.stTheme);

        pPart=SND_fn_pvMallocSnd(sizeof(tdstThemePartOutro));
		pPart->uRes.pstPtr=SND_C_RES_FANTOME_PTR;
		pPart->eTransition=TRANSITION_CROSSFADE;
		pPart->rFadeDuration=DEFAULT_FADEDURATION;

        SCR_M_RdL0_SetContextLong(0,0,(unsigned long)pPart);

		pTheme->pstPartOutro=pPart;
        break;
    case SCR_EA_Anl_Entry:
		SCR_M_RdL0_GetSectionLong(0,0,tdstThemePartOutro *,pPart);

		if (strcmp(szAction,"SetFadeDuration")==0)
			pPart->rFadeDuration=M_DoubleToRealSnd(SCRIPT_DOUBLE(szParams[0]));
		else if (strcmp(szAction,"SetTransitionType")==0)
		{
			if (!strcmp(szParams[0],"TRANSITION_CROSSFADE"))
				pPart->eTransition=TRANSITION_CROSSFADE;
			else if (!strcmp(szParams[0],"TRANSITION_ATENDOFPART"))
				pPart->eTransition=TRANSITION_ATENDOFPART;
			else if (!strcmp(szParams[0],"TRANSITION_OUTRO"))
				pPart->eTransition=TRANSITION_OUTRO;
		}
		else if (strcmp(szAction,"LoadResource")==0)
		{
			if (SCR_fn_uc_RdL0_GetNumberOfParameters(szParams)==1) // A VIRER
				pScriptContext=SCR_fnp_st_RdL0_AnalyseSection(szParams[0],SCR_CDF_uw_Anl_Normal);
			else pScriptContext=((SCR_tdst_Cxt_Values**)szParams[1])[0];
			pResMem=(tdstBlockResourceMem*)SCR_M_ul_RdL0_ExtractLongValue(pScriptContext, 0);
			pPart->uRes.pstPtr=pResMem;
		}
		break;
    }
    return  SCR_ERV_Anl_NormalReturn;
}


//-----------------------------------------------------------------------------
//  Name : SND_fn_eLoadThemePartListM
//-----------------------------------------------------------------------------
//  Description :   Script Callback for  SndThemePartListM section 
//					its only function is to update the part's pointer:
//-----------------------------------------------------------------------------
//  Input :  usual script callback input params        
//  Output : SCR_tde_Anl_ReturnValue    
//-----------------------------------------------------------------------------
//  Creation date : 04/02/98            Author : Frédéric Décréau
//-----------------------------------------------------------------------------
//  Modification date :                 Modification Author :
//  Modifications :
//------------------------------------------------------------------------------*/

SCR_tde_Anl_ReturnValue SND_fn_eLoadThemePartListM(SCR_tdst_File_Description *p_fFile, char *szAction, char *szParams[], SCR_tde_Anl_Action cType)
{
        tdstBlockResourceMem    *pResMem;
        tdstThemePart           *pPart;
        tdstTypeThemeMem         *pTheme;
            
        switch (cType)
        {
            case SCR_EA_Anl_BeginSection:
				SCR_M_RdL0_GetSectionLong(-1,0,tdstBlockResourceMem *,pResMem);
				pTheme=&(pResMem->uRes.stTheme);
				SCR_M_RdL0_SetSectionLong(0,0,(unsigned long)pTheme);
				// Set first part to analyse in child section:
				pPart=SNDLST2_M_StaticGetFirstElement(pTheme);
				SCR_M_RdL0_SetContextLong(1,0,(unsigned long)pPart);
				break;
            case SCR_EA_Anl_EndSubSection:
				// Set next part to analyse in child section:
				SCR_M_RdL0_GetContextLong(1,0,tdstThemePart *,pPart);
				pPart=SNDLST2_M_StaticGetNextElement(pPart);
				SCR_M_RdL0_SetContextLong(1,0,(unsigned long)pPart);
				break;
        }
        return  SCR_ERV_Anl_NormalReturn;
}

//-----------------------------------------------------------------------------
//  Name : SND_fn_eLoadThemePartM
//-----------------------------------------------------------------------------
//  Description :   Script Callback for  SndThemePartM section 
//					Fills a part strcut with its corresponding res pointer
//-----------------------------------------------------------------------------
//  Input :  usual script callback input params        
//  Output : SCR_tde_Anl_ReturnValue    
//-----------------------------------------------------------------------------
//  Creation date : 04/02/98            Author : Frédéric Décréau
//-----------------------------------------------------------------------------
//  Modification date :                 Modification Author :
//  Modifications :
//------------------------------------------------------------------------------*/


SCR_tde_Anl_ReturnValue SND_fn_eLoadThemePartM(SCR_tdst_File_Description *p_fFile, char *szAction, char *szParams[], SCR_tde_Anl_Action cType)
{
    tdstBlockResourceMem    *pResMem;
    tdstThemePart           *pPart;
	SCR_tdst_Cxt_Values* pScriptContext;
        
    switch (cType)
    {
		case SCR_EA_Anl_BeginSection:// NOP
			SCR_M_RdL0_GetContextLong(0,0,tdstThemePart *,pPart);
			//pre-init
			pPart->bLoopStart=FALSE;
			pPart->bLoopEnd=FALSE;
			pPart->lNbLoops=C_THEME_PART_PLAY_ONCE;
			pPart->eTransition=TRANSITION_CROSSFADE;
			pPart->rFadeDuration=DEFAULT_FADEDURATION;

			break;
		case SCR_EA_Anl_Entry:
			// Current part pointer is set by parent section:
			SCR_M_RdL0_GetContextLong(0,0,tdstThemePart *,pPart);

			if (strcmp(szAction,"SetStartLoop")==0)
				pPart->bLoopStart=(SndBool)((char*)szParams[1])[0];
			else if (strcmp(szAction,"SetEndLoop")==0)
				pPart->bLoopEnd=(SndBool)((char*)szParams[1])[0];
			else if (strcmp(szAction,"SetNumberOfLoops")==0)
				pPart->lNbLoops=atoi(szParams[0]);
			else if (strcmp(szAction,"SetFadeDuration")==0)
				pPart->rFadeDuration=M_DoubleToRealSnd(SCRIPT_DOUBLE(szParams[0]));
			else if (strcmp(szAction,"SetTransitionType")==0)
			{
				if (!strcmp(szParams[0],"TRANSITION_CROSSFADE"))
					pPart->eTransition=TRANSITION_CROSSFADE;
				else if (!strcmp(szParams[0],"TRANSITION_ATENDOFPART"))
					pPart->eTransition=TRANSITION_ATENDOFPART;
				else if (!strcmp(szParams[0],"TRANSITION_OUTRO"))
					pPart->eTransition=TRANSITION_OUTRO;
			}
			else if (strcmp(szAction,"LoadResource")==0)
			{
				if (SCR_fn_uc_RdL0_GetNumberOfParameters(szParams)==1)
					pScriptContext=SCR_fnp_st_RdL0_AnalyseSection(szParams[0],SCR_CDF_uw_Anl_Normal);
				else 
					pScriptContext=((SCR_tdst_Cxt_Values**)szParams[1])[0];

				pResMem=(tdstBlockResourceMem*)SCR_M_ul_RdL0_ExtractLongValue(pScriptContext, 0);
				pPart->uRes.pstPtr=pResMem;
			}
			break;
    }
    return  SCR_ERV_Anl_NormalReturn;
}

//***************************************************************************
//***************************************************************************
//*																			*
//*																			*
//*		"SECTION NAME <-> BIN ID <-> POINTER" CONVERSION FUNCTIONS			*
//*																			*
//*																			*
//***************************************************************************
//***************************************************************************


// The following link table contains, for each sound event stored in a binary
//		bank( excluding the extra bank,and whether or not the bank is loaded),
//		the event's section name, and its binary id.


SCR_tdst_Link_Table SND_gs_BinEventConversionTable;
// The same for resources:
SCR_tdst_Link_Table SND_gs_BinResConversionTable;

//-----------------------------------------------------------------------------
//  Name : SND_fn_eLoadEventIdList
//-----------------------------------------------------------------------------
//  Description :   Script Callback for  SndEventIdList section 
//					Fills the section<->bin id conversion table from script
//-----------------------------------------------------------------------------
//  Input :  usual script callback input params        
//  Output : SCR_tde_Anl_ReturnValue    
//-----------------------------------------------------------------------------
//  Creation date : 04/02/98            Author : Frédéric Décréau
//-----------------------------------------------------------------------------
//  Modification date :                 Modification Author :
//  Modifications :
//------------------------------------------------------------------------------*/


SCR_tde_Anl_ReturnValue SND_fn_eLoadEventIdList(SCR_tdst_File_Description *p_fFile, char *szAction, char *szParams[], SCR_tde_Anl_Action cType)
{
	char *pszEventName;
	unsigned long ulBankId,ulPos;
    
	switch (cType)
    {
        case SCR_EA_Anl_Entry:
				if (strcmp(szAction,"SetEvent")==0)  
                {
					// First parameter is the event section name
					pszEventName=szParams[0];
					ulBankId=atoi(szParams[1]);
					ulPos=atoi(szParams[2]);
					ulBankId<<=16; // Store Bank Id in upper part of the unsigned long
					ulBankId+=ulPos; // Store Res Pos in lower part of the unsigned long
					SCR_fnp_st_Link_SetValue(&SND_gs_BinEventConversionTable,
												pszEventName,ulBankId);
				}
	}
        return  SCR_ERV_Anl_NormalReturn;
}


//-----------------------------------------------------------------------------
//  Name : SND_fn_eLoadResIdList
//-----------------------------------------------------------------------------
//  Description :   Script Callback for  SndResIdList section 
//					Fills the section<->bin id conversion table from script
//-----------------------------------------------------------------------------
//  Input :  usual script callback input params        
//  Output : SCR_tde_Anl_ReturnValue    
//-----------------------------------------------------------------------------
//  Creation date : 04/02/98            Author : Frédéric Décréau
//-----------------------------------------------------------------------------
//  Modification date :                 Modification Author :
//  Modifications :
//------------------------------------------------------------------------------*/


SCR_tde_Anl_ReturnValue SND_fn_eLoadResIdList(SCR_tdst_File_Description *p_fFile, char *szAction, char *szParams[], SCR_tde_Anl_Action cType)
{
	char *pszResName;
	unsigned long ulBankId,ulPos;
    
	switch (cType)
    {
        case SCR_EA_Anl_Entry:
				if (strcmp(szAction,"SetResource")==0)  
                {
					// First parameter is the event section name
					pszResName=szParams[0];
					ulBankId=atoi(szParams[1]);
					ulPos=atoi(szParams[2]);
					ulBankId<<=16; // Store Bank Id in upper part of the unsigned long
					ulBankId+=ulPos; // Store Res Pos in lower part of the unsigned long
					SCR_fnp_st_Link_SetValue(&SND_gs_BinResConversionTable,
												pszResName,ulBankId);
				}
	}
        return  SCR_ERV_Anl_NormalReturn;
}


//-----------------------------------------------------------------------------
//  Name : SND_fn_ulGetEventBinIdFromSectionName
//-----------------------------------------------------------------------------
//  Description :   Returns a binary id given an event section name.
//					If the event is in no bank (including extra bank),it
//					analyses the section, which loads the event in extra bank,
//					with referenced data, and returns the created bin id.
//-----------------------------------------------------------------------------
//  Input :  const char *        
//  Output : unsigned long    
//-----------------------------------------------------------------------------
//  Creation date : 04/02/98            Author : Frédéric Décréau
//-----------------------------------------------------------------------------
//  Modification date :                 Modification Author :
//  Modifications :
//------------------------------------------------------------------------------*/


unsigned long SND_fn_ulGetEventBinIdFromSectionName(const char *szSectionName)
{
	char szMessage[256];
	SCR_tdst_Link_Value   *p_stValue;

	// Search the bin id in the binary banks:
	p_stValue=SCR_fnp_st_Link_SearchKey(&SND_gs_BinEventConversionTable,(char*)szSectionName);
	if (p_stValue!=NULL) return SCR_M_ul_Link_GetValue(p_stValue);

	// Search it in the extra bank:
	p_stValue=SCR_fnp_st_Link_SearchKey(&SND_gs_ExtraBankEventsTable,(char*)szSectionName);
	if (p_stValue!=NULL) return SCR_M_ul_Link_GetValue(p_stValue);
	
	// event is in no bank:

	// Print a warning message ( we might want to delete this line):
	sprintf(szMessage,"Event Name: %s",szSectionName);
	SND_fn_vDisplayErrorEx(E_uwSndEventNotInABank,szMessage,SNDERR_DISPLAY_IF_WANTED);
	
	// Analyse the section to load the event in the extra bank:
	SCR_fnp_st_RdL0_AnalyseSection((char *)szSectionName,SCR_CDF_uw_Anl_Normal);
	
	// event should now be in the extra bank, get its id:
	p_stValue=SCR_fnp_st_Link_SearchKey(&SND_gs_ExtraBankEventsTable,(char*)szSectionName);
	if (p_stValue!=NULL) return SCR_M_ul_Link_GetValue(p_stValue);

	// if we're here, it means we couldn't load the event:

	// Print a warning message ( we might want to delete this line):
	sprintf(szMessage,"Could not load sound event in extra bank, Event Name: %s",szSectionName);
	SND_fn_vDisplayErrorEx(E_uwSndEventNotInABank,szMessage,SNDERR_DISPLAY_IF_WANTED);
	
	return SND_INVALID_BINID;
}



//-----------------------------------------------------------------------------
//  Name : SND_fn_szGetEventSectionNameFromBinId
//-----------------------------------------------------------------------------
//  Description :   Returns an event section name given a binary id.
//					Depending on the bank id (extra bank or not), we search a
//					link table or the other for the id, and get the corresponding
//					section name.
//-----------------------------------------------------------------------------
//  Input :  unsigned long ( Bin Id)      
//  Output : const char*  ( section name)
//-----------------------------------------------------------------------------
//  Creation date : 04/02/98            Author : Frédéric Décréau
//-----------------------------------------------------------------------------
//  Modification date :                 Modification Author :
//  Modifications :
//------------------------------------------------------------------------------

const char* SND_fn_szGetEventSectionNameFromBinId(unsigned long _ulBinId)
{
	unsigned long ulBankId;
	SCR_tdst_Link_Value   *p_stValue;
	char szMessage[256];

	ulBankId=HIWORD(_ulBinId);

	// search the id in a link table or the other, depending on the bank id: 
	if (ulBankId==SND_EXTRA_BANKID)
		p_stValue=SCR_fnp_st_Link_SearchValue(&SND_gs_ExtraBankEventsTable,_ulBinId);
	else 
		p_stValue=SCR_fnp_st_Link_SearchValue(&SND_gs_BinEventConversionTable,_ulBinId);

	// If we found it, return corresponding key (i.e. section name) :
	if (p_stValue!=NULL) return SCR_M_p_sz_Link_GetKey(p_stValue);

	// We haven't found the id: abnormal situation:
	sprintf(szMessage,"Unknown Binary event id : %ul(bank id:%ul)",_ulBinId,ulBankId);
	SND_fn_vDisplayErrorEx(E_uwSndEventNotInABank,szMessage,SNDERR_DISPLAY_IF_WANTED);
	return NULL;
}


//-----------------------------------------------------------------------------
//  Name : SND_fn_pGetBinEvent
//-----------------------------------------------------------------------------
//  Description :   Returns a pointer given an event binary id .
//-----------------------------------------------------------------------------
//  Input :  unsigned long  ( Bin Id )      
//  Output : SND_tdstBlockEvent * ( event pointer)
//-----------------------------------------------------------------------------
//  Creation date : 04/02/98            Author : Frédéric Décréau
//-----------------------------------------------------------------------------
//  Modification date :                 Modification Author :
//  Modifications :
//------------------------------------------------------------------------------


// GetBinEvent: return the pointer to a binary event
//	If event is not loaded, returns NULL
SND_tdstBlockEvent* SND_CALL SND_fn_pGetBinEvent(unsigned long _ulBinId)
{
#ifndef NO_ACP_LDBIN
	unsigned long ulBankId,ulEventPos;
	tdstSndBankHead* pBank;
	SND_tdstBlockEvent *pEvent;
	p_tdstSndBankDescriptorElement pBankDescr;
	SCR_tdst_Cxt_Values* pScriptContext;
	char *szSectionName;

	int i;

    if (_ulBinId==0xFFFFFFFF)
        return NULL;

	ulBankId=_ulBinId>>16;
	ulEventPos=_ulBinId&0xFFFF;

	if (ulBankId==SND_EXTRA_BANKID)
	{
		szSectionName=(char*)SND_fn_szGetEventSectionNameFromBinId(_ulBinId);
		if (szSectionName==NULL) 
		{
			SND_fn_vDisplayError(E_uwSndEventNotInABank,NULL);
			return NULL;
		}
		pScriptContext=SCR_fnp_st_RdL0_AnalyseSection(szSectionName,SCR_CDF_uw_Anl_Normal);
		pEvent=(SND_tdstBlockEvent *)SCR_M_ul_RdL0_ExtractLongValue(pScriptContext,0);
		return pEvent;
	}
	
	
	// Get Right bank:
	pBankDescr=fn_pstGetBankDescriptor(ulBankId);

	if (pBankDescr!=NULL)
	{//found !!
		pBank=pBankDescr->pData;
		snd_assert(pBank!=NULL);
		SNDLST2_M_StaticGetElementNumber(&pBank->stEventList,pEvent,(long)ulEventPos,i);
		return(pEvent);
	}
	else
		return NULL;
#else
    return NULL;
#endif
}





//-----------------------------------------------------------------------------
//  Name : SND_fn_ulGetEventBinIdFromPointer
//-----------------------------------------------------------------------------
//  Description :   Returns an event binary id given a pointer.
//					Time consuming function: we must scan the banks in search 
//					of the pointer...
//-----------------------------------------------------------------------------
//  Input :  SND_tdstBlockEvent * ( event pointer)        
//  Output : unsigned long  ( Bin Id )
//-----------------------------------------------------------------------------
//  Creation date : 04/02/98            Author : Frédéric Décréau
//-----------------------------------------------------------------------------
//  Modification date :                 Modification Author :
//  Modifications :
//------------------------------------------------------------------------------



unsigned long SND_fn_ulGetEventBinIdFromPointer(SND_tdstBlockEvent *_pEvent)
{
	const char *szSectionName;
	SCR_tdst_Cxt_Values* pScriptContext;
	SND_tdstBlockEvent *pSectionEvent=NULL;
	SCR_tdst_Link_Value   *p_stValue=NULL;

	p_tdstSndBankDescriptorElement pBankDescr;
	int i,iEventCount,iEventNumber;
	unsigned long ulEventId;
	unsigned int uiPos;

	// By convention, a NULL pointer corresponds to the INVALID_BINID value:
	if (_pEvent==NULL) return SND_INVALID_BINID;

	// Search for the pointer value in each of the loaded binary banks:
	SNDLST2_M_DynamicForEachElementOf(&SND_g_dBankDescriptionList.pList,pBankDescr,i)
	{
		// Is event in this bank
		SNDLST2_M_StaticWhatElementNumber(_pEvent,&pBankDescr->pData->stEventList,iEventNumber);
		iEventCount=SNDLST2_M_StaticGetNumberOfElements(&pBankDescr->pData->stEventList);
		if ((iEventNumber<0)||(iEventNumber>=iEventCount)) continue;
		// Event found, get bank id:
		ulEventId=pBankDescr->iId;
		ulEventId<<=16; // Store Bank Id in upper part of the unsigned long
		ulEventId+=iEventNumber; // Store Res Pos in lower part of the unsigned long
		return ulEventId;
	}

	// Pointer not found in loaded binary banks. Scan the extra bank:
	
	// Initialize the extra bank link table scan:
	uiPos = 0;
	SCR_M_DyAr_GetNextElement(SCR_tdst_Link_Value,uiPos,p_stValue, 
					SCR_M_st_Link_GetDynamicArray(&SND_gs_ExtraBankEventsTable));

	// scan the extra bank link table:
	while(p_stValue) // p_sValue is NULL at the end of the array 
	{
		// Test current retrieved value:
		if(SCR_M_e_Link_GetState(p_stValue) != SCR_ELS_Link_Initialized) continue;

		// Get section complete name :
		szSectionName=SCR_M_p_sz_Link_GetKey(p_stValue);
		
		// analyse the section to retrieve the corresponding pointer:
		pScriptContext=SCR_fnp_st_RdL0_AnalyseSection((char *)szSectionName,SCR_CDF_uw_Anl_Normal);
		pSectionEvent=(SND_tdstBlockEvent *)SCR_M_ul_RdL0_ExtractLongValue(pScriptContext,0);
		// compare this pointer with the one we search, exit the loop if found:
		if (pSectionEvent==_pEvent) break;
		
		// Get next element :
		uiPos++;
		SCR_M_DyAr_GetNextElement(SCR_tdst_Link_Value,uiPos,p_stValue, 
					SCR_M_st_Link_GetDynamicArray(&SND_gs_ExtraBankEventsTable));
	}
	// We exited the loop: have we found the pointer ?
	if (pSectionEvent==_pEvent) //yes: the bin id is in the extra bank link table:
		return SCR_M_ul_Link_GetValue(p_stValue);

	// We haven't found that pointer:
	return SND_INVALID_BINID;
}


//-----------------------------------------------------------------------------
//  Name : SND_fn_szGetSoundEventSectionNameFromPointer
//-----------------------------------------------------------------------------
//  Description :   Returns an event section name given its pointer.
//					Converts Pointer -> bin Id -> Section Name
//-----------------------------------------------------------------------------
//  Input :  SND_tdstBlockEvent * ( event pointer)        
//  Output : onst char*  ( section name)   
//-----------------------------------------------------------------------------
//  Creation date : 04/02/98            Author : Frédéric Décréau
//-----------------------------------------------------------------------------
//  Modification date :                 Modification Author :
//  Modifications :
//------------------------------------------------------------------------------

const char* SND_fn_pszGetSoundEventSectionNameFromPointer(SND_tdstBlockEvent *_pEvent)
{
	unsigned long ulBinId;
	
	ulBinId=SND_fn_ulGetEventBinIdFromPointer(_pEvent);
	return SND_fn_szGetEventSectionNameFromBinId(ulBinId);
}



//***************************************************************************
//***************************************************************************
//*																			*
//*																			*
//*						SCRIPT CALLBACKS : EVENT GROUPS						*
//*																			*
//*																			*
//***************************************************************************
//***************************************************************************

SCR_tdst_Link_Table SND_gs_EventGroupTable;


SCR_tdst_Link_Table* SND_pstGetEventGroupLinkTable(void)
{
	return &SND_gs_EventGroupTable;
}


//-----------------------------------------------------------------------------
//  Name : SND_fn_ulGetEventGroupId
//-----------------------------------------------------------------------------
//  Description :   Returns an event group id given its filename (with extension).
//					Scans the link table...
//-----------------------------------------------------------------------------
//  Input :  usual script callback input params        
//  Output : SCR_tde_Anl_ReturnValue    
//-----------------------------------------------------------------------------
//  Creation date : 04/02/98            Author : Frédéric Décréau
//-----------------------------------------------------------------------------
//  Modification date :                 Modification Author :
//  Modifications :
//------------------------------------------------------------------------------


unsigned long SND_fn_ulGetEventGroupId(const char *_szFileName)
{
	int iLen;
	char szTemp[50];
	SCR_tdst_Link_Value   *pstValue;

	// Trim extension:
	iLen=strlen(_szFileName)-4;
	memcpy(szTemp,_szFileName,iLen);
	*(szTemp+iLen)=0;

	// Get Corresponding key:
	pstValue=SCR_fnp_st_Link_SearchKey(&SND_gs_EventGroupTable,szTemp);
	if  (pstValue==NULL) return 0;
	else return SCR_M_ul_Link_GetValue(pstValue);
}

//-----------------------------------------------------------------------------
//  Name : SND_fn_szGetEventGroupNameFromId
//-----------------------------------------------------------------------------
//  Description :   Returns an event group name (without extension) from its id:
//					Scans the link table...
//-----------------------------------------------------------------------------
//  Input :  unsigned long       
//  Output : const char *    
//-----------------------------------------------------------------------------
//  Creation date : 04/02/98            Author : Frédéric Décréau
//-----------------------------------------------------------------------------
//  Modification date :                 Modification Author :
//  Modifications :
//------------------------------------------------------------------------------


const char *SND_fn_szGetEventGroupNameFromId(unsigned long _ulId)
{
	SCR_tdst_Link_Value   *pstValue;

	// Get Corresponding key:
	pstValue=SCR_fnp_st_Link_SearchValue(&SND_gs_EventGroupTable,_ulId);
	if  (pstValue==NULL) return NULL;
	else return SCR_M_p_sz_Link_GetKey(pstValue);
}



//-----------------------------------------------------------------------------
//  Name : SND_fn_eLoadEventGroupList
//-----------------------------------------------------------------------------
//  Description :   Script Callback for  SndEventGroupList section 
//					Fills the event group "name <-> id"  conversion table
//					from script.
//-----------------------------------------------------------------------------
//  Input :  usual script callback input params        
//  Output : SCR_tde_Anl_ReturnValue    
//-----------------------------------------------------------------------------
//  Creation date : 04/02/98            Author : Frédéric Décréau
//-----------------------------------------------------------------------------
//  Modification date :                 Modification Author :
//  Modifications :
//------------------------------------------------------------------------------

SCR_tde_Anl_ReturnValue SND_fn_eLoadEventGroupList(SCR_tdst_File_Description *p_fFile, char *szAction, char *szParams[], SCR_tde_Anl_Action cType)
{
	unsigned long ulEventGroupId;

    switch (cType)
    {
		case SCR_EA_Anl_Entry:
			if (strcmp(szAction,"LoadEventGroup")==0)
			{
				if (SCR_fn_uc_RdL0_GetNumberOfParameters(szParams)==1) ulEventGroupId=0;
				else ulEventGroupId=atoi(szParams[1]);
				SCR_fnp_st_Link_SetValue(&SND_gs_EventGroupTable,
											szParams[0],ulEventGroupId);
			}
			break;
	}
	return SCR_ERV_Anl_NormalReturn;
}






//***************************************************************************
//***************************************************************************
//*																			*
//*																			*
//*						SCRIPT CALLBACKS : EVENTS			 				*
//*																			*
//*																			*
//***************************************************************************
//***************************************************************************




//-----------------------------------------------------------------------------
//  Name : SND_fn_eLoadEventM
//-----------------------------------------------------------------------------
//  Description :   Script Callback for  SndEventM section. 
//					If the event is in a sound bank,return its pointer,
//					or NULL if the bank is not loaded,
//					If the event is in no sound bank, load it from script,
//					and load referenced events and resources.					
//-----------------------------------------------------------------------------
//  Input :  usual script callback input params        
//  Output : SCR_tde_Anl_ReturnValue    
//-----------------------------------------------------------------------------
//  Creation date : 04/02/98            Author : Frédéric Décréau
//-----------------------------------------------------------------------------
//  Modification date :                 Modification Author :
//  Modifications :
//------------------------------------------------------------------------------


SCR_tde_Anl_ReturnValue SND_fn_eLoadEventM(SCR_tdst_File_Description *p_fFile, char *szAction, char *szParams[], SCR_tde_Anl_Action cType)
{
	static unsigned long ulNextEventBinId=0;
    SND_tdstBlockEvent *pEvent;
	char *pszFileName;
	char *szSectionName;
    unsigned long ulBinId,ulTemp;
	SCR_tdst_Link_Value   *p_stValue;
	char szTemp[MAX_PATH];
	switch (cType)
    {

    case SCR_EA_Anl_AlreadyAnalysed:
		// Reanalysis of the event section: we recheck the pointer value,
		//	in case it changed:
		szSectionName=SCR_M_RdL0_GetCompleteSectionNameR(0);
		ulBinId=SND_fn_ulGetEventBinIdFromSectionName(szSectionName);
		if (HIWORD(ulBinId)==SND_EXTRA_BANKID) break;
		pEvent=SND_fn_pGetBinEvent(ulBinId);
		SCR_M_RdL0_SetSectionLong(0,0,(unsigned long)pEvent);
		break;

	case SCR_EA_Anl_BeginSection:
		// Get current event section name:
		szSectionName=SCR_M_RdL0_GetCompleteSectionNameR(0);
		sprintf(szTemp,"\tLoadEvent(%s)",szSectionName);
		SND_fn_bLogSoundLine(szTemp);
		// Try and find this section in the binary bank list:
		p_stValue=SCR_fnp_st_Link_SearchKey(&SND_gs_BinEventConversionTable,(char*)szSectionName);
		if (p_stValue!=NULL)
		{	// Sound event is in a binary bank, try to get a pointer:
			ulBinId=SCR_M_ul_Link_GetValue(p_stValue);
			pEvent=SND_fn_pGetBinEvent(ulBinId);
			if ((pEvent==NULL)&& g_bForceLoadBanks)
			{	// Bank is not loaded: try to load it
				SND_fn_bLoadBinaryBank(HIWORD(ulBinId));
				pEvent=SND_fn_pGetBinEvent(ulBinId);
			}
			// Set pointer in section context (even if it's null, meaning bank isn't loaded):
			SCR_M_RdL0_SetSectionLong(0,0,(unsigned long)pEvent);
			// Stop this event's section analysis:
			return  SCR_ERV_Anl_TerminateCurrentSection;
		}
		else 
		{ // sound event is in no binary bank: load it in extra bank:
			// Create event:
			pEvent=(SND_tdstBlockEvent*)SND_fn_pvMallocSnd(sizeof(SND_tdstBlockEvent));
			memset(pEvent,0,sizeof(SND_tdstBlockEvent));

			SCR_M_RdL0_SetSectionLong(0,0,(unsigned long)pEvent);
			// Register this new event in the extra bank's link table:  
			ulTemp=(SND_EXTRA_BANKID<<16)+ulNextEventBinId;
			ulNextEventBinId++;
			SCR_fnp_st_Link_SetValue(&SND_gs_ExtraBankEventsTable,
									szSectionName,ulTemp);
			// Get event type:
			pEvent->eType=SND_fn_eGetEventTypeFromString(szParams[0]);

			// Get event editor Id (file id&section name):
			pszFileName=SCR_M_RdL0_GetFileNameR(0);
			ulTemp=SND_fn_ulGetEventGroupId(pszFileName);
			ulTemp<<=16;
			ulTemp+=atoi(szAction);
			pEvent->Id=ulTemp;
			SCR_M_RdL0_SetSectionLong(0,1,ulTemp);

		}
		break;
	case SCR_EA_Anl_Entry:
		SCR_M_RdL0_GetSectionLong(0,0,SND_tdstBlockEvent *,pEvent);

		if (strcmp(szAction,"SetStopable")==0)  
			pEvent->bStopable=(SndBool)(*szParams[1]);

		else if (strcmp(szAction,"SetDynamic")==0)  
			pEvent->bDynamic=(SndBool)(*szParams[1]);

		else
		{
			switch (pEvent->eType)
			{
			case EVT_SON_PLAY: 
				 SND_fn_vAnalyseEventParamsPlay(szAction,szParams,pEvent);
				break;
			case EVT_SON_STOP:
				 SND_fn_vAnalyseEventParamsStop(szAction,szParams,pEvent);
				break;
			case EVT_SON_STOP_N_GO:
				 SND_fn_vAnalyseEventParamsStopNGo(szAction,szParams,pEvent);
					break;
			case EVT_SON_STOP_N_GO_CROSSFADE:
				 SND_fn_vAnalyseEventParamsStopNGoFade(szAction,szParams,pEvent);
					break;
			case EVT_SON_PITCH:
			case EVT_SON_VOLUME:
			case EVT_SON_PAN:
				 SND_fn_vAnalyseEventParamsXtraParam(szAction,szParams,pEvent);
					break;
			case EVT_SON_EXTRA:
				 SND_fn_vAnalyseEventParamsExtraAll(szAction,szParams,pEvent);
					break;
			case EVT_SON_CHANGE_VOLUME:
				 SND_fn_vAnalyseEventChangeVolume(szAction,szParams,pEvent);
					break;
			case EVT_SON_EFFET:
				 SND_fn_vAnalyseEventParamsEffect(szAction,szParams,pEvent);
					break;
			}
		}
    }        // end switch (cType)
    return  SCR_ERV_Anl_NormalReturn;
}





//***************************************************************************
//***************************************************************************
//*																			*
//*																			*
//*						SCRIPT INIT FUNCTIONS				 				*
//*																			*
//*																			*
//***************************************************************************
//***************************************************************************




void SND_fn_vDesInitScriptSnd(void)
{

	
}

char g_czProjectTitle[MAX_PATH]="";

SndBool g_bForceLoadBanks;

//-----------------------------------------------------------------------------
//  Name : SND_fn_bInitDataLoadSnd
//-----------------------------------------------------------------------------
//  Description :   Hybrid initialization based on the lcb file: 
//					Creates and fills the event group link table,
//					creates and fills the section <-> Bin Id convertion table.
//					Requires that the script path for the lcb file be registered. 
//-----------------------------------------------------------------------------
//  Input :  const char * : project file title ( without extension).        
//  Output : SCR_tde_Anl_ReturnValue    
//-----------------------------------------------------------------------------
//  Creation date : 04/02/98            Author : Frédéric Décréau
//-----------------------------------------------------------------------------
//  Modification date :                 Modification Author :
//  Modifications :
//-----------------------------------------------------------------------------


SndBool SND_fn_bInitDataLoadSnd(const char *_szFileTitle)
{

	char szSectionName[256];

	char szText[20];


	//init list of bin-ed resources
	SNDLST2_M_StaticInitAnchor(&SND_g_lListEdSectionResource);


	// get the load options:

	g_bForceLoadBanks=FALSE; // default value

	if (SND_fn_bGetOptions("Options","FORCE_LOAD_BANKS",szText,sizeof(szText)))
	{
		if (!strcmpi(szText,"ON")) g_bForceLoadBanks=TRUE;
	}

	// Save the project title:
	strcpy(g_czProjectTitle,_szFileTitle);

#ifdef SUPERVISATER
	//Send theProjectTitle to the SuperVisater
	fn_vSetProjectTitleForSuperVisater(_szFileTitle);
#endif
 

	
	// Initialize script callbacks:
    SCR_fn_v_RdL0_RegisterCallback("SndResourceDisk",SND_fn_eLoadResDisk,SCR_CRC_c_RdL0_ForSection);
    SCR_fn_v_RdL0_RegisterCallback("SndEventM",SND_fn_eLoadEventM,SCR_CRC_c_RdL0_ForSection);
    SCR_fn_v_RdL0_RegisterCallback("SndResourceM",SND_fn_eLoadResM,SCR_CRC_c_RdL0_ForSection);
    SCR_fn_v_RdL0_RegisterCallback("SndTrackListM",SND_fn_eLoadPolyTrackM,SCR_CRC_c_RdL0_ForSection);
    SCR_fn_v_RdL0_RegisterCallback("SndTrackM",SND_fn_eLoadTrackM,SCR_CRC_c_RdL0_ForSection);
    SCR_fn_v_RdL0_RegisterCallback("SndSplitRangeListM",SND_fn_eLoadPolyRangeM,SCR_CRC_c_RdL0_ForSection);
    SCR_fn_v_RdL0_RegisterCallback("SndSplitRangeM",SND_fn_eLoadRangeM,SCR_CRC_c_RdL0_ForSection);
    SCR_fn_v_RdL0_RegisterCallback("SndSwitchListM",SND_fn_eLoadSwitchListM,SCR_CRC_c_RdL0_ForSection);
    SCR_fn_v_RdL0_RegisterCallback("SndSwitchElementM",SND_fn_eLoadSwitchElementM,SCR_CRC_c_RdL0_ForSection);
    SCR_fn_v_RdL0_RegisterCallback("SndRandomListM",SND_fn_eLoadRandomListM,SCR_CRC_c_RdL0_ForSection);
    SCR_fn_v_RdL0_RegisterCallback("SndRandomElementM",SND_fn_eLoadRandomElementM,SCR_CRC_c_RdL0_ForSection);
    
    SCR_fn_v_RdL0_RegisterCallback("SndThemePartListM",SND_fn_eLoadThemePartListM,SCR_CRC_c_RdL0_ForSection);
    SCR_fn_v_RdL0_RegisterCallback("SND_fn_eLoadThemePartOutroM",SND_fn_eLoadThemePartOutroM,SCR_CRC_c_RdL0_ForSection);
    SCR_fn_v_RdL0_RegisterCallback("SndThemePartM",SND_fn_eLoadThemePartM,SCR_CRC_c_RdL0_ForSection);

    SCR_fn_v_RdL0_RegisterCallback("SndEventGroupList",SND_fn_eLoadEventGroupList,SCR_CRC_c_RdL0_ForSection);
     SCR_fn_v_RdL0_RegisterCallback("SndEventIdList",SND_fn_eLoadEventIdList,SCR_CRC_c_RdL0_ForSection);
     SCR_fn_v_RdL0_RegisterCallback("SndResIdList",SND_fn_eLoadResIdList,SCR_CRC_c_RdL0_ForSection);
	SCR_fn_v_RdL0_RegisterCallback("SndMapE",SND_fn_eLoadBinMap,SCR_CRC_c_RdL0_ForSection);


	// Initialize the link tables:
	
	// For extra bank management:
	SCR_fn_v_Link_InitTable(&SND_gs_ExtraBankEventsTable);
	SCR_fn_v_Link_InitTable(&SND_gs_ExtraBankResTable);
	// For event group list:
	SCR_fn_v_Link_InitTable(&SND_gs_EventGroupTable);
	//For section to bin id conversion:
	SCR_fn_v_Link_InitTable(&SND_gs_BinEventConversionTable);
	SCR_fn_v_Link_InitTable(&SND_gs_BinResConversionTable);



	// Create the event group list section name and analyze it:
	sprintf(szSectionName,"%s.lcb^SndEventGroupList",_szFileTitle);
	SCR_fnp_st_RdL0_AnalyseSection(szSectionName, SCR_CDF_uw_Anl_Normal);

	// Create the binary convertion list section name and analyze it:
	sprintf(szSectionName,"%s.lcb^SndEventIdList",_szFileTitle);
	if (SCR_fn_c_RdL0_IsSectionExists(szSectionName))
		SCR_fnp_st_RdL0_AnalyseSection(szSectionName, SCR_CDF_uw_Anl_Normal);

	return TRUE;
}



//-----------------------------------------------------------------------------
//  Name : SND_fn_bDesInitDataLoadSnd
//-----------------------------------------------------------------------------
//  Description :   Counterpart to InitDataLoadSnd: 
//					Frees and deletes the extra bank,closes the link tables,...
//-----------------------------------------------------------------------------
//  Input :  const char * : project file title ( without extension).        
//  Output : SCR_tde_Anl_ReturnValue    
//-----------------------------------------------------------------------------
//  Creation date : 04/02/98            Author : Frédéric Décréau
//-----------------------------------------------------------------------------
//  Modification date :                 Modification Author :
//  Modifications :
//-----------------------------------------------------------------------------


SndBool SND_fn_bDesInitDataLoadSnd(void)
{
	tdstBlockResourceMem *pResMem;
	unsigned int uiPos;
	const char *szSectionName;
	SCR_tdst_Cxt_Values* pScriptContext;
    SND_tdstBlockEvent *pSectionEvent;
	SCR_tdst_Link_Value   *p_stValue=NULL;

	//***********************************************
	//*			Free the  events in the extra bank	*
	//***********************************************

	// Initialize the extra bank link table scan:
	uiPos = 0;
	SCR_M_DyAr_GetNextElement(SCR_tdst_Link_Value,uiPos,p_stValue, 
					SCR_M_st_Link_GetDynamicArray(&SND_gs_ExtraBankEventsTable));

	// scan the extra bank link table:
	while(p_stValue) // p_sValue is NULL at the end of the array 
	{
		// Test current retrieved value:
		if(SCR_M_e_Link_GetState(p_stValue) != SCR_ELS_Link_Initialized) continue;

		// Get section complete name :
		szSectionName=SCR_M_p_sz_Link_GetKey(p_stValue);
		
		// analyse the section to retrieve the corresponding pointer:
		pScriptContext=SCR_fnp_st_RdL0_AnalyseSection((char*)szSectionName,SCR_CDF_uw_Anl_Normal);
		pSectionEvent=(SND_tdstBlockEvent *)SCR_M_ul_RdL0_ExtractLongValue(pScriptContext,0);		
		// Free the event structure:
		SND_fn_vFreeSnd(pSectionEvent);

		// Get next element :
		uiPos++;
		SCR_M_DyAr_GetNextElement(SCR_tdst_Link_Value,uiPos,p_stValue, 
					SCR_M_st_Link_GetDynamicArray(&SND_gs_ExtraBankEventsTable));
	}

	//***********************************************
	//*			Free the resources in the extra bank*
	//***********************************************

	// Initialize the extra bank link table scan:
	uiPos = 0;
	SCR_M_DyAr_GetNextElement(SCR_tdst_Link_Value,uiPos,p_stValue, 
					SCR_M_st_Link_GetDynamicArray(&SND_gs_ExtraBankResTable));

	// scan the extra bank link table:
	while(p_stValue) // p_sValue is NULL at the end of the array 
	{
		// Test current retrieved value:
		if(SCR_M_e_Link_GetState(p_stValue) != SCR_ELS_Link_Initialized) continue;

		// Get section complete name :
		szSectionName=SCR_M_p_sz_Link_GetKey(p_stValue);
		
		// analyse the section to retrieve the corresponding pointer:
		pScriptContext=SCR_fnp_st_RdL0_AnalyseSection((char *)szSectionName,SCR_CDF_uw_Anl_Normal);
		pResMem=(tdstBlockResourceMem *)SCR_M_ul_RdL0_ExtractLongValue(pScriptContext,0);		

		// Free extra, resource specific referenced data:
		switch (pResMem->eType)
		{
		case TYPE_SAMPLE:
		case TYPE_MIDI:
		case TYPE_CDAUDIO:
			break;
		case TYPE_SEQUENCE:
			SND_fn_vFreeSnd(SNDLST2_M_StaticGetFirstElement(&(pResMem->uRes.stSequence.stResList)));
			break;
		case TYPE_SPLIT:
			SND_fn_vFreeSnd(SNDLST2_M_StaticGetFirstElement(&(pResMem->uRes.stSplit.stRangeList)));
			break;
		case TYPE_SWITCH:
			SND_fn_vFreeSnd(SNDLST2_M_StaticGetFirstElement(&(pResMem->uRes.stSwitch)));
			break;
		case TYPE_THEME:
			SND_fn_vFreeSnd(SNDLST2_M_StaticGetFirstElement(&(pResMem->uRes.stTheme)));
			break;
		case TYPE_RANDOM:
			SND_fn_vFreeSnd(SNDLST2_M_StaticGetFirstElement(&(pResMem->uRes.stRandom)));
			break;
		case TYPE_INVALID:// Problem!
		default:
			SND_fn_vDisplayError(E_uwSndInvalidTypeOfResource,"");
		}

		//stop any instance of resource before unload it
		SND_fn_vStopBeforeUnLoadResSnd(pResMem);

		// Unload the resource:
		SND_fn_vUnLoadResSnd(pResMem);

		// Free the res structure:
		SND_fn_vFreeSnd(pResMem);

		// Get next element :
		uiPos++;
		SCR_M_DyAr_GetNextElement(SCR_tdst_Link_Value,uiPos,p_stValue, 
					SCR_M_st_Link_GetDynamicArray(&SND_gs_ExtraBankResTable));
	}


	// Desinit the link tables:
	
	// For extra bank management:
	SCR_fn_v_Link_CloseTable(&SND_gs_ExtraBankEventsTable);
	SCR_fn_v_Link_CloseTable(&SND_gs_ExtraBankResTable);
	// For event group list:
	SCR_fn_v_Link_CloseTable(&SND_gs_EventGroupTable);
	//For section to bin id conversion:
	SCR_fn_v_Link_CloseTable(&SND_gs_BinEventConversionTable);
	SCR_fn_v_Link_CloseTable(&SND_gs_BinResConversionTable);

	
	
	// delete the sound  script sections:
    SCR_fn_v_RdL0_DeleteOpenSection("SndResourceDisk",SCR_CDR_c_RdL0_Contains);
    SCR_fn_v_RdL0_DeleteOpenSection("SndEventM",SCR_CDR_c_RdL0_Contains);
    SCR_fn_v_RdL0_DeleteOpenSection("SndResourceM",SCR_CDR_c_RdL0_Contains);
    SCR_fn_v_RdL0_DeleteOpenSection("SndTrackListM",SCR_CDR_c_RdL0_Contains);
    SCR_fn_v_RdL0_DeleteOpenSection("SndTrackM",SCR_CDR_c_RdL0_Contains);
    SCR_fn_v_RdL0_DeleteOpenSection("SndSplitRangeListM",SCR_CDR_c_RdL0_Contains);
    SCR_fn_v_RdL0_DeleteOpenSection("SndSplitRangeM",SCR_CDR_c_RdL0_Contains);
    SCR_fn_v_RdL0_DeleteOpenSection("SndSwitchListM",SCR_CDR_c_RdL0_Contains);
    SCR_fn_v_RdL0_DeleteOpenSection("SndSwitchElementM",SCR_CDR_c_RdL0_Contains);
    SCR_fn_v_RdL0_DeleteOpenSection("SndRandomListM",SCR_CDR_c_RdL0_Contains);
    SCR_fn_v_RdL0_DeleteOpenSection("SndRandomElementM",SCR_CDR_c_RdL0_Contains);
    
    SCR_fn_v_RdL0_DeleteOpenSection("SndThemePartListM",SCR_CDR_c_RdL0_Contains);
    SCR_fn_v_RdL0_DeleteOpenSection("SndThemePartM",SCR_CDR_c_RdL0_Contains);

    SCR_fn_v_RdL0_DeleteOpenSection("SndEventGroupList",SCR_CDR_c_RdL0_Contains);
     SCR_fn_v_RdL0_DeleteOpenSection("SndEventIdList",SCR_CDR_c_RdL0_Contains);
     SCR_fn_v_RdL0_DeleteOpenSection("SndResIdList",SCR_CDR_c_RdL0_Contains);
	SCR_fn_v_RdL0_DeleteOpenSection("SndMapE",SCR_CDR_c_RdL0_Contains);

	
	 	// Initialize script callbacks:
    SCR_fn_v_RdL0_DeleteRegisterCallback("SndResourceDisk",SCR_CRC_c_RdL0_ForSection,SCR_CDR_c_RdL0_Match);
    SCR_fn_v_RdL0_DeleteRegisterCallback("SndEventM",SCR_CRC_c_RdL0_ForSection,SCR_CDR_c_RdL0_Match);
    SCR_fn_v_RdL0_DeleteRegisterCallback("SndResourceM",SCR_CRC_c_RdL0_ForSection,SCR_CDR_c_RdL0_Match);
    SCR_fn_v_RdL0_DeleteRegisterCallback("SndTrackListM",SCR_CRC_c_RdL0_ForSection,SCR_CDR_c_RdL0_Match);
    SCR_fn_v_RdL0_DeleteRegisterCallback("SndTrackM",SCR_CRC_c_RdL0_ForSection,SCR_CDR_c_RdL0_Match);
    SCR_fn_v_RdL0_DeleteRegisterCallback("SndSplitRangeListM",SCR_CRC_c_RdL0_ForSection,SCR_CDR_c_RdL0_Match);
    SCR_fn_v_RdL0_DeleteRegisterCallback("SndSplitRangeM",SCR_CRC_c_RdL0_ForSection,SCR_CDR_c_RdL0_Match);
    SCR_fn_v_RdL0_DeleteRegisterCallback("SndSwitchListM",SCR_CRC_c_RdL0_ForSection,SCR_CDR_c_RdL0_Match);
    SCR_fn_v_RdL0_DeleteRegisterCallback("SndSwitchElementM",SCR_CRC_c_RdL0_ForSection,SCR_CDR_c_RdL0_Match);
    SCR_fn_v_RdL0_DeleteRegisterCallback("SndRandomListM",SCR_CRC_c_RdL0_ForSection,SCR_CDR_c_RdL0_Match);
    SCR_fn_v_RdL0_DeleteRegisterCallback("SndRandomElementM",SCR_CRC_c_RdL0_ForSection,SCR_CDR_c_RdL0_Match);
    
    SCR_fn_v_RdL0_DeleteRegisterCallback("SndThemePartListM",SCR_CRC_c_RdL0_ForSection,SCR_CDR_c_RdL0_Match);
    SCR_fn_v_RdL0_DeleteRegisterCallback("SndThemePartM",SCR_CRC_c_RdL0_ForSection,SCR_CDR_c_RdL0_Match);

    SCR_fn_v_RdL0_DeleteRegisterCallback("SndEventGroupList",SCR_CRC_c_RdL0_ForSection,SCR_CDR_c_RdL0_Match);
     SCR_fn_v_RdL0_DeleteRegisterCallback("SndEventIdList",SCR_CRC_c_RdL0_ForSection,SCR_CDR_c_RdL0_Match);
     SCR_fn_v_RdL0_DeleteRegisterCallback("SndResIdList",SCR_CRC_c_RdL0_ForSection,SCR_CDR_c_RdL0_Match);
	SCR_fn_v_RdL0_DeleteRegisterCallback("SndMapE",SCR_CRC_c_RdL0_ForSection,SCR_CDR_c_RdL0_Match);

	
	
	
	return TRUE;
}



//-----------------------------------------------------------------------------
//  Name : SND_fn_czGetProjectTitle(void)
//-----------------------------------------------------------------------------
//  Description :   Returns the title of a project, as passed to InitDataLoadSnd: 
//-----------------------------------------------------------------------------
//  Input :  void.        
//  Output : const char *    
//-----------------------------------------------------------------------------
//  Creation date : 04/02/98            Author : Frédéric Décréau
//-----------------------------------------------------------------------------
//  Modification date :                 Modification Author :
//  Modifications :
//-----------------------------------------------------------------------------
const char * SND_fn_czGetProjectTitle(void)
{
	return g_czProjectTitle;
}


// Get ptr onto resource from ist editor-id
// return NULL when resource cannot be solved
tdstBlockResourceMem* SND_fn_pstGetResFromEdIdSnd(unsigned long ulEdId)
{
	char section[128];
	SCR_tdst_Cxt_Values* pScriptContext;
	p_tdstSndBankDescriptorElement	pBankDescr;
	tduBlockResource*			pRes;
	//tdstBlockResourceMem*			pRes;
	int i,j;

	
	//first check in BNMs...
	SNDLST2_M_DynamicForEachElementOf(&SND_g_dBankDescriptionList.pList,pBankDescr,i)
	{
		SNDLST2_M_StaticForEachElementOf(&(pBankDescr->pData->stResList),pRes,j)
		{
			if (pRes->stMem.Id==ulEdId)
				return &(pRes->stMem);
		}
	}

	//resource is in no BNM...
	//do check in SND_g_lListEdSectionResource
	fn_vGetSectionNameFromEdIdResource(ulEdId,section,sizeof(section));

	pScriptContext=SCR_fnp_st_RdL0_AnalyseSection(section,SCR_CDF_uw_Anl_Normal);
	return (tdstBlockResourceMem*)SCR_M_ul_RdL0_ExtractLongValue(pScriptContext, 0);
}


#endif 
//NO_ACP_SOUND*
