3 Ann Hell Ex Machina - Music Software
4 Copyright (C) 2003 Angel Ortega <angel@triptico.com>
6 instrument.c - 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
34 #include "generator.h"
35 #include "instrument.h"
41 /* note frequencies */
43 double _middle_A_freq
=440.0;
45 double _note_frequency
[NUM_NOTES
];
51 static void _build_note_frequencies(void)
59 for(n
=0;n
< NUM_NOTES
;n
++)
60 _note_frequency
[n
]=(_middle_A_freq
/ 32.0) *
61 pow(2.0, (((double)n
- 9.0) / 12.0));
67 void init_instrument(struct instrument
* i
)
69 float vol
[2]={ 1, 1 };
71 memset(i
, '\0', sizeof(struct instrument
));
73 /* sets the default channel mapping */
74 instrument_set_channel_map(i
, 2, vol
);
76 /* sets an 1% of frequency as sustain to avoid
78 instrument_set_sustain(i
, _frequency
/ 100);
83 * add_instrument_layer - Adds a layer to an instrument.
85 * @base_note: MIDI note number for the base note
86 * @min_note: minimum MIDI note number
87 * @max_note: maximum MIDI note number
88 * @n_channels: number of channels in wave
89 * @wave: the wave PCM data
90 * @size: size of the wave in samples
91 * @loop_start: sample number of the start of the loop (-1, no loop)
92 * @loop_end: sample number of the end of the loop
94 * Adds a layer to an instrument. The instrument will use this layer
95 * when asked to play a note in a range from @min_note to @max_note,
96 * using @base_note as a base to calculate the final frequency. Notes
97 * are expressed as MIDI notes (in a range from 0 to 127).
99 * Returns -1 if the instrument has already the maximum number of
100 * layers (controlled by the LAY_PER_INSTR constant), or 0 if
101 * everything went OK.
103 int add_instrument_layer(struct instrument
* i
,
104 int base_note
, int min_note
, int max_note
,
105 int n_channels
, float * wave
[],
106 double size
, double loop_start
, double loop_end
)
111 _build_note_frequencies();
114 if(i
->n_layers
== LAY_PER_INSTR
)
117 l
=&i
->layers
[i
->n_layers
];
119 l
->base_note
=base_note
;
120 l
->min_note
=min_note
;
121 l
->max_note
=max_note
;
123 l
->n_channels
=n_channels
;
125 for(n
=0;n
< l
->n_channels
;n
++)
129 l
->loop_start
=loop_start
;
130 l
->loop_end
=loop_end
;
138 void instrument_set_channel_map(struct instrument
* i
,
139 int n_channels
, float vol
[])
143 for(n
=0;n
< n_channels
;n
++)
146 for(;n
< CHANNELS
;n
++)
151 void instrument_set_sustain(struct instrument
* i
, int sustain
)
158 * instrument_note_on - Plays a note.
160 * @note: MIDI note to be played
164 * Locates a layer to play a note, and starts a generator to
165 * play it. The @note is expressed as a MIDI note and the
166 * desired volume (from 0 to 1) stored in @vol. The note @id
167 * should be a unique identifier for this note; no two simultaneously
168 * playing notes should share this id (you can use @note as @id
169 * if you don't plan to play the same note at the same time).
171 * The channels of the found layer are distributed sequentially by
172 * using the instrument's channel map, skipping those with a volume
173 * of 0.0. So, for example, for a stereo layer with channels L and R
174 * and 6 channel output with volumes of 1 1 1 0 1 0, the channel
175 * mapping distribution will be L R L 0 R 0. If you want the fifth
176 * channel to be L, just use a virtually unhearable volume of 0.0001
177 * (but greater than 0).
179 * Returns -1 if the note is already playing, -2 if no layer was found
180 * to play that note, -3 if the instrument went out of generators, or
181 * 0 if everything went OK.
183 int instrument_note_on(struct instrument
* i
, int note
, float vol
, int id
)
186 struct layer
* l
=NULL
;
187 struct generator
* g
=NULL
;
189 float vols
[CHANNELS
];
190 float * wave
[CHANNELS
];
192 /* test if note is already playing */
193 for(n
=0,f
=-1;n
< GEN_PER_INSTR
;n
++)
195 if(i
->id_gen
[n
] == id
+ 1)
198 /* if a free generator is found, store it */
199 if(f
== -1 && i
->generators
[n
].mode
== GEN_MODE_FREE
)
203 /* if no generator is free, fail */
207 /* find a layer containing the note */
208 for(n
=0;n
< i
->n_layers
;n
++)
212 if(l
->min_note
<= note
&& l
->max_note
>= note
)
216 /* no layer to play this note; fail */
217 if(l
== NULL
|| n
== i
->n_layers
) return(-2);
219 /* get generator and mark as used */
223 /* calculate increment */
224 inc
=_note_frequency
[note
] / _note_frequency
[l
->base_note
];
226 /* assign the channels and their volumes */
227 for(n
=m
=0;n
< CHANNELS
;n
++)
231 /* assign next channel of layer */
232 vols
[n
]=i
->vol
[n
] * vol
;
233 wave
[n
]=l
->wave
[m
++];
235 if(m
>= l
->n_channels
)
242 /* start the generator */
243 generator_play(g
, wave
, vols
, l
->size
, inc
,
244 l
->loop_start
, l
->loop_end
, i
->sustain
);
251 * instrument_note_off - Releases a note.
253 * @id: the id of the note to be released
257 * Returns -2 if note is not being played, or the output from
258 * generator_release() in any other case (that should probably
259 * be 0 or 1, meaning both OK).
261 int instrument_note_off(struct instrument
* i
, int id
)
264 struct generator
* g
;
266 /* find the generator that is playing this note */
267 for(n
=0;n
< GEN_PER_INSTR
;n
++)
269 if(i
->id_gen
[n
] == id
+ 1)
273 /* if note is not being played, return */
274 if(n
== GEN_PER_INSTR
)
277 /* detach the generator from the note (though it can
278 still generate sound because of sustain and such) */
281 /* release generator */
283 return(generator_release(g
));
288 * generate_instrument - Generates a frame of samples.
290 * @sample: array where the output samples will be stored
292 * Generates a frame of samples mixing all the active generators
295 void generate_instrument(struct instrument
* i
, float sample
[])
299 for(n
=0;n
< GEN_PER_INSTR
;n
++)
300 generator(&i
->generators
[n
], sample
);