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 #ifndef SYSDEP_H_INCLUDED
22 #define SYSDEP_H_INCLUDED 1
23 
24 #if defined(HAVE_LIMITS_H)
25 #include <limits.h>
26 #endif
27 
28 #if defined(__WIN32__) && !defined(__W32__)
29 #define __W32__
30 #endif
31 
32 
33 /* The size of the internal buffer is 2^AUDIO_BUFFER_BITS samples.
34    This determines maximum number of samples ever computed in a row.
35 
36    For Linux and FreeBSD users:
37 
38    This also specifies the size of the buffer fragment.  A smaller
39    fragment gives a faster response in interactive mode -- 10 or 11 is
40    probably a good number. Unfortunately some sound cards emit a click
41    when switching DMA buffers. If this happens to you, try increasing
42    this number to reduce the frequency of the clicks.
43 
44    For other systems:
45 
46    You should probably use a larger number for improved performance.
47 
48 */
49 #ifndef DEFAULT_AUDIO_BUFFER_BITS
50 # ifdef __W32__
51 #  define DEFAULT_AUDIO_BUFFER_BITS 12
52 # else
53 #  define DEFAULT_AUDIO_BUFFER_BITS 11
54 # endif
55 #endif
56 
57 #define SAMPLE_LENGTH_BITS 32
58 
59 #ifndef NO_VOLATILE
60 # define VOLATILE_TOUCH(val) /* do nothing */
61 # define VOLATILE volatile
62 #else
63 extern int volatile_touch(void* dmy);
64 # define VOLATILE_TOUCH(val) volatile_touch(&(val))
65 # define VOLATILE
66 #endif /* NO_VOLATILE */
67 
68 /* Byte order */
69 #ifdef WORDS_BIGENDIAN
70 # ifndef BIG_ENDIAN
71 #  define BIG_ENDIAN 4321
72 # endif
73 # undef LITTLE_ENDIAN
74 #else
75 # undef BIG_ENDIAN
76 # ifndef LITTLE_ENDIAN
77 #  define LITTLE_ENDIAN 1234
78 # endif
79 #endif
80 
81 
82 /* integer type definitions: ISO C now knows a better way */
83 #if defined(HAVE_STDINT_H) || __GNUC__ >= 3
84 #include <stdint.h> // int types are defined here
85 typedef  int8_t   int8;
86 typedef uint8_t  uint8;
87 typedef  int16_t  int16;
88 typedef uint16_t uint16;
89 typedef  int32_t  int32;
90 typedef uint32_t uint32;
91 typedef  int64_t  int64;
92 typedef uint64_t uint64;
93 #define TIMIDITY_HAVE_INT64 1
94 
95 #else /* not C99 */
96 #ifdef HPUX
97 typedef          char   int8;
98 typedef unsigned char  uint8;
99 typedef          short  int16;
100 typedef unsigned short uint16;
101 #else
102 typedef   signed char   int8;
103 typedef unsigned char  uint8;
104 typedef   signed short  int16;
105 typedef unsigned short uint16;
106 #endif
107 /* DEC MMS has 64 bit long words */
108 /* Linux-Axp has also 64 bit long words */
109 #if defined(DEC) || defined(__alpha__) \
110 		|| defined(__ia64__) || defined (__x86_64__) \
111 		|| defined(__ppc64__) || defined(__s390x__) \
112                 || defined(__mips64__) || defined(__LP64__) \
113                 || defined(_LP64)
114 typedef          int   int32;
115 typedef unsigned int  uint32;
116 typedef          long  int64;
117 typedef unsigned long uint64;
118 #define TIMIDITY_HAVE_INT64 1
119 #else /* 32bit architectures */
120 typedef          long  int32;
121 typedef unsigned long uint32;
122 #if __GNUC__
123 /* gcc version<3 (gcc3 has c99 support) */
124 typedef          long long  int64;
125 typedef unsigned long long uint64;
126 #define TIMIDITY_HAVE_INT64 1
127 #elif defined(_MSC_VER)
128 /* VC++. or PellesC */
129 # ifdef __POCC__
130 typedef          __int64  int64;
131 typedef unsigned __int64 uint64;
132 # else
133 typedef          _int64  int64;
134 typedef unsigned _int64 uint64;
135 # endif
136 #define TIMIDITY_HAVE_INT64 1
137 #elif defined(__BORLANDC__) || defined(__WATCOMC__)
138 typedef 	__int64 int64;
139 typedef unsigned __int64 uint64;
140 #define TIMIDITY_HAVE_INT64 1
141 #elif __MACOS__
142 /* Mac's C compiler seems to have these types in common */
143 typedef SInt64  int64;
144 typedef UInt64 uint64;
145 #define TIMIDITY_HAVE_INT64 1
146 #endif
147 #endif /* 64bit arch */
148 #endif /* C99 */
149 
150 /*  pointer size is not long in   WIN64 */
151 #if defined(WIN32)  && defined(_AMD64_)
152 typedef long long  ptr_size_t;
153 typedef unsigned long long  u_ptr_size_t;
154 #else
155 typedef long  ptr_size_t;
156 typedef unsigned long  u_ptr_size_t;
157 #endif
158 
159 
160 /* Instrument files are little-endian, MIDI files big-endian, so we
161    need to do some conversions. */
162 #define XCHG_SHORT(x) ((((x)&0xFF)<<8) | (((x)>>8)&0xFF))
163 #if defined(__i486__) && !defined(__i386__)
164 # define XCHG_LONG(x) \
165      ({ int32 __value; \
166         asm ("bswap %1; movl %1,%0" : "=g" (__value) : "r" (x)); \
167        __value; })
168 #else
169 # define XCHG_LONG(x) ((((x)&0xFF)<<24) | \
170 		      (((x)&0xFF00)<<8) | \
171 		      (((x)&0xFF0000)>>8) | \
172 		      (((x)>>24)&0xFF))
173 #endif
174 
175 #ifdef LITTLE_ENDIAN
176 # define LE_SHORT(x) (x)
177 # define LE_LONG(x) (x)
178 # ifdef __FreeBSD__
179 #  include <osreldate.h>
180 #  if __FreeBSD_version <= 500000
181 #    define BE_SHORT(x) __byte_swap_word(x)
182 #    define BE_LONG(x) __byte_swap_long(x)
183 #  else
184 #    if __FreeBSD_version <= 500028
185 #      define BE_SHORT(x) __uint8_swap_uint16(x)
186 #      define BE_LONG(x) __uint8_swap_uint32(x)
187 #    else
188 #      define BE_SHORT(x) __bswap16(x)
189 #      define BE_LONG(x) __bswap32(x)
190 #    endif
191 #  endif
192 # else
193 #  define BE_SHORT(x) XCHG_SHORT(x)
194 #  define BE_LONG(x) XCHG_LONG(x)
195 # endif
196 #else /* BIG_ENDIAN */
197 # define BE_SHORT(x) (x)
198 # define BE_LONG(x) (x)
199 # ifdef __FreeBSD__
200 #  include <osreldate.h>
201 #  if __FreeBSD_version <= 500000
202 #    define LE_SHORT(x) __byte_swap_word(x)
203 #    define LE_LONG(x) __byte_swap_long(x)
204 #  else
205 #    if __FreeBSD_version <= 500028
206 #      define LE_SHORT(x) __uint8_swap_uint16(x)
207 #      define LE_LONG(x) __uint8_swap_uint32(x)
208 #    else
209 #      define LE_SHORT(x) __bswap16(x)
210 #      define LE_LONG(x) __bswap32(x)
211 #    endif
212 #  endif
213 # else
214 #  define LE_SHORT(x) XCHG_SHORT(x)
215 #  define LE_LONG(x) XCHG_LONG(x)
216 # endif /* __FreeBSD__ */
217 #endif /* LITTLE_ENDIAN */
218 
219 /* max_channels is defined in "timidity.h" */
220 #if MAX_CHANNELS <= 32
221 typedef struct _ChannelBitMask
222 {
223     uint32 b; /* 32-bit bitvector */
224 } ChannelBitMask;
225 #define CLEAR_CHANNELMASK(bits)		((bits).b = 0)
226 #define FILL_CHANNELMASK(bits)		((bits).b = ~0)
227 #define IS_SET_CHANNELMASK(bits, c) ((bits).b &   (1u << (c)))
228 #define SET_CHANNELMASK(bits, c)    ((bits).b |=  (1u << (c)))
229 #define UNSET_CHANNELMASK(bits, c)  ((bits).b &= ~(1u << (c)))
230 #define TOGGLE_CHANNELMASK(bits, c) ((bits).b ^=  (1u << (c)))
231 #define COPY_CHANNELMASK(dest, src)	((dest).b = (src).b)
232 #define REVERSE_CHANNELMASK(bits)	((bits).b = ~(bits).b)
233 #define COMPARE_CHANNELMASK(bitsA, bitsB)	((bitsA).b == (bitsB).b)
234 #else
235 typedef struct _ChannelBitMask
236 {
237     uint32 b[8];		/* 256-bit bitvector */
238 } ChannelBitMask;
239 #define CLEAR_CHANNELMASK(bits) \
240 	memset((bits).b, 0, sizeof(ChannelBitMask))
241 #define FILL_CHANNELMASK(bits) \
242 	memset((bits).b, 0xFF, sizeof(ChannelBitMask))
243 #define IS_SET_CHANNELMASK(bits, c) \
244 	((bits).b[((c) >> 5) & 0x7] &   (1u << ((c) & 0x1F)))
245 #define SET_CHANNELMASK(bits, c) \
246 	((bits).b[((c) >> 5) & 0x7] |=  (1u << ((c) & 0x1F)))
247 #define UNSET_CHANNELMASK(bits, c) \
248 	((bits).b[((c) >> 5) & 0x7] &= ~(1u << ((c) & 0x1F)))
249 #define TOGGLE_CHANNELMASK(bits, c) \
250 	((bits).b[((c) >> 5) & 0x7] ^=  (1u << ((c) & 0x1F)))
251 #define COPY_CHANNELMASK(dest, src) \
252 	memcpy(&(dest), &(src), sizeof(ChannelBitMask))
253 #define REVERSE_CHANNELMASK(bits) \
254 	((bits).b[((c) >> 5) & 0x7] = ~(bits).b[((c) >> 5) & 0x7])
255 #define COMPARE_CHANNELMASK(bitsA, bitsB) \
256 	(memcmp((bitsA).b, (bitsB).b, sizeof ((bitsA).b)) == 0)
257 #endif
258 
259 #ifdef LOOKUP_HACK
260    typedef int8 sample_t;
261    typedef uint8 final_volume_t;
262 #  define FINAL_VOLUME(v) ((final_volume_t)~_l2u[v])
263 #  define MIXUP_SHIFT 5
264 #  define MAX_AMP_VALUE 4095
265 #else
266    typedef int16 sample_t;
267    typedef int32 final_volume_t;
268 #  define FINAL_VOLUME(v) (v)
269 #  define MAX_AMP_VALUE ((1<<(AMP_BITS+1))-1)
270 #endif
271 #define MIN_AMP_VALUE (MAX_AMP_VALUE >> 9)
272 
273 #if SAMPLE_LENGTH_BITS > 32
274 #if TIMIDITY_HAVE_INT64
275 typedef int64 splen_t;
276 #define SPLEN_T_MAX (splen_t)((((uint64)1)<<63)-1)
277 #else	/* TIMIDITY_HAVE_INT64 */
278 typedef uint32 splen_t;
279 #define SPLEN_T_MAX (splen_t)((uint32)0xFFFFFFFF)
280 #endif	/* TIMIDITY_HAVE_INT64 */
281 #elif SAMPLE_LENGTH_BITS == 32
282 typedef uint32 splen_t;
283 #define SPLEN_T_MAX (splen_t)((uint32)0xFFFFFFFF)
284 #else	/* SAMPLE_LENGTH_BITS */
285 typedef int32 splen_t;
286 #define SPLEN_T_MAX (splen_t)((((uint32)1)<<31)-1)
287 #endif	/* SAMPLE_LENGTH_BITS */
288 
289 #ifdef USE_LDEXP
290 #  define TIM_FSCALE(a,b) ldexp((double)(a),(b))
291 #  define TIM_FSCALENEG(a,b) ldexp((double)(a),-(b))
292 #  include <math.h>
293 #else
294 #  define TIM_FSCALE(a,b) ((a) * (double)(1<<(b)))
295 #  define TIM_FSCALENEG(a,b) ((a) * (1.0 / (double)(1<<(b))))
296 #endif
297 
298 #ifdef HPUX
299 #undef mono
300 #endif
301 
302 #ifdef sun
303 #ifndef SOLARIS
304 /* SunOS 4.x */
305 #include <sys/stdtypes.h>
306 #include <memory.h>
307 #define memcpy(x, y, n) bcopy(y, x, n)
308 #else
309 /* Solaris */
310 int usleep(unsigned int useconds); /* shut gcc warning up */
311 #endif
312 #endif /* sun */
313 
314 
315 #ifdef __W32__
316 #undef PATCH_EXT_LIST
317 #define PATCH_EXT_LIST { ".pat", 0 }
318 
319 #define URL_DIR_CACHE_DISABLE
320 #endif
321 
322 /* The path separator (D.M.) */
323 /* Windows: "\"
324  * Cygwin:  both "\" and "/"
325  * Macintosh: ":"
326  * Unix: "/"
327  */
328 #if defined(__W32__)
329 #  define PATH_SEP '\\'
330 #  define PATH_STRING "\\"
331 #if defined(__CYGWIN32__) || defined(__MINGW32__)
332 #  define PATH_SEP2 '/'
333 #endif
334 #elif defined(__MACOS__)
335 #  define PATH_SEP ':'
336 #  define PATH_STRING ":"
337 #else
338 #  define PATH_SEP '/'
339 #  define PATH_STRING "/"
340 #endif
341 
342 #ifdef PATH_SEP2
343 #define IS_PATH_SEP(c) ((c) == PATH_SEP || (c) == PATH_SEP2)
344 #else
345 #define IS_PATH_SEP(c) ((c) == PATH_SEP)
346 #endif
347 
348 /* new line code */
349 #ifndef NLS
350 #ifdef __W32__
351 #if defined(__BORLANDC__) || defined(__CYGWIN32__) || defined(__MINGW32__)
352 #  define NLS "\n"
353 #else
354 #  define NLS "\r\n"
355 #endif
356 #else /* !__W32__ */
357 #  define NLS "\n"
358 #endif
359 #endif /* NLS */
360 
361 #ifndef M_PI
362 #define M_PI 3.14159265358979323846
363 #endif /* M_PI */
364 
365 #ifndef __W32__
366 #undef MAIL_NAME
367 #endif /* __W32__ */
368 
369 #if defined(__BORLANDC__) || defined(__WATCOMC__) || defined(__DMC__)
370 /* strncasecmp() -> strncmpi(char *,char *,size_t) */
371 //#define strncasecmp(a,b,c) strncmpi(a,b,c)
372 //#define strcasecmp(a,b) strcmpi(a,b)
373 #define strncasecmp(a,b,c) strnicmp(a,b,c)
374 #define strcasecmp(a,b) stricmp(a,b)
375 #endif /* __BORLANDC__ */
376 
377 #if defined(_MSC_VER)
378 #define strncasecmp(a,b,c)	_strnicmp((a),(b),(c))
379 #define strcasecmp(a,b)		_stricmp((a),(b))
380 #ifndef __POCC__
381 #define open _open
382 #define close _close
383 //#define write _write
384 #define lseek _lseek
385 #define unlink _unlink
386 #if _MSC_VER < 1500    /* 1500(VC9)  */
387 #define write _write
388 #ifdef HAVE_VSNPRINTF
389 #define vsnprintf _vsnprintf
390 #endif
391 #endif
392 #pragma warning( 4 : 4305 4244 )
393 #else
394 #ifndef EPERM
395 #define EPERM 1
396 #endif
397 #ifndef EINTR
398 #define EINTR 4
399 #endif
400 #ifndef STDOUT_FILENO
401 #define STDOUT_FILENO 1
402 #endif
403 #ifndef _MAX_PATH
404 #define _MAX_PATH 260
405 #endif
406 #undef strncasecmp
407 #endif
408 #endif /* _MSC_VER */
409 
410 #define SAFE_CONVERT_LENGTH(len) (6 * (len) + 1)
411 
412 #ifdef __MACOS__
413 #include "mac_com.h"
414 #endif
415 
416 #ifndef HAVE_POPEN
417 # undef DECOMPRESSOR_LIST
418 # undef PATCH_CONVERTERS
419 #endif
420 
421 #endif /* SYSDEP_H_INCUDED */
422