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.
18 heres a list of instrument ideas...
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 #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
);
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
;
64 float normalize(float note
){
72 const float min
= -48;
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
;
82 if(ans
< 0.125) return 0.125;
88 /* ORG_DEFAULT: default fallback instrument */
99 void default_gen(struct defstate
* data
, int z
, float out
[], int count
){
100 if(data
->on
[z
] == 0) return;
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){
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
;
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
);
147 data
->step
[i
] = step
;
148 data
->note
[i
] = note
;
151 data
->release
[i
] = 1.0;
152 default_bend_gen(data
, i
, data
->bend
);
158 void default_turn_off(struct defstate
* data
, int note
){
161 if(data
->note
[i
] == note
&& data
->on
[i
] == 1){
168 void default_bend(struct defstate
* data
, int amount
){
170 int relative
= amount
- max
/2;
171 float bend
= 1.0*relative
/max
* 4;
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
;
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(){
192 struct defstate
* data
= malloc(sizeof(struct defstate
));
199 data
->bendstep
[i
] = 0;
202 ins
.mix
= default_mix
;
203 ins
.control
= default_control
;
204 ins
.cleanup
= default_cleanup
;
212 /* ORG_KARPLUS: karplus strong string synth */
226 void karplus_mix(void* ud
, float out
[], int count
){
228 struct karplus
* data
= ud
;
229 for(i
=0; i
<count
; i
++){
230 int L
= data
->length
;
231 int z1
= floor(data
->ptr
);
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;
247 void karplus_control(void* ud
, int type
, int val1
, int val2
, int val
){
248 struct karplus
* data
= ud
;
254 f
= 440*(pow(2, val1
/12.0));
255 L
= SAMPLE_RATE
* 1.0 / f
;
257 for(i
=0; i
<100; i
++){
258 data
->buf
[i
] += ((double)rand())/RAND_MAX
- 0.5;
262 for(i
=0; i
<KARF
; i
++){
266 // case EV_PITCHBEND: default_bend(data, val); break;
271 void karplus_cleanup(void* data
){ free(data
); }
273 instrument
make_karplus(){
275 struct karplus
* data
= malloc(sizeof(struct karplus
));
279 for(i
=0; i
<KARF
; i
++){
282 ins
.mix
= karplus_mix
;
283 ins
.control
= karplus_control
;
284 ins
.cleanup
= karplus_cleanup
;
291 /*** exported methods ***/
292 instrument
load_instrument(enum instrument_name 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();
304 for(i
=0; i
<TABLE_SIZE
; i
++){
305 sine_table
[i
] = sin( (PI2
*i
) / TABLE_SIZE
);