Expose voice detune parameter of the lv2 plugin
[zyn.git] / util.c
blobb9bd531cb43cd30ba2cc9369fa1f1cf7c6778ea4
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*****************************************************************************
4 * Copyright (C) 2006,2007,2008,2009 Nedko Arnaudov <nedko@arnaudov.name>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *****************************************************************************/
21 #include <math.h>
22 #include <stdlib.h>
23 #include <assert.h>
25 #include "common.h"
26 #include "util.h"
29 * Transform the velocity according the scaling parameter (velocity sensing)
31 float
32 VelF(REALTYPE velocity,unsigned char scaling)
34 float x;
36 x = (64.0 - scaling) / 64.0; /* 0 .. 127 -> 1 .. -1 */
38 x = pow(VELOCITY_MAX_SCALE, x);
40 if (scaling == 127 || velocity > 0.99)
42 return 1.0;
44 else
46 return pow(velocity, x);
50 float
51 zyn_velocity_scale(float velocity, float scaling)
53 float x;
55 x = pow(VELOCITY_MAX_SCALE, scaling);
57 if (scaling < -0.99 || velocity > 0.99)
59 return 1.0;
61 else
63 return pow(velocity, x);
68 * Get the detune in cents
70 REALTYPE
71 zyn_get_detune(
72 signed int type,
73 signed int octave,
74 signed int coarse,
75 float fine)
77 REALTYPE cdet;
78 REALTYPE findet;
80 switch (type)
82 case ZYN_DETUNE_TYPE_L35CENTS:
83 cdet = coarse * 50.0;
84 findet = fabs(fine) * 35.0; // almost like "Paul's Sound Designer 2"
85 case ZYN_DETUNE_TYPE_L10CENTS:
86 cdet = fabs(coarse * 10.0);
87 findet = fabs(fine) * 10.0;
88 break;
89 case ZYN_DETUNE_TYPE_E100CENTS:
90 cdet = coarse * 100;
91 findet = pow(10, fabs(fine) * 3.0) / 10.0 - 0.1;
92 break;
93 case ZYN_DETUNE_TYPE_E1200CENTS:
94 cdet = coarse * 701.95500087; // perfect fifth
95 findet = (pow(2, fabs(fine) * 12.0) - 1.0) / 4095 * 1200;
96 break;
97 default:
98 assert(0);
99 return 0;
102 /* we compute detune using fabs(fine) so we need to adjust detune sign */
103 if (fine < 0.0)
105 findet = -findet;
108 return octave * 1200.0 + cdet + findet;
111 void
112 silence_buffer(
113 zyn_sample_type * buffer,
114 size_t size)
116 while (size)
118 size--;
119 buffer[size] = 0.0;
123 void
124 silence_two_buffers(
125 zyn_sample_type * buffer1,
126 zyn_sample_type * buffer2,
127 size_t size)
129 while (size)
131 size--;
132 buffer1[size] = 0.0;
133 buffer2[size] = 0.0;
137 void
138 mix_add_two_buffers(
139 zyn_sample_type * buffer_mix_1,
140 zyn_sample_type * buffer_mix_2,
141 zyn_sample_type * buffer1,
142 zyn_sample_type * buffer2,
143 size_t size)
145 while (size)
147 size--;
148 buffer_mix_1[size] += buffer1[size];
149 buffer_mix_2[size] += buffer2[size];
153 void
154 fadeout_two_buffers(
155 zyn_sample_type * buffer1,
156 zyn_sample_type * buffer2,
157 size_t size)
159 zyn_sample_type fade;
161 while (size)
163 fade = 1.0 - (zyn_sample_type)size / (zyn_sample_type)SOUND_BUFFER_SIZE;
164 size--;
165 buffer1[size] *= fade;
166 buffer2[size] *= fade;
170 void
171 copy_buffer(
172 zyn_sample_type * buffer_dest,
173 zyn_sample_type * buffer_src,
174 size_t size)
176 while (size)
178 size--;
179 buffer_dest[size] = buffer_src[size];
183 void
184 multiply_buffer(
185 zyn_sample_type * buffer,
186 float multiplyer,
187 size_t size)
189 while (size)
191 size--;
192 buffer[size] *= multiplyer;
196 float
197 zyn_random()
199 return rand() / (RAND_MAX + 1.0);