Updated TODO.
[ahxm.git] / ss_ins.c
blobd79497971e057861b84072b8da740e34ad873c12
1 /*
3 Ann Hell Ex Machina - Music Software
4 Copyright (C) 2003/2005 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 "annhell.h"
35 /*******************
36 Data
37 ********************/
39 /*******************
40 Code
41 ********************/
43 /* grows a dynamic array */
44 #define GROW(b,n,t) b=(t *)realloc(b,((n) + 1) * sizeof(t))
46 /**
47 * ss_ins_init - Initializes an instrument.
48 * @i: the instrument
50 * Initializes an instrument structure.
52 void ss_ins_init(struct ss_ins * i)
54 memset(i, '\0', sizeof(struct ss_ins));
56 /* default channels: stereo left and right, full volume */
57 ss_ins_set_channel(i, 0, 1.0);
58 ss_ins_set_channel(i, 1, 1.0);
60 /* sustain shouldn't be 0 to avoid end of note clicks */
61 ss_ins_set_sustain(i, 50.0);
62 ss_ins_set_vibrato(i, 0.0, 0.0);
66 /**
67 * ss_ins_add_layer - Adds a layer to an instrument.
68 * @i: the instrument
69 * @w: wave describing the layer
71 * Adds a layer to an instrument.
73 * Returns 0 if the layer was added successfully.
75 void ss_ins_add_layer(struct ss_ins * i, struct ss_wave * w)
77 /* grow layers */
78 GROW(i->layers, i->n_layers, struct ss_wave *);
80 i->layers[i->n_layers]=w;
82 i->n_layers++;
86 /**
87 * ss_ins_copy_layers - Copies all layers from an instrument into another
88 * @i: the destination instrument
89 * @o: the source instrument
91 * Copies all layers inside the @o instrument into @i.
93 void ss_ins_copy_layers(struct ss_ins * i, struct ss_ins * o)
95 int n;
97 for(n=0;n < o->n_layers;n++)
98 ss_ins_add_layer(i, o->layers[n]);
103 * ss_ins_find_layer - Finds a layer inside an instrument
104 * @i: the instrument
105 * @freq: the desired frequency
106 * @off: pointer to element offset to start searching
108 * Finds a layer inside the @i instrument with a matching @freq, starting
109 * from the layer number pointed by @off. If a matching layer is found, its
110 * struct ss_wave is returned and @off is left pointing to the next layer
111 * (allowing it to be used as an enumerator). If no layer is found, NULL
112 * is returned.
114 struct ss_wave * ss_ins_find_layer(struct ss_ins * i, double freq, int * off)
116 int n=0;
117 struct ss_wave * w=NULL;
119 /* if off is NULL, point to the first layer */
120 if(off == NULL) off=&n;
122 /* find a matching layer, starting from *off */
123 for(;*off < i->n_layers;(*off)++)
125 w=i->layers[*off];
127 if(freq >= w->min_freq && freq <= w->max_freq)
128 break;
131 /* passed the end; none found */
132 if(*off == i->n_layers)
133 w=NULL;
134 else
135 (*off)++;
137 return(w);
142 * ss_ins_set_channel - Sets the volume for an instrument's channel.
143 * @i: the instrument
144 * @channel: channel number
145 * @vol: volume
147 * Sets the volume for an instrument's channel. If the channel does
148 * not exist, it's created and space allocated for it in the volume and
149 * effect dynamic arrays.
151 void ss_ins_set_channel(struct ss_ins * i, int channel, sample_t vol)
153 /* if channel is new, alloc space for it */
154 if(channel <= i->n_channels)
156 int n;
158 GROW(i->vols, channel, sample_t);
159 GROW(i->effs, channel, struct ss_eff *);
161 /* fill newly allocated space */
162 for(n=i->n_channels;n <= channel;n++)
164 i->vols[n]=1.0;
165 i->effs[n]=NULL;
168 i->n_channels=channel + 1;
171 /* store volume */
172 i->vols[channel]=vol;
177 * ss_ins_set_sustain - Sets the sustain for an instrument.
178 * @i: the instrument
179 * @sustain: the sustain time in milliseconds
181 * Sets the sustain for an instrument. @sustain is expressed in
182 * milliseconds.
184 void ss_ins_set_sustain(struct ss_ins * i, double sustain)
186 i->sustain=sustain;
191 * ss_ins_set_vibrato - Sets the vibrato for an instrument.
192 * @i: the instrument
193 * @depth: vibrato depth in msecs
194 * @freq: vibrato frequency in Hzs
196 * Sets the vibrato for an instrument. @depth is expressed in
197 * milliseconds and @freq in Hzs.
199 void ss_ins_set_vibrato(struct ss_ins * i, double depth, double freq)
201 i->vib_depth=depth;
202 i->vib_freq=freq;
207 * ss_ins_play - Plays a note given the desired wave.
208 * @i: the instrument
209 * @freq: frequency
210 * @vol: volume
211 * @note_id: note id
212 * @w: the wave
214 * Orders the instrument to start playing a note, given a specific wave.
215 * The wave is usually one of the instrument's layers, but it doesn't
216 * have to.
218 * Returns zero if there were no free generators, or non-zero otherwise.
220 int ss_ins_play(struct ss_ins * i, double freq, sample_t vol, int note_id,
221 struct ss_wave * w)
223 struct ss_gen * g;
225 /* get a free generator, or fail */
226 if((g=ss_gen_alloc(&i->gens)) == NULL)
227 return(0);
229 /* start the generator */
230 ss_gen_play(g, freq, vol, note_id, w);
232 /* set sustain and vibrato */
233 ss_gen_sustain(g, i->sustain);
234 ss_gen_vibrato(g, i->vib_depth, i->vib_freq);
236 return(1);
241 * ss_ins_note_on - Plays a note.
242 * @i: the instrument
243 * @note: MIDI note to be played
244 * @vol: note volume
245 * @note_id: note id
247 * Locates a layer to play a note, and starts generators to
248 * play it. The @note is expressed as a MIDI note and the
249 * desired volume (from 0 to 1) stored in @vol. The note @id
250 * should be a positive, unique identifier for this note; no two
251 * simultaneously playing notes should share this id.
253 * Returns the number of generators that were activated.
255 int ss_ins_note_on(struct ss_ins * i, int note, sample_t vol, int note_id)
257 int n, g;
258 struct ss_wave * w;
259 double freq;
261 freq=ss_note_frequency(note);
263 for(n=g=0;(w=ss_ins_find_layer(i, freq, &n)) != NULL;g++)
265 if(!ss_ins_play(i, freq, vol, note_id, w))
266 break;
269 return(g);
274 * ss_ins_note_off - Releases a note.
275 * @i: the instrument
276 * @note_id: the id of the note to be released
278 * Releases a note. The generators associated to it will enter release mode.
280 void ss_ins_note_off(struct ss_ins * i, int note_id)
282 struct ss_gen * g;
284 /* releases all generators with that note_id */
285 for(g=i->gens;g != NULL;g=g->next)
287 if(g->note_id == note_id)
288 ss_gen_release(g);
294 * ss_ins_frame - Generates a frame of samples.
295 * @i: the instrument
296 * @frame: array where the output samples will be stored
298 * Generates a frame of samples mixing all the active generators
299 * of a track.
301 void ss_ins_frame(struct ss_ins * i, sample_t frame[])
303 struct ss_gen * g;
304 struct ss_gen * t;
305 int n;
306 sample_t l_frame[SS_MAX_CHANNELS];
308 /* resets this local frame */
309 ss_output_init_frame(l_frame);
311 /* loops through the generators */
312 for(g=i->gens;g != NULL;g=t)
314 t=g->next;
316 /* if the generator has stopped, free it */
317 if(ss_gen_frame(g, i->n_channels, l_frame))
318 ss_gen_free(&i->gens, g);
321 /* loops through the effects and remixes */
322 for(n=0;n < i->n_channels;n++)
323 frame[n] += ss_eff_process(i->effs[n],
324 l_frame[n] * i->vols[n]);