1 /*
2 TiMidity++ -- MIDI to WAVE converter and player
3 Copyright (C) 1999-2002 Masanao Izumo <mo@goice.co.jp>
4 Copyright (C) 1995 Tuukka Toivonen <tt@cgs.fi>
5
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; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 /*================================================================
22 * SBK --> SF2 Conversion
23 *
24 * Copyright (C) 1996,1997 Takashi Iwai
25 *
26 * This program is free software; you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License as published by
28 * the Free Software Foundation; either version 2 of the License, or
29 * (at your option) any later version.
30 *
31 * This program is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 * GNU General Public License for more details.
35 *
36 * You should have received a copy of the GNU General Public License
37 * along with this program; if not, write to the Free Software
38 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
39 *================================================================*/
40
41 #include <stdio.h>
42 #include <math.h>
43 #include "common.h"
44 #include "sfitem.h"
45
46
47 namespace TimidityPlus
48 {
49
50 /*----------------------------------------------------------------
51 * prototypes
52 *----------------------------------------------------------------*/
53
54 static int sbk_cutoff(int gen, int val);
55 static int sbk_filterQ(int gen, int val);
56 static int sbk_tenpct(int gen, int val);
57 static int sbk_panpos(int gen, int val);
58 static int sbk_atten(int gen, int val);
59 static int sbk_scale(int gen, int val);
60 static int sbk_time(int gen, int val);
61 static int sbk_tm_key(int gen, int val);
62 static int sbk_freq(int gen, int val);
63 static int sbk_pshift(int gen, int val);
64 static int sbk_cshift(int gen, int val);
65 static int sbk_tremolo(int gen, int val);
66 static int sbk_volsust(int gen, int val);
67 static int sbk_modsust(int gen, int val);
68
69 /*----------------------------------------------------------------
70 * convertor function table
71 *----------------------------------------------------------------*/
72
73 static SBKConv sbk_convertors[T_EOT] = {
74 NULL, NULL, NULL, NULL, NULL,
75
76 sbk_cutoff, sbk_filterQ, sbk_tenpct, sbk_panpos, sbk_atten, sbk_scale,
77
78 sbk_time, sbk_tm_key, sbk_freq, sbk_pshift, sbk_cshift,
79 sbk_tremolo, sbk_modsust, sbk_volsust,
80 };
81
82
83 /*----------------------------------------------------------------
84 * sbk --> sf2 conversion
85 *----------------------------------------------------------------*/
86
sbk_to_sf2(int oper,int amount,const LayerItem * layer_items)87 int sbk_to_sf2(int oper, int amount, const LayerItem *layer_items)
88 {
89 const LayerItem *item = &layer_items[oper];
90 if (item->type < 0 || item->type >= T_EOT) {
91 fprintf(stderr, "illegal gen item type %d\n", item->type);
92 return amount;
93 }
94 if (sbk_convertors[item->type])
95 return sbk_convertors[item->type](oper, amount);
96 return amount;
97 }
98
99 /*----------------------------------------------------------------
100 * conversion rules for each type
101 *----------------------------------------------------------------*/
102
103 /* initial cutoff */
sbk_cutoff(int gen,int val)104 static int sbk_cutoff(int gen, int val)
105 {
106 if (val == 127)
107 return 14400;
108 else
109 return 59 * val + 4366;
110 /*return 50 * val + 4721;*/
111 }
112
113 /* initial resonance */
sbk_filterQ(int gen,int val)114 static int sbk_filterQ(int gen, int val)
115 {
116 return val * 3 / 2;
117 }
118
119 /* chorus/reverb */
sbk_tenpct(int gen,int val)120 static int sbk_tenpct(int gen, int val)
121 {
122 return val * 1000 / 256;
123 }
124
125 /* pan position */
sbk_panpos(int gen,int val)126 static int sbk_panpos(int gen, int val)
127 {
128 return val * 1000 / 127 - 500;
129 }
130
131 /* initial attenuation */
sbk_atten(int gen,int val)132 static int sbk_atten(int gen, int val)
133 {
134 if (val == 0)
135 return 1000;
136 return (int)(-200.0 * log10((double)val / 127.0) * 10);
137 }
138
139 /* scale tuning */
sbk_scale(int gen,int val)140 static int sbk_scale(int gen, int val)
141 {
142 return (val ? 50 : 100);
143 }
144
145 /* env/lfo time parameter */
sbk_time(int gen,int val)146 static int sbk_time(int gen, int val)
147 {
148 if (val <= 0) val = 1;
149 return (int)(log((double)val / 1000.0) / log(2.0) * 1200.0);
150 }
151
152 /* time change per key */
sbk_tm_key(int gen,int val)153 static int sbk_tm_key(int gen, int val)
154 {
155 return (int)(val * 5.55);
156 }
157
158 /* lfo frequency */
sbk_freq(int gen,int val)159 static int sbk_freq(int gen, int val)
160 {
161 if (val == 0) {
162 if (gen == SF_freqLfo1) return -725;
163 else /* SF_freqLfo2*/ return -15600;
164 }
165 /*return (int)(3986.0 * log10((double)val) - 7925.0);*/
166 return (int)(1200 * log10((double)val) / log10(2.0) - 7925.0);
167
168 }
169
170 /* lfo/env pitch shift */
sbk_pshift(int gen,int val)171 static int sbk_pshift(int gen, int val)
172 {
173 return (1200 * val / 64 + 1) / 2;
174 }
175
176 /* lfo/env cutoff freq shift */
sbk_cshift(int gen,int val)177 static int sbk_cshift(int gen, int val)
178 {
179 if (gen == SF_lfo1ToFilterFc)
180 return (1200 * 3 * val) / 64;
181 else
182 return (1200 * 6 * val) / 64;
183 }
184
185 /* lfo volume shift */
sbk_tremolo(int gen,int val)186 static int sbk_tremolo(int gen, int val)
187 {
188 return (120 * val) / 64;
189 }
190
191 /* mod env sustain */
sbk_modsust(int gen,int val)192 static int sbk_modsust(int gen, int val)
193 {
194 if (val < 96)
195 return 1000 * (96 - val) / 96;
196 else
197 return 0;
198 }
199
200 /* vol env sustain */
sbk_volsust(int gen,int val)201 static int sbk_volsust(int gen, int val)
202 {
203 if (val < 96)
204 return (2000 - 21 * val) / 2;
205 else
206 return 0;
207 }
208 }