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
31 #include "generator.h"
32 #include "instrument.h"
38 /* note frequencies */
40 double _middle_A_freq
=440.0;
42 double _note_frequency
[NUM_NOTES
];
48 void build_note_frequencies(void)
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
)
70 if(i
->n_layers
== LAYERS_PER_INSTR
)
73 l
=&i
->layers
[i
->n_layers
];
75 l
->base_note
=base_note
;
81 l
->loop_start
=loop_start
;
91 int instrument_play_note(struct instrument
* i
, int note
, double lvol
, double rvol
)
98 /* test if note is already playing */
102 /* find a layer containing the note */
103 for(n
=0;n
< i
->n_layers
;n
++)
107 if(l
->min_note
<= note
&& l
->max_note
>= note
)
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
++)
119 if(g
->mode
== GEN_MODE_FREE
)
123 /* no free generator; also fail */
124 if(n
== GENERATORS_PER_INSTR
)
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
);
141 int instrument_release_note(struct instrument
* i
, int note
)
144 struct generator
* g
;
146 /* do nothing is this note is not playing */
147 if((n
=i
->note_gen
[note
])==0)
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) */
162 void generate_instrument(struct instrument
* i
, int * lsample
, int * rsample
)
166 for(n
=0;n
< GENERATORS_PER_INSTR
;n
++)
167 generator(&i
->generators
[n
], lsample
, rsample
);