A change in the title of the documentation.
[ahxm.git] / ss_ins.c
blob988f5f98ac7da292003f4fc729018c27c2d2c936
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 struct
49 * @i: the instrument
50 * @trk_id: track id for the instrument
52 * Initializes an instrument structure.
54 void ss_ins_init(struct ss_ins * i, int trk_id)
56 float vol[2]={ 1, 1 };
58 memset(i, '\0', sizeof(struct ss_ins));
60 /* stores the track ID */
61 i->trk_id=trk_id;
63 /* sets the default channel mapping */
64 ss_ins_set_channel_map(i, 2, vol);
66 /* sets sustain shouldn't be 0
67 to avoid end of note clicks */
68 ss_ins_set_sustain(i, 50.0);
72 /**
73 * ss_ins_add_layer - Adds a layer to an instrument.
74 * @i: the instrument
75 * @w: wave describing the layer
77 * Adds a layer to an instrument.
79 * Returns 0 if the layer was added successfully.
81 void ss_ins_add_layer(struct ss_ins * i, struct ss_wave * w)
83 struct ss_wave * l;
85 /* grow layers */
86 i->layers=(struct ss_wave *) realloc(i->layers,
87 (i->n_layers + 1) * sizeof(struct ss_wave));
89 l=&i->layers[i->n_layers];
91 memcpy(l, w, sizeof(struct ss_wave));
93 i->n_layers++;
97 /**
98 * ss_ins_set_channel_map - Sets the channel map for an instrument
99 * @i: the instrument
100 * @n_channels: number of channels in vol
101 * @vol: the channel volumes
103 * Sets the current channel map for an instrument. @vol holds volume values
104 * for upto @n_channels channels. The volume for the rest of channels
105 * (upto CHANNELS) is set to 0.
107 void ss_ins_set_channel_map(struct ss_ins * i, int n_channels, float vol[])
109 int n;
111 for(n=0;n < n_channels;n++)
112 i->vol[n] = vol[n];
114 for(;n < CHANNELS;n++)
115 i->vol[n] = 0.0;
120 * ss_ins_set_sustain - Sets the sustain for an instrument
121 * @i: the instrument
122 * @sustain: the sustain time in milliseconds
124 * Sets the sustain for an instrument. @sustain is expressed in
125 * milliseconds.
127 void ss_ins_set_sustain(struct ss_ins * i, double sustain)
129 i->sustain=sustain;
134 * ss_ins_note_on - Plays a note.
135 * @i: the instrument
136 * @note: MIDI note to be played
137 * @vol: note volume
138 * @note_id: note id
140 * Locates a layer to play a note, and starts generators to
141 * play it. The @note is expressed as a MIDI note and the
142 * desired volume (from 0 to 1) stored in @vol. The note @id
143 * should be a unique identifier for this note; no two simultaneously
144 * playing notes should share this id.
146 * The channels of the found layers are distributed sequentially by
147 * using the instrument's channel map, skipping those with a volume
148 * of 0.0. So, for example, for a stereo layer with channels L and R
149 * and 6 channel output with volumes of 1 1 1 0 1 0, the channel
150 * mapping distribution will be L R L 0 R 0. If you want the fifth
151 * channel to be L, just use a virtually unhearable volume of 0.0001
152 * for the fourth one (but greater than 0).
154 * Returns the number of generators that were activated.
156 int ss_ins_note_on(struct ss_ins * i, int note, float vol, int note_id)
158 int n, m, f;
159 struct ss_wave * l;
160 struct ss_wave w;
161 float vols[CHANNELS];
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 /* assign the channels and their volumes */
183 for(f=m=0;f < CHANNELS;f++)
185 if(i->vol[f] > 0.0)
187 /* assign next channel of layer */
188 vols[f]=i->vol[f] * vol;
189 w.wave[f]=l->wave[m++];
191 if(m >= l->n_channels)
192 m=0;
194 else
195 w.wave[f]=NULL;
198 /* start the generator */
199 ss_gen_play(g, note_id, note_freq, vols, &w);
201 /* set sustain */
202 ss_gen_sustain(g, i->sustain);
204 /* TEST: portamento */
205 if(i->trk_id == 2)
206 g->portamento=-0.000001;
208 notes++;
211 return(notes);
216 * ss_ins_note_off - Releases a note.
217 * @i: the instrument
218 * @note_id: the id of the note to be released
220 * Releases a note. The generators associated to it will enter release mode.
222 void ss_ins_note_off(struct ss_ins * i, int note_id)
224 struct ss_gen * g;
226 /* releases all generators with that note_id */
227 for(g=i->gens;g != NULL;g=g->next)
229 if(g->note_id == note_id)
230 ss_gen_release(g);
236 * ss_ins_frame - Generates a frame of samples.
237 * @trk_id: the track
238 * @sample: array where the output samples will be stored
240 * Generates a frame of samples mixing all the active generators
241 * of a track.
243 void ss_ins_frame(struct ss_ins * i, float frame[])
245 struct ss_gen * g;
246 struct ss_gen * t;
247 int n;
248 float l_frame[CHANNELS];
250 /* resets this local frame */
251 ss_output_init_frame(l_frame);
253 /* loops through the generators */
254 for(g=i->gens;g != NULL;g=t)
256 t=g->next;
258 /* if the generator has stopped, free it */
259 if(ss_gen_frame(g, l_frame))
260 ss_gen_free(&i->gens, g);
263 /* loops through the effects and remixes */
264 for(n=0;n < CHANNELS;n++)
265 frame[n] += ss_eff_process(i->effs[n], l_frame[n]);