1 /*
2 
3     TiMidity -- Experimental MIDI to WAVE converter
4     Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.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., 675 Mass Ave, Cambridge, MA 02139, USA.
19 
20 */
21 
22 #ifdef USE_TIMIDITY_MIDI
23 
24 #ifndef TIMIDITY_H_INCLUDED
25 #define TIMIDITY_H_INCLUDED
26 
27 #define NS_TIMIDITY Timidity_Pentagram
28 
29 #ifdef NS_TIMIDITY
30 namespace NS_TIMIDITY {
31 #endif
32 
33 struct MidiSong;
34 
35 //extern int Timidity_Init(int rate, int format, int channels, int samples);
36 extern char *Timidity_Error();
37 extern void Timidity_SetVolume(int volume);
38 extern int Timidity_PlaySome(void *stream, int samples);
39 extern MidiSong *Timidity_LoadSong(char *midifile);
40 extern void Timidity_Start(MidiSong *song);
41 extern int Timidity_Active();
42 extern void Timidity_Stop();
43 extern void Timidity_FreeSong(MidiSong *song);
44 
45 
46 /* Data format encoding bits */
47 
48 #define PE_MONO 	0x01  /* versus stereo */
49 #define PE_SIGNED	0x02  /* versus unsigned */
50 #define PE_16BIT 	0x04  /* versus 8-bit */
51 #define PE_ULAW 	0x08  /* versus linear */
52 #define PE_BYTESWAP	0x10  /* versus the other way */
53 
54 int Timidity_Init_Simple(int rate, int samples, sint32 encoding);
55 void Timidity_DeInit();
56 extern void Timidity_FinalInit(const bool patches[128], const bool drums[128]);
57 extern void Timidity_PlayEvent(unsigned char status, int a, int b);
58 extern void Timidity_GenerateSamples(void *stream, int samples);
59 
60 
61 /* This is for use with the SDL library */
62 #define SDL
63 #if (defined(WIN32) || defined(_WIN32)) && !defined(__WIN32__)
64 #define __WIN32__
65 #endif
66 
67 
68 /* When a patch file can't be opened, one of these extensions is
69    appended to the filename and the open is tried again.
70  */
71 #define PATCH_EXT_LIST { ".pat", nullptr }
72 
73 /* Acoustic Grand Piano seems to be the usual default instrument. */
74 #define DEFAULT_PROGRAM 0
75 
76 /* 9 here is MIDI channel 10, which is the standard percussion channel.
77    Some files (notably C:\WINDOWS\CANYON.MID) think that 16 is one too.
78    On the other hand, some files know that 16 is not a drum channel and
79    try to play music on it. This is now a runtime option, so this isn't
80    a critical choice anymore. */
81 #define DEFAULT_DRUMCHANNELS (1<<9)
82 
83 /* A somewhat arbitrary frequency range. The low end of this will
84    sound terrible as no lowpass filtering is performed on most
85    instruments before resampling. */
86 #define MIN_OUTPUT_RATE 	4000
87 #define MAX_OUTPUT_RATE 	65000
88 
89 /* In percent. */
90 #define DEFAULT_AMPLIFICATION 	70
91 
92 /* Default sampling rate, default polyphony, and maximum polyphony.
93    All but the last can be overridden from the command line. */
94 #define DEFAULT_RATE	32000
95 #define DEFAULT_VOICES	32
96 #define MAX_VOICES	48
97 
98 /* 1000 here will give a control ratio of 22:1 with 22 kHz output.
99    Higher CONTROLS_PER_SECOND values allow more accurate rendering
100    of envelopes and tremolo. The cost is CPU time. */
101 #define CONTROLS_PER_SECOND 1000
102 
103 /* Strongly recommended. This option increases CPU usage by half, but
104    without it sound quality is very poor. */
105 #define LINEAR_INTERPOLATION
106 
107 /* This is an experimental kludge that needs to be done right, but if
108    you've got an 8-bit sound card, or cheap multimedia speakers hooked
109    to your 16-bit output device, you should definitely give it a try.
110 
111    Defining LOOKUP_HACK causes table lookups to be used in mixing
112    instead of multiplication. We convert the sample data to 8 bits at
113    load time and volumes to logarithmic 7-bit values before looking up
114    the product, which degrades sound quality noticeably.
115 
116    Defining LOOKUP_HACK should save ~20% of CPU on an Intel machine.
117    LOOKUP_INTERPOLATION might give another ~5% */
118 /* #define LOOKUP_HACK
119    #define LOOKUP_INTERPOLATION */
120 
121 /* Make envelopes twice as fast. Saves ~20% CPU time (notes decay
122    faster) and sounds more like a GUS. There is now a command line
123    option to toggle this as well. */
124 //#define FAST_DECAY
125 
126 /* How many bits to use for the fractional part of sample positions.
127    This affects tonal accuracy. The entire position counter must fit
128    in 32 bits, so with FRACTION_BITS equal to 12, the maximum size of
129    a sample is 1048576 samples (2 megabytes in memory). The GUS gets
130    by with just 9 bits and a little help from its friends...
131    "The GUS does not SUCK!!!" -- a happy user :) */
132 #define FRACTION_BITS 12
133 
134 #define MAX_SAMPLE_SIZE (1 << (32-FRACTION_BITS))
135 
136 /* For some reason the sample volume is always set to maximum in all
137    patch files. Define this for a crude adjustment that may help
138    equalize instrument volumes. */
139 #define ADJUST_SAMPLE_VOLUMES
140 
141 /* The number of samples to use for ramping out a dying note. Affects
142    click removal. */
143 #define MAX_DIE_TIME 20
144 
145 /* On some machines (especially PCs without math coprocessors),
146    looking up sine values in a table will be significantly faster than
147    computing them on the fly. Uncomment this to use lookups. */
148 /* #define LOOKUP_SINE */
149 
150 /* Shawn McHorse's resampling optimizations. These may not in fact be
151    faster on your particular machine and compiler. You'll have to run
152    a benchmark to find out. */
153 #define PRECALC_LOOPS
154 
155 /* If calling ldexp() is faster than a floating point multiplication
156    on your machine/compiler/libm, uncomment this. It doesn't make much
157    difference either way, but hey -- it was on the TODO list, so it
158    got done. */
159 /* #define USE_LDEXP */
160 
161 /**************************************************************************/
162 /* Anything below this shouldn't need to be changed unless you're porting
163    to a new machine with other than 32-bit, big-endian words. */
164 /**************************************************************************/
165 
166 /* change FRACTION_BITS above, not these */
167 #define INTEGER_BITS (32 - FRACTION_BITS)
168 #define INTEGER_MASK (0xFFFFFFFF << FRACTION_BITS)
169 #define FRACTION_MASK (~ INTEGER_MASK)
170 
171 /* This is enforced by some computations that must fit in an int */
172 #define MAX_CONTROL_RATIO 255
173 
174 /* Byte order, defined in <machine/endian.h> for FreeBSD and DEC OSF/1 */
175 #ifdef DEC
176 #include <machine/endian.h>
177 #endif
178 
179 #ifdef linux
180 /*
181  * Byte order is defined in <bytesex.h> as __BYTE_ORDER, that need to
182  * be checked against __LITTLE_ENDIAN and __BIG_ENDIAN defined in <endian.h>
183  * <endian.h> includes automagically <bytesex.h>
184  * for Linux.
185  */
186 #include <endian.h>
187 
188 
189 # if __BYTE_ORDER == __LITTLE_ENDIAN
190 #  define TIMIDITY_LITTLE_ENDIAN
191 # elif __BYTE_ORDER == __BIG_ENDIAN
192 #  define TIMIDITY_BIG_ENDIAN
193 # else
194 # error No byte sex defined
195 # endif
196 #endif /* linux */
197 
198 /* Win32 on Intel machines */
199 #ifdef __WIN32__
200 #  define TIMIDITY_LITTLE_ENDIAN
201 #endif
202 
203 #ifdef i386
204 #define TIMIDITY_LITTLE_ENDIAN
205 #endif
206 
207 /* Instrument files are little-endian, MIDI files big-endian, so we
208    need to do some conversions. */
209 
210 #define XCHG_SHORT(x) ((((x)&0xFF)<<8) | (((x)>>8)&0xFF))
211 # define XCHG_LONG(x) ((((x)&0xFF)<<24) | \
212 		      (((x)&0xFF00)<<8) | \
213 		      (((x)&0xFF0000)>>8) | \
214 		      (((x)>>24)&0xFF))
215 
216 #ifdef TIMIDITY_LITTLE_ENDIAN
217 #define LE_SHORT(x) x
218 #define LE_LONG(x) x
219 #define BE_SHORT(x) XCHG_SHORT(x)
220 #define BE_LONG(x) XCHG_LONG(x)
221 #else
222 #define BE_SHORT(x) x
223 #define BE_LONG(x) x
224 #define LE_SHORT(x) XCHG_SHORT(x)
225 #define LE_LONG(x) XCHG_LONG(x)
226 #endif
227 
228 #define MAX_AMPLIFICATION 800
229 
230 /* You could specify a complete path, e.g. "/etc/timidity.cfg", and
231    then specify the library directory in the configuration file. */
232 #define CONFIG_FILE	"timidity.cfg"
233 #ifndef DEFAULT_TIMIDITY_PATH
234 #ifdef __WIN32__
235 #define DEFAULT_TIMIDITY_PATH	"\\TIMIDITY"
236 #else
237 #define DEFAULT_TIMIDITY_PATH	"/usr/share/timidity"
238 #endif
239 #endif
240 
241 /* These affect general volume */
242 #define GUARD_BITS 3
243 #define AMP_BITS (15-GUARD_BITS)
244 
245 #ifdef LOOKUP_HACK
246    using sample_t = sint8;
247    using final_volume_t = uint8;
248 #  define FINAL_VOLUME(v) (~_l2u[v])
249 #  define MIXUP_SHIFT 5
250 #  define MAX_AMP_VALUE 4095
251 #else
252    using sample_t = sint16;
253    using final_volume_t = sint32;
254 #  define FINAL_VOLUME(v) (v)
255 #  define MAX_AMP_VALUE ((1<<(AMP_BITS+1))-1)
256 #endif
257 
258 #ifdef USE_LDEXP
259 #  define FSCALE(a,b) ldexp((a),(b))
260 #  define FSCALENEG(a,b) ldexp((a),-(b))
261 #else
262 #  define FSCALE(a,b) static_cast<float>((a) * static_cast<double>(1<<(b)))
263 #  define FSCALENEG(a,b) static_cast<float>((a) * (1.0L / static_cast<double>(1<<(b))))
264 #endif
265 
266 /* Vibrato and tremolo Choices of the Day */
267 #define SWEEP_TUNING 38
268 #define VIBRATO_AMPLITUDE_TUNING 1.0L
269 #define VIBRATO_RATE_TUNING 38
270 #define TREMOLO_AMPLITUDE_TUNING 1.0L
271 #define TREMOLO_RATE_TUNING 38
272 
273 #define SWEEP_SHIFT 16
274 #define RATE_SHIFT 5
275 
276 #define VIBRATO_SAMPLE_INCREMENTS 32
277 
278 #ifndef PI
279   #define PI 3.14159265358979323846
280 #endif
281 
282 /* The path separator (D.M.) */
283 #ifdef __WIN32__
284 #  define PATH_SEP '\\'
285 #  define PATH_STRING "\\"
286 #else
287 #  define PATH_SEP '/'
288 #  define PATH_STRING "/"
289 #endif
290 
291 #ifdef NS_TIMIDITY
292 }
293 #endif
294 
295 #endif
296 
297 #endif //USE_TIMIDITY_MIDI
298