Put the instruments in org.c
[cantaveria.git] / org.c
blob7163105052c0db48b2bc53ddcdc39f8cf6128498
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 note2f(int note){
29 return 440*3.14159*2*pow(2, note/12.0)/SAMPLE_RATE;
32 struct foo {
33 int on;
34 float t;
35 float f;
38 void foo_mix(void* data, float out[], int count){
39 struct foo* foo = data;
40 int i;
42 if(foo->on == 0) return;
44 for(i=0; i<count; i++){
45 out[i] += sin(foo->t);
46 foo->t += foo->f;
47 while(foo->t > 2*3.14159){
48 foo->t -= 2*3.14159;
53 void foo_control(void* data, int type, int val1, int val2, int val){
54 struct foo* foo = data;
55 switch(type){
56 case 0: foo->on = 1; break;
57 case 2: foo->on = 0; break;
58 case 1: foo->f = note2f(val1); break;
62 void foo_cleanup(void* data){ free(data); }
64 instrument make_foo(){
65 instrument ins;
66 ins.mix = foo_mix;
67 ins.control = foo_control;
68 ins.cleanup = foo_cleanup;
69 ins.data = malloc(sizeof(struct foo));
70 return ins;
73 instrument load_instrument(enum instrument_name name){
74 switch(name){
75 case ORG_FOO: return make_foo();
76 case ORG_COOL: return make_cool();
77 default: return make_foo();