⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 maphrcnv.c

📁 是一个手机功能的模拟程序
💻 C
📖 第 1 页 / 共 5 页
字号:
/*==============================================================================
//	Copyright(c) 2001-2003 YAMAHA CORPORATION
//
//	Title		: MAPHRCNV.C
//
//	Description	: MA-3 SMAF/Phrase Stream Converter Module.
//
//	Version		: 1.6.1.0		2003.04.04
//
//============================================================================*/
#include "maphrcnv.h"
#include "malib.h"

/*== common ============================================================*/
/*=============================================================================
//	gloval values
=============================================================================*/
static UINT8		gbCreateStatus = 0; /* bit0:SMAF/Phrase bit1:SMAF/Audio  */
static UINT8		gbSeqStatus = 0;    /* bit0:SMAF/Phrase bit1:SMAF/Audio  */
								   /* 1:sequence playing 0:sequence stop */
#define MASK_STATUS_PHRASE	(0x01)
#define MASK_STATUS_AUDIO	(0x02)


/*== SMAF/Phrase ============================================================*/
/*=============================================================================
//	gloval values
=============================================================================*/
static UINT8		gbPlayStatus;  /* bit0:ch0 - bit3:ch3 ... 1:Playing 0:not playing */
static UINT8		gbPauseStatus; /* bit0:ch0 - bit3:ch3 ... 1:Pause   0:not pause   */
static const UINT8	gMaskPlay[MAX_PHRASE_CHANNEL] = {0x1, 0x2, 0x4, 0x8};
static UINT32		gdwCtrlStatus; /* bit0..3:ch0 - bit12..15:ch3 */
								   /* bit4n:set init param        */
								   /* bit4n+1:change volume       */
								   /* bit4n+2:change panpot       */

static UINT32		gdwPhrStatus;  /* bit0..3:ch0 - bit12..15:ch3 */
								   /* bit4n:start flag            */
								   /* bit4n+1:stop flag           */
								   /* bit4n+2:pause flag          */
								   /* bit4n+3:restart flag        */
#define MASK_PHRASE_STATUS_START	(0x00001111)
#define MASK_PHRASE_STATUS_STOP		(0x00002222)
#define MASK_PHRASE_STATUS_PAUSE	(0x00004444)
#define MASK_PHRASE_STATUS_RESTART	(0x00008888)
static const UINT32 gMaskStart[MAX_PHRASE_CHANNEL] =	{0x1, 0x10, 0x100, 0x1000};
#if MA_STOPWAIT
static const UINT32 gMaskStop[MAX_PHRASE_CHANNEL] =		{0x2, 0x20, 0x200, 0x2000};
#endif
static const UINT32 gMaskPause[MAX_PHRASE_CHANNEL] =	{0x4, 0x40, 0x400, 0x4000};
static const UINT32 gMaskRestart[MAX_PHRASE_CHANNEL] =	{0x8, 0x80, 0x800, 0x8000};

static CHINFO		gChInfo[MASMW_NUM_CHANNEL];


/* Phrase Wrapper */
typedef	SINT32	(*PCALLBACK)(UINT8 id);
static SINT32	CallBack_Service1(UINT8 id);
static SINT32	CallBack_Service2(UINT8 id);
static SINT32	CallBack_Service3(UINT8 id);
static SINT32	CallBack_Service4(UINT8 id);

static SINT32		gdwPhrId = -1L;			/*	MA-3 SMAF/Phrase Stream Converter ID */
static SINT32		gdwSlave = 0L;
static APIINFO		gApiInfo[MAX_PHRASE_DATA];
static PCALLBACK	gCallBack[4] = {CallBack_Service1, CallBack_Service2, CallBack_Service3, CallBack_Service4};

static void		(* gEvHandler)(struct event* eve);

static UINT32	gPhrRamAdrs;		/* RAM address for SMAF/Phrase converter */
static UINT32	gPhrRamSize;		/* RAM size for SMAF/Phrase converter */


