midi.c
[cantaveria.git] / org.c
blob09f4f9e422d4f83602b71fd6ca4ffd17c9b8ee98
1 /*
2 org.c
3 various software synths and samples
5 Copyright (C) 2009 Evan Rinehart
7 This software comes with no warranty.
8 1. This software can be used for any purpose, good or evil.
9 2. Modifications must retain this license, at least in spirit.
12 #include <stdlib.h>
13 #include <math.h>
15 #include <org.h>
18 heres a list of instrument ideas...
20 piano (not sure yet)
21 brass (not sure yet)
22 string (not sure yet)
23 drum (probably samples)
24 bass kick (classic analog algorithm)
25 square triangle saw (polyblep ?)
26 wind instruments (additive synth via uninterpolated table)
27 sampler (perhaps a compressed array of samples)
28 rendered ambient (PADsynth)
32 /* SINE TABLE */
33 #define TABLE_SIZE (1<<8)
34 #define RELEASE_STEPS (SAMPLE_RATE/333)
35 #define RELEASE_RATE 0.999
36 float sine_table[TABLE_SIZE];
38 float note2tablestep(float note){
39 /* critical formula for tuning a table */
40 /* table steps for 2pif/SAMPLE_RATE radians is...*/
41 /* TABLE_SIZE/PI2 == entries per radian */
42 /* step == 2pif/SAMPLE_RATE * (TABLE_SIZE/PI2) */
43 /* == f * TABLE_SIZE / SAMPLE_RATE */
44 /* == f_0 2^(note/12) TABLE_SIZE / SAMPLE_RATE */
45 float f = 440 * pow(2, note/12.0);
46 return f*TABLE_SIZE / SAMPLE_RATE;
49 float sine_table_interp(float ptr){
50 float i0 = floor(ptr);
51 float i1 = i0+1;
52 int I0 = i0;
53 int I1 = i1;
54 if(I0 < 0) I0 += TABLE_SIZE;
55 if(I1 >= TABLE_SIZE) I1 -= TABLE_SIZE;
56 float y0 = sine_table[I0];
57 float y1 = sine_table[I1];
59 float ans = (ptr-i0)*((y1-y0)/(i1-i0)) + y0;
60 return ans;
64 float normalize(float note){
65 /* original
66 -inf, -24 : 4
67 -24, -12: 2
68 -12, 24: 1
69 24,36: 0.5
70 36,48: 0.25
71 48, +inf: 0.125*/
72 const float min = -48;
73 const float max = 48;
74 const float left = 4;
75 const float right = 0.125;
77 if(note < min) return left;
78 if(note > max) return right;
80 float ans = (note - min)*((right-left) / (max-min)) + left;
81 if(ans > 4) return 4;
82 if(ans < 0.125) return 0.125;
83 return ans;
88 /* ORG_DEFAULT: default fallback instrument */
89 struct defstate {
90 int on[16];
91 int note[16];
92 float step[16];
93 float ptr[16];
94 float release[16];
95 float bendstep[16];
96 float bend;
99 void default_gen(struct defstate* data, int z, float out[], int count){
100 if(data->on[z] == 0) return;
101 int i;
102 for(i=0; i<count; i++){
103 float step = data->step[z] + data->bendstep[z];
104 if(data->on[z] == 2){
105 data->release[z] *= RELEASE_RATE;
106 if(data->release[z] < 0.01){
107 data->on[z] = 0;
108 break;
112 float factor = normalize(data->note[z] + data->bend) * data->release[z];
113 //float amp = sine_table[data->ptr[z]];
114 float amp = sine_table_interp(data->ptr[z]);
115 out[i] += amp * factor;
116 data->ptr[z] += step;
117 while(data->ptr[z] >= TABLE_SIZE){
118 data->ptr[z] -= TABLE_SIZE;
120 while(data->ptr[z] < 0){
121 data->ptr[z] += TABLE_SIZE;
126 void default_mix(void* ud, float out[], int count){
127 struct defstate* data = ud;
128 int i;
129 for(i=0; i<16; i++){
130 default_gen(data, i, out, count);
135 void default_bend_gen(struct defstate* data, int i, float bend){
136 if(data->on[i] == 0) return;
137 int note = data->note[i];
138 data->bendstep[i] = note2tablestep(note + bend) - data->step[i];
141 void default_turn_on(struct defstate* data, int note){
142 float step = note2tablestep(note);
143 int i;
144 for(i=0; i<16; i++){
145 if(data->on[i]==0){
146 data->step[i] = step;
147 data->note[i] = note;
148 data->on[i] = 1;
149 data->ptr[i] = 0;
150 data->release[i] = 1.0;
151 default_bend_gen(data, i, data->bend);
152 return;
157 void default_turn_off(struct defstate* data, int note){
158 int i;
159 for(i=0; i<16; i++){
160 if(data->note[i] == note && data->on[i] == 1){
161 data->on[i] = 2;
162 return;
167 void default_bend(struct defstate* data, int amount){
168 int max = 16383;
169 int relative = amount - max/2;
170 float bend = 1.0*relative/max * 4;
171 int i;
172 data->bend = bend;
173 for(i=0; i<16; i++){
174 default_bend_gen(data, i, bend);
178 void default_control(void* ud, int type, int val1, int val2, int val){
179 struct defstate* data = ud;
180 switch(type){
181 case EV_NOTEON: default_turn_on(data, val1); break;
182 case EV_NOTEOFF: default_turn_off(data, val1); break;
183 case EV_PITCHBEND: default_bend(data, val); break;
187 void default_cleanup(void* data){ free(data); }
189 instrument make_default(){
190 instrument ins;
191 struct defstate* data = malloc(sizeof(struct defstate));
192 int i;
193 for(i=0; i<16; i++){
194 data->on[i] = 0;
195 data->note[i] = 0;
196 data->step[i] = 100;
197 data->ptr[i] = 0;
198 data->bendstep[i] = 0;
199 data->bend = 0;
201 ins.mix = default_mix;
202 ins.control = default_control;
203 ins.cleanup = default_cleanup;
204 ins.data = data;
205 return ins;
211 /* ORG_KARPLUS: karplus strong string synth */
213 #define KARF 1024
215 struct karplus {
216 float buf[KARF];
217 float ptr;
218 float length;
219 float note;
220 float bend;
221 float step;
222 float offset;
225 void karplus_mix(void* ud, float out[], int count){
226 int i;
227 struct karplus* data = ud;
228 for(i=0; i<count; i++){
229 int L = data->length;
230 int z1 = floor(data->ptr);
231 int z0 = z1-1;
232 if(z1 >= L) z1 -= L;
233 if(z1 < 0) z1 += L;
234 if(z0 >= L) z0 -= L;
235 if(z0 < 0) z0 += L;
236 //out[i] = linterp(data->buf, data->ptr) * 6;
237 out[i] = data->buf[z0] * 10;
238 data->buf[z1] = (data->buf[z1] + data->buf[z0]) / 2.015;
239 data->ptr += 1;
240 if(data->ptr >= L){
241 data->ptr -= L;
246 void karplus_control(void* ud, int type, int val1, int val2, int val){
247 struct karplus* data = ud;
248 int i;
249 float f, L;
250 switch(type){
251 case EV_NOTEON:
252 data->note = val1;
253 f = 440*(pow(2, val1/12.0));
254 L = SAMPLE_RATE * 1.0 / f;
255 data->length = L*2;
256 for(i=0; i<100; i++){
257 data->buf[i] += ((double)rand())/RAND_MAX - 0.5;
259 return;
260 // case EV_NOTEOFF: default_turn_off(data, val1); break;
261 // case EV_PITCHBEND: default_bend(data, val); break;
266 void karplus_cleanup(void* data){ free(data); }
268 instrument make_karplus(){
269 instrument ins;
270 struct karplus* data = malloc(sizeof(struct karplus));
271 int i;
272 data->length = 512;
273 data->ptr = 0;
274 for(i=0; i<KARF; i++){
275 data->buf[i] = 0;
277 ins.mix = karplus_mix;
278 ins.control = karplus_control;
279 ins.cleanup = karplus_cleanup;
280 ins.data = data;
281 return ins;
287 /*** exported methods ***/
288 instrument load_instrument(enum instrument_name name){
289 switch(name){
290 case ORG_DEFAULT: return make_default();
291 case ORG_KARPLUS: return make_karplus();
292 // case ORG_COOL: return make_cool();
293 default: return make_default();
298 void org_init(){
299 int i;
300 for(i=0; i<TABLE_SIZE; i++){
301 sine_table[i] = sin( (PI2*i) / TABLE_SIZE);