Tile favorites.
[cantaveria.git] / orc.c
bloba28864db5bb53e7965effe424ced8dec9400e6bb
1 /*
2 orc.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 <orc.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)
33 /* SINE TABLE */
34 static float SAMPLE_RATE = 44100;
36 #define TABLE_SIZE (1<<8)
37 #define RELEASE_STEPS (SAMPLE_RATE/333)
38 #define RELEASE_RATE 0.999
41 float sine_table[TABLE_SIZE];
43 float note2tablestep(float note){
44 /* critical formula for tuning a table */
45 /* table steps for 2pif/SAMPLE_RATE radians is...*/
46 /* TABLE_SIZE/PI2 == entries per radian */
47 /* step == 2pif/SAMPLE_RATE * (TABLE_SIZE/PI2) */
48 /* == f * TABLE_SIZE / SAMPLE_RATE */
49 /* == f_0 2^(note/12) TABLE_SIZE / SAMPLE_RATE */
50 float f = 440 * pow(2, note/12.0);
51 return f*TABLE_SIZE / SAMPLE_RATE;
54 float sine_table_interp(float ptr){
55 float i0 = floor(ptr);
56 float i1 = i0+1;
57 int I0 = i0;
58 int I1 = i1;
59 if(I0 < 0) I0 += TABLE_SIZE;
60 if(I1 >= TABLE_SIZE) I1 -= TABLE_SIZE;
61 float y0 = sine_table[I0];
62 float y1 = sine_table[I1];
64 float ans = (ptr-i0)*((y1-y0)/(i1-i0)) + y0;
65 return ans;
69 float normalize(float note){
70 /* original
71 -inf, -24 : 4
72 -24, -12: 2
73 -12, 24: 1
74 24,36: 0.5
75 36,48: 0.25
76 48, +inf: 0.125*/
77 const float min = -48;
78 const float max = 48;
79 const float left = 4;
80 const float right = 0.125;
82 if(note < min) return left;
83 if(note > max) return right;
85 float ans = (note - min)*((right-left) / (max-min)) + left;
86 if(ans > 4) return 4;
87 if(ans < 0.125) return 0.125;
88 return ans;
93 /* ORG_DEFAULT: default fallback instrument */
94 struct defstate {
95 int on[16];
96 int note[16];
97 float step[16];
98 float ptr[16];
99 float release[16];
100 float bendstep[16];
101 float bend;
104 void default_gen(struct defstate* data, int z, float out[], int count){
105 if(data->on[z] == 0) return;
106 int i;
107 for(i=0; i<count; i++){
108 float step = data->step[z] + data->bendstep[z];
109 if(data->on[z] == 2){
110 data->release[z] *= RELEASE_RATE;
111 if(data->release[z] < 0.01){
112 data->on[z] = 0;
113 break;
117 // float factor = normalize(data->note[z] + data->bend) * data->release[z];
118 //float amp = sine_table[data->ptr[z]];
119 float amp = sine_table_interp(data->ptr[z]);
120 // out[i] += amp * factor;
121 out[i] += 13*(amp / 16.0 * data->release[z]);
122 data->ptr[z] += step;
123 while(data->ptr[z] >= TABLE_SIZE){
124 data->ptr[z] -= TABLE_SIZE;
126 while(data->ptr[z] < 0){
127 data->ptr[z] += TABLE_SIZE;
132 void default_mix(void* ud, float out[], int count){
133 struct defstate* data = ud;
134 int i;
135 for(i=0; i<16; i++){
136 default_gen(data, i, out, count);
141 void default_bend_gen(struct defstate* data, int i, float bend){
142 if(data->on[i] == 0) return;
143 int note = data->note[i];
144 data->bendstep[i] = note2tablestep(note + bend) - data->step[i];
147 void default_turn_on(struct defstate* data, int note){
148 float step = note2tablestep(note);
149 int i;
150 for(i=0; i<16; i++){
151 if(data->on[i]==0){
152 data->step[i] = step;
153 data->note[i] = note;
154 data->on[i] = 1;
155 data->ptr[i] = 0;
156 data->release[i] = 1.0;
157 default_bend_gen(data, i, data->bend);
158 return;
163 void default_turn_off(struct defstate* data, int note){
164 int i;
165 for(i=0; i<16; i++){
166 if(data->note[i] == note && data->on[i] == 1){
167 data->on[i] = 2;
168 return;
173 void default_bend(struct defstate* data, int amount){
174 int max = 16383;
175 int relative = amount - max/2;
176 float bend = 1.0*relative/max * 4;
177 int i;
178 data->bend = bend;
179 for(i=0; i<16; i++){
180 default_bend_gen(data, i, bend);
184 void default_control(void* ud, int type, int val1, int val2, int val){
185 struct defstate* data = ud;
186 switch(type){
187 case EV_NOTEON: default_turn_on(data, val1); break;
188 case EV_NOTEOFF: default_turn_off(data, val1); break;
189 case EV_PITCHBEND: default_bend(data, val); break;
193 void default_cleanup(void* data){ free(data); }
195 instrument make_default(){
196 instrument ins;
197 struct defstate* data = malloc(sizeof(struct defstate));
198 int i;
199 for(i=0; i<16; i++){
200 data->on[i] = 0;
201 data->note[i] = 0;
202 data->step[i] = 100;
203 data->ptr[i] = 0;
204 data->bendstep[i] = 0;
205 data->bend = 0;
207 ins.mix = default_mix;
208 ins.control = default_control;
209 ins.cleanup = default_cleanup;
210 ins.data = data;
211 return ins;
217 /* ORG_KARPLUS: karplus strong string synth */
219 #define KARF 1024
221 struct karplus {
222 float buf[KARF];
223 float ptr;
224 float length;
225 float note;
226 float bend;
227 float step;
228 float offset;
231 void karplus_mix(void* ud, float out[], int count){
232 int i;
233 struct karplus* data = ud;
234 for(i=0; i<count; i++){
235 int L = data->length;
236 int z1 = floor(data->ptr);
237 int z0 = z1-1;
238 if(z1 >= L) z1 -= L;
239 if(z1 < 0) z1 += L;
240 if(z0 >= L) z0 -= L;
241 if(z0 < 0) z0 += L;
242 //out[i] = linterp(data->buf, data->ptr) * 6;
243 out[i] = data->buf[z0] * 10;
244 data->buf[z1] = (data->buf[z1] + data->buf[z0]) / 2.015;
245 data->ptr += 1;
246 if(data->ptr >= L){
247 data->ptr -= L;
252 void karplus_control(void* ud, int type, int val1, int val2, int val){
253 struct karplus* data = ud;
254 int i;
255 float f, L;
256 switch(type){
257 case EV_NOTEON:
258 data->note = val1;
259 f = 440*(pow(2, val1/12.0));
260 L = SAMPLE_RATE * 1.0 / f;
261 data->length = L*2;
262 for(i=0; i<100; i++){
263 data->buf[i] += ((double)rand())/RAND_MAX - 0.5;
265 return;
266 case EV_NOTEOFF:
267 for(i=0; i<KARF; i++){
268 data->buf[i] = 0;
270 return;
271 // case EV_PITCHBEND: default_bend(data, val); break;
276 void karplus_cleanup(void* data){ free(data); }
278 instrument make_karplus(){
279 instrument ins;
280 struct karplus* data = malloc(sizeof(struct karplus));
281 int i;
282 data->length = 512;
283 data->ptr = 0;
284 for(i=0; i<KARF; i++){
285 data->buf[i] = 0;
287 ins.mix = karplus_mix;
288 ins.control = karplus_control;
289 ins.cleanup = karplus_cleanup;
290 ins.data = data;
291 return ins;
296 /*** exported methods ***/
297 instrument orc_load(enum instrument_name name){
298 switch(name){
299 case ORC_DEFAULT: return make_default();
300 case ORC_KARPLUS: return make_karplus();
301 // case ORC_COOL: return make_cool();
302 default: return make_default();
307 void orc_init(int sample_rate){
308 int i;
309 SAMPLE_RATE = sample_rate;
310 for(i=0; i<TABLE_SIZE; i++){
311 sine_table[i] = sin( (PI2*i) / TABLE_SIZE);