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 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
;
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
);
146 data
->step
[i
] = step
;
147 data
->note
[i
] = note
;
150 data
->release
[i
] = 1.0;
151 default_bend_gen(data
, i
, data
->bend
);
157 void default_turn_off(struct defstate
* data
, int note
){
160 if(data
->note
[i
] == note
&& data
->on
[i
] == 1){
167 void default_bend(struct defstate
* data
, int amount
){
169 int relative
= amount
- max
/2;
170 float bend
= 1.0*relative
/max
* 4;
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
;
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(){
191 struct defstate
* data
= malloc(sizeof(struct defstate
));
198 data
->bendstep
[i
] = 0;
201 ins
.mix
= default_mix
;
202 ins
.control
= default_control
;
203 ins
.cleanup
= default_cleanup
;
211 /* ORG_KARPLUS: karplus strong string synth */
225 void karplus_mix(void* ud
, float out
[], int count
){
227 struct karplus
* data
= ud
;
228 for(i
=0; i
<count
; i
++){
229 int L
= data
->length
;
230 int z1
= floor(data
->ptr
);
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;
246 void karplus_control(void* ud
, int type
, int val1
, int val2
, int val
){
247 struct karplus
* data
= ud
;
253 f
= 440*(pow(2, val1
/12.0));
254 L
= SAMPLE_RATE
* 1.0 / f
;
256 for(i
=0; i
<100; i
++){
257 data
->buf
[i
] += ((double)rand())/RAND_MAX
- 0.5;
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(){
270 struct karplus
* data
= malloc(sizeof(struct karplus
));
274 for(i
=0; i
<KARF
; i
++){
277 ins
.mix
= karplus_mix
;
278 ins
.control
= karplus_control
;
279 ins
.cleanup
= karplus_cleanup
;
287 /*** exported methods ***/
288 instrument
load_instrument(enum instrument_name 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();
300 for(i
=0; i
<TABLE_SIZE
; i
++){
301 sine_table
[i
] = sin( (PI2
*i
) / TABLE_SIZE
);