Updated dependencies.
[ahxm.git] / instrument.c
blob30321367a532ab25a30dbf20808e8d17c11a692b
1 /*
3 PROGRAM_NAME PROGRAM_VERSION - PROGRAM_DESCRIPTION
5 Copyright (C) 2003 Angel Ortega <angel@triptico.com>
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 http://www.triptico.com
25 #include "config.h"
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <math.h>
31 #include "generator.h"
32 #include "instrument.h"
34 /*******************
35 Data
36 ********************/
38 /* note frequencies */
40 double _middle_A_freq=440.0;
42 double _note_frequency[NUM_NOTES];
44 /*******************
45 Code
46 ********************/
48 void build_note_frequencies(void)
50 int n;
52 for(n=0;n < NUM_NOTES;n++)
53 _note_frequency[n]=(_middle_A_freq / 32.0) *
54 pow(2.0, (((double)n - 9.0) / 12.0));
58 void init_instrument(struct instrument * i)
60 memset(i, '\0', sizeof(struct instrument));
64 int add_instrument_layer(struct instrument * i, int base_note,
65 int min_note, int max_note, int * lwave, int * rwave,
66 int size, int loop_start, int loop_end, int sustain)
68 struct layer * l;
70 if(i->n_layers == LAYERS_PER_INSTR)
71 return(0);
73 l=&i->layers[i->n_layers];
75 l->base_note=base_note;
76 l->min_note=min_note;
77 l->max_note=max_note;
78 l->lwave=lwave;
79 l->rwave=rwave;
80 l->size=size;
81 l->loop_start=loop_start;
82 l->loop_end=loop_end;
83 l->sustain=sustain;
85 i->n_layers++;
87 return(1);
91 int instrument_play_note(struct instrument * i, int note, double lvol, double rvol)
93 int n;
94 struct layer * l;
95 struct generator * g;
96 double inc;
98 /* test if note is already playing */
99 if(i->note_gen[note])
100 return(0);
102 /* find a layer containing the note */
103 for(n=0;n < i->n_layers;n++)
105 l=&i->layers[n];
107 if(l->min_note <= note && l->max_note >= note)
108 break;
111 /* no layer to play this note; fail */
112 if(n == i->n_layers) return(0);
114 /* find now an empty generator */
115 for(n=0;n < GENERATORS_PER_INSTR;n++)
117 g=&i->generators[n];
119 if(g->mode == GEN_MODE_FREE)
120 break;
123 /* no free generator; also fail */
124 if(n == GENERATORS_PER_INSTR)
125 return(0);
127 /* store the generator */
128 i->note_gen[note]=n + 1;
130 /* calculate increment */
131 inc=_note_frequency[note] / _note_frequency[l->base_note];
133 /* start the generator */
134 generator_play(g, l->lwave, l->rwave, l->size, inc,
135 l->loop_start, l->loop_end, lvol, rvol, l->sustain);
137 return(1);
141 int instrument_release_note(struct instrument * i, int note)
143 int n;
144 struct generator * g;
146 /* do nothing is this note is not playing */
147 if((n=i->note_gen[note])==0)
148 return(1);
150 /* release generator */
151 g=&i->generators[n - 1];
152 generator_release(g);
154 /* detach the generator to the note (though it can
155 still be generating sound because of sustain and such) */
156 i->note_gen[note]=0;
158 return(1);
162 void generate_instrument(struct instrument * i, int * lsample, int * rsample)
164 int n;
166 for(n=0;n < GENERATORS_PER_INSTR;n++)
167 generator(&i->generators[n], lsample, rsample);