3 Ann Hell Ex Machina - Music Software
4 Copyright (C) 2003/2005 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
39 /* maximum number of generators */
40 int ss_gen_num
=SS_MAX_GENERATORS
;
43 static struct ss_gen
* ss_gen_pool
=NULL
;
45 /* free generator queue */
46 static struct ss_gen
* ss_gen_free_queue
=NULL
;
53 static void ss_gen_enqueue(struct ss_gen
** q
, struct ss_gen
* g
)
54 /* Enqueues a generator in a generator queue */
66 static struct ss_gen
* ss_gen_dequeue(struct ss_gen
** q
, struct ss_gen
* g
)
67 /* Dequeues a generator from a generator queue */
70 g
->prev
->next
=g
->next
;
75 g
->next
->prev
=g
->prev
;
81 static struct ss_gen
* ss_gen_pop(struct ss_gen
** q
)
82 /* gets the first enqueued generator from q */
84 struct ss_gen
* g
=NULL
;
87 g
=ss_gen_dequeue(q
, *q
);
94 * ss_gen_alloc - Allocs and enqueues a generator
95 * @q: queue where the generator will be enqueued
97 * Allocs and enqueues a generator into the @q queue.
99 * The new generator is returned, or NULL if the
100 * generator pool is empty.
102 struct ss_gen
* ss_gen_alloc(struct ss_gen
** q
)
107 /* first time allocation */
108 if(ss_gen_pool
== NULL
)
110 ss_gen_pool
=(struct ss_gen
*) malloc(ss_gen_num
*
111 sizeof(struct ss_gen
));
113 memset(ss_gen_pool
, '\0',
114 ss_gen_num
* sizeof(struct ss_gen
));
116 /* enqueue all into the free generator queue */
117 for(n
=0;n
< ss_gen_num
;n
++)
118 ss_gen_enqueue(&ss_gen_free_queue
, &ss_gen_pool
[n
]);
121 if((g
=ss_gen_pop(&ss_gen_free_queue
)) != NULL
)
122 ss_gen_enqueue(q
, g
);
129 * ss_gen_free - Dequeues a generator and frees it
130 * @q: the queue holding the generator
133 * Dequeues a generator and sends it back to the generator pool.
135 void ss_gen_free(struct ss_gen
** q
, struct ss_gen
* g
)
137 ss_gen_enqueue(&ss_gen_free_queue
, ss_gen_dequeue(q
, g
));
142 * ss_gen_sustain - Sets sustain for a generator
144 * @sustain: sustain time in msecs
146 * Sets sustain for a generator, where @sustain is expressed
149 void ss_gen_sustain(struct ss_gen
* g
, double sustain
)
151 g
->sustain
=MS2F(sustain
);
156 * ss_gen_vibrato - Sets vibrato for a generator
158 * @depth: vibrato depth in msecs
159 * @freq: vibrato frequency
161 * Sets vibrato for a generator, with a @depth expressed in
162 * milliseconds and a frequency @freq expressed in hzs.
164 void ss_gen_vibrato(struct ss_gen
* g
, double depth
, double freq
)
166 /* good freq: 6 Hz (0.001) */
167 /* good depth: 1/6 semitone (20, 30 frames) */
169 g
->vib_depth
=MS2F(depth
);
170 g
->vib_inc
=( 6.28 * freq
) / (double)ss_frequency
;
176 * ss_gen_play - Activates a generator.
178 * @freq: frequency of the sound to be generated
183 * Activates a generator, usually as a response for a 'note on'
184 * message from an upper level. The wave @w holds all the sample
185 * data (PCM data, base frequency, etc.), @freq is the desired
186 * frequency, @vol the volume and @note_id a positive, unique
187 * identifier for the note.
189 void ss_gen_play(struct ss_gen
* g
, double freq
, sample_t vol
, int note_id
,
197 /* start from the beginning */
200 /* calculate increment */
201 g
->inc
=freq
/ w
->base_freq
;
202 g
->inc
*= (double) w
->s_rate
/ (double) ss_frequency
;
204 /* default sustain, vibrato and portamento */
205 ss_gen_sustain(g
, 50.0);
206 ss_gen_vibrato(g
, 0.0, 0.0);
212 * ss_gen_release - Releases a generator.
215 * Releases a generator, usually as a response for a 'note off'
216 * message from an upper level. The generator enters SS_GEN_RELEASED
217 * mode, which starts generating sustain data until it's over.
219 void ss_gen_release(struct ss_gen
* g
)
221 /* note needs not be tracked anymore */
224 /* calculates the delta volume */
225 g
->dvol
=g
->vol
/ (sample_t
) g
->sustain
;
230 * ss_gen_frame - Generates a frame of samples.
232 * @n_channels: the desired number of channels
233 * @frame: array where the output samples will be stored
235 * Generates a frame of samples from the @g generator, that will be stored
236 * in the @frame array. If @n_channels is greater than the number
237 * of channels the generator has, they are sequentially spread.
239 * Returns non-zero if the generator is stopped and should be freed.
241 int ss_gen_frame(struct ss_gen
* g
, int n_channels
, sample_t frame
[])
246 sample_t l_frame
[SS_MAX_CHANNELS
];
252 /* process vibrato */
255 g
->vibrato
+= g
->vib_inc
;
256 v
+= sin(g
->vibrato
) * g
->vib_depth
;
259 /* add samples to frame */
260 for(n
=0;n
< w
->n_channels
;n
++)
261 l_frame
[n
] = ss_gesample_t(w
->wave
[n
], w
->size
, v
) * g
->vol
;
263 /* spread the frame into n_channels */
264 for(n
=0;n
< n_channels
;n
++)
265 frame
[n
] += l_frame
[n
% w
->n_channels
];
267 /* increment pointer */
270 /* test loop boundaries */
271 if(g
->cursor
> w
->loop_end
)
274 if(w
->loop_start
< 0)
277 g
->cursor
=w
->loop_start
;
280 /* process sustain */
285 if(--g
->sustain
<= 0)
289 /* process portamento */
290 g
->inc
+= g
->portamento
;