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

📄 melody_api.c

📁 是一个手机功能的模拟程序
💻 C
📖 第 1 页 / 共 2 页
字号:
/**
 *	Author: 
 *		Robert.Chen
 *
 *	Module:
 *		Low level API wrapping Yamaha YMU765/762 chip sound middleware, interrupt driving
 *
 *  History:
 *		=== 2003/12/01   Robert.Chen ===	
 *		1)	Initiated.
 *
 *		=== 2004/05/08	 Robert.Chen ===
 *		1)	Adjusted volume setting sequence, replacing volume control after starting of
 *			playing a music (MI_PlayMusic_Start(), MI_PlayMusic_Resume()) to avoid pop noise
 *		2)  Removed EQ, HP and SP volume setting in MI_Melody_Initialize() to avoid pop noise
 *
 *		=== 2004/07/05	 Robert.Chen ===
 *		1) Add function to support play music data from FFS file, using static memory buffer
 *
 *		=== 2004/07/10	 Robert.Chen ===
 *		1) Add function to support play music data from FFS file, using dynamic memory buffer
 *		   (configuralbe in melody_cfg.h)	
 **/
#include "nucleus.h"
#include "typedefs.h"


#include "rvf_api.h"

#include "mamachdep.h"
#include "madefs.h"

#include "rvm_use_id_list.h"
#include "melody_cfg.h"
#include "melody_struct_i.h"
#include "melody_api.h"
#include "melody_env.h"

#include "masound.h"					/* MA Sound Player API */
#include "madebug.h"
#include "mmiutilapi.h"

extern T_MELODY_CTRL_BLK melodyCtrlBlk;

UINT8 gbMelLP = 1;

/* speaker level to setting value */
UINT8 spkLev2val[MI_MELODY_VOLUME_MAX+1] = {0, 6, 12, 18, 24, 31};

/******************************************************************************
 *
 * Function:
 * 		MI_Melody_Initialize()
 *
 * Description:
 *		Initialize the YAMAHA sound middleware and internal control block.
 *
 * Input:
 *		option	
 *			MelInitNormal   normal init
 *			MelInitForced   forced init even if init has previously completed
 *          
 * Output:
 *		MI_MELODY_OK
 *		MI_MELODY_INITIALIZED
 *      MI_MELODY_ERROR
 *         
 *****************************************************************************/
