Fixed a bug in list pop, again.
[cantaveria.git] / synth.c
blobd2f2ed32594ccb7a29300ea106d0e7337d295cbc
1 /*
2 Cantaveria - action adventure platform game
3 Copyright (C) 2009 2010 Evan Rinehart
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to
18 The Free Software Foundation, Inc.
19 51 Franklin Street, Fifth Floor
20 Boston, MA 02110-1301, USA
22 evanrinehart@gmail.com
25 #include <synth.h>
26 #include <seq.h>
29 timing stuff
31 ticks - 384 per beat (tpb)
32 beats - 120 per minute (bpm)
33 samples - 22050 per second (srate)
35 control happens on tick boundaries
36 control must take effect on nearest sample boundary
38 samples per tick = (srate*60) / (bpm*tpb)
39 which in the above example = 28 and 32760/46080
44 int srate;
45 int bpm = 120;
46 int tpb = 384;
48 int serr = 0; // 1/1000 of a sample
50 int tick;
51 int terr = 0; // 1/(bpm*tpb) of a sample
52 int terrd = 46080; //bpm * tpb
54 void set_sample_rate(int x){ srate = x; }
55 void set_bpm(int x){ bpm = x; }
58 void synth_init(int sample_rate){
59 srate = sample_rate;
60 seq_init();
63 void generate(float left[], float right[], int count){
64 int i;
66 /* supposed to mix all generators for count samples
67 this includes synthesizers and sample generators */
68 /* this loop is unsuitable as an inner loop */
69 for(i=0; i<count; i++){
70 left[i] = 0;
71 right[i] = 0;
75 /* instead zero them and pass them to each generator
76 which will accumulate their output into left and right
78 in the end divide by maximum number of generators
80 the final stage is master volume, which will be increased if
81 the result is too quiet */
84 void control(event* e){
85 /* we decided on polyphonic synth (and multiple instances of
86 a playing samlple). important to remember that all controller
87 events affect entire instrument channels. this means it affects
88 all generators for that intrument */
91 void synth_generate(float left[], float right[], int samples){
92 int i=0;
93 for(;;){
94 int next = seq_lookahead(samples);
95 if(next < 0) break;
96 generate(left+i, right+i, next-i);
97 control(seq_get_event());
98 i = next;
100 generate(left+i, right+i, samples-i);
101 seq_advance(samples);
107 /* possible generators (scratch work)
109 square wave
110 saw wave
111 triangle wave
112 square saw produce a band limited signal
114 string signal
115 this uses a karplus strong algorithm
116 noise -> variable delay (linear interpolating) -> first order low pass
118 voice signal
119 formant synthesis
120 two or three gaussians (perhaps padded) -> IFFT
121 also works for bell
123 detuned oscillators
125 samples on ch 10
126 just samples
128 effects
129 portamento
130 vibrato
131 echo
132 chorus
133 reverb
134 appegiator
135 adsr envelope