Added support for comb, allpass and flanger events.
[ahxm.git] / ss_ins.c
blob0217536fbdcd11c96dc2fe7556eb8dbe6ffe4a4f
1 /*
3 Ann Hell Ex Machina - Music Software
4 Copyright (C) 2003/2004 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 "core.h"
34 #include "ss_gen.h"
35 #include "ss_eff.h"
36 #include "ss_ins.h"
37 #include "output.h"
39 /*******************
40 Data
41 ********************/
43 /*******************
44 Code
45 ********************/
47 void ss_ins_init(struct ss_ins * i, int trk_id)
49 float vol[2]={ 1, 1 };
51 memset(i, '\0', sizeof(struct ss_ins));
53 /* stores the track ID */
54 i->trk_id=trk_id;
56 /* sets the default channel mapping */
57 ss_ins_set_channel_map(i, 2, vol);
59 /* sets an 1% of frequency as sustain to avoid
60 end of note clicks */
61 ss_ins_set_sustain(i, _frequency / 100);
65 /**
66 * ss_ins_add_layer - Adds a layer to an instrument.
67 * @i: the instrument
68 * @base_freq: base frequency
69 * @min_freq: minimum frequency this layer serves
70 * @max_freq: maximum frequency this layer serves
71 * @n_channels: number of channels in wave
72 * @wave: the wave PCM data
73 * @size: size of the wave in samples
74 * @s_rate: sample rate of the wave (frequency)
75 * @loop_start: sample number of the start of the loop (-1, no loop)
76 * @loop_end: sample number of the end of the loop
78 * Adds a layer to an instrument. The instrument will use this layer
79 * when asked to play a note in a range from @min_freq to @max_freq,
80 * using @base_freq as a base to calculate the final frequency. Layer
81 * frequencies can overlap.
83 * Returns 0 if the layer was added successfully.
85 int ss_ins_add_layer(struct ss_ins * i, struct ss_wave * w)
87 struct ss_wave * l;
89 /* grow layers */
90 i->layers=(struct ss_wave *) realloc(i->layers,
91 (i->n_layers + 1) * sizeof(struct ss_wave));
93 l=&i->layers[i->n_layers];
95 memcpy(l, w, sizeof(struct ss_wave));
97 i->n_layers++;
99 return(0);
104 * ss_ins_set_channel_map - Sets the channel map for an instrument
105 * @i: the instrument
106 * @n_channels: number of channels in vol
107 * @vol: the channel volumes
109 * Sets the current channel map for an instrument. @vol holds volume values
110 * for upto @n_channels channels. The volume for the rest of channels
111 * (upto CHANNELS) is set to 0.
113 void ss_ins_set_channel_map(struct ss_ins * i, int n_channels, float vol[])
115 int n;
117 for(n=0;n < n_channels;n++)
118 i->vol[n] = vol[n];
120 for(;n < CHANNELS;n++)
121 i->vol[n] = 0.0;
126 * ss_ins_set_sustain - Sets the sustain for an instrument
127 * @i: the instrument
128 * @sustain: the sustain time
130 * Sets the sustain for an instrument. @sustain is expressed in frames.
132 void ss_ins_set_sustain(struct ss_ins * i, int sustain)
134 i->sustain=sustain;
139 * ss_ins_note_on - Plays a note.
140 * @i: the instrument
141 * @note: MIDI note to be played
142 * @vol: note volume
143 * @id: note id
145 * Locates a layer to play a note, and starts generators to
146 * play it. The @note is expressed as a MIDI note and the
147 * desired volume (from 0 to 1) stored in @vol. The note @id
148 * should be a unique identifier for this note; no two simultaneously
149 * playing notes should share this id.
151 * The channels of the found layers are distributed sequentially by
152 * using the instrument's channel map, skipping those with a volume
153 * of 0.0. So, for example, for a stereo layer with channels L and R
154 * and 6 channel output with volumes of 1 1 1 0 1 0, the channel
155 * mapping distribution will be L R L 0 R 0. If you want the fifth
156 * channel to be L, just use a virtually unhearable volume of 0.0001
157 * for the fourth one (but greater than 0).
159 * Returns the number of generators that were activated.
161 int ss_ins_note_on(struct ss_ins * i, int note, float vol, int note_id)
163 int n, m, f;
164 struct ss_wave * l;
165 struct ss_wave w;
166 float vols[CHANNELS];
167 double note_freq;
168 int notes=0;
169 struct ss_gen * g;
171 note_freq=note_frequency(note);
173 /* loop layers */
174 for(n=0;n < i->n_layers;n++)
176 l=&i->layers[n];
178 if(note_freq < l->min_freq || note_freq > l->max_freq)
179 continue;
181 /* get a free generator, or fail */
182 if((g=_ss_gen_get_free()) == NULL)
183 break;
185 /* enqueue the generator to this ins. queue */
186 ss_gen_enqueue(&i->gens, g);
188 memcpy(&w, l, sizeof(struct ss_wave));
190 /* assign the channels and their volumes */
191 for(f=m=0;f < CHANNELS;f++)
193 if(i->vol[f] > 0.0)
195 /* assign next channel of layer */
196 vols[f]=i->vol[f] * vol;
197 w.wave[f]=l->wave[m++];
199 if(m >= l->n_channels)
200 m=0;
202 else
203 w.wave[f]=NULL;
206 /* start the generator */
207 ss_gen_play(g, note_id, note_freq, vols, &w);
209 /* TEST: portamento */
210 if(i->trk_id == 2)
211 g->portamento=-0.000001;
213 notes++;
216 return(notes);
221 * ss_ins_note_off - Releases a note.
222 * @i: the instrument
223 * @id: the id of the note to be released
225 * Releases a note. The generators associated to it will enter release mode.
227 void ss_ins_note_off(struct ss_ins * i, int note_id)
229 struct ss_gen * g;
231 /* releases all generators with that note_id */
232 for(g=i->gens;g != NULL;g=g->next)
234 if(g->note_id == note_id)
235 ss_gen_release(g);
241 * ss_ins_frame - Generates a frame of samples.
242 * @trk_id: the track
243 * @sample: array where the output samples will be stored
245 * Generates a frame of samples mixing all the active generators
246 * of a track.
248 void ss_ins_frame(struct ss_ins * i, float frame[])
250 struct ss_gen * g;
251 struct ss_gen * t;
252 int n;
253 float l_frame[CHANNELS];
255 /* resets this local frame */
256 output_init_frame(l_frame);
258 /* loops through the generators */
259 for(g=i->gens;g != NULL;g=t)
261 t=g->next;
263 if(ss_gen_frame(g, l_frame))
265 /* generator has been freed; dequeue */
266 ss_gen_dequeue(&i->gens, g);
268 /* requeue back to free pool */
269 ss_gen_enqueue(&_ss_gen_free, g);
273 /* loops through the effects and remixes */
274 for(n=0;n < CHANNELS;n++)
275 frame[n] += ss_eff_process(i->effs[n], l_frame[n]);