T_MELODY_RET MI_Melody_Initialize(T_MELODY_INIT_OPT option)
{
	int result;
 	 	
	MELODYTRACE_MSG(("MelFunc:MI_Melody_Initialize(option = %d)!", option));
 	
	if(melodyCtrlBlk.initialized && (option == MelInitNormal))
	{
		/* Initialisation has completed, send trace message */
		MELODYTRACE_MSG(("MelRet:Init finished previously!"));		
	 	return MI_MELODY_INITIALIZED;	  
	}	

	gbMelLP = 1;
	/*
	 * start init control block
	 */
	melodyCtrlBlk.musicID		 = MI_MELODY_MUSICID_INVALID;/* melody player isn't running */
	melodyCtrlBlk.curVolume		 = MI_MELODY_VOLUME_DEFAULT;
	melodyCtrlBlk.remainCnt		 = 0;	
	melodyCtrlBlk.fileType		 = 0;
	melodyCtrlBlk.curPos		 = 0;	
	melodyCtrlBlk.fileID		 = -1;	
	melodyCtrlBlk.vbrSta.blink	 = MI_VBR_BLK_DEFAULT;
	melodyCtrlBlk.vbrSta.ctrlSrc = MI_VBR_SRC_FORCED;
	melodyCtrlBlk.vbrSta.frcCtrl = MI_VBR_FORCED_OFF;
	/* init converter funcID table to all invalid */
	for(result = 0; result < MASMW_NUM_SRMCNV; result++)
	{
		melodyCtrlBlk.funcID[result] = -1;  /* use result as index only */
	}
	
  #ifdef MELODY_POWERSAVE
	melodyCtrlBlk.pwrState = MI_pwrDown;
		
	if(melodyCtrlBlk.initialized && option == MelInitForced)
	{
		/* disable the previously created timer */
		result = NU_Control_Timer(&melodyCtrlBlk.pwrSaveTmr, NU_DISABLE_TIMER);
		if(result != NU_SUCCESS)
		{
			MELODYTRACE_MSG(("MelRet:forced init, ctrl tmr err,result=%d!", result));
			melodyCtrlBlk.initialized = 0;
			return MI_MELODY_ERROR;
		}		
	}
	else
	{
		/* create power management process timer */
		result = NU_Create_Timer(&melodyCtrlBlk.pwrSaveTmr,								
	             	            "PwrSave Tmr",
	             	            MI_pwrSaveTmrProcess,
	                         	0, /* Parameter supplied to the routine: not used. */
	                         	RVF_SECS_TO_TICKS(MI_POWERSAVE_SECS),
	                         	0, /* The timer expires once. */
	                         	NU_DISABLE_TIMER);
	                         	
		if(result != NU_SUCCESS) 
		{
			MELODYTRACE_MSG(("MelRet:create timer failed:result=%d", result));
			melodyCtrlBlk.initialized = 0; /* init not executed */
			return MI_MELODY_ERROR;
		}
	}	
  #endif /* POWERSAVE */
  	/*
  	 * end control block init
  	 */

  	/*
  	 * start init MA sound middleware and device
  	 */

	/* reset device */
	MI_Melody_Reset();
			
	#if 0

	for(reg = 0; reg < 16; reg++)
	{
		machdep_WriteStatusFlagReg((UINT8)reg);
		MELODYTRACE_MSG(("BANK0, Reg%d = 0x%02x", reg, machdep_ReadDataReg()));
	}

	machdep_WriteStatusFlagReg(4);
	machdep_WriteDataReg(0x01);

	for(reg = 5; reg < 7; reg++)
	{
		machdep_WriteStatusFlagReg((UINT8)reg);
		MELODYTRACE_MSG(("BANK1, Reg%d = 0x%02x", reg, machdep_ReadDataReg()));
	}

	machdep_WriteStatusFlagReg(4);
	machdep_WriteDataReg(0x0);
	#endif
	
	/* init middleware */
	result = MaSound_Initialize();
	if(result != MASMW_SUCCESS)
	{
		MELODYTRACE_MSG(("MelRet:init middleware failed: result=%d!", result));
		melodyCtrlBlk.initialized = 0;
		return MI_MELODY_ERROR;
	}

	/* Device Control */
	#if (MIDI_DEVICE == 2)	/* Robert.Chen, 2004-05-15 */
	/* initialize MA-5 */
	result = MaSound_DeviceControl( MASMW_HW_INITIALIZE, 0, 0, 0 );
	if ( result < MASMW_SUCCESS )
	{
		MELODYTRACE_MSG(("MelRet:###init Device failed###: result=%d!", result));
		return MASMW_ERROR;
	}
	#endif
	
#if 0
	/* set EQ amplifier */
	result = MaSound_DeviceControl(MASMW_EQ_VOLUME, 31, 0, 0);
	if ( result != MASMW_SUCCESS )
	{
		MELODYTRACE_MSG(("MelRet:EQ err,result=%d!", result));
		return MI_MELODY_ERROR;
	}

	/* set speaker amplifier */
	result = MaSound_DeviceControl(MASMW_SP_VOLUME, 31, 0, 0);
	if ( result != MASMW_SUCCESS )
	{
		MELODYTRACE_MSG(("MelRet:Spk err,result=%d!", result));
		return MI_MELODY_ERROR;
	}

	result = MaSound_DeviceControl(MASMW_HP_VOLUME, 0, 31, 31);
	if ( result < MASMW_SUCCESS )
	{
		MELODYTRACE_MSG(("MelRet:HP err,result=%d!", result));
		return MI_MELODY_ERROR;
	}
#endif	

  #ifdef HEADPHONE	/* headphone equipped */
    /* set headphone stereophonic output and R/L amplifier */
	result = MaSound_DeviceControl(MASMW_HP_VOLUME, 0, spkLev2val[melodyCtrlBlk.curVolume], spkLev2val[melodyCtrlBlk.curVolume]);
	if ( result != MASMW_SUCCESS )
	{
		MELODYTRACE_MSG(("MelRet:HP err1,result=%d!", result));
		return MI_MELODY_ERROR;
	}
  #else
  	#ifdef MELODY_POWERSAVE
  	/* set headphone output in power down state for power saving */
	result = MaSound_DeviceControl(MASMW_PWM_ANALOG, 0x30, 0, 0);
	if ( result != MASMW_SUCCESS )
	{
		MELODYTRACE_MSG(("MelRet:HP err2,result=%d!", result));
		return MI_MELODY_ERROR;
	}
	#endif
  #endif

	/* set vibrator mode */  
	result = MaSound_DeviceControl(MASMW_MTR_MASTER, melodyCtrlBlk.vbrSta.ctrlSrc, 0, 0);
  	if(result != MASMW_SUCCESS)
  	{
  		MELODYTRACE_MSG(("MelRet:VBR err1,result=%d!", result));
		return MI_MELODY_ERROR;
  	}

  	result = MaSound_DeviceControl(MASMW_MTR_BLINK, melodyCtrlBlk.vbrSta.blink, 0, 0);
  	if(result != MASMW_SUCCESS)
  	{
  		MELODYTRACE_MSG(("MelRet:VBR err2,result=%d!", result));
		return MI_MELODY_ERROR;
  	}	
  	
  	/*
  	 * end init MA sound middleware and device
  	 */
  	 
  	 /* start power saving timer */
  #ifdef MELODY_POWERSAVE	
	result = NU_Control_Timer(&melodyCtrlBlk.pwrSaveTmr, NU_ENABLE_TIMER);
	if(result != NU_SUCCESS)
	{
		MELODYTRACE_MSG(("MelRet:enable timer failed,result=%d!", result));
		return MI_MELODY_ERROR;
	}
  #endif

  #ifdef MASMW_SRMCNV_SMAF
  	melodyCtrlBlk.funcID[MASMW_CNVID_MMF] = MaSound_Create(MASMW_CNVID_MMF);
  	MELODYTRACE_MSG(("MelRet:cnvt create, MMFID:%d", melodyCtrlBlk.funcID[MASMW_CNVID_MMF]));
  #endif
#if 0
  #ifdef MASMW_SRMCNV_SMAF_PHRASE
 	melodyCtrlBlk.funcID[MASMW_CNVID_PHR] = MaSound_Create(MASMW_CNVID_PHR);
  	MELODYTRACE_MSG(("MelRet:cnvt create, MMFID:%d", melodyCtrlBlk.funcID[MASMW_CNVID_PHR]));
  #endif
#endif
  #ifdef MASMW_SRMCNV_REALTIME_MIDI
	melodyCtrlBlk.funcID[MASMW_CNVID_RMD] = MaSound_Create(MASMW_CNVID_RMD);
  	MELODYTRACE_MSG(("MelRet:cnvt create, MMFID:%d", melodyCtrlBlk.funcID[MASMW_CNVID_RMD]));
  #endif

  #ifdef MASMW_SRMCNV_SMAF_AUDIO
	melodyCtrlBlk.funcID[MASMW_CNVID_AUD] = MaSound_Create(MASMW_CNVID_AUD);
  	MELODYTRACE_MSG(("MelRet:cnvt create, MMFID:%d", melodyCtrlBlk.funcID[MASMW_CNVID_AUD]));
  #endif

  #ifdef MASMW_SRMCNV_SMF
	melodyCtrlBlk.funcID[MASMW_CNVID_MID] = MaSound_Create(MASMW_CNVID_MID);
  	MELODYTRACE_MSG(("MelRet:cnvt create, MMFID:%d", melodyCtrlBlk.funcID[MASMW_CNVID_MID]));
  #endif

  #ifdef MASMW_SRMCNV_WAV
	melodyCtrlBlk.funcID[MASMW_CNVID_WAV] = MaSound_Create(MASMW_CNVID_WAV);
  	MELODYTRACE_MSG(("MelRet:cnvt create, MMFID:%d", melodyCtrlBlk.funcID[MASMW_CNVID_WAV]));
  #endif
	
	MELODYTRACE_MSG(("MelRet:init success!"));
	melodyCtrlBlk.initialized	= 1;
	
  #ifdef MELODY_POWERSAVE
	melodyCtrlBlk.pwrState		= MI_pwrRelease;
  #endif

	
  	MI_Melody_HpAmpCtrl(0, MI_PWRCTRL_ALL);

#ifdef COLORLED_SUPPORT  
	MaSound_DeviceControl( MASMW_LED_MASTER, 1, 0, 0 );
	MaSound_DeviceControl( MASMW_LED_DIRECT, 0, 0, 0 );  
#endif	

	return MI_MELODY_OK;	
}

