Added processing for ss_eff_fader. Also, yacc code for fader effects
[ahxm.git] / ss_gen.c
blobba4662b05b42611b7000bd7236789ca3d0801ab8
1 /*
3 Ann Hell Ex Machina - Music Software
4 Copyright (C) 2003/2004 Angel Ortega <angel@triptico.com>
6 ss_gen.c - Software syntesizer's sound generators
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 http://www.triptico.com
26 #include "config.h"
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <math.h>
33 #include "core.h"
34 #include "ss_gen.h"
36 /*******************
37 Data
38 ********************/
40 /* number of generators */
41 int _ss_gen_num=SS_GEN_NUM;
43 /* generator pool */
44 struct ss_gen * _ss_gen_pool=NULL;
46 /* free generator queue */
47 struct ss_gen * _ss_gen_free=NULL;
50 /*******************
51 Code
52 ********************/
54 void ss_gen_enqueue(struct ss_gen ** q, struct ss_gen * g)
56 g->prev=NULL;
57 g->next=*q;
59 if(*q != NULL)
60 (*q)->prev=g;
62 *q=g;
66 void ss_gen_dequeue(struct ss_gen ** q, struct ss_gen * g)
68 if(g->prev != NULL)
69 g->prev->next=g->next;
70 else
71 *q=g->next;
73 if(g->next != NULL)
74 g->next->prev=g->prev;
78 struct ss_gen * ss_gen_dequeue_first(struct ss_gen ** q)
80 struct ss_gen * g=NULL;
82 if(*q != NULL)
84 g=*q;
85 ss_gen_dequeue(q, g);
88 return(g);
92 struct ss_gen * _ss_gen_get_free(void)
94 int n;
96 /* first time allocation */
97 if(_ss_gen_pool == NULL)
99 _ss_gen_pool=(struct ss_gen *) malloc(_ss_gen_num *
100 sizeof(struct ss_gen));
102 memset(_ss_gen_pool, '\0',
103 _ss_gen_num * sizeof(struct ss_gen));
105 /* enqueue all into the free generator queue */
106 for(n=0;n < _ss_gen_num;n++)
107 ss_gen_enqueue(&_ss_gen_free, &_ss_gen_pool[n]);
110 return(ss_gen_dequeue_first(&_ss_gen_free));
115 * ss_gen_play - Activates a generator.
116 * @note_id: note id
117 * @trk_id: track id
118 * @wave: wave samples for each channel
119 * @vol: volume for each channel
120 * @size: size of each wave in samples
121 * @inc: cursor increment (frequency)
122 * @loop_start: sample number of the start of the loop (-1, no loop)
123 * @loop_end: sample number of the end of the loop
124 * @sustain: number of samples the sound will fade out after release
126 * Activates a generator, usually as a response for a 'note on'
127 * message from an upper level. @wave sould contain pointers to PCM wave
128 * data. @inc is the cursor increment, and should be the result of the
129 * original wave's sample rate divided by the desired frequency.
130 * @loop_start and @loop_end should mark the start and end of
131 * a loop in the sound. If @loop_start is -1 and the sound cursor
132 * reaches the end of the sample, the generator stops automatically.
133 * if a loop is defined, the generator produces sound indefinitely until a
134 * call to ss_gen_release() is done.
136 * This function returns -1 if the pool went out of generators, or
137 * 0 if everything is OK and the generator entered GEN_MODE_PLAYING mode.
139 int ss_gen_play(struct ss_gen * g, int note_id, double freq, float vol[],
140 struct ss_wave * w)
142 int n;
144 g->mode=SS_GEN_PLAYING;
145 g->note_id=note_id;
147 memcpy(&g->w, w, sizeof(struct ss_wave));
149 /* transfer volumes */
150 for(n=0;n < CHANNELS;n++)
151 g->vol[n]=vol[n];
153 /* calculate increment */
154 g->inc=freq / w->base_freq;
155 g->inc *= (double) w->s_rate / (double) _frequency;
157 /* g->sustain=sustain; */
158 g->sustain=_frequency / 100;
160 /* no portamento by default */
161 g->portamento=0;
163 /* start from the beginning */
164 g->cursor=0;
166 g->vibrato=0;
168 /* TEST: vibrato */
169 /* good freq: 6 Hz */
170 /* good depth: 1/6 semitone */
171 #ifdef QQ
172 if(trk_id == 2)
174 g->vib_depth=30; /* 20 */
175 g->vib_inc=( 6.28 * 6.0 ) / (double)_frequency; /* 0.001 */
177 else
178 #endif
179 g->vib_inc=0.0;
181 return(0);
186 * ss_gen_release - Releases generators.
187 * @note_id: note id
189 * Releases the generators associated to @note_id, usually as a response
190 * for a 'note off' message from an upper level. The generator enters
191 * GEN_MODE_RELEASED mode.
193 * Returns the number of released generators.
195 int ss_gen_release(struct ss_gen * g)
197 int n;
199 /* note needs not be tracked anymore */
200 g->note_id=-1;
202 /* generator is released */
203 g->mode=SS_GEN_RELEASED;
205 /* calculates delta volume values */
206 for(n=0;n < CHANNELS;n++)
208 if(g->w.wave[n])
209 g->dvol[n]=g->vol[n] / (float) g->sustain;
210 else
211 g->dvol[n]=0.0;
214 return(1);
219 * ss_gen_frame - Generates a frame of samples.
220 * @trk_id: track id
221 * @frame: array where the output samples will be stored
223 * Generates a frame of samples for the track @trk_id, that will be stored
224 * in the @sample array. If the generator is in GEN_MODE_RELEASED mode,
225 * the sound should keep fading-out until the sustain time is exhausted and
226 * then moved to GEN_MODE_FREE mode; if the generator is playing and
227 * a non-looped sound reaches the end, it's immediately stopped.
228 * It also processes portamento.
230 int ss_gen_frame(struct ss_gen * g, float frame[])
232 float s;
233 int n;
234 int to_free=0;
235 double v;
237 /* process vibrato */
238 if(g->vib_inc)
240 g->vibrato += g->vib_inc;
241 v=sin(g->vibrato) * g->vib_depth;
243 else
244 v=0.0;
246 /* get samples */
247 for(n=0;n < CHANNELS;n++)
249 if(g->w.wave[n] != NULL)
251 s=get_sample(&g->w, n, g->cursor + v);
252 s *= g->vol[n];
254 frame[n] += s;
258 /* increment pointer */
259 g->cursor += g->inc;
261 /* test loop boundaries */
262 if(g->cursor > g->w.loop_end)
264 /* loop mode? */
265 if(g->w.loop_start == -1)
266 to_free=1;
267 else
268 g->cursor=g->w.loop_start;
271 /* process sustain */
272 if(g->mode == SS_GEN_RELEASED)
274 for(n=0;n < CHANNELS;n++)
275 g->vol[n] -= g->dvol[n];
277 if(--g->sustain <= 0)
278 to_free=1;
281 /* process portamento */
282 g->inc += g->portamento;
284 return(to_free);
289 * ss_gen_portamento - Sets portamento for a generator.
290 * @g: the generator
291 * @portamento: portamento time
292 * @dest_inc: destination inc (frequency)
294 * Sets the portamento for a generator. @portamento is the
295 * number of frames the generator should move from its current
296 * cursor increment to @dest_inc. See generator_play() for an
297 * explanation of the value contained in @dest_inc.
299 * Returns -1 if the generator is not active, or 0 if the
300 * portamento was accepted.
302 * FIXME: The API for portamento will surely change.
304 int ss_gen_portamento(struct ss_gen * g, int portamento, double dest_inc)
306 /* generator must be playing */
307 if(g->mode != SS_GEN_PLAYING)
308 return(-1);
310 g->portamento=portamento;
311 g->dest_inc=dest_inc;
312 g->dinc=((dest_inc - g->inc) / (double) portamento);
314 return(0);
318 void ss_gen_vibrato(struct ss_gen * g, double depth, double freq)
320 g->vib_depth=depth;
321 g->vib_inc=( 6.28 * freq ) / (double)_frequency;