/*	Short type to Standard type Volume converting table	*/
static const UINT8	gbVolTbl[16] = {0, 0, 31, 39, 47, 55, 63, 71, 79, 87, 95, 103, 111, 119, 127, 127};

/*	Short type to Standard type Modulation converting table	*/
static const UINT8	gbModTbl[16] = {0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4};



/*== SMAF/Audio =============================================================*/
typedef struct _MAAUDCNV_INFO
{
	UINT8		bSMAFType;						/* SMAF Type    */
	UINT8		bCodeType;						/* Code Type	*/
	UINT8		bOptionType;					/* Option Type	*/
	UINT8*		pConti;							/* Top of CNTI  */
	UINT8*		pInfo;							/* Top of OPDA  */
	UINT32		dwSize;							/* Size of OPDA */

	UINT32		dFs;							/* [Hz]         */
	UINT32		dFormat;						/*              */
	UINT8*		pSource;						/* Top of Data  */
	UINT32		dSizeOfSource;					/* Size of Data */
} MAAUDCNV_INFO, *PMAAUDCNV_INFO;


/*=============================================================================
//	gloval values
=============================================================================*/
static UINT8		gbAudCtrlStatus;	/* bit0:change master volume  1:change 0:not change */
#define CTRL_AUDIO_MASTERVOLUME_CHANGE	(0x01)

static UINT8		gbAudStatus;		/* bit0:start flag */
										/* bit1:stop flag  */
										/* bit2:stop and start from top */
#define MASK_AUDIO_STATUS_START			(0x01)
#define MASK_AUDIO_STATUS_STOP			(0x02)
#define MASK_AUDIO_STATUS_REPLAY		(0x04)

static SINT32		gSeqID = -1;							/* Sequence ID  */
static SINT32		gFileID;						/* File ID      */
static UINT8		gNumOfLoaded;					/*              */

static UINT8		gbAudEnding;					/* Flag of stop proccess */

static MAAUDCNV_INFO	gInfo[2];					/* Top of OPDA  */
static UINT8		gFormat;						/*              */
static UINT8		gPanpot;						/* Value of Panpot        */
static UINT8		gMasterVol;						/* Value of Master volume */
static UINT32		gFs;							/* [Hz]         */
static UINT8*		gpSource;						/* Top of data  */
static UINT32		gSizeOfSource;					/* Size of Data */

/*===========================================================================*/

SINT32 MaPhrCnv_Convert( void );
extern void machdep_memcpy( UINT8 *d, UINT8 *s, UINT32 size );
extern void machdep_Sleep( UINT32 time );






