The song statistics message in ss_song.c (shown when
[ahxm.git] / ss_ins.c
blobb27869074e26daf6c3481fd8accac72951139d9c
1 /*
3 Ann Hell Ex Machina - Music Software
4 Copyright (C) 2003/2006 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_vibrato(i, 0.0, 0.0);
60 ss_ins_set_portamento(i, 0.0);
64 /**
65 * ss_ins_disable - Disables an instrument.
66 * @i: the instrument
68 * Disables an instrument. From now on, no more notes can be played
69 * on this instrument. If no note are left sounding, ss_ins_frame()
70 * will return immediately.
72 void ss_ins_disable(struct ss_ins * i)
74 i->disabled = 1;
78 /**
79 * ss_ins_add_layer - Adds a layer to an instrument.
80 * @i: the instrument
81 * @w: wave describing the layer
83 * Adds a layer to an instrument.
85 * Returns 0 if the layer was added successfully.
87 void ss_ins_add_layer(struct ss_ins * i, struct ss_wave * w)
89 /* grow layers */
90 GROW(i->layers, i->n_layers, struct ss_wave *);
92 i->layers[i->n_layers] = w;
94 i->n_layers++;
98 /**
99 * ss_ins_find_layer - Finds a layer inside an instrument
100 * @i: the instrument
101 * @freq: the desired frequency
102 * @off: pointer to element offset to start searching
104 * Finds a layer inside the @i instrument with a matching @freq, starting
105 * from the layer number pointed by @off. If a matching layer is found, its
106 * struct ss_wave is returned and @off is left pointing to the next layer
107 * (allowing it to be used as an enumerator). If no layer is found, NULL
108 * is returned.
110 struct ss_wave * ss_ins_find_layer(struct ss_ins * i, double freq, int * off)
112 int n = 0;
113 struct ss_wave * w = NULL;
115 /* if off is NULL, point to the first layer */
116 if(off == NULL) off = &n;
118 /* find a matching layer, starting from *off */
119 for(;*off < i->n_layers;(*off)++)
121 w = i->layers[*off];
123 if(freq >= w->min_freq && freq <= w->max_freq)
124 break;
127 /* passed the end; none found */
128 if(*off == i->n_layers)
129 w = NULL;
130 else
131 (*off)++;
133 return(w);
138 * ss_ins_set_channel - Sets the volume for an instrument's channel.
139 * @i: the instrument
140 * @channel: channel number
141 * @vol: volume
143 * Sets the volume for an instrument's channel. If the channel does
144 * not exist, it's created and space allocated for it in the volume and
145 * effect dynamic arrays.
147 void ss_ins_set_channel(struct ss_ins * i, int channel, sample_t vol)
149 /* if channel is new, alloc space for it */
150 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++)
160 i->vols[n] = 1.0;
161 i->effs[n] = NULL;
164 i->n_channels = channel + 1;
167 /* store volume */
168 i->vols[channel] = vol;
173 * ss_ins_set_sustain - Sets the sustain for an instrument.
174 * @i: the instrument
175 * @sustain: the sustain time in milliseconds
177 * Sets the sustain for an instrument. @sustain is expressed in
178 * milliseconds.
180 void ss_ins_set_sustain(struct ss_ins * i, double sustain)
182 i->sustain = sustain;
187 * ss_ins_set_vibrato - Sets the vibrato for an instrument.
188 * @i: the instrument
189 * @depth: vibrato depth in msecs
190 * @freq: vibrato frequency in Hzs
192 * Sets the vibrato for an instrument. @depth is expressed in
193 * milliseconds and @freq in Hzs.
195 void ss_ins_set_vibrato(struct ss_ins * i, double depth, double freq)
197 i->vib_depth = depth;
198 i->vib_freq = freq;
203 * ss_ins_set_portamento - Sets portamento for an instrument.
204 * @i: the instrument
205 * @portamento: portamento value
207 * Sets portamento for an instrument.
209 void ss_ins_set_portamento(struct ss_ins * i, double portamento)
211 i->portamento = portamento;
216 * ss_ins_play - Plays a note given the desired wave.
217 * @i: the instrument
218 * @freq: frequency
219 * @vol: volume
220 * @note_id: note id
221 * @w: the wave
223 * Orders the instrument to start playing a note, given a specific wave.
224 * The wave is usually one of the instrument's layers, but it doesn't
225 * have to.
227 * Returns -1 if the instrument is disabled, -2 if no free generators
228 * were found, or 0 if everything went ok.
230 int ss_ins_play(struct ss_ins * i, double freq, sample_t vol, int note_id,
231 struct ss_wave * w)
233 struct ss_gen * g;
235 /* if the instrument is disabled, no more notes are allowed */
236 if(i->disabled) return(-1);
238 /* get a free generator, or fail */
239 if((g = ss_gen_alloc(&i->gens)) == NULL)
240 return(-2);
242 /* start the generator */
243 ss_gen_play(g, freq, vol, note_id, w);
245 /* set sustain and vibrato */
246 ss_gen_sustain(g, i->sustain);
247 ss_gen_vibrato(g, i->vib_depth, i->vib_freq);
248 ss_gen_portamento(g, i->portamento);
250 return(1);
255 * ss_ins_note_on - Plays a note.
256 * @i: the instrument
257 * @note: MIDI note to be played
258 * @vol: note volume
259 * @note_id: note id
261 * Locates a layer to play a note, and starts generators to
262 * play it. The @note is expressed as a MIDI note and the
263 * desired volume (from 0 to 1) stored in @vol. The note @id
264 * should be a positive, unique identifier for this note; no two
265 * simultaneously playing notes should share this id.
267 * Returns the number of generators that were activated.
269 int ss_ins_note_on(struct ss_ins * i, int note, sample_t vol, int note_id)
271 int n, g;
272 struct ss_wave * w;
273 double freq;
275 freq = ss_note_frequency(note);
277 for(n = g = 0;(w=ss_ins_find_layer(i, freq, &n)) != NULL;g++)
279 if(ss_ins_play(i, freq, vol, note_id, w) < 0)
280 break;
283 return(g);
288 * ss_ins_note_off - Releases a note.
289 * @i: the instrument
290 * @note_id: the id of the note to be released
292 * Releases a note. The generators associated to it will enter release mode.
294 void ss_ins_note_off(struct ss_ins * i, int note_id)
296 struct ss_gen * g;
298 /* releases all generators with that note_id */
299 for(g = i->gens;g != NULL;g = g->next)
301 if(g->note_id == note_id)
302 ss_gen_release(g);
308 * ss_ins_frame - Generates a frame of samples.
309 * @i: the instrument
310 * @frame: array where the output samples will be stored
312 * Generates a frame of samples mixing all the active generators
313 * of a track.
315 void ss_ins_frame(struct ss_ins * i, sample_t frame[])
317 struct ss_gen * g;
318 struct ss_gen * t;
319 int n;
320 sample_t l_frame[SS_MAX_CHANNELS];
322 /* if instrument is disabled and there is no more generators, exit */
323 if(i->disabled && i->gens == NULL) return;
325 /* resets this local frame */
326 ss_output_init_frame(l_frame);
328 /* loops through the generators */
329 for(g = i->gens;g != NULL;g = t)
331 t = g->next;
333 /* if the generator has stopped, free it */
334 if(ss_gen_frame(g, i->n_channels, l_frame))
335 ss_gen_free(&i->gens, g);
338 /* loops through the effects and remixes */
339 for(n = 0;n < i->n_channels;n++)
340 frame[n] += ss_eff_process(i->effs[n],
341 l_frame[n] * i->vols[n]);