📄 note.c
字号:
#include <libap.h>
/* Function to generate samples from a second order oscillator.
key = 0-127 "MIDI" note number (69 = 440 Hz).
rate = envelope rate of change parameter (close to 1).
change_flag = indicates that key and/or rate have changed.
10-31-94 CAC adapted to CAC boards
*/
#define PI 3.14159265358979323846
#ifdef KHZ8
#define SAMPLE_RATE 8e3
#else
#ifdef KHZ16
#define SAMPLE_RATE 16e3
#else
#define SAMPLE_RATE 32e3
#endif
#endif
/* key constant is 1/12 */
#define KEY_CONSTANT 0.083333333333333
/* this set the A above middle C reference frequency of 440 Hz */
/* calculate this as a globable so it never happens again */
#define TWO_PI_DIV_FS_440 (880.0 * PI / SAMPLE_RATE)
/*
key:
semi-tone pitch to generate,
number 69 will give A above middle C at 440 Hz.
voice:
voice number from 0 to MAX_VOICES-1
change_flag:
-1 = start new sequence from t=0
0 = no change, generate next sample in sequence
1 = change rate after start
rate:
rate constant determines the decay or rise of envelope
*/
/* 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 ts arrays (reset when t=0) */
typedef struct {
int key,t,cindex;
float cw,a,b;
float y1,y0;
} NOTE_STATE;
/* NOTE_STATE structure, time break point array, rate parameter array */
float note(sparm,tbreak_array,rate_array)
NOTE_STATE *sparm;
int *tbreak_array;
float *rate_array;
{
register NOTE_STATE *s = sparm;
register int ti,ci;
register float rate,out;
float wosc;
ti = s->t;
/* t=0 re-start case, set state variables */
if(!ti) {
wosc = TWO_PI_DIV_FS_440 * xtoy(2.0,(s->key-69) * KEY_CONSTANT);
s->cw = 2.0 * qcos(wosc);
rate = rate_array[0];
s->a = s->cw * rate; /* rate change */
s->b = -rate * rate;
s->y0 = 0.0;
out = rate*qsin(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 + -