Defined PI and PI2 (two times PI) in org.h
[cantaveria.git] / org.c
blob7d0a9d1ab95ec682569aa4a3cdc83ea2f635a6c5
1 /*
2 org.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 <audio.h> /* to have SAMPLE_RATE */
16 #include <org.h>
19 instrument make_cool() {
20 instrument ins;
21 ins.mix = NULL;
22 ins.control = NULL;
23 ins.cleanup = NULL;
24 ins.data = NULL;
25 return ins;
28 float note2step(int note){
29 /* critical formula for tuning */
30 /* delta(omega*t) == wave increment */
31 /* == 2 pi f / samp_rate */
32 /* == 2pi f_0 2^(note/12) / samp_rate */
33 return 440*PI2*pow(2, note/12.0)/SAMPLE_RATE;
36 struct foo {
37 int on;
38 float t;
39 float step;
42 void foo_mix(void* data, float out[], int count){
43 struct foo* foo = data;
44 int i;
46 if(foo->on == 0) return;
48 for(i=0; i<count; i++){
49 out[i] += sin(foo->t);
50 foo->t += foo->step;
51 while(foo->t > PI2){
52 foo->t -= PI2;
57 void foo_control(void* data, int type, int val1, int val2, int val){
58 struct foo* foo = data;
59 switch(type){
60 case EV_NOTEON:
61 foo->step = note2step(val1);
62 foo->on = 1;
63 break;
64 case EV_NOTEOFF:
65 if(foo->step == note2step(val1)){
66 foo->on = 0;
68 break;
72 void foo_cleanup(void* data){ free(data); }
74 instrument make_foo(){
75 instrument ins;
76 ins.mix = foo_mix;
77 ins.control = foo_control;
78 ins.cleanup = foo_cleanup;
79 ins.data = malloc(sizeof(struct foo));
80 return ins;
83 instrument load_instrument(enum instrument_name name){
84 switch(name){
85 case ORG_FOO: return make_foo();
86 case ORG_COOL: return make_cool();
87 default: return make_foo();