Updated TODO.
[ahxm.git] / ss_ins.c
blobce3042d4bd210e37dc8fa34719788327c8290945
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 /**
48 * ss_ins_init - Initializes an instrument.
49 * @i: the instrument
51 * Initializes an instrument structure.
53 void ss_ins_init(struct ss_ins * i)
55 memset(i, '\0', sizeof(struct ss_ins));
57 /* default channels: stereo left and right, full volume */
58 ss_ins_set_channel(i, 0, 1.0);
59 ss_ins_set_channel(i, 1, 1.0);
61 /* sustain shouldn't be 0 to avoid end of note clicks */
62 ss_ins_set_sustain(i, 50.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 struct ss_wave * l;
79 /* grow layers */
80 i->layers=(struct ss_wave *) realloc(i->layers,
81 (i->n_layers + 1) * sizeof(struct ss_wave));
83 l=&i->layers[i->n_layers];
85 memcpy(l, w, sizeof(struct ss_wave));
87 i->n_layers++;
91 /**
92 * ss_ins_set_channel - Sets the volume for an instrument's channel.
93 * @i: the instrument
94 * @channel: channel number
95 * @vol: volume
97 * Sets the volume for an instrument's channel. If the channel does
98 * not exist, it's created and space allocated for it in the volume and
99 * effect dynamic arrays.
101 void ss_ins_set_channel(struct ss_ins * i, int channel, float vol)
103 /* if channel is new, alloc space for it */
104 if(channel <= i->n_channels)
106 int n;
108 i->vols=(float *)realloc(i->vols,
109 (channel + 1) * sizeof(float));
110 i->effs=(struct ss_eff **)realloc(i->effs,
111 (channel + 1) * sizeof(struct ss_eff *));
113 /* fill newly allocated space */
114 for(n=i->n_channels;n <= channel;n++)
116 i->vols[n]=1.0;
117 i->effs[n]=NULL;
120 i->n_channels=channel + 1;
123 /* store volume */
124 i->vols[channel]=vol;
129 * ss_ins_set_sustain - Sets the sustain for an instrument.
130 * @i: the instrument
131 * @sustain: the sustain time in milliseconds
133 * Sets the sustain for an instrument. @sustain is expressed in
134 * milliseconds.
136 void ss_ins_set_sustain(struct ss_ins * i, double sustain)
138 i->sustain=sustain;
143 * ss_ins_note_on - Plays a note.
144 * @i: the instrument
145 * @note: MIDI note to be played
146 * @vol: note volume
147 * @note_id: note id
149 * Locates a layer to play a note, and starts generators to
150 * play it. The @note is expressed as a MIDI note and the
151 * desired volume (from 0 to 1) stored in @vol. The note @id
152 * should be a positive, unique identifier for this note; no two
153 * simultaneously playing notes should share this id.
155 * Returns the number of generators that were activated.
157 int ss_ins_note_on(struct ss_ins * i, int note, float vol, int note_id)
159 int n;
160 struct ss_wave * l;
161 struct ss_wave w;
162 double note_freq;
163 int notes=0;
164 struct ss_gen * g;
166 note_freq=ss_note_frequency(note);
168 /* loop layers */
169 for(n=0;n < i->n_layers;n++)
171 l=&i->layers[n];
173 if(note_freq < l->min_freq || note_freq > l->max_freq)
174 continue;
176 /* get a free generator, or fail */
177 if((g=ss_gen_alloc(&i->gens)) == NULL)
178 break;
180 memcpy(&w, l, sizeof(struct ss_wave));
182 /* start the generator */
183 ss_gen_play(g, note_freq, vol, note_id, &w);
185 /* set sustain */
186 ss_gen_sustain(g, i->sustain);
187 #if 0
188 /* TEST: portamento */
189 if(i->trk_id == 2)
190 g->portamento=-0.000001;
191 #endif
192 notes++;
195 return(notes);
200 * ss_ins_note_off - Releases a note.
201 * @i: the instrument
202 * @note_id: the id of the note to be released
204 * Releases a note. The generators associated to it will enter release mode.
206 void ss_ins_note_off(struct ss_ins * i, int note_id)
208 struct ss_gen * g;
210 /* releases all generators with that note_id */
211 for(g=i->gens;g != NULL;g=g->next)
213 if(g->note_id == note_id)
214 ss_gen_release(g);
220 * ss_ins_frame - Generates a frame of samples.
221 * @i: the instrument
222 * @frame: array where the output samples will be stored
224 * Generates a frame of samples mixing all the active generators
225 * of a track.
227 void ss_ins_frame(struct ss_ins * i, float frame[])
229 struct ss_gen * g;
230 struct ss_gen * t;
231 int n;
232 float l_frame[SS_MAX_CHANNELS];
234 /* resets this local frame */
235 ss_output_init_frame(l_frame);
237 /* loops through the generators */
238 for(g=i->gens;g != NULL;g=t)
240 t=g->next;
242 /* if the generator has stopped, free it */
243 if(ss_gen_frame(g, i->n_channels, l_frame))
244 ss_gen_free(&i->gens, g);
247 /* loops through the effects and remixes */
248 for(n=0;n < i->n_channels;n++)
250 frame[n] += ss_eff_process(i->effs[n], l_frame[n]);
251 frame[n] *= i->vols[n];