#if (MELODY_FFS_SUPPORT == 1)
/******************************************************************************
 *
 * Function:
 * 		MI_PlayFile_Start()
 *
 * Description:
 *		Start play a music saved as a FFS file
 *
 * Input:
 *		musicName	string
 *		type		music type, if type equal to 0xFF means type not specified
 *		count		playback count(0 ~ 255), 0 means loop play
 *          
 * Output:
 *		MI_MELODY_OK
 *		MI_MELODY_NOT_INIT
 *      MI_MELODY_ERROR
 *		MI_MELODY_PARA_ERR
 *      MI_MELODY_FILE_SIZE_ZERO
 *		MI_MELORY_FILE_SIZE_LARGE
 *		MI_MELODY_FILE_TYPE_ERR
 *		MI_MELODY_FILE_TYPE_UNKNOW
 *		MI_MELODY_FILE_READ_ERR
 *      MI_MELODY_FILE_NOT_EXIST
 *
 *****************************************************************************/
T_MELODY_RET MI_PlayFile_Start(const char *musicName, UINT8 type, UINT8 count)
{
	SINT32	result;

	MELODYTRACE_MSG(("MelFunc:MI_PlayFile_Start(),n=\"%s\",c=%d", musicName, count));

	/* check arguments */
	if(musicName == NULL || (type >= MASMW_NUM_SRMCNV && type != 0xff) || count > 255)
	{
		MELODYTRACE_MSG(("MI_PlayFile_Start():arg err!"));
		return MI_MELODY_PARA_ERR;
	}

	if(!melodyCtrlBlk.initialized)
 	{
 		MELODYTRACE_MSG(("MI_PlayFile_Start():middleware not init!"));
 		return MI_MELODY_NOT_INIT;
 	}

 	if(melodyCtrlBlk.funcID[type] == -1)
 	{
 		MELODYTRACE_MSG(("MI_PlayFile_Start():not supported music type!"));
 		return MI_MELODY_FILE_TYPE_ERR;
 	}
	
	result = MI_LoadMusicByName(musicName, type);
	if(result != MI_MELODY_OK)
	{	
		MI_CloseMusicFile();
		MELODYTRACE_MSG(("MI_PlayFile_Start():load FFS file err!"));
		return result;
	}

	return MI_PlayMusic_Start(MI_MELODY_MUSICID_FILE, count);
}
#endif //MELODY_FFS_SUPPORT
 
