recognize "All Sound Off" and "All Notes Off" MIDI events
[zyn.git] / util.c
blob77d8c9d416e1efa5c8c59f52095e5b3e8277dfcd
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*****************************************************************************
4 * Copyright (C) 2006,2007 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>
24 #include "common.h"
25 #include "util.h"
28 * Transform the velocity according the scaling parameter (velocity sensing)
30 float
31 VelF(REALTYPE velocity,unsigned char scaling)
33 float x;
35 x = (64.0 - scaling) / 64.0; /* 0 .. 127 -> 1 .. -1 */
37 x = pow(VELOCITY_MAX_SCALE, x);
39 if (scaling == 127 || velocity > 0.99)
41 return 1.0;
43 else
45 return pow(velocity, x);
49 float
50 zyn_velocity_scale(float velocity, float scaling)
52 float x;
54 x = pow(VELOCITY_MAX_SCALE, scaling);
56 if (scaling < -0.99 || velocity > 0.99)
58 return 1.0;
60 else
62 return pow(velocity, x);
67 * Get the detune in cents
69 REALTYPE
70 getdetune(
71 unsigned char type,
72 unsigned short int coarsedetune,
73 unsigned short int finedetune)
75 REALTYPE det=0.0,octdet=0.0,cdet=0.0,findet=0.0;
77 //Get Octave
78 int octave=coarsedetune/1024;
79 if (octave>=8) octave-=16;
80 octdet=octave*1200.0;
82 //Coarse and fine detune
83 int cdetune=coarsedetune%1024;
84 if (cdetune>512) cdetune-=1024;
86 int fdetune=finedetune-8192;
88 switch (type){
89 // case 1: is used for the default (see below)
90 case 2: cdet=fabs(cdetune*10.0);
91 findet=fabs(fdetune/8192.0)*10.0;
92 break;
93 case 3: cdet=fabs(cdetune*100);
94 findet=pow(10,fabs(fdetune/8192.0)*3.0)/10.0-0.1;
95 break;
96 case 4: cdet=fabs(cdetune*701.95500087); //perfect fifth
97 findet=(pow(2,fabs(fdetune/8192.0)*12.0)-1.0)/4095*1200;
98 break;
99 //case ...: need to update N_DETUNE_TYPES, if you'll add more
100 default:cdet=fabs(cdetune*50.0);
101 findet=fabs(fdetune/8192.0)*35.0;//almost like "Paul's Sound Designer 2"
102 break;
104 if (finedetune<8192) findet=-findet;
105 if (cdetune<0) cdet=-cdet;
107 det=octdet+cdet+findet;
108 return(det);
111 void
112 silence_two_buffers(
113 zyn_sample_type * buffer1,
114 zyn_sample_type * buffer2,
115 size_t size)
117 while (size)
119 size--;
120 buffer1[size] = 0.0;
121 buffer2[size] = 0.0;
125 void
126 mix_add_two_buffers(
127 zyn_sample_type * buffer_mix_1,
128 zyn_sample_type * buffer_mix_2,
129 zyn_sample_type * buffer1,
130 zyn_sample_type * buffer2,
131 size_t size)
133 while (size)
135 size--;
136 buffer_mix_1[size] += buffer1[size];
137 buffer_mix_2[size] += buffer2[size];
141 void
142 fadeout_two_buffers(
143 zyn_sample_type * buffer1,
144 zyn_sample_type * buffer2,
145 size_t size)
147 zyn_sample_type fade;
149 while (size)
151 fade = 1.0 - (zyn_sample_type)size / (zyn_sample_type)SOUND_BUFFER_SIZE;
152 size--;
153 buffer1[size] *= fade;
154 buffer2[size] *= fade;
158 void
159 copy_buffer(
160 zyn_sample_type * buffer_dest,
161 zyn_sample_type * buffer_src,
162 size_t size)
164 while (size)
166 size--;
167 buffer_dest[size] = buffer_src[size];
171 void
172 multiply_buffer(
173 zyn_sample_type * buffer,
174 float multiplyer,
175 size_t size)
177 while (size)
179 size--;
180 buffer[size] *= multiplyer;
184 float
185 zyn_random()
187 return rand() / (RAND_MAX + 1.0);