Updated TODO.
[ahxm.git] / ss_ins.c
blob47a276a35465b8bfb588ded1f9a9365f8878462a
1 /*
3 Ann Hell Ex Machina - Music Software
4 Copyright (C) 2003/2007 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"
35 /*******************
36 Data
37 ********************/
39 /*******************
40 Code
41 ********************/
43 /**
44 * ss_ins_init - Initializes an instrument.
45 * @i: the instrument
47 * Initializes an instrument structure.
49 void ss_ins_init(struct ss_ins *i)
51 memset(i, '\0', sizeof(struct ss_ins));
53 /* default channels: stereo left and right, full volume */
54 ss_ins_set_channel(i, 0, 1.0);
55 ss_ins_set_channel(i, 1, 1.0);
57 /* sustain shouldn't be 0 to avoid end of note clicks */
58 ss_ins_set_sustain(i, 50.0);
59 ss_ins_set_attack(i, 0.0);
60 ss_ins_set_vibrato(i, 0.0, 0.0);
61 ss_ins_set_portamento(i, 0.0);
65 /**
66 * ss_ins_disable - Disables an instrument.
67 * @i: the instrument
69 * Disables an instrument. From now on, no more notes can be played
70 * on this instrument. If no note are left sounding, ss_ins_frame()
71 * will return immediately.
73 void ss_ins_disable(struct ss_ins *i)
75 i->disabled = 1;
79 /**
80 * ss_ins_add_layer - Adds a layer to an instrument.
81 * @i: the instrument
82 * @w: wave describing the layer
84 * Adds a layer to an instrument.
86 * Returns 0 if the layer was added successfully.
88 void ss_ins_add_layer(struct ss_ins *i, struct ss_wave *w)
90 /* grow layers */
91 GROW(i->layers, i->n_layers, struct ss_wave *);
93 i->layers[i->n_layers] = w;
95 i->n_layers++;
99 /**
100 * ss_ins_find_layer - Finds a layer inside an instrument
101 * @i: the instrument
102 * @freq: the desired frequency
103 * @off: pointer to element offset to start searching
105 * Finds a layer inside the @i instrument with a matching @freq, starting
106 * from the layer number pointed by @off. If a matching layer is found, its
107 * struct ss_wave is returned and @off is left pointing to the next layer
108 * (allowing it to be used as an enumerator). If no layer is found, NULL
109 * is returned.
111 struct ss_wave *ss_ins_find_layer(struct ss_ins *i, double freq, int *off)
113 int n = 0;
114 struct ss_wave *w = NULL;
116 /* if off is NULL, point to the first layer */
117 if (off == NULL)
118 off = &n;
120 /* find a matching layer, starting from *off */
121 for (; *off < i->n_layers; (*off)++) {
122 w = i->layers[*off];
124 if (freq >= w->min_freq && freq <= w->max_freq)
125 break;
128 /* passed the end; none found */
129 if (*off == i->n_layers)
130 w = NULL;
131 else
132 (*off)++;
134 return w;
139 * ss_ins_set_channel - Sets the volume for an instrument's channel.
140 * @i: the instrument
141 * @channel: channel number
142 * @vol: volume
144 * Sets the volume for an instrument's channel. If the channel does
145 * not exist, it's created and space allocated for it in the volume and
146 * effect dynamic arrays.
148 void ss_ins_set_channel(struct ss_ins *i, int channel, sample_t vol)
150 /* if channel is new, alloc space for it */
151 if (channel <= i->n_channels) {
152 int n;
154 GROW(i->vols, channel, sample_t);
155 GROW(i->effs, channel, struct ss_eff *);
157 /* fill newly allocated space */
158 for (n = i->n_channels; n <= channel; n++) {
159 i->vols[n] = 1.0;
160 i->effs[n] = NULL;
163 i->n_channels = channel + 1;
166 /* store volume */
167 i->vols[channel] = vol;
172 * ss_ins_set_sustain - Sets the sustain for an instrument.
173 * @i: the instrument
174 * @sustain: the sustain time in milliseconds
176 * Sets the sustain for an instrument. @sustain is expressed in
177 * milliseconds.
179 void ss_ins_set_sustain(struct ss_ins *i, double sustain)
181 i->sustain = sustain;
186 * ss_ins_set_attack - Sets the attack for an instrument.
187 * @i: the instrument
188 * @attack: the attack time in milliseconds
190 * Sets the attack for an instrument. @attack is expressed in
191 * milliseconds.
193 void ss_ins_set_attack(struct ss_ins *i, double attack)
195 i->attack = attack;
200 * ss_ins_set_vibrato - Sets the vibrato for an instrument.
201 * @i: the instrument
202 * @depth: vibrato depth in msecs
203 * @freq: vibrato frequency in Hzs
205 * Sets the vibrato for an instrument. @depth is expressed in
206 * milliseconds and @freq in Hzs.
208 void ss_ins_set_vibrato(struct ss_ins *i, double depth, double freq)
210 i->vib_depth = depth;
211 i->vib_freq = freq;
216 * ss_ins_set_portamento - Sets portamento for an instrument.
217 * @i: the instrument
218 * @portamento: portamento value
220 * Sets portamento for an instrument.
222 void ss_ins_set_portamento(struct ss_ins *i, double portamento)
224 i->portamento = portamento;
229 * ss_ins_play - Plays a note given the desired wave.
230 * @i: the instrument
231 * @freq: frequency
232 * @vol: volume
233 * @note_id: note id
234 * @w: the wave
236 * Orders the instrument to start playing a note, given a specific wave.
237 * The wave is usually one of the instrument's layers, but it doesn't
238 * have to.
240 * Returns -1 if the instrument is disabled, -2 if no free generators
241 * were found, or 0 if everything went ok.
243 int ss_ins_play(struct ss_ins *i, double freq, sample_t vol, int note_id, struct ss_wave *w)
245 struct ss_gen *g;
247 /* if the instrument is disabled, no more notes are allowed */
248 if (i->disabled)
249 return -1;
251 /* get a free generator, or fail */
252 if ((g = ss_gen_alloc(&i->gens)) == NULL)
253 return -2;
255 /* start the generator */
256 ss_gen_play(g, freq, vol, note_id, w);
258 /* set generator parameters */
259 ss_gen_sustain(g, i->sustain);
260 ss_gen_attack(g, i->attack);
261 ss_gen_vibrato(g, i->vib_depth, i->vib_freq);
262 ss_gen_portamento(g, i->portamento);
264 return 1;
269 * ss_ins_note_on - Plays a note.
270 * @i: the instrument
271 * @note: MIDI note to be played
272 * @vol: note volume
273 * @note_id: note id
275 * Locates a layer to play a note, and starts generators to
276 * play it. The @note is expressed as a MIDI note and the
277 * desired volume (from 0 to 1) stored in @vol. The note @id
278 * should be a positive, unique identifier for this note; no two
279 * simultaneously playing notes should share this id.
281 * Returns the number of generators that were activated.
283 int ss_ins_note_on(struct ss_ins *i, int note, sample_t vol, int note_id)
285 int n, g;
286 struct ss_wave *w;
287 double freq;
289 freq = ss_note_frequency(note);
291 for (n = g = 0; (w = ss_ins_find_layer(i, freq, &n)) != NULL; g++) {
292 if (ss_ins_play(i, freq, vol, note_id, w) < 0)
293 break;
296 return g;
301 * ss_ins_note_off - Releases a note.
302 * @i: the instrument
303 * @note_id: the id of the note to be released
305 * Releases a note. The generators associated to it will enter release mode.
307 void ss_ins_note_off(struct ss_ins *i, int note_id)
309 struct ss_gen *g;
311 /* releases all generators with that note_id */
312 for (g = i->gens; g != NULL; g = g->next) {
313 if (g->note_id == note_id)
314 ss_gen_release(g);
320 * ss_ins_frame - Generates a frame of samples.
321 * @i: the instrument
322 * @frame: array where the output samples will be stored
324 * Generates a frame of samples mixing all the active generators
325 * of a track.
327 * Returns 0 if the instrument is disabled and is no longer
328 * emitting sound, or 1 otherwise.
330 int ss_ins_frame(struct ss_ins *i, sample_t frame[])
332 struct ss_gen *g;
333 struct ss_gen *t;
334 int n;
335 sample_t l_frame[SS_MAX_CHANNELS];
337 /* if instrument is disabled and there is no more generators, exit */
338 if (i->disabled && i->gens == NULL) {
339 /* reset all effects */
340 for (n = 0; n < i->n_channels; n++)
341 ss_eff_off(&i->effs[n]);
343 return 0;
346 /* resets this local frame */
347 ss_output_init_frame(l_frame);
349 /* loops through the generators */
350 for (g = i->gens; g != NULL; g = t) {
351 t = g->next;
353 /* if the generator has stopped, free it */
354 if (ss_gen_frame(g, i->n_channels, l_frame))
355 ss_gen_free(&i->gens, g);
358 /* loops through the effects and remixes */
359 for (n = 0; n < i->n_channels; n++)
360 frame[n] += ss_eff_process(i->effs[n], l_frame[n] * i->vols[n]);
362 return 1;