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

📄 effects processor.c

📁 语音信号进行Fir滤波
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <signal.h> 
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include "ADDS_21161_EzKit.h"

#include <def21161.h>
asm("#include <def21161.h>");


#define BFSZ        4096    /* main delay buffer */
#define MAX_XTAB    512     /* crossfade lookup table size for pitch change */
#define NA          0.0     /* param not applicable */

#ifndef TRUE
#define TRUE        1
#define FALSE       0
#endif

/* program description struct */
struct program {
    char *name;         /* ASCII name of program */
    void (*vec)(struct program *p);
    float input;     /* dry (unaffected) signal mix */
    float output;     /* wet (affected) signal mix */
    float feedback;    /* feedback */
    float rate;        /* usually rate of sweep */
    float depth;       /* width of sweep */
    float delay;       /* fixed delay factor (base pitch for phaser) */
} program;


/* handy typedefs */
typedef union bw {
    unsigned char b[2];
    int w;
} bw;
typedef union wl {
    unsigned int w[2];
    long l;
} wl;

/* protos */
void thru(struct program *p);
void noise_gate(struct program *p);
void flange_chorus(struct program *p);
void pitch_change(struct program *p);
void phase_shift(struct program *p);
void chk_keystroke(void);
void usage(void);
void init_1848(int samprate, int mic, int gain);
void wr_reg(int reg, int val);
int  rd_reg(int reg);

/* table of programs (it's very easy to modify and add programs) */
struct program programs[] = {
    /*  Name/vector      Dry     Wet   Feedback Rate    Depth   Delay */
    { "Echoes",
        flange_chorus,  0.999,  0.999,  0.7,    0.0,    0.0,    250.0},
    { "Slow flange",
        flange_chorus,  0.999,  0.999,  0.0,    2.0,    6.0,    0.0},
    { "Slow invert flange with feedback",
        flange_chorus,  0.999,  -0.999, -0.7,   2.0,    6.0,    0.0},
    { "Slow chorus",
        flange_chorus,  0.999,  0.999,  0.0,    11.0,   20.0,   20.0},
    { "Cheesy \"take me to your leader\" robot voice",
        flange_chorus,  0.0,    0.999,  0.75,   0.0,    0.0,    12.5},
    { "Crazy pitch bend chorus",
        flange_chorus,  0.999,  0.999,  0.3,    150.0,  40.0,   40.0},
    { "Darth",
        pitch_change,   0.0,    0.999,  0.0,    -0.35,  25.0,   0.0},
    { "Major third up",
        pitch_change,   0.999,  0.999,  0.0,    0.2599, 35.0,   0.0},
    { "Octave up",
        pitch_change,   0.999,  0.999,  0.0,    1.0,    40.0,   0.0},
    { "Munchkins on helium",
        pitch_change,   0.0,    0.999,  0.4,    0.3348, 35.0,   0.0},
    { "Descending echoes",
        pitch_change,   0.0,    0.999,  0.5,    -0.2,   35.0,   120.0},
    { "Ascending echoes",
        pitch_change,   0.0,    0.999,  0.4,    0.2599, 35.0,   120.0},
    { "Phase shift",
        phase_shift,    0.999,  0.999,  0.0,    1.0,    4.0,    100.0},
    { "Slow invert phase shift with feedback",
        phase_shift,    0.999,  -0.999, -0.6,   0.2,    6.0,    100.0},
    { "Noise gate",
        noise_gate,     NA,     NA,     NA,     500.0,  0.05,   NA},
    { "Straight Thru",
        thru,           NA,     NA,     NA,     NA,     NA,     NA},
};
#define NPROGS (sizeof(programs) / sizeof(struct program))


/* globals */
unsigned int SampleRate = 48000;            /* sample rate 1836 */
float	 Buf[BFSZ];                   /* buffer used by delay based effects */



void	Process_Samples( int sig_int)
{
	float output;
	int patch = 1;
	Receive_Samples();

	p = &programs[patch];

	p->input = Left_Channel0;
	(*p->vec)(p);
	Left_Channel0 = p->output;

	Transmit_Samples();
}

