📄 dss.c
字号:
#include "LPC23xx.h" /* LPC23xx definitions */
#include "type.h"
#include "../l3/inc/l3.h"
#include "../Application/inc/envelope.h"
// This is a simple example of direct digital synthesis of a sine wave for the LPC2138
// The default configuration is an accumulator size of 2^32
// This is overkill but it's the default register size
// Also uses 100000 kHz sample rate (interrupt is set for 10 uS)
// Nothing particularily sacred about this value, can be set slower
// For technical details see http://alfa.iele.polsl.gliwice.pl/~izi/DDS(1).pdf
// "Direct Digital Synthesis: A Tool for Periodic Wave Generation (Part 1)" by Lionel Cordesses"
// The sine table has 256 elements, you can easily use 128, 256, 512, 1024, 2048 (files are provided in folder)
// The number of elements defines the lowest achievable frequency
// There is some phase jitter in some tables at certain frequencies, tables could be cleaned up
// change numbers if accumulator size or sample rate is changed
// there is no good reason to change accumulator size from 32 bits
// since it is native register size
// accumulator size / sample rate
// 2^32 / 100000 kHz
// 429496729 / 100000 = 42950
//#define freq_mult 42950
#define freq_mult 97391 /* 429496729/44100 */
//#define freq_mult 48696 /* 429496729/88200 */ /* interrupt happens 88200/sec */
extern unsigned int songindex;
extern int I2S_muteFlag;
DWORD sampleCount = 0;
unsigned int freq;
unsigned int accum=0;
/* functions exported from this file */
void nextTone(void);
void nextTone(void)
{
signed short sample, tone;
DWORD buffer=0;
static long j=3, direction =1;
/* phase increment = accumulator size / sample rate * frequency */
if(I2S_muteFlag == 0) /* if inside envelope play note */
{
accum += (freq_mult) * (freq);
sample = sine_table1024[accum>>22]; // 10 bit sine pointer, use for 1024 table
/* send the sample to the I2S, fixed support 16-bit stereo */
sampleCount++;
tone = adjustTone(sample, sampleCount);
if (direction == 0) tone = tone>>1; /* adjust amplitude */
buffer = (tone*(0x4-j)); /* Adjust left channel */
// buffer = (tone);
buffer &= (0xFFFF); /* clear sign extention on negative numbers */
buffer = buffer | (tone*j << 16 ); /* Adjust right channel */
// buffer = buffer | (tone << 16 );
I2S_TX_FIFO = buffer; /* write to I2S */
}
else
{
I2S_TX_FIFO = 0; /* write to I2S */
}
/* ADSR envelope = attackCount + decayCount + sustainCount + releaseCount */
if (sampleCount >= releaseCount) /* Set mute flag every envelope/44100 Hz */
{
I2S_muteFlag = 1;
FIO2CLR = 0x1;
sampleCount = 0;
if (direction ==1)j++;
else j--;
if (j == 4) direction = 0 ; /* four notes in front going left */
if (j == 0) direction = 1 ; /* two notes in back going right */
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -