📄 note.c
字号:
#include <stdlib.h>
#include <math.h>
#include "rtdspc.h"
/* Function to generate samples from a second order oscillator */
/* key constant is 1/12 */
#define KEY_CONSTANT 0.083333333333333
/* this sets the A above middle C reference frequency of 440 Hz */
#define TWO_PI_DIV_FS_440 (880.0 * PI / SAMPLE_RATE)
/* cw is the cosine constant required for fast changes of envelope */
/* a and b are the coefficients for the difference equation */
/* y1 and y0 are the history values */
/* t is time index for this note */
/* cindex is the index into rate and tbreak arrays (reset when t=0) */
typedef struct {
int key,t,cindex;
float cw,a,b;
float y1,y0;
} NOTE_STATE;
/*
key:
semi-tone pitch to generate,
number 69 will give A above middle C at 440 Hz.
rate_array:
rate constants determines decay or rise of envelope (close to 1)
tbreak_array:
determines time index when to change rate
*/
/* NOTE_STATE structure, time break point array, rate parameter array */
float note(NOTE_STATE *s,int *tbreak_array,float *rate_array)
{
register int ti,ci;
float wosc,rate,out;
ti = s->t;
/* t=0 re-start case, set state variables */
if(!ti) {
wosc = TWO_PI_DIV_FS_440 * pow(2.0,(s->key-69) * KEY_CONSTANT);
s->cw = 2.0 * cos(wosc);
rate = rate_array[0];
s->a = s->cw * rate; /* rate change */
s->b = -rate * rate;
s->y0 = 0.0;
out = rate*sin(wosc);
s->cindex = 0;
}
else {
ci = s->cindex;
/* rate change case */
if(ti == tbreak_array[ci]) {
rate = rate_array[++ci];
s->a = s->cw * rate;
s->b = -rate * rate;
s->cindex = ci;
}
/* make new sample */
out = s->a * s->y1 + s->b * s->y0;
s->y0 = s->y1;
}
s->y1 = out;
s->t = ++ti;
return(out);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -