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

📄 wave.c

📁 Taiwan sunplus develop spce3200, it is a test program ----- testboard source code
💻 C
字号:
//====================================================================================
//File Name:	Wave.c
//Description: 	Wave files play program
//Update:		2007.01.17 V0.1	by wangtao <wangtao@sunnorth.com.cn>
//====================================================================================
#include "SPCE3200_Register.h"
#include "SPCE3200_Constant.h"
#include "DAC.h"
#include "Resource.h"

unsigned short HalfBuffer_Flag;											// Half buffer flags
unsigned short *Buffer_BA;												// Save Buffer address
unsigned short Ex_Buffer[512];											// 4K external buffer
unsigned short *WAVE_Addr;												// Wave resource address pointer
unsigned short *WAVE_EndAddr;
unsigned short Wave_Status;

//=========================================================================
// Prototype:		void SP_Wave_Init(void)
// Description: 	Initialize function, enable DAC and its interrupt
// Arguments:		None
// Return Value:	None
//=========================================================================
void SP_Wave_Init(void)
{
	*P_INT_MASK_CTRL1 &= (~C_INT_DAC_DIS);								// Enable DAC interrupt
	
	*P_DAC_CLK_CONF = C_DAC_CLK_EN										// Enable DAC module clock
					| C_DAC_RST_DIS;									// Not reset DAC module
	*P_DAC_MODE_CTRL1 = ~(C_DAC_CTRL_DIS								// Enable DAC module
						| C_DAC_OUTPUT_DIS);							// Enable DAC output	
	Wave_Status = 0;
	HalfBuffer_Flag = 0;
}

//=========================================================================
// Prototype:		void SP_Wave_Setup(unsigned short *WAVE_SA,unsigned int SampleRate)
// Description: 	DAC arguments settings, buffer address setting, fill the 8KByte buffer and then enable the channel
// Arguments:		WAVE_SA (wave resource start address), SampleRate
// Return Value:	None
//=========================================================================
void SP_Wave_Setup(unsigned short *WAVE_SA, unsigned short *WAVE_EA, unsigned int SampleRate, short Channel)
{
	unsigned short WAVE_Data;												// Save resource data
	unsigned int i;															// Used for cycle
	unsigned short *Buffer_Addr;											// Buffer pointer,used to fill Buffer
	
	*P_DAC_FIFOBA_LOW = ((unsigned int)Ex_Buffer);							// Set the base address low 16-bit of external buffer 
	*P_DAC_FIFOBA_HIGH = ((unsigned int)Ex_Buffer) >> 16;					// High 16-bit
	*P_DAC_SAMPLE_CLK = 27000000/SampleRate - 1;							// Set the sample rate

	*P_DAC_INT_STATUS = C_DAC_INT_EN										// Enable DAC interrupt
						| C_DAC_STEREO_MODE  								// stereo mode
						| C_DAC_BUFFER_1K;									// 1K buffer
	Buffer_BA = (unsigned short*)(((unsigned int)Ex_Buffer) & 0x8FFFFFF);	// The low 29-bit address are valid
	WAVE_Addr = WAVE_SA;													// Start at the wave resource start address
	WAVE_EndAddr = WAVE_EA;
	Buffer_Addr = Buffer_BA;												// Buffer pointer points to the external buffer
	
//----------------------------Take off the head of wave file--------------------//
	WAVE_Addr += 16;
	WAVE_Data = *WAVE_Addr;
	if(WAVE_Data!=0x0018)
		WAVE_Addr = WAVE_SA+0x3A;
	else
		WAVE_Addr = WAVE_SA+0x3C;
	
	for(i=0;i<256;i++)														// Fill 4K double-byte buffer
	{
		WAVE_Data = *WAVE_Addr;												// Get the sound data from wave resource address
		WAVE_Data ^= 0x8000;												// Change to None
		WAVE_Addr++;														// Wave resource pointer pointes to the next address
		*Buffer_Addr = WAVE_Data;											// Fill buffer with 16-bit data
		Buffer_Addr++;														// Buffer pointer pointes to the next address	
		*Buffer_Addr = WAVE_Data;											// Fill buffer with 16-bit data
		Buffer_Addr++;														// Buffer pointer pointes to the next address	
	}
	*P_DAC_MODE_CTRL2  = C_DAC_FIFO_EN;										// Enable DAC FIFO
	if(Channel & 0x01)														// Enable channel0
		*P_DAC_MODE_CTRL2 |= C_DAC_CHANNAL0_EN;
	if(Channel & 0x02)														// Enable channel1	
		*P_DAC_MODE_CTRL2 |= C_DAC_CHANNAL1_EN;
	Wave_Status = 1;
}

//=========================================================================
// Prototype:		void SP_Fill_ExBuffer()
// Description: 	Fill external buffer, half buffer one time
// Arguments:   	WAVE_SA,wave resource start address
// Return Value:	None
//=========================================================================
void SP_Fill_ExBuffer(void)
{
	unsigned short WAVE_Data;												// Save resource data
	unsigned int i;															// Used for cycle
	unsigned short *Buffer_Addr;											// Buffer pointer,used to fill Buffer

	if(Wave_Status==0)return;
	
	Buffer_Addr = Buffer_BA;
	for(i=0;i<128;i++)														// Fill 2K double-byte buffer(half buffer)
	{
		WAVE_Data = *WAVE_Addr;												// Get the sound data from wave resource address	
		WAVE_Data ^= 0x8000;												// Change to None
		WAVE_Addr++;														// Wave resource pointer pointes to the next address
		if(WAVE_Addr <= WAVE_EndAddr)										// Address for not the playing end 
		{
			*Buffer_Addr = WAVE_Data;										// Fill buffer with 16-bit data
			Buffer_Addr++;													// Buffer pointer pointes to the next address
			*Buffer_Addr = WAVE_Data;										// Fill buffer with 16-bit data
			Buffer_Addr++;													// Buffer pointer pointes to the next address	
		}
		if(WAVE_Addr >= WAVE_EndAddr)										// Address for the playing end
		{
			Wave_Status = 0;
			*P_DAC_INT_STATUS = ~C_DAC_INT_EN;								
			*P_DAC_MODE_CTRL2  &= ~(C_DAC_FIFO_EN							// Enable DAC FIFO
						| C_DAC_CHANNAL0_EN									// Enable channel0
						| C_DAC_CHANNAL1_EN)								// Enable channel1		
						;
		}
	}
	if(HalfBuffer_Flag != 0)												// Pointe to the first half Buffer
	{
		Buffer_BA -= 256;
		HalfBuffer_Flag = 0;
	}
	else																	// Pointe to the last half Buffer
	{
		Buffer_BA += 256;
		HalfBuffer_Flag = 1;
	}
}

//=========================================================================
// Prototype:		unsigned int SP_Play_Status(void)
// Description: 	Get the play status
// Arguments:    	None
// Return Value: 	Play status
//=========================================================================
unsigned int SP_Play_Status(void)
{
	return Wave_Status;
}

//=========================================================================
// Prototype:		void SP_Wave_Stop(void)
// Description: 	Stop playing
// Arguments:     	None
// Return Value:  	None
//=========================================================================
void SP_Wave_Stop(void)
{
	Wave_Status = 0;
	Wave_Status = 0;
	*P_DAC_INT_STATUS = ~C_DAC_INT_EN;
	*P_DAC_MODE_CTRL2  &= ~(C_DAC_FIFO_EN									// Enable DAC FIFO
		| C_DAC_CHANNAL0_EN													// Enable channel0
		| C_DAC_CHANNAL1_EN)												// Enable channel1	
		;
}

⌨️ 快捷键说明

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