给你个例程看看,网上找的。
#include "sineTable.h"
#include "dds.h"
#define DDS_FRAME_SIZE 2000
#define DDS_UNITS_NUM 16
STEREO_SAMPLE ddsFrameSample0[DDS_FRAME_SIZE];
STEREO_SAMPLE ddsFrameSample1[DDS_FRAME_SIZE];
DDS_CFG ddsCfg[DDS_UNITS_NUM];
void DdsInitCfg(DDS_CFG* ddsCfg)
{
ddsCfg->cst1=0xFFFFFFFF/ddsCfg->sampleRate;
}
void DdsChangeFreq(uint32_t freq,uint32_t chn)
{
ddsCfg[chn].desireFreq=freq;
}
void DdsInit(void)
{
uint32_t i;
for(i=0; i<DDS_UNITS_NUM; i++)
{
ddsCfg[i].desireFreq=0;
ddsCfg[i].phaseAccReg=0;
ddsCfg[i].sampleRate=44100;
DdsInitCfg(&ddsCfg[i]);
}
SoundBufferAssign((uint32_t)ddsFrameSample0,DDS_FRAME_SIZE*4,(uint32_t)ddsFrameSample1,DDS_FRAME_SIZE*4);
}
void PhaseAccRegUpdate(DDS_CFG* ddsCfg)
{
ddsCfg->phaseAccReg=ddsCfg->desireFreq*ddsCfg->cst1+ddsCfg->phaseAccReg;
}
void DdsPeriodRoutine(void)
{
uint32_t i,j;
uint32_t* rawBufPtr;
int16_t* samplePtr;
int32_t res;
uint32_t waveTableIdx=0;
int32_t waveValue;
res=SoundOpenFreeBuffer(&rawBufPtr);
if(res!=-1)
{
samplePtr=(int16_t*)rawBufPtr;
waveValue=0;
for(i=0; i<DDS_FRAME_SIZE; i++)
{
for(j=0; j<DDS_UNITS_NUM; j++)
{
PhaseAccRegUpdate(&ddsCfg[j]);
waveTableIdx=(ddsCfg[j].phaseAccReg>>18)&0x3FFF;
waveValue+=sineTbl[waveTableIdx];
}
waveValue/=DDS_UNITS_NUM;
*samplePtr=waveValue;
samplePtr++;
*samplePtr=waveValue;
samplePtr++;
}
res=SoundCloseBuffer(&rawBufPtr);
}
}
|