/*=============================================================================
//	Function Name	:	UINT32	ConvertMA3Voice(UINT8* pM2V, UINT8 bM2Size, UINT8* pM3V)
//
//	Description		:	Convert MA-2 Voice Parameter to MA-3 format
//
//	Argument		:	pM2V	...	Pointer to MA-2 voice parameter
//						bM2Size	...	Size of MA-2 voice parameter
//						pM3V	...	Pointer to MA-3 voice parameter
//
//	Return			:	converted size
//
=============================================================================*/
static UINT32	ConvertMA3Voice(UINT8* pM2V, UINT8 bM2Size, UINT8* pM3V)
{
	UINT8	bFb;
	UINT8	bAlg;
	UINT8	bNumofOp;
	UINT8	bOp;
	UINT8	bVib;
	UINT8	bEgt;
	UINT8	bSus;
	UINT8	bKsr;
	UINT8	bRR;
	UINT8	bDR;
	UINT8	bDvb;
	UINT8	bDam;
	UINT8	bAm;
	UINT8	bWs;
	UINT32	dwSize = 0L;

	bNumofOp = 4;
	bFb	 = (UINT8)((pM2V[3] & 0x38) >> 3);
	bAlg = (UINT8)(pM2V[3] & 0x07);
	if((bAlg & 0x06) == 0)			bNumofOp = 2;
	if(bM2Size < MA2VOICE_4OP_SIZE)	bNumofOp = 2;

	pM3V[0] = (UINT8)(0x80 | (pM2V[4] & 0x03));	/*	panpot = 0x10 center	*/
	pM3V[1] = (UINT8)((pM2V[3] & 0xC0) | bAlg);

	pM2V += 5;
	pM3V += 2;
	dwSize = 2;
	for(bOp = 0; bOp < bNumofOp; bOp++)
	{
		bVib = (UINT8)((pM2V[0] & 0x08) >> 3);
		bEgt = (UINT8)((pM2V[0] & 0x04) >> 2);
		bSus = (UINT8)((pM2V[0] & 0x02) >> 1);
		bKsr = (UINT8)((pM2V[0] & 0x01) >> 0);

		bRR	 = (UINT8)((pM2V[1] & 0xF0) >> 4);
		bDR	 = (UINT8)(pM2V[1] & 0x0F);

		bDvb = (UINT8)((pM2V[4] & 0xC0) >> 6);
		bDam = (UINT8)((pM2V[4] & 0x30) >> 4);
		bAm	 = (UINT8)((pM2V[4] & 0x08) >> 3);
		bWs	 = (UINT8)((pM2V[4] & 0x07));

		pM3V[0] = (UINT8)((((bEgt) ? 0x00 : bRR) << 4)| 0x04 | bKsr);	/*	SR | DR					*/
		pM3V[1] = (UINT8)(((bSus) ? 0x04 : bRR) << 4| bDR);				/*	RR | DR					*/
		pM3V[2] = pM2V[2];												/*	AR | SL					*/
		pM3V[3] = pM2V[3];												/*	TL | KSL				*/
		pM3V[4] = (UINT8)(((bDam) << 5) | (bAm << 4) | (bDvb << 1) | (bVib));
																		/*	DAM | EAM | DVB | EVB	*/
		pM3V[5] = (UINT8)(pM2V[0] & 0xF0);								/*	MULTI | DT=0			*/
		pM3V[6] = (UINT8)(((bWs) << 3) | ((bOp == 0) ? bFb : 0x00));	/*	WS | FB					*/

		pM2V += 5;
		pM3V += 7;
		dwSize += 7;
	}

	return(dwSize);
}

/*=============================================================================
//	Function Name	:	void	PhrChk_InitPhraseInfo(PPHRINFO pPhrInfo)
//
//	Description		:	Initialize PHRINFO structure
//
//	Argument		:	pPhrInfo		...	Pointer to PHRINFO structure
//
//	Return			:
//
=============================================================================*/
static void	PhrChk_InitPhraseInfo(PPHRINFO pPhrInfo)
{
	UINT8	i;

	pPhrInfo->bPhrNum				= 0;
	pPhrInfo->bCntiClass			= 0;
	pPhrInfo->bCntiType				= 0;
	pPhrInfo->bCodeType				= 0;
	pPhrInfo->bCopyStatus			= 0;
	pPhrInfo->bCopyCount			= 0;
	pPhrInfo->pbCntiOption			= NULL;
	pPhrInfo->pbOptionChunk			= NULL;
	pPhrInfo->pbInfoChunk			= NULL;
	pPhrInfo->pbVoiceChunk			= NULL;
	pPhrInfo->pbSequenceChunk		= NULL;
	pPhrInfo->dwCntiOptionSize		= 0L;
	pPhrInfo->dwOptionDataSize		= 0L;
	pPhrInfo->dwInfoChunkSize		= 0L;
	pPhrInfo->dwVoiceChunkSize		= 0L;
	pPhrInfo->dwSequenceChunkSize	= 0L;
	pPhrInfo->dwPlayTime			= 0L;
	pPhrInfo->dwTimer				= 0L;
	pPhrInfo->dwCurrentTime			= 0L;
	pPhrInfo->dwDataPosition		= 0L;
	for(i = 0; i < MAX_PHRASE_VOICES; i++)
	{
		pPhrInfo->VocInfo[i].bBankNo = 0;
		pPhrInfo->VocInfo[i].bProgNo = 0;
	}
}

