New lfo() test function.
[ahxm.git] / ss_gen.c
blobb0c02f8f4c0be4acf0ced366ede61f9892fe5879
1 /*
3 PROGRAM_NAME PROGRAM_VERSION - PROGRAM_DESCRIPTION
5 Copyright (C) 2003 Angel Ortega <angel@triptico.com>
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 http://www.triptico.com
25 #include "config.h"
27 #include "core.h"
28 #include "generator.h"
30 /*******************
31 Data
32 ********************/
34 /*******************
35 Code
36 ********************/
38 int generator_play(struct generator * g, int * lwave, int * rwave,
39 int size, double inc, int loop_start, int loop_end,
40 double lvol, double rvol, int sustain)
42 if(g->mode != GEN_MODE_FREE) return(0);
44 g->mode=GEN_MODE_PLAYING;
45 g->lwave=lwave;
46 g->rwave=rwave;
47 g->size=size;
48 g->loop_start=(double) loop_start;
49 g->loop_end=(double) loop_end;
50 g->inc=inc;
51 g->lvol=lvol;
52 g->rvol=rvol;
53 g->sustain=sustain;
55 g->cursor=0;
57 return(1);
61 int generator_release(struct generator * g)
63 /* if free or already released, fail */
64 if(g->mode == GEN_MODE_FREE ||
65 g->mode == GEN_MODE_RELEASED)
66 return(0);
68 /* if stopped or no sustain, become free immediately */
69 if(g->mode == GEN_MODE_STOPPED || g->sustain == 0)
71 g->mode=GEN_MODE_FREE;
72 return(1);
75 /* generator is released */
76 g->mode=GEN_MODE_RELEASED;
78 /* calculates delta volume values */
79 g->dlvol=g->lvol / (double) g->sustain;
80 g->drvol=g->rvol / (double) g->sustain;
82 /* put generator in auto-stop */
83 /* g->loop_start=-1; */
84 g->loop_end=(double) g->size;
86 return(1);
90 void generator(struct generator * g, int * left, int * right)
92 int l,r;
94 /* return if generator is not active */
95 if(g->mode==GEN_MODE_STOPPED || g->mode==GEN_MODE_FREE)
96 return;
98 /* get samples */
99 l=get_sample(g->lwave, g->loop_end, g->cursor);
100 r=get_sample(g->rwave, g->loop_end, g->cursor);
102 /* increment pointer */
103 g->cursor+=g->inc;
105 /* test boundaries */
106 if(g->cursor > g->loop_end)
108 /* auto-stop? */
109 if(g->loop_start == -1)
110 g->mode=GEN_MODE_STOPPED;
111 else
112 g->cursor=g->loop_start;
115 l=(int)((double)l * g->lvol);
116 r=(int)((double)r * g->rvol);
118 if(g->mode == GEN_MODE_RELEASED && g->sustain != -1)
120 /* sustain mode */
121 g->lvol -= g->dlvol;
122 g->rvol -= g->drvol;
124 if(--g->sustain == 0)
125 g->mode=GEN_MODE_FREE;
128 if(g->portamento)
130 if(--g->portamento == 0)
131 g->inc=g->dest_inc;
132 else
133 g->inc+=g->dinc;
136 *left+=l;
137 *right+=r;
141 int generator_portamento(struct generator * g, int portamento,
142 double dest_inc)
144 /* generator must be playing */
145 if(g->mode != GEN_MODE_PLAYING)
146 return(0);
148 g->portamento=portamento;
149 g->dest_inc=dest_inc;
150 g->dinc=((dest_inc - g->inc) / (double) portamento);
152 return(1);