Updated TODO.
[ahxm.git] / ss_ins.c
blobe2eac29c66fb6b189b108d18eca1883bee7f7feb
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 "ss_core.h"
34 #include "ss_gen.h"
35 #include "ss_eff.h"
36 #include "ss_ins.h"
37 #include "ss_output.h"
39 /*******************
40 Data
41 ********************/
43 /*******************
44 Code
45 ********************/
47 /* grows a dynamic array */
48 #define GROW(b,n,t) b=(t *)realloc(b,((n) + 1) * sizeof(t))
50 /**
51 * ss_ins_init - Initializes an instrument.
52 * @i: the instrument
54 * Initializes an instrument structure.
56 void ss_ins_init(struct ss_ins * i)
58 memset(i, '\0', sizeof(struct ss_ins));
60 /* default channels: stereo left and right, full volume */
61 ss_ins_set_channel(i, 0, 1.0);
62 ss_ins_set_channel(i, 1, 1.0);
64 /* sustain shouldn't be 0 to avoid end of note clicks */
65 ss_ins_set_sustain(i, 50.0);
69 /**
70 * ss_ins_add_layer - Adds a layer to an instrument.
71 * @i: the instrument
72 * @w: wave describing the layer
74 * Adds a layer to an instrument.
76 * Returns 0 if the layer was added successfully.
78 void ss_ins_add_layer(struct ss_ins * i, struct ss_wave * w)
80 /* grow layers */
81 GROW(i->layers, i->n_layers, struct ss_wave *);
83 i->layers[i->n_layers]=w;
85 i->n_layers++;
89 /**
90 * ss_ins_find_layer - Finds a layer inside an instrument
91 * @i: the instrument
92 * @freq: the desired frequency
93 * @off: pointer to element offset to start searching
95 * Finds a layer inside the @i instrument with a matching @freq, starting
96 * from the layer number pointed by @off. If a matching layer is found, its
97 * struct ss_wave is returned and @off is left pointing to the next layer
98 * (allowing it to be used as an enumerator). If no layer is found, NULL
99 * is returned.
101 struct ss_wave * ss_ins_find_layer(struct ss_ins * i, double freq, int * off)
103 struct ss_wave * w;
105 /* find a matching layer, starting from *off */
106 for(;*off < i->n_layers;(*off)++)
108 w=i->layers[*off];
110 if(freq >= w->min_freq && freq <= w->max_freq)
111 break;
114 /* passed the end; none found */
115 if(*off == i->n_layers)
116 w=NULL;
117 else
118 (*off)++;
120 return(w);
125 * ss_ins_set_channel - Sets the volume for an instrument's channel.
126 * @i: the instrument
127 * @channel: channel number
128 * @vol: volume
130 * Sets the volume for an instrument's channel. If the channel does
131 * not exist, it's created and space allocated for it in the volume and
132 * effect dynamic arrays.
134 void ss_ins_set_channel(struct ss_ins * i, int channel, float vol)
136 /* if channel is new, alloc space for it */
137 if(channel <= i->n_channels)
139 int n;
141 GROW(i->vols, channel, float);
142 GROW(i->effs, channel, struct ss_eff *);
144 /* fill newly allocated space */
145 for(n=i->n_channels;n <= channel;n++)
147 i->vols[n]=1.0;
148 i->effs[n]=NULL;
151 i->n_channels=channel + 1;
154 /* store volume */
155 i->vols[channel]=vol;
160 * ss_ins_set_sustain - Sets the sustain for an instrument.
161 * @i: the instrument
162 * @sustain: the sustain time in milliseconds
164 * Sets the sustain for an instrument. @sustain is expressed in
165 * milliseconds.
167 void ss_ins_set_sustain(struct ss_ins * i, double sustain)
169 i->sustain=sustain;
174 * ss_ins_note_on_by_freq - Plays a note by its desired frequency
175 * @i: the instrument
176 * @freq: the frequency
177 * @vol: note volume
178 * @note_id: note id
180 * Locates a layer to play a note, and starts generators to
181 * play it. The @note is expressed as a MIDI note and the
182 * desired volume (from 0 to 1) stored in @vol. The note @id
183 * should be a positive, unique identifier for this note; no two
184 * simultaneously playing notes should share this id.
186 * Returns the number of generators that were activated.
188 int ss_ins_note_on_by_freq(struct ss_ins * i, double freq, float vol, int note_id)
190 int n=0;
191 struct ss_wave * w;
192 int notes=0;
193 struct ss_gen * g;
195 while((w=ss_ins_find_layer(i, freq, &n)) != NULL)
197 /* get a free generator, or fail */
198 if((g=ss_gen_alloc(&i->gens)) == NULL)
199 break;
201 /* start the generator */
202 ss_gen_play(g, freq, vol, note_id, w);
204 /* set sustain */
205 ss_gen_sustain(g, i->sustain);
206 #if 0
207 /* TEST: portamento */
208 if(i->trk_id == 2)
209 g->portamento=-0.000001;
210 #endif
211 notes++;
214 return(notes);
219 * ss_ins_note_on - Plays a note.
220 * @i: the instrument
221 * @note: MIDI note to be played
222 * @vol: note volume
223 * @note_id: note id
225 * Locates a layer to play a note, and starts generators to
226 * play it. The @note is expressed as a MIDI note and the
227 * desired volume (from 0 to 1) stored in @vol. The note @id
228 * should be a positive, unique identifier for this note; no two
229 * simultaneously playing notes should share this id.
231 * Returns the number of generators that were activated.
233 int ss_ins_note_on(struct ss_ins * i, int note, float vol, int note_id)
235 return(ss_ins_note_on_by_freq(i, ss_note_frequency(note), vol, note_id));
240 * ss_ins_note_off - Releases a note.
241 * @i: the instrument
242 * @note_id: the id of the note to be released
244 * Releases a note. The generators associated to it will enter release mode.
246 void ss_ins_note_off(struct ss_ins * i, int note_id)
248 struct ss_gen * g;
250 /* releases all generators with that note_id */
251 for(g=i->gens;g != NULL;g=g->next)
253 if(g->note_id == note_id)
254 ss_gen_release(g);
260 * ss_ins_frame - Generates a frame of samples.
261 * @i: the instrument
262 * @frame: array where the output samples will be stored
264 * Generates a frame of samples mixing all the active generators
265 * of a track.
267 void ss_ins_frame(struct ss_ins * i, float frame[])
269 struct ss_gen * g;
270 struct ss_gen * t;
271 int n;
272 float l_frame[SS_MAX_CHANNELS];
274 /* resets this local frame */
275 ss_output_init_frame(l_frame);
277 /* loops through the generators */
278 for(g=i->gens;g != NULL;g=t)
280 t=g->next;
282 /* if the generator has stopped, free it */
283 if(ss_gen_frame(g, i->n_channels, l_frame))
284 ss_gen_free(&i->gens, g);
287 /* loops through the effects and remixes */
288 for(n=0;n < i->n_channels;n++)
289 frame[n] += ss_eff_process(i->effs[n],
290 l_frame[n] * i->vols[n]);