/*=============================================================================
//	Function Name	:	void	PhrChk_Initialize(PPHRCHECK pPhrChk, UINT8 bPhrNum,
//										 UINT8 bMode, UINT8* pBuffer, UINT32 dwSize)
//
//	Description		:	Initialize PHRCHECK structure
//
//	Argument		:	pPhrChk		...	Pointer to PHRCHECK structure
//						bPhrNum		...	Phrase Number (0..4).
//						bMode		...	Check mode.
//						pBuffer		...	Pointer to data.
//						dwSize		...	The Size of data(in Byte).
//	Return			:
//
=============================================================================*/
static void	PhrChk_Initialize(PPHRCHECK pPhrChk, UINT8 bPhrNum, UINT8 bMode, UINT8* pBuffer, UINT32 dwSize)
{
	pPhrChk->bMode				= bMode;
	pPhrChk->wErrStatus			= PHRASE_DATA_NOERROR;
	pPhrChk->pbBuffer			= pBuffer;
	pPhrChk->dwSize				= dwSize;
	pPhrChk->pbPhrase			= pBuffer;
	pPhrChk->dwPhraseSize		= dwSize;
	pPhrChk->pbVoice			= pBuffer;
	pPhrChk->dwVoiceSize		= dwSize;
	pPhrChk->pbPhrase			= NULL;
	pPhrChk->dwPhraseSize		= 0L;
	pPhrChk->pPhrInfo			= NULL;
	if(bPhrNum < MAX_PHRASE_DATA) {
		pPhrChk->pPhrInfo = &(gApiInfo[bPhrNum].gPhraseInfo);
		PhrChk_InitPhraseInfo(pPhrChk->pPhrInfo);
		pPhrChk->pPhrInfo->bPhrNum	= bPhrNum;
	}
}

/*=============================================================================
//	Function Name	:	PPHRINFO	SmafPhrChecker(UINT8 bPhrNum, UINT8 bMode,
//										UINT8* pBuffer, UINT32 dwSize)
//
//	Description		:	Check SMAF/Phrase
//
//	Argument		:	bPhrNum		...	#Phrase(0..4  4:only check)
//						bMode		...	check mode
//											bit0 : error check	0:disable 1:enable
//											bit1 : contents info check 0:not only 1:only
//						pBuffer		...	Pointer to the data
//						dwSize		...	Data size
//
//	Return			:	data length(msec) or error code
//
=============================================================================*/
static SINT32	SmafPhrChecker(UINT8 bPhrNum, UINT8 bMode, UINT8* pBuffer, UINT32 dwSize)
{
	PHRCHECK	PhrChkArea;				/*	work area for SMAF/Phrase format Check	*/
	PPHRCHECK	pPCA = (PPHRCHECK)(&PhrChkArea);

	if(bPhrNum >= MAX_PHRASE_DATA)		return 0;

	/*	Initialize PHRCHECK structure		*/
	PhrChk_Initialize(pPCA, bPhrNum, bMode, pBuffer, dwSize);
/*
	return ((PPHRINFO)pPCA->pPhrInfo);
*/
	return malib_smafphrase_checker(pBuffer, dwSize, pPCA);
}

/*==============================================================================
//	Function Name	:	UINT32	ProgramChange(UINT8 bCh, UINT8 bVoice)
//
//	Description		:	Convert to ProgramChange(Native format)
//
//	Argument		:	bCh			...	#Channel(0..15)
//						bVoiceNo	...	#Voice(0..3)
//
//	Return			:	0:NoError  <0:Error Code
//
==============================================================================*/
static UINT32	ProgramChange(UINT8 bCh, UINT8 bVoice)
{
	MAPHRCNV_DBGMSG(("ProgramChange[%d %02X] \n", bCh, bVoice));

	if(gChInfo[bCh].bVoiceNo != bVoice) {
		gChInfo[bCh].bVoiceNo = bVoice;
		gChInfo[bCh].bNew	= 1;
	}
	return (0L);
}