void	main()
{
	int i;


	/* Setup Interrupt edges and flag I/O directions */
	Setup_ADSP21161N();

	/* Setup SDRAM Controller */
	Setup_SDRAM();

	Setup_AD1836();
	Init_AD1852_DACs();

	Program_SPORT02_TDM_Registers();
	Program_SPORT02_DMA_Channels();

	interruptf(	SIG_SP0I,	Process_Samples);

//	asm("TPERIOD = 29966664;TCOUNT = TPERIOD;bit set mode2 TIMEN;");

//	interruptf( SIG_TMZ,	Tick);
//	interruptf( SIG_IRQ0,	Perterb_Model);
	*(int *) SP02MCTL |= MCE;

	for (;;)
	{
		asm("idle;");
	} 
}




void
thru(struct program *p)
{
    p->output = p->input;               /* quiet the compiler */
}


/*
                                noise_gate

    Super simple noise gate to demonstrate how much of the hiss
    comes directly from the ADC on this card, but how quiet the
    DACs are by comparison.
        
    Only parms are:
        rate        decay time in ms
        depth       threshold for turn on
*/
void
noise_gate(struct program *p)
{
    float inval,decay_fac,gain = 0;

    /* calculate decay factor for 20db atten in spec'd time */
    decay_fac = pow(10.0,1.0 / ((p->rate / 1000.0) * (float)SampleRate));
    decay_fac = 1.0 / decay_fac;
        
    inval = p->input;
    if(inval > p->depth)                /* see if we crossed threshold */
           gain = 1.0;                     /* turn gate on */
    p->output = inval * gain;
        
    gain *= decay_fac;                  /* adjust attenuation */
}

        
/*
                                flange_chorus
    
    Does flanging/chorusing family of effects based on a single
    varying delay.
    
    dry_mix     mix of unaffected signal (-0.999 to 0.999)
    wet_mix     mix of affected signal (-0.999 - 0.999)
    feedback    amount of recirculation (-0.9 - 0.9)
    rate        rate of delay change in millisecs per sec
    sweep       sweep range in millisecs
    delay       fixed additional delay in millisecs
*/
void
flange_chorus(struct program *p)
{
    int fp,ep1,ep2;
    int step,depth,delay,min_sweep,max_sweep;
    float inval,outval,ifac = 65536.0;
    long scan = 0;
    int data;
    int sweep;

	if (!p->setup)
	{
	    step = (int)(p->rate * 65.536);
	    depth = (int)(p->depth * (float)SampleRate / 1000.0);
	    delay = (int)(p->delay * (float)SampleRate / 1000.0);
	    
	    /* init/calc some stuff */
	    max_sweep = BFSZ - 2 - delay;
	    min_sweep = max_sweep - depth;
	    if(min_sweep < 0) {
	        printf("Can't do that much delay or depth at this sample rate.\n");
	        exit(1);
	    }
	    sweep = (min_sweep + max_sweep) / 2;
	
	    /* init store and read ptrs to known value */
	    fp = ep1 = ep2 = 0;
		!p->setup = 1;
    }

        /* interpolate from the 2 read values */
        outval = Buf[ep1] * sweep + Buf[ep2] * (ifac - sweep) / ifac;
        
        /* store finished input plus feedback */
        Buf[fp] = inval = (float)data + outval * p->feedback;
        
        /* develop final output mix */
        p->output = outval * p->wet_mix + inval * p->dry_mix;
            
        /* update ptrs */
        fp = (fp + 1) & (BFSZ - 1);
        sweep += step;
        ep1 = (fp + sweep) & (BFSZ - 1);
        ep2 = (ep1 - 1) & (BFSZ - 1);

        /* check for sweep reversal */      
        if(sweep > max_sweep)          /* see if we hit top of sweep */
            step = -step;                   /* reverse */
        else if(sweep < min_sweep)     /* or if we hit bottom of sweep */
            step = -step;                   /* reverse */
            
}

/*
                                pitch_change
    
    dry_mix     mix of unaffected signal (-0.999 to 0.999)
    wet_mix     mix of affected signal (-0.999 - 0.999)
    feedback    amount of recirculation (-0.9 - 0.9)
    rate        amount of pitch change (see table below for values)
    depth       sweep range in millisecs for generating pitch shift
    delay       fixed additional delay

    Semitones      Up              Down
        1       0.059463        -0.056126
        2       0.122462        -0.109101
        3       0.189207        -0.159104
        4       0.259921        -0.206299
        5       0.334840        -0.250846
        6       0.414214        -0.292893
        7       0.498307        -0.332580
        8       0.587401        -0.370039
        9       0.681793        -0.405396
        10      0.781797        -0.438769
        11      0.887749        -0.470268
        12      1.000000        -0.500000
*/
void
pitch_change(register struct program *p)
{

⌨️ 快捷键说明

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