/******************************************************************************
 *
 * Function:
 * 		MI_PlayMusic_Start()
 *
 * Description:
 *		Start play a music according to specified replay count.
 *
 * Input:
 *		musicNo	music ID to be played
 *		count	playback count(0 ~ 255), 0 means loop play
 *          
 * Output:
 *		MI_MELODY_OK
 *		MI_MELODY_NOT_INIT
 *      MI_MELODY_ERROR
 *      MI_MELODY_FILE_SIZE_ZERO
 *		MI_MELODY_FILE_TYPE_ERR
 *      MI_MELODY_FILE_NOT_EXIST
 *
 *****************************************************************************/
T_MELODY_RET MI_PlayMusic_Start(UINT8 musicNo, UINT8 count)
{
	SINT32 result;
	SINT32 func_id;
	SINT32 file_id;
	UINT32 repeat;
		
	MELODYTRACE_MSG(("MelFunc:MI_PlayMusic_Start(music = %d, count = %d)", musicNo, count));

	if(!melodyCtrlBlk.initialized)
 	{ 		
 		return(MI_MELODY_NOT_INIT);
 	}	
	
	result = MI_PlayPrepare(musicNo, count);
	
#if (MELODY_FFS_SUPPORT == 1)	/* Robert.Chen add, 2004-07-09 */
	MI_CloseMusicFile();
#endif
	
	if(result != MI_MELODY_OK)
	{
		MELODYTRACE_MSG(("MelRet:play prepare failed,result=%d!", result));		
		return result;
	}

	func_id = melodyCtrlBlk.funcID[melodyCtrlBlk.fileType];
	file_id = melodyCtrlBlk.fileID;

	/* Set repeat */
	AI_MaskIT(0x02);
	repeat = count;
	result = MaSound_Control( func_id, file_id, MASMW_SET_REPEAT, &repeat, NULL );
	if ( result < MASMW_SUCCESS )
	{
		MI_Melody_ErrHdr( func_id, file_id );
		
		AI_UnmaskIT(0x02);
		return MASMW_ERROR;
	}
	
  	result = MaSound_Start(func_id, file_id, 0, NULL);
	if(result != MASMW_SUCCESS)
	{
		MELODYTRACE_MSG(("MelRet:start play err,result=%d!", result));
		MI_Melody_ErrHdr(func_id, file_id);
		AI_UnmaskIT(0x02);
		return MI_MELODY_ERROR;
	}

	MaSound_DeviceControl(MASMW_SP_VOLUME, spkLev2val[melodyCtrlBlk.curVolume], 0, 0);
	#if (MIDI_STEREO == 1)
	MaSound_DeviceControl(MASMW_HP_VOLUME, 0, spkLev2val[melodyCtrlBlk.curVolume], spkLev2val[melodyCtrlBlk.curVolume]);
	#endif
	MaSound_DeviceControl(MASMW_EQ_VOLUME, 31, 0, 0);
	
	MELODYTRACE_MSG(("MelSta:music play success!"));

	//gbMelLP = 0;	/* Robert.Chen, 2004-0410, combined sleep control with chip power mode control */
	AI_UnmaskIT(0x02);
	return MI_MELODY_OK;	
}

/******************************************************************************
 *
 * Function:
 * 		MI_PlayMusic_Stop()
 *
 * Description:
 *		Stop play the current music arbitrarily.
 *
 * Input:
 *		none.
 *          
 * Output:
 *		MI_MELODY_OK
 *		MI_MELODY_NOT_INIT
 *      MI_MELODY_ERROR
 *
 *****************************************************************************/
T_MELODY_RET MI_PlayMusic_Stop(void)
{
	SINT32 result;
	SINT32 func_id;
	SINT32 file_id;	
	
	MELODYTRACE_MSG(("MelFunc:MI_PlayMusic_Stop()!"));
	
	AI_MaskIT(0x02);
		
	if(!melodyCtrlBlk.initialized)
		return MI_MELODY_NOT_INIT;
		
	/* no music play running */	
	if(melodyCtrlBlk.musicID == MI_MELODY_MUSICID_INVALID)
		return MI_MELODY_OK;

	func_id = melodyCtrlBlk.funcID[melodyCtrlBlk.fileType];
	file_id = melodyCtrlBlk.fileID;

	/* Robert.Chen 2004-02-07 changed */
	/* from */

	/*#####
	result = MaSound_Stop(func_id, file_id, NULL);
	if(result != MASMW_SUCCESS)
	{
		MELODYTRACE_MSG(("MelRet:stop play failed,result=%d!", result));
		MI_Melody_ErrHdr(func_id, file_id);
		return MI_MELODY_ERROR;
	}
	#####*/

	/* to */

⌨️ 快捷键说明

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