Minot coding style fix
[zyn.git] / util.c
blob1c67feeaf4a120701008cff9f38b78aaef930571
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 unsigned char type,
73 unsigned short int coarsedetune,
74 unsigned short int finedetune)
76 REALTYPE octdet;
77 REALTYPE cdet;
78 REALTYPE findet;
80 // Get Octave
81 int octave = coarsedetune / 1024;
82 if (octave >= 8)
84 octave -= 16;
87 octdet = octave * 1200.0;
89 // Coarse and fine detune
90 int cdetune = coarsedetune % 1024;
91 if (cdetune > 512)
93 cdetune -= 1024;
96 int fdetune = finedetune - 8192;
98 switch (type)
100 case ZYN_DETUNE_TYPE_L35CENTS:
101 cdet = fabs(cdetune * 50.0);
102 findet = fabs(fdetune / 8192.0) * 35.0; // almost like "Paul's Sound Designer 2"
103 case ZYN_DETUNE_TYPE_L10CENTS:
104 cdet = fabs(cdetune * 10.0);
105 findet = fabs(fdetune / 8192.0) * 10.0;
106 break;
107 case ZYN_DETUNE_TYPE_E100CENTS:
108 cdet = fabs(cdetune * 100);
109 findet = pow(10, fabs(fdetune / 8192.0) * 3.0) / 10.0 - 0.1;
110 break;
111 case ZYN_DETUNE_TYPE_E1200CENTS:
112 cdet = fabs(cdetune * 701.95500087); // perfect fifth
113 findet = (pow(2, fabs(fdetune / 8192.0) * 12.0) - 1.0) / 4095 * 1200;
114 break;
115 default:
116 assert(0);
117 return 0;
120 if (finedetune < 8192)
122 findet = -findet;
125 if (cdetune < 0)
127 cdet = -cdet;
130 return octdet + cdet + findet;
133 void
134 silence_buffer(
135 zyn_sample_type * buffer,
136 size_t size)
138 while (size)
140 size--;
141 buffer[size] = 0.0;
145 void
146 silence_two_buffers(
147 zyn_sample_type * buffer1,
148 zyn_sample_type * buffer2,
149 size_t size)
151 while (size)
153 size--;
154 buffer1[size] = 0.0;
155 buffer2[size] = 0.0;
159 void
160 mix_add_two_buffers(
161 zyn_sample_type * buffer_mix_1,
162 zyn_sample_type * buffer_mix_2,
163 zyn_sample_type * buffer1,
164 zyn_sample_type * buffer2,
165 size_t size)
167 while (size)
169 size--;
170 buffer_mix_1[size] += buffer1[size];
171 buffer_mix_2[size] += buffer2[size];
175 void
176 fadeout_two_buffers(
177 zyn_sample_type * buffer1,
178 zyn_sample_type * buffer2,
179 size_t size)
181 zyn_sample_type fade;
183 while (size)
185 fade = 1.0 - (zyn_sample_type)size / (zyn_sample_type)SOUND_BUFFER_SIZE;
186 size--;
187 buffer1[size] *= fade;
188 buffer2[size] *= fade;
192 void
193 copy_buffer(
194 zyn_sample_type * buffer_dest,
195 zyn_sample_type * buffer_src,
196 size_t size)
198 while (size)
200 size--;
201 buffer_dest[size] = buffer_src[size];
205 void
206 multiply_buffer(
207 zyn_sample_type * buffer,
208 float multiplyer,
209 size_t size)
211 while (size)
213 size--;
214 buffer[size] *= multiplyer;
218 float
219 zyn_random()
221 return rand() / (RAND_MAX + 1.0);