2 orgux - a just-for-fun real time synth
3 Copyright (C) 2009 Evan Rinehart
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 /* precompute waveforms */
25 #include "constants.h"
27 extern float square_wave
[WAVEFORM_LENGTH
];
28 extern float saw_wave
[WAVEFORM_LENGTH
];
29 extern float triangle_wave
[WAVEFORM_LENGTH
];
31 extern const unsigned char instr_config
[128][16];
33 extern float waveform
[128][WAVEFORM_LENGTH
];
34 extern float samples
[SAMPLE_COUNT
][SAMPLE_LENGTH
];
35 extern float antipop
[ANTIPOP_LENGTH
];
37 float linterp(float x
, int k
, float wave
[]){
40 float Xw
= X
- WAVEFORM_LENGTH
*floor(X
/WAVEFORM_LENGTH
);
41 int X0
= (int)floor(Xw
);
43 if(X1
== WAVEFORM_LENGTH
) X1
= 0;
47 float m
= (y1
-y0
)/(X1
-X0
);
52 void precompute(int sample_rate
){
54 float dt
= 1.0/sample_rate
;
56 printf("generating band limited bases... ");
58 for(int i
=0; i
<WAVEFORM_LENGTH
; i
++){
62 for(int j
=0; j
<7; j
++){
63 square_wave
[i
] += (4/PI
)*sin(i
*(2*j
+1)*PI2
/WAVEFORM_LENGTH
)/(2*j
+1);
64 triangle_wave
[i
] += (8/(PI
*PI
))*(j
&1?1:-1)*sin(i
*(2*j
+1)*PI2
/WAVEFORM_LENGTH
)/((2*j
+1)*(2*j
+1));
65 saw_wave
[i
] += (2/PI
)*sin(i
*(j
+1)*PI2
/WAVEFORM_LENGTH
)/(j
+1);
70 //float maxf[3] = {0,0,0};
71 //for(int i=0; i<WAVEFORM_LENGTH; i++){
72 // if(maxf[0] < square_wave[i]) maxf[0] = square_wave[i];
73 // if(maxf[1] < triangle_wave[i]) maxf[1] = triangle_wave[i];
74 // if(maxf[2] < saw_wave[i]) maxf[2] = saw_wave[i];
77 for(int i
=0; i
<WAVEFORM_LENGTH
; i
++){
79 triangle_wave
[i
] /= 1;
83 //printf("\nsquare max = %f\n",maxf[0]);
84 //printf("triangle max = %f\n",maxf[1]);
85 //printf("saw max = %f\n\n",maxf[2]);
90 /* setup wave tables */
92 printf("generating instruments... \n");
94 printf(" harmonic 0 1 2 3 4 5 6 7 |basis FM FM_freq LP HP\n");
95 for(int i
=0; i
<128; i
++){
100 printf("instrument[%3d] = {",i
);
102 for(int j
=0; j
<HARMONIC_COUNT
; j
++){
103 printf("%3d ",instr_config
[i
][j
]);
104 if(instr_config
[i
][j
] > max
){ max
= instr_config
[i
][j
]; }
109 switch(instr_config
[i
][BASE_CONFIG
]){
110 case SINE_BASE
: printf("SINE "); break;
111 case SQUARE_BASE
: printf("PULSE "); break;
112 case SAW_BASE
: printf("SAW "); break;
113 case TRIANGLE_BASE
: printf("TRI "); break;
114 case NOISE_BASE
: printf("NOISE "); break;
115 default: printf("%5d ",instr_config
[i
][BASE_CONFIG
]); break;
118 for(int j
=0; j
<HARMONIC_COUNT
; j
++){
119 if(instr_config
[i
][j
]==0) continue;
120 float M
= ((float)instr_config
[i
][j
])/max
;
124 switch(instr_config
[i
][BASE_CONFIG
]){
126 for(int k
=0; k
<WAVEFORM_LENGTH
; k
++){
127 waveform
[i
][k
]+=M
*sin(k
*(j
+1)*PI2
/WAVEFORM_LENGTH
);
131 for(int k
=0; k
<WAVEFORM_LENGTH
; k
++){
132 waveform
[i
][k
]+=M
*linterp(k
,j
+1,square_wave
);;
136 for(int k
=0; k
<WAVEFORM_LENGTH
; k
++){
137 waveform
[i
][k
]+=M
*linterp(k
,j
+1,saw_wave
);;
141 for(int k
=0; k
<WAVEFORM_LENGTH
; k
++){
142 waveform
[i
][k
]+=M
*linterp(k
,j
+1,triangle_wave
);
150 for(int j
=0; j
<WAVEFORM_LENGTH
; j
++){
151 waveform
[i
][j
] /= N
*10;
156 for(int j=0; j<WAVEFORM_LENGTH; j++){
157 waveform[i][j] = 0.0f;
159 for(int k=0; k<HARMONIC_COUNT; k++){
160 float M = ((float)instr_config[i][k])/max;
165 switch(instr_config[i][BASE_CONFIG]){
166 case SINE_BASE: y = sin(j*(k+1)*PI2/WAVEFORM_LENGTH); break;
167 //case SQUARE_BASE: y = ((2*j*(1<<k)/WAVEFORM_LENGTH)&1)?1:-1; break;
168 case SQUARE_BASE: y = linterp(j,k+1,square_wave); break;
169 case TRIANGLE_BASE: y = linterp(j,k+1,triangle_wave); break;
170 case SAW_BASE: y = linterp(j,k+1,saw_wave); break;
171 //case NOISE_BASE: y = RANDF()*2 - 1; break;
172 case NOISE_BASE: y = 0; break;
173 default: y = 0;//use previously defined waveform
176 waveform[i][j] += M*y;
178 if(N>0){ waveform[i][j] /= N*10; }
188 //generate antipop cosine
189 for(int i
=0; i
<ANTIPOP_LENGTH
; i
++){
190 antipop
[i
] = -cos(i
*PI2
/(2.0*ANTIPOP_LENGTH
))/2.0+0.5;
196 float par
[16][8] = {/**/
197 {50, 500, 0.5, 10000, 0, 0, 0, 0},
198 {200, 200, 0.5, 10000, 0, 0, 0, 0},
199 {100, 500, 0.5, 10000, 0, 0, 0, 0},
200 {100, 1000, 0.5, 10000, 0, 0, 0, 0},
202 {100, 500, 0.5, 3000, 0, 0, 0, 0},
203 {200, 500, 0.5, 3000, 0, 0, 0, 0},
204 {300, 500, 0.5, 3000, 0, 0, 0, 0},
205 {400, 500, 0.5, 3000, 0, 0, 0, 0},
207 {400, 200, 0.5, 3000, 0, 0, 0, 0},
208 {400, 100, 0.5, 3000, 0, 0, 0, 0},
209 {400, 50, 0.5, 3000, 0, 0, 0, 0},
210 {400, 20, 0.5, 3000, 0, 0, 0, 0},
212 {100, 500, 0.5, 10000, 0, 0, 0, 0},
213 {100, 500, 0.5, 10000, 0, 0, 0, 0},
214 {100, 500, 0.5, 10000, 0, 0, 0, 0},
215 {100, 500, 0.5, 10000, 0, 0, 0, 0}
218 double K
= PI2
/ sample_rate
;
219 printf("generating samples... ");
220 for(int i
=0; i
<SAMPLE_LENGTH
; i
++){
221 for(int j
=0; j
<SAMPLE_COUNT
; j
++){
222 double F_i
= par
[j
][0];
223 double F_r
= par
[j
][1];
224 double A_i
= par
[j
][2];
225 double A_r
= par
[j
][3];
227 if(F_r
< 1){F_r
= 1;}
228 double f
= F_i
- i
/F_r
;
231 if(A_r
< 1){A_r
= 1;}
232 double A
= A_i
- i
/A_r
;
237 E
= -cos((i
/64.0) * PI2
/ 2)/2+0.5;
240 samples
[j
][i
] = E
*A
*sin(i
*f
*K
);
254 tau2 = 0.0001 / (1 + i/50.0)
255 alpha2 = tau2 / (tau2 + dt)
256 A = 0.2 - (i/10000.0)
264 for(int i
=0; i
<SAMPLE_LENGTH
; i
++){
266 float tau1
= (i
/200.0)*0.0001;
267 float alpha1
= dt
/(tau1
+dt
);
269 float tau2
= 0.0001 / (1 + i
/50.0);
270 float alpha2
= tau2
/(tau2
+dt
);
272 float y
= RAND_SIGNAL();
274 //y = y_l0 + alpha1*(y - y_l0);
278 y
= alpha2
*(y
- y_h0
+ y_h1
);
282 float A
= 0.2 - (i
/10000.0);
287 E
= -cos((i
/64.0) * PI2
/ 2)/2+0.5;
290 samples
[15][i
] = A
*E
*y
;
295 printf("precompute OK\n");