More reformatting.
[ahxm.git] / ss_ins.c
blobec84988db183a6068427903e741ced1a52cca3d4
1 /*
3 Ann Hell Ex Machina - Music Software
4 Copyright (C) 2003/2008 Angel Ortega <angel@triptico.com>
6 ss_ins.c - Software synthesizer's instruments
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 "ahxm.h"
36 /**
37 * ss_ins_init - Initializes an instrument.
38 * @i: the instrument
40 * Initializes an instrument structure.
42 void ss_ins_init(struct ss_ins *i)
44 memset(i, '\0', sizeof(struct ss_ins));
46 /* default channels: stereo left and right, full volume */
47 ss_ins_set_channel(i, 0, 1.0);
48 ss_ins_set_channel(i, 1, 1.0);
50 /* sustain shouldn't be 0 to avoid end of note clicks */
51 ss_ins_set_sustain(i, 50.0);
52 ss_ins_set_attack(i, 0.0);
53 ss_ins_set_vibrato(i, 0.0, 0.0);
54 ss_ins_set_portamento(i, 0.0);
58 /**
59 * ss_ins_disable - Disables an instrument.
60 * @i: the instrument
62 * Disables an instrument. From now on, no more notes can be played
63 * on this instrument. If no note are left sounding, ss_ins_frame()
64 * will return immediately.
66 void ss_ins_disable(struct ss_ins *i)
68 i->disabled = 1;
72 /**
73 * ss_ins_add_layer - Adds a layer to an instrument.
74 * @i: the instrument
75 * @w: wave describing the layer
77 * Adds a layer to an instrument.
79 * Returns 0 if the layer was added successfully.
81 void ss_ins_add_layer(struct ss_ins *i, struct ss_wave *w)
83 /* grow layers */
84 GROW(i->layers, i->n_layers, struct ss_wave *);
86 i->layers[i->n_layers] = w;
88 i->n_layers++;
92 /**
93 * ss_ins_find_layer - Finds a layer inside an instrument
94 * @i: the instrument
95 * @freq: the desired frequency
96 * @off: pointer to element offset to start searching
98 * Finds a layer inside the @i instrument with a matching @freq, starting
99 * from the layer number pointed by @off. If a matching layer is found, its
100 * struct ss_wave is returned and @off is left pointing to the next layer
101 * (allowing it to be used as an enumerator). If no layer is found, NULL
102 * is returned.
104 struct ss_wave *ss_ins_find_layer(const struct ss_ins *i, double freq,
105 int *off)
107 int n = 0;
108 struct ss_wave *w = NULL;
110 /* if off is NULL, point to the first layer */
111 if (off == NULL)
112 off = &n;
114 /* find a matching layer, starting from *off */
115 for (; *off < i->n_layers; (*off)++) {
116 w = i->layers[*off];
118 if (freq >= w->min_freq && freq <= w->max_freq)
119 break;
122 /* passed the end; none found */
123 if (*off == i->n_layers)
124 w = NULL;
125 else
126 (*off)++;
128 return w;
133 * ss_ins_set_channel - Sets the volume for an instrument's channel.
134 * @i: the instrument
135 * @channel: channel number
136 * @vol: volume
138 * Sets the volume for an instrument's channel. If the channel does
139 * not exist, it's created and space allocated for it in the volume and
140 * effect dynamic arrays.
142 void ss_ins_set_channel(struct ss_ins *i, int channel, sample_t vol)
144 /* if channel is new, alloc space for it */
145 if (channel <= i->n_channels) {
146 int n;
148 GROW(i->vols, channel, sample_t);
149 GROW(i->effs, channel, struct ss_eff *);
151 /* fill newly allocated space */
152 for (n = i->n_channels; n <= channel; n++) {
153 i->vols[n] = 1.0;
154 i->effs[n] = NULL;
157 i->n_channels = channel + 1;
160 /* store volume */
161 i->vols[channel] = vol;
166 * ss_ins_set_sustain - Sets the sustain for an instrument.
167 * @i: the instrument
168 * @sustain: the sustain time in milliseconds
170 * Sets the sustain for an instrument. @sustain is expressed in
171 * milliseconds.
173 void ss_ins_set_sustain(struct ss_ins *i, double sustain)
175 i->sustain = sustain;
180 * ss_ins_set_attack - Sets the attack for an instrument.
181 * @i: the instrument
182 * @attack: the attack time in milliseconds
184 * Sets the attack for an instrument. @attack is expressed in
185 * milliseconds.
187 void ss_ins_set_attack(struct ss_ins *i, double attack)
189 i->attack = attack;
194 * ss_ins_set_vibrato - Sets the vibrato for an instrument.
195 * @i: the instrument
196 * @depth: vibrato depth in msecs
197 * @freq: vibrato frequency in Hzs
199 * Sets the vibrato for an instrument. @depth is expressed in
200 * milliseconds and @freq in Hzs.
202 void ss_ins_set_vibrato(struct ss_ins *i, double depth, double freq)
204 i->vib_depth = depth;
205 i->vib_freq = freq;
210 * ss_ins_set_portamento - Sets portamento for an instrument.
211 * @i: the instrument
212 * @portamento: portamento value
214 * Sets portamento for an instrument.
216 void ss_ins_set_portamento(struct ss_ins *i, double portamento)
218 i->portamento = portamento;
223 * ss_ins_play - Plays a note given the desired wave.
224 * @i: the instrument
225 * @freq: frequency
226 * @vol: volume
227 * @note_id: note id
228 * @w: the wave
230 * Orders the instrument to start playing a note, given a specific wave.
231 * The wave is usually one of the instrument's layers, but it doesn't
232 * have to.
234 * Returns -1 if the instrument is disabled, -2 if no free generators
235 * were found, or 0 if everything went ok.
237 int ss_ins_play(struct ss_ins *i, double freq, sample_t vol, int note_id,
238 struct ss_wave *w)
240 struct ss_gen *g;
242 /* if the instrument is disabled, no more notes are allowed */
243 if (i->disabled)
244 return -1;
246 /* get a free generator, or fail */
247 if ((g = ss_gen_alloc(&i->gens)) == NULL)
248 return -2;
250 /* start the generator */
251 ss_gen_play(g, freq, vol, note_id, w);
253 /* set generator parameters */
254 ss_gen_sustain(g, i->sustain);
255 ss_gen_attack(g, i->attack);
256 ss_gen_vibrato(g, i->vib_depth, i->vib_freq);
257 ss_gen_portamento(g, i->portamento);
259 return 1;
264 * ss_ins_note_on - Plays a note.
265 * @i: the instrument
266 * @note: MIDI note to be played
267 * @vol: note volume
268 * @note_id: note id
270 * Locates a layer to play a note, and starts generators to
271 * play it. The @note is expressed as a MIDI note and the
272 * desired volume (from 0 to 1) stored in @vol. The note @id
273 * should be a positive, unique identifier for this note; no two
274 * simultaneously playing notes should share this id.
276 * Returns the number of generators that were activated.
278 int ss_ins_note_on(struct ss_ins *i, int note, sample_t vol, int note_id)
280 int n, g;
281 struct ss_wave *w;
282 double freq;
284 freq = ss_note_frequency(note);
286 for (n = g = 0; (w = ss_ins_find_layer(i, freq, &n)) != NULL; g++) {
287 if (ss_ins_play(i, freq, vol, note_id, w) < 0)
288 break;
291 return g;
296 * ss_ins_note_off - Releases a note.
297 * @i: the instrument
298 * @note_id: the id of the note to be released
300 * Releases a note. The generators associated to it will enter release mode.
302 void ss_ins_note_off(struct ss_ins *i, int note_id)
304 struct ss_gen *g;
306 /* releases all generators with that note_id */
307 for (g = i->gens; g != NULL; g = g->next) {
308 if (g->note_id == note_id)
309 ss_gen_release(g);
315 * ss_ins_frame - Generates a frame of samples.
316 * @i: the instrument
317 * @frame: array where the output samples will be stored
319 * Generates a frame of samples mixing all the active generators
320 * of a track.
322 * Returns 0 if the instrument is disabled and is no longer
323 * emitting sound, or 1 otherwise.
325 int ss_ins_frame(struct ss_ins *i, sample_t frame[])
327 struct ss_gen *g;
328 struct ss_gen *t;
329 int n;
330 sample_t l_frame[SS_MAX_CHANNELS];
332 /* if instrument is disabled and there is no more generators, exit */
333 if (i->disabled && i->gens == NULL) {
334 /* reset all effects */
335 for (n = 0; n < i->n_channels; n++)
336 ss_eff_off(&i->effs[n]);
338 return 0;
341 /* resets this local frame */
342 ss_output_init_frame(l_frame);
344 /* loops through the generators */
345 for (g = i->gens; g != NULL; g = t) {
346 t = g->next;
348 /* if the generator has stopped, free it */
349 if (ss_gen_frame(g, i->n_channels, l_frame))
350 ss_gen_free(&i->gens, g);
353 /* loops through the effects and remixes */
354 for (n = 0; n < i->n_channels; n++)
355 frame[n] += ss_eff_process(i->effs[n], l_frame[n] * i->vols[n]);
357 return 1;