Expose "enabled" bool param of voice
[zyn.git] / envelope_parameters.cpp
blobeafd2f2d25129ecefd308a2d45c3b816dfefda8e
1 /*
2 ZynAddSubFX - a software synthesizer
4 EnvelopeParams.C - Parameters for Envelope
5 Copyright (C) 2002-2005 Nasca Octavian Paul
6 Author: Nasca Octavian Paul
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of version 2 of the GNU General Public License
10 as published by the Free Software Foundation.
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 (version 2) for more details.
17 You should have received a copy of the GNU General Public License (version 2)
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <math.h> // pow()
24 #include <assert.h>
26 #include "globals.h"
27 #include "envelope_parameters.h"
29 EnvelopeParams::EnvelopeParams()
31 int i;
33 for (i = 0 ; i < MAX_ENVELOPE_POINTS ; i++)
35 Penvdt[i] = 32;
36 m_values[i] = 64;
39 Penvdt[0] = 0; // no used
40 Penvsustain = 1;
41 Penvpoints = 1;
42 m_stretch = 64;
43 m_forced_release = true;
44 m_linear = false;
46 m_attack_duration_index = -1;
47 m_decay_duration_index = -1;
48 m_release_duration_index = -1;
50 m_attack_value_index = -1;
51 m_decay_value_index = -1;
52 m_sustain_value_index = -1;
53 m_release_value_index = -1;
56 EnvelopeParams::~EnvelopeParams()
60 REALTYPE EnvelopeParams::getdt(unsigned char i)
62 return (pow(2.0 , Penvdt[i] / 127.0 * 12.0) - 1.0) * 10.0; // miliseconds
65 void EnvelopeParams::set_point_value(int i, unsigned char value)
67 m_values_params[i] = value;
69 switch (m_mode)
71 case ZYN_ENVELOPE_MODE_ADSR:
72 if (m_linear)
74 m_values[i] = value / 127.0;
76 else
78 m_values[i] = (1.0 - value / 127.0) * MIN_ENVELOPE_DB;
80 break;
81 case ZYN_ENVELOPE_MODE_ASR:
82 m_values[i] = (pow(2,6.0 * fabs(value - 64.0) / 64.0) - 1.0) * 100.0;
83 if (value < 64)
85 m_values[i] = -m_values[i];
87 break;
88 case ZYN_ENVELOPE_MODE_ADSR_FILTER:
89 m_values[i] = (value - 64.0) / 64.0 * 6.0; // 6 octaves (filtru)
90 break;
91 case ZYN_ENVELOPE_MODE_ASR_BW:
92 m_values[i] = (value - 64.0) / 64.0 * 10;
93 break;
94 default:
95 assert(0);
99 void
100 EnvelopeParams::init_adsr(
101 unsigned char stretch,
102 bool forced_release,
103 char attack_duration,
104 char decay_duration,
105 char sustain_value,
106 char release_duration,
107 bool linear)
109 m_stretch = stretch;
110 m_forced_release = forced_release;
111 m_linear = linear;
112 m_mode = ZYN_ENVELOPE_MODE_ADSR;
114 Penvpoints = 4;
115 Penvsustain = 2;
117 set_point_value(0, 0);
119 m_attack_duration_index = 1;
120 Penvdt[1] = attack_duration;
122 set_point_value(1, 127);
124 Penvdt[2] = decay_duration;
125 m_decay_duration_index = 2;
127 set_point_value(2, sustain_value);
128 m_sustain_value_index = 2;
130 Penvdt[3] = release_duration;
131 m_release_duration_index = 3;
133 set_point_value(3, 0);
136 void
137 EnvelopeParams::init_asr(
138 unsigned char stretch,
139 bool forced_release,
140 char attack_value,
141 char attack_duration,
142 char release_value,
143 char release_duration)
145 m_stretch = stretch;
146 m_forced_release = forced_release;
147 m_mode = ZYN_ENVELOPE_MODE_ASR;
149 Penvpoints = 3;
150 Penvsustain = 1;
152 set_point_value(0, attack_value);
153 m_attack_value_index = 0;
155 Penvdt[1] = attack_duration;
156 m_attack_duration_index = 1;
158 set_point_value(1, 64);
160 set_point_value(2, release_value);
161 m_release_value_index = 2;
163 Penvdt[2] = release_duration;
164 m_release_duration_index = 2;
167 void
168 EnvelopeParams::init_adsr_filter(
169 unsigned char stretch,
170 bool forced_release,
171 char attack_value,
172 char attack_duration,
173 char decay_value,
174 char decay_duration,
175 char release_value,
176 char release_duration)
178 m_stretch = stretch;
179 m_forced_release = forced_release;
180 m_mode = ZYN_ENVELOPE_MODE_ADSR_FILTER;
182 Penvpoints = 4;
183 Penvsustain = 2;
185 set_point_value(0, attack_value);
186 m_attack_value_index = 0;
188 Penvdt[1] = attack_duration;
189 m_attack_duration_index = 1;
191 set_point_value(1, decay_value);
192 m_decay_value_index = 1;
194 Penvdt[2] = decay_duration;
195 m_decay_duration_index = 2;
197 set_point_value(2, 64);
199 Penvdt[3] = release_duration;
200 m_release_duration_index = 3;
202 set_point_value(3, release_value);
203 m_release_value_index = 3;
206 void
207 EnvelopeParams::init_asr_bw(
208 unsigned char stretch,
209 bool forced_release,
210 char attack_value,
211 char attack_duration,
212 char release_value,
213 char release_duration)
215 m_stretch = stretch;
216 m_forced_release = forced_release;
217 m_mode = ZYN_ENVELOPE_MODE_ASR_BW;
219 Penvpoints = 3;
220 Penvsustain = 1;
222 set_point_value(0, attack_value);
223 m_attack_value_index = 0;
225 set_point_value(1, 64);
227 Penvdt[1] = attack_duration;
228 m_attack_duration_index = 1;
230 Penvdt[2] = release_duration;
231 m_release_duration_index = 2;
233 set_point_value(2, release_value);
234 m_release_value_index = 2;
237 unsigned char
238 EnvelopeParams::get_value(
239 int index)
241 assert(index >= 0);
242 assert(index < MAX_ENVELOPE_POINTS);
243 return m_values_params[index];
246 void
247 EnvelopeParams::set_value(
248 int index,
249 unsigned char value)
251 assert(index >= 0);
252 assert(index < MAX_ENVELOPE_POINTS);
253 set_point_value(index, value);
256 unsigned char
257 EnvelopeParams::get_duration(
258 int index)
260 assert(index >= 0);
261 assert(index < MAX_ENVELOPE_POINTS);
262 return Penvdt[index];
265 void
266 EnvelopeParams::set_duration(
267 int index,
268 unsigned char duration)
270 assert(index >= 0);
271 assert(index < MAX_ENVELOPE_POINTS);
272 Penvdt[index] = duration;