Fixed note off bug in default instrument. 'Fixed' volume.
[cantaveria.git] / org.c
blob69233df0bf93982110527669988de33711095337
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 out[i] += amp / 16.0 * data->release[z];
117 data->ptr[z] += step;
118 while(data->ptr[z] >= TABLE_SIZE){
119 data->ptr[z] -= TABLE_SIZE;
121 while(data->ptr[z] < 0){
122 data->ptr[z] += TABLE_SIZE;
127 void default_mix(void* ud, float out[], int count){
128 struct defstate* data = ud;
129 int i;
130 for(i=0; i<16; i++){
131 default_gen(data, i, out, count);
136 void default_bend_gen(struct defstate* data, int i, float bend){
137 if(data->on[i] == 0) return;
138 int note = data->note[i];
139 data->bendstep[i] = note2tablestep(note + bend) - data->step[i];
142 void default_turn_on(struct defstate* data, int note){
143 float step = note2tablestep(note);
144 int i;
145 for(i=0; i<16; i++){
146 if(data->on[i]==0){
147 data->step[i] = step;
148 data->note[i] = note;
149 data->on[i] = 1;
150 data->ptr[i] = 0;
151 data->release[i] = 1.0;
152 default_bend_gen(data, i, data->bend);
153 return;
158 void default_turn_off(struct defstate* data, int note){
159 int i;
160 for(i=0; i<16; i++){
161 if(data->note[i] == note && data->on[i] == 1){
162 data->on[i] = 2;
163 return;
168 void default_bend(struct defstate* data, int amount){
169 int max = 16383;
170 int relative = amount - max/2;
171 float bend = 1.0*relative/max * 4;
172 int i;
173 data->bend = bend;
174 for(i=0; i<16; i++){
175 default_bend_gen(data, i, bend);
179 void default_control(void* ud, int type, int val1, int val2, int val){
180 struct defstate* data = ud;
181 switch(type){
182 case EV_NOTEON: default_turn_on(data, val1); break;
183 case EV_NOTEOFF: default_turn_off(data, val1); break;
184 case EV_PITCHBEND: default_bend(data, val); break;
188 void default_cleanup(void* data){ free(data); }
190 instrument make_default(){
191 instrument ins;
192 struct defstate* data = malloc(sizeof(struct defstate));
193 int i;
194 for(i=0; i<16; i++){
195 data->on[i] = 0;
196 data->note[i] = 0;
197 data->step[i] = 100;
198 data->ptr[i] = 0;
199 data->bendstep[i] = 0;
200 data->bend = 0;
202 ins.mix = default_mix;
203 ins.control = default_control;
204 ins.cleanup = default_cleanup;
205 ins.data = data;
206 return ins;
212 /* ORG_KARPLUS: karplus strong string synth */
214 #define KARF 1024
216 struct karplus {
217 float buf[KARF];
218 float ptr;
219 float length;
220 float note;
221 float bend;
222 float step;
223 float offset;
226 void karplus_mix(void* ud, float out[], int count){
227 int i;
228 struct karplus* data = ud;
229 for(i=0; i<count; i++){
230 int L = data->length;
231 int z1 = floor(data->ptr);
232 int z0 = z1-1;
233 if(z1 >= L) z1 -= L;
234 if(z1 < 0) z1 += L;
235 if(z0 >= L) z0 -= L;
236 if(z0 < 0) z0 += L;
237 //out[i] = linterp(data->buf, data->ptr) * 6;
238 out[i] = data->buf[z0] * 10;
239 data->buf[z1] = (data->buf[z1] + data->buf[z0]) / 2.015;
240 data->ptr += 1;
241 if(data->ptr >= L){
242 data->ptr -= L;
247 void karplus_control(void* ud, int type, int val1, int val2, int val){
248 struct karplus* data = ud;
249 int i;
250 float f, L;
251 switch(type){
252 case EV_NOTEON:
253 data->note = val1;
254 f = 440*(pow(2, val1/12.0));
255 L = SAMPLE_RATE * 1.0 / f;
256 data->length = L*2;
257 for(i=0; i<100; i++){
258 data->buf[i] += ((double)rand())/RAND_MAX - 0.5;
260 return;
261 case EV_NOTEOFF:
262 for(i=0; i<KARF; i++){
263 data->buf[i] = 0;
265 return;
266 // case EV_PITCHBEND: default_bend(data, val); break;
271 void karplus_cleanup(void* data){ free(data); }
273 instrument make_karplus(){
274 instrument ins;
275 struct karplus* data = malloc(sizeof(struct karplus));
276 int i;
277 data->length = 512;
278 data->ptr = 0;
279 for(i=0; i<KARF; i++){
280 data->buf[i] = 0;
282 ins.mix = karplus_mix;
283 ins.control = karplus_control;
284 ins.cleanup = karplus_cleanup;
285 ins.data = data;
286 return ins;
291 /*** exported methods ***/
292 instrument load_instrument(enum instrument_name name){
293 switch(name){
294 case ORG_DEFAULT: return make_default();
295 case ORG_KARPLUS: return make_karplus();
296 // case ORG_COOL: return make_cool();
297 default: return make_default();
302 void org_init(){
303 int i;
304 for(i=0; i<TABLE_SIZE; i++){
305 sine_table[i] = sin( (PI2*i) / TABLE_SIZE);