/*==============================================================================
//	Function Name	:	SINT32	CnvOctaveShift(UINT8 bCh, UINT8 bOct)
//
//	Description		:	Convert to OctaveShift(Native format)
//
//	Argument		:	bCh			...	#Channel(0..15)
//						bOct		...	OctaveShift
//
//	Return			:	0:NoError  <0:Error Code
//
==============================================================================*/
static UINT32 OctaveShift(UINT8 bCh, UINT8 bOct)
{
	MAPHRCNV_DBGMSG(("OctaveShift[%d %02X] \n", bCh, bOct));

	switch(bOct) {
	case 0x00:
	case 0x01:
	case 0x02:
	case 0x03:
	case 0x04:
		gChInfo[bCh].nOctShift = (SINT16)bOct;
		break;
	case 0x81:
		gChInfo[bCh].nOctShift = -1;
		break;
	case 0x82:
		gChInfo[bCh].nOctShift = -2;
		break;
	case 0x83:
		gChInfo[bCh].nOctShift = -3;
		break;
	case 0x84:
		gChInfo[bCh].nOctShift = -4;
		break;
	default:   /* reserved */
		gChInfo[bCh].nOctShift = 0;
		break;
	}

	return (0L);
}

/*==============================================================================
//	Function Name	:	UINT32	CnvChannelVolume(UINT8 bCh, UINT8 bVol)
//
//	Description		:	Convert to Channel Volume(Native format)
//
//	Argument		:	bCh			...	#Channel(0..15)
//						bVol		...	Volume(0..127)
//
//	Return			:	>0:Number of converted data   <0:Error Code
//
==============================================================================*/
static UINT32	CnvChannelVolume(UINT8 bCh, UINT8 bVol)
{
	MAPHRCNV_DBGMSG(("CnvChannelVolume[%d %02X] \n", bCh, bVol));

	if(gChInfo[bCh].bVolume != bVol)
	{
		MaSndDrv_SetCommand(gdwPhrId, -1L, MASNDDRV_CMD_CHANNEL_VOLUME, (UINT32)bCh, (UINT32)bVol, 0L);
		gChInfo[bCh].bVolume = bVol;
		return (1L);
	}
	return (0L);
}

/*==============================================================================
//	Function Name	:	UINT32	CnvModulation(UINT8 bCh, UINT8 bMod)
//
//	Description		:	Convert to Modulation(Native format)
//
//	Argument		:	bCh			...	#Channel(0..15)
//						bMod		...	Modulation(0..4)
//
//	Return			:	>0:Number of converted data  <0:Error Code
//
==============================================================================*/
static UINT32	CnvModulation(UINT8 bCh, UINT8 bMod)
{
	MAPHRCNV_DBGMSG(("CnvModulation[%d %02X] \n", bCh, bMod));

	if(gChInfo[bCh].bMod != bMod)
	{
		MaSndDrv_SetCommand(gdwPhrId, -1L, MASNDDRV_CMD_MODULATION_DEPTH, (UINT32)bCh, (UINT32)bMod, 0L);
		gChInfo[bCh].bMod = bMod;
		return (1L);
	}
	return (0L);
}

/*==============================================================================
//	Function Name	:	UINT32	CnvExpression(UINT8 bCh, UINT8 bExp)
//
//	Description		:	Convert to Expression(Native format)
//
//	Argument		:	bCh			...	#Channel(0..15)
//						bExp		...	Expression(0..127)
//
//	Return			:	>0:Number of converted data  <0:Error Code
//
==============================================================================*/
static UINT32	CnvExpression(UINT8 bCh, UINT8 bExp)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -