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)
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
);
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
;
69 float normalize(float note
){
77 const float min
= -48;
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
;
87 if(ans
< 0.125) return 0.125;
93 /* ORG_DEFAULT: default fallback instrument */
104 void default_gen(struct defstate
* data
, int z
, float out
[], int count
){
105 if(data
->on
[z
] == 0) return;
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){
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
;
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
);
152 data
->step
[i
] = step
;
153 data
->note
[i
] = note
;
156 data
->release
[i
] = 1.0;
157 default_bend_gen(data
, i
, data
->bend
);
163 void default_turn_off(struct defstate
* data
, int note
){
166 if(data
->note
[i
] == note
&& data
->on
[i
] == 1){
173 void default_bend(struct defstate
* data
, int amount
){
175 int relative
= amount
- max
/2;
176 float bend
= 1.0*relative
/max
* 4;
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
;
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(){
197 struct defstate
* data
= malloc(sizeof(struct defstate
));
204 data
->bendstep
[i
] = 0;
207 ins
.mix
= default_mix
;
208 ins
.control
= default_control
;
209 ins
.cleanup
= default_cleanup
;
217 /* ORG_KARPLUS: karplus strong string synth */
231 void karplus_mix(void* ud
, float out
[], int count
){
233 struct karplus
* data
= ud
;
234 for(i
=0; i
<count
; i
++){
235 int L
= data
->length
;
236 int z1
= floor(data
->ptr
);
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;
252 void karplus_control(void* ud
, int type
, int val1
, int val2
, int val
){
253 struct karplus
* data
= ud
;
259 f
= 440*(pow(2, val1
/12.0));
260 L
= SAMPLE_RATE
* 1.0 / f
;
262 for(i
=0; i
<100; i
++){
263 data
->buf
[i
] += ((double)rand())/RAND_MAX
- 0.5;
267 for(i
=0; i
<KARF
; i
++){
271 // case EV_PITCHBEND: default_bend(data, val); break;
276 void karplus_cleanup(void* data
){ free(data
); }
278 instrument
make_karplus(){
280 struct karplus
* data
= malloc(sizeof(struct karplus
));
284 for(i
=0; i
<KARF
; i
++){
287 ins
.mix
= karplus_mix
;
288 ins
.control
= karplus_control
;
289 ins
.cleanup
= karplus_cleanup
;
296 /*** exported methods ***/
297 instrument
orc_load(enum instrument_name 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
){
309 SAMPLE_RATE
= sample_rate
;
310 for(i
=0; i
<TABLE_SIZE
; i
++){
311 sine_table
[i
] = sin( (PI2
*i
) / TABLE_SIZE
);