1 /* WhySynth DSSI software synthesizer plugin
2  *
3  * Copyright (C) 2005-2006 Sean Bolton.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of
8  * the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be
11  * useful, but WITHOUT ANY WARRANTY; without even the implied
12  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13  * PURPOSE.  See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free
17  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301 USA.
19  */
20 
21 /* -FIX- add some more-efficient versions of the common ranges (-1..+1, -.5..+.5, 0..+1) */
22 static inline float
random_float(float lower_bound,float range)23 random_float(float lower_bound, float range)
24 {
25     /* -FIX- bit-shifting is not the most efficient way to generate noise on some platforms....? */
26     return lower_bound + range * ((float)random() / (float)RAND_MAX);
27 }
28 
29 /*
30  * volume_cv_to_amplitude
31  */
32 static inline float
volume_cv_to_amplitude(float cv)33 volume_cv_to_amplitude(float cv)
34 {
35     int i;
36     float f;
37 
38     cv *= 100.0f;
39     if (cv > 127.0f) cv = 127.0f;
40     else if (cv < -127.0f) cv = -127.0f;
41     i = lrintf(cv - 0.5f);
42     f = cv - (float)i;
43     return volume_cv_to_amplitude_table[i + 128] + f *
44            (volume_cv_to_amplitude_table[i + 129] -
45             volume_cv_to_amplitude_table[i + 128]);
46 }
47 
48 /*
49  * pitch_to_frequency
50  */
51 static inline float
pitch_to_frequency(float pitch)52 pitch_to_frequency(float pitch)
53 {
54     int i = lrintf(pitch - 0.5f);
55     float f = pitch - (float)i;
56     i &= 0x7f;
57     /* -FIX- this probably isn't accurate enough! */
58     return y_pitch[i] + f * (y_pitch[i + 1] - y_pitch[i]);
59 }
60 
61 /*
62  * y_voice_mod_index
63  */
64 static inline int
y_voice_mod_index(LADSPA_Data * p)65 y_voice_mod_index(LADSPA_Data *p)
66 {
67     int i = lrintf(*p);
68 
69     if (i < 0 || i >= Y_MODS_COUNT)
70         return 0;
71 
72     return i;
73 }
74 
75 /*
76  * wavetable_select
77  */
78 static inline void
wavetable_select(struct vosc * vosc,int key)79 wavetable_select(struct vosc *vosc, int key)
80 {
81     int i;
82 
83     if (key > 256) key = 256;
84     vosc->wave_select_key = key;
85 
86     i = 0;
87     while (i < WAVETABLE_MAX_WAVES) {
88         if (key <= wavetable[vosc->waveform].wave[i].max_key)
89             break;
90         i++;
91     }
92     if ((wavetable[vosc->waveform].wave[i].max_key - key >=
93             WAVETABLE_CROSSFADE_RANGE) ||
94         wavetable[vosc->waveform].wave[i].max_key == 256) {
95         /* printf("vosc %p: new wave index %d (sel = %d) no crossfade\n", vosc, i, key); */
96         vosc->wave0 = wavetable[vosc->waveform].wave[i].data;
97         vosc->wave1 = wavetable[vosc->waveform].wave[i].data;
98         vosc->wavemix0 = 1.0f;
99         vosc->wavemix1 = 0.0f;
100     } else {
101         vosc->wave0 = wavetable[vosc->waveform].wave[i].data;
102         vosc->wave1 = wavetable[vosc->waveform].wave[i + 1].data;
103         vosc->wavemix0 = (float)(wavetable[vosc->waveform].wave[i].max_key - key + 1) /
104                                  (float)(WAVETABLE_CROSSFADE_RANGE + 1);
105         vosc->wavemix1 = 1.0f - vosc->wavemix0;
106         /* printf("vosc %p: new wave index %d (sel = %d) CROSSFADE = %f/%f\n", vosc, i, key, vosc->wavemix0, vosc->wavemix1); */
107     }
108 }
109 
110