OSDN Git Service

Merge UnkoTim224
[timidity41/timidity41.git] / timidity / timidity.c
1 /*
2     TiMidity++ -- MIDI to WAVE converter and player
3     Copyright (C) 1999-2018 Masanao Izumo <iz@onicos.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 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif /* HAVE_CONFIG_H */
24 #ifdef __POCC__
25 #include <sys/types.h>
26 #endif //for off_t
27 #include <stdio.h>
28 #ifdef STDC_HEADERS
29 #include <stdlib.h>
30 #include <ctype.h>
31 #include <stddef.h>
32 #endif /* STDC_HEADERS */
33 #ifndef NO_STRING_H
34 #include <string.h>
35 #else
36 #include <strings.h>
37 #endif /* !NO_STRING_H */
38 #ifdef __W32__
39 #include <windows.h>
40 #include <io.h>
41 #include <shlobj.h>
42 #endif /* __W32__ */
43 #ifdef __MINGW32__
44 #define _BSD_SOURCE 1
45 #endif
46 #include "tmdy_getopt.h"
47 #ifdef HAVE_UNISTD_H
48 #include <unistd.h>
49 #endif /* HAVE_UNISTD_H */
50 #ifdef TIME_WITH_SYS_TIME
51 #include <sys/time.h>
52 #include <time.h>
53 #else
54 #ifdef HAVE_SYS_TIME_H
55 #include <sys/time.h>
56 #else
57 #include <time.h>
58 #endif /* HAVE_SYS_TIME_H */
59 #endif /* TIME_WITH_SYS_TIME */
60 #ifdef HAVE_FCNTL_H
61 #include <fcntl.h> /* for open */
62 #endif /* HAVE_FCNTL_H */
63
64 #ifdef BORLANDC_EXCEPTION
65 #include <excpt.h>
66 #endif /* BORLANDC_EXCEPTION */
67 #include <signal.h>
68
69 #if defined(__FreeBSD__) && !defined(__alpha__)
70 #include <floatingpoint.h> /* For FP exceptions */
71 #endif
72 #if defined(__NetBSD__) || defined(__OpenBSD__)
73 #include <ieeefp.h> /* For FP exceptions */
74 #endif
75
76 #include "timidity.h"
77 #include "common.h"
78 #include "instrum.h"
79 #include "playmidi.h"
80 #include "readmidi.h"
81 #include "output.h"
82 #include "controls.h"
83 #include "tables.h"
84 #include "miditrace.h"
85 #include "effect.h"
86 #include "freq.h"
87 #ifdef SUPPORT_SOUNDSPEC
88 #include "soundspec.h"
89 #endif /* SUPPORT_SOUNDSPEC */
90 #include "resample.h"
91 #include "recache.h"
92 #include "arc.h"
93 #include "strtab.h"
94 #include "wrd.h"
95 #define DEFINE_GLOBALS
96 #include "mid.defs"
97 #include "aq.h"
98 #include "mix.h"
99 #include "unimod.h"
100 #include "quantity.h"
101 #include "rtsyn.h"
102 #include "sndfontini.h"
103 #include "thread.h"
104 #include "miditrace.h"
105 #include "flac_a.h"
106 #include "dls.h"
107 #include "sfz.h"
108 ///r
109 #ifdef __BORLANDC__
110 #define inline
111 #endif
112
113 #ifdef IA_W32GUI
114 #include "w32g.h"
115 #include "w32g_subwin.h"
116 #include "w32g_utl.h"
117 #endif
118
119 #ifdef WINDRV
120 /* supress std outputs */
121 #define fputs(a, b)
122 #endif
123
124 #ifndef __GNUC__
125 #define __attribute__(x) /* ignore */
126 #endif
127
128 ///r
129 #ifdef AU_W32
130 #include "w32_a.h"
131 #endif
132 #ifdef AU_WDMKS
133 #include "wdmks_a.h"
134 #endif
135 #ifdef AU_WASAPI
136 #include "wasapi_a.h"
137 #endif
138
139 #ifdef AU_PORTAUDIO
140 #include "portaudio_a.h"
141 #endif
142
143 #ifdef __W32G__
144 #include "w32g_utl.h"
145 #endif
146
147
148 uint8 opt_normal_chorus_plus = 5; // chorusEX
149
150 extern DWORD processPriority;
151 DWORD processPriority = NORMAL_PRIORITY_CLASS;  // \83v\83\8d\83Z\83X\82Ì\83v\83\89\83C\83I\83\8a\83e\83B
152 #if defined(IA_W32G_SYN) || defined(WINDRV)
153 DWORD syn_ThreadPriority = THREAD_PRIORITY_NORMAL;
154 #else
155 int GUIThreadPriority = THREAD_PRIORITY_NORMAL;
156 #endif
157 int PlayerThreadPriority = THREAD_PRIORITY_NORMAL;
158
159 /* option enums */
160 enum {
161         TIM_OPT_FIRST = 256,
162         /* first entry */
163         TIM_OPT_VOLUME = TIM_OPT_FIRST,
164         TIM_OPT_MASTER_VOLUME,
165         TIM_OPT_DRUM_POWER,
166         TIM_OPT_VOLUME_COMP,
167         TIM_OPT_ANTI_ALIAS,
168         TIM_OPT_BUFFER_FRAGS,
169         TIM_OPT_CONTROL_RATIO,
170         TIM_OPT_CONFIG_FILE,
171         TIM_OPT_DRUM_CHANNEL,
172         TIM_OPT_IFACE_PATH,
173         TIM_OPT_EXT,
174         TIM_OPT_MOD_WHEEL,
175         TIM_OPT_PORTAMENTO,
176         TIM_OPT_VIBRATO,
177         TIM_OPT_CH_PRESS,
178         TIM_OPT_MOD_ENV,
179         TIM_OPT_TRACE_TEXT,
180         TIM_OPT_OVERLAP,
181 ///r
182         TIM_OPT_OVERLAP_COUNT,
183         TIM_OPT_MAX_CHANNEL_VOICES,
184
185         TIM_OPT_TEMPER_CTRL,
186         TIM_OPT_DEFAULT_MID,
187         TIM_OPT_SYSTEM_MID,
188         TIM_OPT_DEFAULT_BANK,
189         TIM_OPT_FORCE_BANK,
190         TIM_OPT_DEFAULT_PGM,
191         TIM_OPT_FORCE_PGM,
192         TIM_OPT_DELAY,
193         TIM_OPT_CHORUS,
194         TIM_OPT_REVERB,
195         TIM_OPT_VOICE_LPF,
196         TIM_OPT_VOICE_HPF,
197         TIM_OPT_NS,
198         TIM_OPT_RESAMPLE,
199 ///r
200         TIM_OPT_RESAMPLE_FILTER,
201         TIM_OPT_RESAMPLE_OVER_SAMPLING,
202         TIM_OPT_PRE_RESAMPLE,
203         TIM_OPT_EVIL,
204         TIM_OPT_FAST_PAN,
205         TIM_OPT_FAST_DECAY,
206         TIM_OPT_SEGMENT,
207         TIM_OPT_SPECTROGRAM,
208         TIM_OPT_KEYSIG,
209         TIM_OPT_HELP,
210         TIM_OPT_INTERFACE,
211         TIM_OPT_VERBOSE,
212         TIM_OPT_QUIET,
213         TIM_OPT_TRACE,
214         TIM_OPT_LOOP,
215         TIM_OPT_RANDOM,
216         TIM_OPT_SORT,
217         TIM_OPT_BACKGROUND,
218         TIM_OPT_RT_PRIO,
219         TIM_OPT_SEQ_PORTS,
220         TIM_OPT_RTSYN_LATENCY,
221         TIM_OPT_RTSYN_SKIP_AQ,
222     TIM_OPT_RTSYN_PRINT_PORTS,
223         TIM_OPT_REALTIME_LOAD,
224         TIM_OPT_ADJUST_KEY,
225         TIM_OPT_VOICE_QUEUE,
226 ///r
227         TIM_OPT_RESAMPLE_QUEUE,
228         TIM_OPT_POLYPHONY_QUEUE,
229         TIM_OPT_ADD_PLAY_TIME,
230         TIM_OPT_ADD_SILENT_TIME,
231         TIM_OPT_EMU_DELAY_TIME,
232         TIM_OPT_PROCESS_PRIORITY,
233         TIM_OPT_PLAYER_THREAD_PRIORITY,
234
235         TIM_OPT_OD_LEVEL_GS,
236         TIM_OPT_OD_DRIVE_GS,
237         TIM_OPT_OD_LEVEL_XG,
238         TIM_OPT_OD_DRIVE_XG,
239     TIM_OPT_OD_LEVEL_SD,
240     TIM_OPT_OD_DRIVE_SD,
241
242         TIM_OPT_PATCH_PATH,
243         TIM_OPT_PCM_FILE,
244         TIM_OPT_DECAY_TIME,
245         TIM_OPT_INTERPOLATION,
246         TIM_OPT_OUTPUT_MODE,
247         TIM_OPT_OUTPUT_STEREO,
248         TIM_OPT_OUTPUT_SIGNED,
249         TIM_OPT_OUTPUT_BITWIDTH,
250         TIM_OPT_OUTPUT_FORMAT,
251         TIM_OPT_OUTPUT_SWAB,
252 ///r
253         TIM_OPT_OUTPUT_DEVICE_ID,
254         TIM_OPT_WMME_DEVICE_ID,
255         TIM_OPT_WAVE_FORMAT_EXT,
256         TIM_OPT_WDMKS_DEVICE_ID,
257         TIM_OPT_WDMKS_LATENCY,
258         TIM_OPT_WDMKS_FORMAT_EXT,
259         TIM_OPT_WDMKS_POLLING,
260         TIM_OPT_WDMKS_THREAD_PRIORITY,
261         TIM_OPT_WDMKS_RT_PRIORITY,
262         TIM_OPT_WDMKS_PIN_PRIORITY,
263         TIM_OPT_WASAPI_DEVICE_ID,
264         TIM_OPT_WASAPI_LATENCY,
265         TIM_OPT_WASAPI_FORMAT_EXT,
266         TIM_OPT_WASAPI_EXCLUSIVE,
267         TIM_OPT_WASAPI_POLLING,
268         TIM_OPT_WASAPI_PRIORITY,
269         TIM_OPT_WASAPI_STREAM_CATEGORY,
270         TIM_OPT_WASAPI_STREAM_OPTION,
271         TIM_OPT_PA_WMME_DEVICE_ID,
272         TIM_OPT_PA_DS_DEVICE_ID,
273         TIM_OPT_PA_ASIO_DEVICE_ID,
274         TIM_OPT_PA_WDMKS_DEVICE_ID,
275         TIM_OPT_PA_WASAPI_DEVICE_ID,
276         TIM_OPT_PA_WASAPI_FLAG,
277         TIM_OPT_PA_WASAPI_STREAM_CATEGORY,
278         TIM_OPT_PA_WASAPI_STREAM_OPTION,
279         
280         TIM_OPT_WAVE_EXTENSIBLE,
281         TIM_OPT_WAVE_UPDATE_STEP,
282         TIM_OPT_FLAC_VERIFY,
283         TIM_OPT_FLAC_PADDING,
284         TIM_OPT_FLAC_COMPLEVEL,
285         TIM_OPT_FLAC_OGGFLAC,
286         TIM_OPT_OPUS_NFRAMES,
287         TIM_OPT_OPUS_BITRATE,
288         TIM_OPT_OPUS_COMPLEXITY,
289         TIM_OPT_OPUS_VBR,
290         TIM_OPT_OPUS_CVBR,
291         TIM_OPT_SPEEX_QUALITY,
292         TIM_OPT_SPEEX_VBR,
293         TIM_OPT_SPEEX_ABR,
294         TIM_OPT_SPEEX_VAD,
295         TIM_OPT_SPEEX_DTX,
296         TIM_OPT_SPEEX_COMPLEXITY,
297         TIM_OPT_SPEEX_NFRAMES,
298         TIM_OPT_OUTPUT_FILE,
299         TIM_OPT_PATCH_FILE,
300         TIM_OPT_POLYPHONY,
301         TIM_OPT_POLY_REDUCE,
302         TIM_OPT_MUTE,
303         TIM_OPT_TEMPER_MUTE,
304         TIM_OPT_PRESERVE_SILENCE,
305         TIM_OPT_AUDIO_BUFFER,
306 ///r
307         TIM_OPT_COMPUTE_BUFFER,
308         TIM_OPT_MIX_ENV,
309         TIM_OPT_MOD_UPDATE,
310         TIM_OPT_CUT_SHORT,
311         TIM_OPT_LIMITER,
312         TIM_OPT_COMPUTE_THREAD_NUM,
313         TIM_OPT_TRACE_MODE_UPDATE,      
314         TIM_OPT_LOAD_ALL_INSTRUMENT,
315         TIM_OPT_LOOP_REPEAT,
316         TIM_OPT_LOOP_FILTER,
317
318         TIM_OPT_CACHE_SIZE,
319         TIM_OPT_SAMPLE_FREQ,
320         TIM_OPT_ADJUST_TEMPO,
321         TIM_OPT_CHARSET,
322         TIM_OPT_UNLOAD_INST,
323         TIM_OPT_VOLUME_CALC_RMS,
324         TIM_OPT_VOLUME_CALC_TRIM,
325         TIM_OPT_VOLUME_CURVE,
326         TIM_OPT_VERSION,
327         TIM_OPT_WRD,
328         TIM_OPT_RCPCV_DLL,
329         TIM_OPT_CONFIG_STR,
330         TIM_OPT_FREQ_TABLE,
331         TIM_OPT_PURE_INT,
332         TIM_OPT_MODULE,
333         TIM_OPT_DUMMY_SETTING,
334         TIM_OPT_INT_SYNTH_RATE,
335         TIM_OPT_INT_SYNTH_SINE,
336         TIM_OPT_INT_SYNTH_UPDATE,
337         /* last entry */
338         TIM_OPT_LAST = TIM_OPT_MODULE,
339 };
340
341 #ifdef IA_WINSYN
342 const char *optcommands =
343 #else
344 static const char *optcommands =
345 #endif
346 ///r
347 //              "4A:aB:b:C:c:D:d:E:eFfg:H:hI:i:jK:k:L:M:m:N:"
348 //              "O:o:P:p:Q:q:R:S:s:T:t:UV:vW:"
349                 "4A:aB:b:C:c:D:d:E:eF:fg:H:hI:i:jK:k:L:lM:m:N:n"
350                 "O:o:P:p:Q:q:R:rS:s:T:t:UV:vW:"
351 #ifdef __W32__
352                 "w:"
353 #endif
354                 "xY:Z:";                /* Only GJluXyz are remain... */
355 #ifdef IA_WINSYN
356 const struct option longopts[] = {
357 #else
358 static const struct option longopts[] = {
359 #endif
360         { "volume",                 required_argument, NULL, TIM_OPT_VOLUME },
361         { "master-volume",          required_argument, NULL, TIM_OPT_MASTER_VOLUME },
362         { "drum-power",             required_argument, NULL, TIM_OPT_DRUM_POWER },
363         { "no-volume-compensation", optional_argument, NULL, TIM_OPT_VOLUME_COMP },
364         { "volume-compensation",    optional_argument, NULL, TIM_OPT_VOLUME_COMP },
365         { "no-anti-alias",          no_argument,       NULL, TIM_OPT_ANTI_ALIAS },
366         { "anti-alias",             optional_argument, NULL, TIM_OPT_ANTI_ALIAS },
367         { "buffer-fragments",       required_argument, NULL, TIM_OPT_BUFFER_FRAGS },
368         { "control-ratio",          required_argument, NULL, TIM_OPT_CONTROL_RATIO },
369         { "config-file",            required_argument, NULL, TIM_OPT_CONFIG_FILE },
370         { "drum-channel",           required_argument, NULL, TIM_OPT_DRUM_CHANNEL },
371         { "interface-path",         required_argument, NULL, TIM_OPT_IFACE_PATH },
372         { "ext",                    required_argument, NULL, TIM_OPT_EXT },
373         { "no-mod-wheel",           no_argument,       NULL, TIM_OPT_MOD_WHEEL },
374         { "mod-wheel",              optional_argument, NULL, TIM_OPT_MOD_WHEEL },
375         { "no-portamento",          no_argument,       NULL, TIM_OPT_PORTAMENTO },
376         { "portamento",             optional_argument, NULL, TIM_OPT_PORTAMENTO },
377         { "no-vibrato",             no_argument,       NULL, TIM_OPT_VIBRATO },
378         { "vibrato",                optional_argument, NULL, TIM_OPT_VIBRATO },
379         { "no-ch-pressure",         no_argument,       NULL, TIM_OPT_CH_PRESS },
380         { "ch-pressure",            optional_argument, NULL, TIM_OPT_CH_PRESS },
381         { "no-mod-envelope",        no_argument,       NULL, TIM_OPT_MOD_ENV },
382         { "mod-envelope",           optional_argument, NULL, TIM_OPT_MOD_ENV },
383         { "no-trace-text-meta",     no_argument,       NULL, TIM_OPT_TRACE_TEXT },
384         { "trace-text-meta",        optional_argument, NULL, TIM_OPT_TRACE_TEXT },
385         { "no-overlap-voice",       no_argument,       NULL, TIM_OPT_OVERLAP },
386         { "overlap-voice",          optional_argument, NULL, TIM_OPT_OVERLAP },
387 ///r
388         { "overlap-voice-count",    required_argument, NULL, TIM_OPT_OVERLAP_COUNT },
389         { "max-channel-voices",     required_argument, NULL, TIM_OPT_MAX_CHANNEL_VOICES },
390
391         { "no-temper-control",      no_argument,       NULL, TIM_OPT_TEMPER_CTRL },
392         { "temper-control",         optional_argument, NULL, TIM_OPT_TEMPER_CTRL },
393         { "default-mid",            required_argument, NULL, TIM_OPT_DEFAULT_MID },
394         { "system-mid",             required_argument, NULL, TIM_OPT_SYSTEM_MID },
395         { "default-bank",           required_argument, NULL, TIM_OPT_DEFAULT_BANK },
396         { "force-bank",             required_argument, NULL, TIM_OPT_FORCE_BANK },
397         { "default-program",        required_argument, NULL, TIM_OPT_DEFAULT_PGM },
398         { "force-program",          required_argument, NULL, TIM_OPT_FORCE_PGM },
399         { "delay",                  required_argument, NULL, TIM_OPT_DELAY },
400         { "chorus",                 required_argument, NULL, TIM_OPT_CHORUS },
401         { "reverb",                 required_argument, NULL, TIM_OPT_REVERB },
402         { "voice-lpf",              required_argument, NULL, TIM_OPT_VOICE_LPF },
403         { "voice-hpf",              required_argument, NULL, TIM_OPT_VOICE_HPF },
404         { "noise-shaping",          required_argument, NULL, TIM_OPT_NS },
405 #ifndef FIXED_RESAMPLATION
406         { "resample",               required_argument, NULL, TIM_OPT_RESAMPLE },
407 #endif
408         { "resample-filter",        required_argument, NULL, TIM_OPT_RESAMPLE_FILTER },
409         { "resample-over-sampling", required_argument, NULL, TIM_OPT_RESAMPLE_OVER_SAMPLING },
410         { "pre-resample",           required_argument, NULL, TIM_OPT_PRE_RESAMPLE },
411         { "evil",                   no_argument,       NULL, TIM_OPT_EVIL },
412         { "no-fast-panning",        no_argument,       NULL, TIM_OPT_FAST_PAN },
413         { "fast-panning",           optional_argument, NULL, TIM_OPT_FAST_PAN },
414         { "no-fast-decay",          no_argument,       NULL, TIM_OPT_FAST_DECAY },
415         { "fast-decay",             optional_argument, NULL, TIM_OPT_FAST_DECAY },
416         { "segment",                required_argument, NULL, TIM_OPT_SEGMENT },
417         { "spectrogram",            required_argument, NULL, TIM_OPT_SPECTROGRAM },
418         { "force-keysig",           required_argument, NULL, TIM_OPT_KEYSIG },
419         { "help",                   optional_argument, NULL, TIM_OPT_HELP },
420         { "interface",              required_argument, NULL, TIM_OPT_INTERFACE },
421         { "verbose",                optional_argument, NULL, TIM_OPT_VERBOSE },
422         { "quiet",                  optional_argument, NULL, TIM_OPT_QUIET },
423         { "no-trace",               no_argument,       NULL, TIM_OPT_TRACE },
424         { "trace",                  optional_argument, NULL, TIM_OPT_TRACE },
425         { "no-loop",                no_argument,       NULL, TIM_OPT_LOOP },
426         { "loop",                   optional_argument, NULL, TIM_OPT_LOOP },
427         { "no-random",              no_argument,       NULL, TIM_OPT_RANDOM },
428         { "random",                 optional_argument, NULL, TIM_OPT_RANDOM },
429         { "no-sort",                no_argument,       NULL, TIM_OPT_SORT },
430         { "sort",                   optional_argument, NULL, TIM_OPT_SORT },
431 #ifdef IA_ALSASEQ
432         { "no-background",          no_argument,       NULL, TIM_OPT_BACKGROUND },
433         { "background",             optional_argument, NULL, TIM_OPT_BACKGROUND },
434         { "realtime-priority",      required_argument, NULL, TIM_OPT_RT_PRIO },
435         { "sequencer-ports",        required_argument, NULL, TIM_OPT_SEQ_PORTS },
436 #endif
437         { "no-realtime-load",       no_argument,       NULL, TIM_OPT_REALTIME_LOAD },
438         { "realtime-load",          optional_argument, NULL, TIM_OPT_REALTIME_LOAD },
439         { "adjust-key",             required_argument, NULL, TIM_OPT_ADJUST_KEY },
440         { "voice-queue",            required_argument, NULL, TIM_OPT_VOICE_QUEUE },
441 ///r
442         { "resample-queue",         required_argument, NULL, TIM_OPT_RESAMPLE_QUEUE },
443         { "polyphony-queue",        required_argument, NULL, TIM_OPT_POLYPHONY_QUEUE },
444         { "add-play-time",                      required_argument, NULL, TIM_OPT_ADD_PLAY_TIME },
445         { "add-silent-time",            required_argument, NULL, TIM_OPT_ADD_SILENT_TIME },
446         { "emu-delay-time",                     required_argument, NULL, TIM_OPT_EMU_DELAY_TIME },
447 #if defined(__W32__)
448         { "process-priority",           required_argument, NULL, TIM_OPT_PROCESS_PRIORITY },
449 #if !(defined(IA_W32G_SYN) || defined(WINDRV))
450         { "player-thread-priority",     required_argument, NULL, TIM_OPT_PLAYER_THREAD_PRIORITY },
451 #endif
452 #endif
453         { "od-level-gs",            required_argument, NULL, TIM_OPT_OD_LEVEL_GS },
454         { "od-drive-gs",            required_argument, NULL, TIM_OPT_OD_DRIVE_GS },
455         { "od-level-xg",            required_argument, NULL, TIM_OPT_OD_LEVEL_XG },
456         { "od-drive-xg",            required_argument, NULL, TIM_OPT_OD_DRIVE_XG },
457         { "patch-path",             required_argument, NULL, TIM_OPT_PATCH_PATH },
458         { "pcm-file",               required_argument, NULL, TIM_OPT_PCM_FILE },
459         { "decay-time",             required_argument, NULL, TIM_OPT_DECAY_TIME },
460         { "interpolation",          required_argument, NULL, TIM_OPT_INTERPOLATION },
461         { "output-mode",            required_argument, NULL, TIM_OPT_OUTPUT_MODE },
462         { "output-stereo",          no_argument,       NULL, TIM_OPT_OUTPUT_STEREO },
463         { "output-mono",            no_argument,       NULL, TIM_OPT_OUTPUT_STEREO },
464         { "output-signed",          no_argument,       NULL, TIM_OPT_OUTPUT_SIGNED },
465         { "output-unsigned",        no_argument,       NULL, TIM_OPT_OUTPUT_SIGNED },
466         { "output-16bit",           no_argument,       NULL, TIM_OPT_OUTPUT_BITWIDTH },
467         { "output-24bit",           no_argument,       NULL, TIM_OPT_OUTPUT_BITWIDTH },
468 ///r
469         { "output-32bit",           no_argument,       NULL, TIM_OPT_OUTPUT_BITWIDTH },
470         { "output-f32bit",          no_argument,       NULL, TIM_OPT_OUTPUT_BITWIDTH },
471         { "output-float32bit",      no_argument,       NULL, TIM_OPT_OUTPUT_BITWIDTH },
472         { "output-64bit",           no_argument,       NULL, TIM_OPT_OUTPUT_BITWIDTH },
473         { "output-f64bit",          no_argument,       NULL, TIM_OPT_OUTPUT_BITWIDTH },
474         { "output-float64bit",      no_argument,       NULL, TIM_OPT_OUTPUT_BITWIDTH },
475
476         { "output-8bit",            no_argument,       NULL, TIM_OPT_OUTPUT_BITWIDTH },
477         { "output-linear",          no_argument,       NULL, TIM_OPT_OUTPUT_FORMAT },
478         { "output-ulaw",            no_argument,       NULL, TIM_OPT_OUTPUT_FORMAT },
479         { "output-alaw",            no_argument,       NULL, TIM_OPT_OUTPUT_FORMAT },
480         { "no-output-swab",         no_argument,       NULL, TIM_OPT_OUTPUT_SWAB },
481         { "output-swab",            optional_argument, NULL, TIM_OPT_OUTPUT_SWAB },
482 #if defined(IA_WINSYN) || defined(IA_NPSYN) || defined(IA_PORTMIDI) || \
483     defined(IA_W32G_SYN) || defined(IA_W32GUI)
484     { "rtsyn-latency",              required_argument, NULL, TIM_OPT_RTSYN_LATENCY },
485     { "rtsyn-skip-aq",              required_argument, NULL, TIM_OPT_RTSYN_SKIP_AQ },
486     { "rtsyn-print-ports",          no_argument,       NULL, TIM_OPT_RTSYN_PRINT_PORTS },
487     { "rtsyn-list",                 no_argument,       NULL, TIM_OPT_RTSYN_PRINT_PORTS },
488 #endif /* RTSYN || IA_W32GUI */
489 ///r
490         { "output-device-id",           required_argument, NULL, TIM_OPT_OUTPUT_DEVICE_ID },
491 #ifdef AU_W32
492         { "wmme-device-id",                     required_argument, NULL, TIM_OPT_WMME_DEVICE_ID },
493         { "wave-format-ext",            required_argument, NULL, TIM_OPT_WAVE_FORMAT_EXT },
494         { "wmme-format-ext",            required_argument, NULL, TIM_OPT_WAVE_FORMAT_EXT },
495 #endif
496 #ifdef AU_WDMKS
497         { "wdmks-device-id",            required_argument, NULL, TIM_OPT_WDMKS_DEVICE_ID },
498         { "wdmks-latency",                      required_argument, NULL, TIM_OPT_WDMKS_LATENCY },
499         { "wdmks-format-ext",           required_argument, NULL, TIM_OPT_WDMKS_FORMAT_EXT },
500         { "wdmks-polling",                      required_argument, NULL, TIM_OPT_WDMKS_POLLING },
501         { "wdmks-thread-priority",      required_argument, NULL, TIM_OPT_WDMKS_THREAD_PRIORITY },
502         { "wdmks-rt-priority",          required_argument, NULL, TIM_OPT_WDMKS_RT_PRIORITY },
503         { "wdmks-pin-priority",         required_argument, NULL, TIM_OPT_WDMKS_PIN_PRIORITY },
504 #endif
505 #ifdef AU_WASAPI
506         { "wasapi-device-id",           required_argument, NULL, TIM_OPT_WASAPI_DEVICE_ID },
507         { "wasapi-latency",                     required_argument, NULL, TIM_OPT_WASAPI_LATENCY },
508         { "wasapi-format-ext",          required_argument, NULL, TIM_OPT_WASAPI_FORMAT_EXT },
509         { "wasapi-exclusive",           required_argument, NULL, TIM_OPT_WASAPI_EXCLUSIVE },
510         { "wasapi-polling",                     required_argument, NULL, TIM_OPT_WASAPI_POLLING },
511         { "wasapi-priority",            required_argument, NULL, TIM_OPT_WASAPI_PRIORITY },
512         { "wasapi-stream-category",             required_argument, NULL, TIM_OPT_WASAPI_STREAM_CATEGORY },
513         { "wasapi-stream-option",               required_argument, NULL, TIM_OPT_WASAPI_STREAM_OPTION },
514 #endif
515 #ifdef AU_PORTAUDIO     
516         { "pa-wmme-device-id",          required_argument, NULL, TIM_OPT_PA_WMME_DEVICE_ID },
517         { "pa-ds-device-id",            required_argument, NULL, TIM_OPT_PA_DS_DEVICE_ID },
518         { "pa-asio-device-id",          required_argument, NULL, TIM_OPT_PA_ASIO_DEVICE_ID },
519 #ifdef PORTAUDIO_V19
520         { "pa-wdmks-device-id",         required_argument, NULL, TIM_OPT_PA_WDMKS_DEVICE_ID },
521         { "pa-wasapi-device-id",        required_argument, NULL, TIM_OPT_PA_WASAPI_DEVICE_ID },
522         { "pa-wasapi-flag",                     required_argument, NULL, TIM_OPT_PA_WASAPI_FLAG },
523         { "pa-wasapi-stream-category",required_argument, NULL, TIM_OPT_PA_WASAPI_STREAM_CATEGORY },
524         { "pa-wasapi-stream-option",required_argument, NULL, TIM_OPT_PA_WASAPI_STREAM_OPTION },
525 #endif
526 #endif
527         { "wave-extensible",        no_argument,       NULL, TIM_OPT_WAVE_EXTENSIBLE },
528         { "wave-update-step",       optional_argument, NULL, TIM_OPT_WAVE_UPDATE_STEP },
529 #ifdef AU_FLAC
530         { "flac-verify",            no_argument,       NULL, TIM_OPT_FLAC_VERIFY },
531         { "flac-padding",           required_argument, NULL, TIM_OPT_FLAC_PADDING },
532         { "flac-complevel",         required_argument, NULL, TIM_OPT_FLAC_COMPLEVEL },
533 #ifdef AU_OGGFLAC
534         { "oggflac",                no_argument,       NULL, TIM_OPT_FLAC_OGGFLAC },
535 #endif /* AU_OGGFLAC */
536 #endif /* AU_FLAC */
537 #ifdef AU_OPUS
538         { "opus-nframes",           required_argument, NULL, TIM_OPT_OPUS_NFRAMES },
539         { "opus-bitrate",           required_argument, NULL, TIM_OPT_OPUS_BITRATE },
540         { "opus-complexity",        required_argument, NULL, TIM_OPT_OPUS_COMPLEXITY },
541         { "opus-vbr",               no_argument,       NULL, TIM_OPT_OPUS_VBR },
542         { "opus-cvbr",              no_argument,       NULL, TIM_OPT_OPUS_CVBR },
543 #endif /* AU_OPUS */
544 #ifdef AU_SPEEX
545         { "speex-quality",          required_argument, NULL, TIM_OPT_SPEEX_QUALITY },
546         { "speex-vbr",              no_argument,       NULL, TIM_OPT_SPEEX_VBR },
547         { "speex-abr",              required_argument, NULL, TIM_OPT_SPEEX_ABR },
548         { "speex-vad",              no_argument,       NULL, TIM_OPT_SPEEX_VAD },
549         { "speex-dtx",              no_argument,       NULL, TIM_OPT_SPEEX_DTX },
550         { "speex-complexity",       required_argument, NULL, TIM_OPT_SPEEX_COMPLEXITY },
551         { "speex-nframes",          required_argument, NULL, TIM_OPT_SPEEX_NFRAMES },
552 #endif /* AU_SPEEX */
553         { "output-file",            required_argument, NULL, TIM_OPT_OUTPUT_FILE },
554         { "patch-file",             required_argument, NULL, TIM_OPT_PATCH_FILE },
555         { "polyphony",              required_argument, NULL, TIM_OPT_POLYPHONY },
556         { "no-polyphony-reduction", no_argument,       NULL, TIM_OPT_POLY_REDUCE },
557         { "polyphony-reduction",    optional_argument, NULL, TIM_OPT_POLY_REDUCE },
558         { "mute",                   required_argument, NULL, TIM_OPT_MUTE },
559         { "temper-mute",            required_argument, NULL, TIM_OPT_TEMPER_MUTE },
560         { "preserve-silence",       no_argument,       NULL, TIM_OPT_PRESERVE_SILENCE },
561         { "audio-buffer",           required_argument, NULL, TIM_OPT_AUDIO_BUFFER },
562 ///r
563         { "compute-buffer",         required_argument, NULL, TIM_OPT_COMPUTE_BUFFER },
564         { "mix-envelope",           required_argument, NULL, TIM_OPT_MIX_ENV },
565         { "modulation-update",      required_argument, NULL, TIM_OPT_MOD_UPDATE },
566         { "cut-short-time",         required_argument, NULL, TIM_OPT_CUT_SHORT },
567         { "limiter",                required_argument, NULL, TIM_OPT_LIMITER },
568         { "compute-thread-num",     required_argument, NULL, TIM_OPT_COMPUTE_THREAD_NUM },
569         { "trace-mode-update-time", required_argument, NULL, TIM_OPT_TRACE_MODE_UPDATE },       
570         { "load-all-instrument",    required_argument, NULL, TIM_OPT_LOAD_ALL_INSTRUMENT },     
571         { "loop-repeat",            required_argument, NULL, TIM_OPT_LOOP_REPEAT },
572     { "loop-filter",            required_argument, NULL, TIM_OPT_LOOP_FILTER },
573
574         { "cache-size",             required_argument, NULL, TIM_OPT_CACHE_SIZE },
575         { "sampling-freq",          required_argument, NULL, TIM_OPT_SAMPLE_FREQ },
576         { "adjust-tempo",           required_argument, NULL, TIM_OPT_ADJUST_TEMPO },
577         { "output-charset",         required_argument, NULL, TIM_OPT_CHARSET },
578         { "no-unload-instruments",  no_argument,       NULL, TIM_OPT_UNLOAD_INST },
579         { "unload-instruments",     optional_argument, NULL, TIM_OPT_UNLOAD_INST },
580 #if defined(AU_VOLUME_CALC)
581         { "volume-calc-rms",        no_argument,       NULL, TIM_OPT_VOLUME_CALC_RMS },
582         { "volume-calc-trim",       no_argument,       NULL, TIM_OPT_VOLUME_CALC_TRIM },
583 #endif /* AU_VOLUME_CALC */
584         { "volume-curve",           required_argument, NULL, TIM_OPT_VOLUME_CURVE },
585         { "version",                no_argument,       NULL, TIM_OPT_VERSION },
586         { "wrd",                    required_argument, NULL, TIM_OPT_WRD },
587 #ifdef __W32__
588         { "rcpcv-dll",              required_argument, NULL, TIM_OPT_RCPCV_DLL },
589 #endif
590         { "config-string",          required_argument, NULL, TIM_OPT_CONFIG_STR },
591         { "freq-table",             required_argument, NULL, TIM_OPT_FREQ_TABLE },
592         { "pure-intonation",        optional_argument, NULL, TIM_OPT_PURE_INT },
593         { "module",                 required_argument, NULL, TIM_OPT_MODULE },
594         { "disable-chorus-plus",    no_argument,       NULL, TIM_OPT_DUMMY_SETTING},
595         
596         { "int-synth-rate",         required_argument, NULL, TIM_OPT_INT_SYNTH_RATE },
597         { "int-synth-sine",         required_argument, NULL, TIM_OPT_INT_SYNTH_SINE },
598         { "int-synth-update",       required_argument, NULL, TIM_OPT_INT_SYNTH_UPDATE },
599
600         { NULL,                     no_argument,       NULL, '\0'     }
601 };
602 #define INTERACTIVE_INTERFACE_IDS "kmqagrwAWNP"
603
604
605
606 /* main interfaces (To be used another main) */
607 #if defined(main) || defined(ANOTHER_MAIN) || defined ( IA_W32GUI ) || defined ( IA_W32G_SYN )
608 #define MAIN_INTERFACE
609 #else
610 #define MAIN_INTERFACE static
611 #endif /* main */
612
613 MAIN_INTERFACE void timidity_start_initialize(void);
614 MAIN_INTERFACE int timidity_pre_load_configuration(void);
615 MAIN_INTERFACE int timidity_post_load_configuration(void);
616 MAIN_INTERFACE void timidity_init_player(void);
617 MAIN_INTERFACE int timidity_play_main(int nfiles, char **files);
618 MAIN_INTERFACE int got_a_configuration;
619 char *wrdt_open_opts = NULL;
620 char *opt_aq_max_buff = NULL,
621      *opt_aq_fill_buff = NULL;
622 void timidity_init_aq_buff(void);
623 int opt_control_ratio = 0; /* Save -C option */
624 ///r
625 char *opt_reduce_voice_threshold = NULL;
626 char *opt_reduce_quality_threshold = NULL;
627 char *opt_reduce_polyphony_threshold = NULL;
628
629 #if (defined(IA_W32GUI) || defined(IA_W32G_SYN)) && \
630     defined(__GNUC__) && !defined(SUPPORT_WINMAIN)
631 extern int RestartTimidity;
632 #endif /* (IA_W32GUI || IA_W32G_SYN) && __GNUC__ && !SUPPORT_WINMAIN */
633
634 int set_extension_modes(char *);
635 int set_ctl(char *);
636 int set_play_mode(char *);
637 int set_wrd(char *);
638 MAIN_INTERFACE int set_tim_opt_short(int c, const char *);
639 MAIN_INTERFACE int set_tim_opt_long(int, const char *, int);
640 static inline int parse_opt_A(const char *);
641 static inline int parse_opt_master_volume(const char *arg);
642 static inline int parse_opt_drum_power(const char *);
643 static inline int parse_opt_volume_comp(const char *);
644 static inline int parse_opt_a(const char *);
645 static inline int parse_opt_B(const char *);
646 static inline int parse_opt_C(const char *);
647 static inline int parse_opt_c(const char *);
648 static inline int parse_opt_D(const char *);
649 static inline int parse_opt_d(const char *);
650 static inline int parse_opt_E(const char *);
651 static inline int parse_opt_mod_wheel(const char *);
652 static inline int parse_opt_portamento(const char *);
653 static inline int parse_opt_vibrato(const char *);
654 static inline int parse_opt_ch_pressure(const char *);
655 static inline int parse_opt_mod_env(const char *);
656 static inline int parse_opt_trace_text(const char *);
657 static inline int parse_opt_overlap_voice(const char *);
658 ///r
659 static inline int parse_opt_overlap_voice_count(const char *);
660 static inline int parse_opt_max_channel_voices(const char *);
661
662 static inline int parse_opt_temper_control(const char *);
663 static inline int parse_opt_default_mid(const char *);
664 static inline int parse_opt_system_mid(const char *);
665 static inline int parse_opt_default_bank(const char *);
666 static inline int parse_opt_force_bank(const char *);
667 static inline int parse_opt_default_program(const char *);
668 static inline int parse_opt_force_program(const char *);
669 static inline int set_default_program(int);
670 static inline int parse_opt_delay(const char *arg);
671 static inline int parse_opt_chorus(const char *);
672 static inline int parse_opt_reverb(const char *);
673 static int parse_opt_reverb_freeverb(const char *arg, char type);
674 static inline int parse_opt_voice_lpf(const char *);
675 static inline int parse_opt_voice_hpf(const char *);
676 static inline int parse_opt_noise_shaping(const char *);
677 static inline int parse_opt_resample(const char *);
678 static inline int parse_opt_e(const char *);
679 static inline int parse_opt_F(const char *);
680 static inline int parse_opt_f(const char *);
681 static inline int parse_opt_G(const char*);
682 static inline int parse_opt_G1(const char*);
683 static int parse_segment(TimeSegment*, const char*);
684 static int parse_segment2(TimeSegment*, const char*);
685 static int parse_time(FLOAT_T*, const char*);
686 static int parse_time2(Measure*, const char*);
687 static inline int parse_opt_g(const char *);
688 static inline int parse_opt_H(const char *);
689 __attribute__((noreturn))
690 static inline int parse_opt_h(const char *);
691 #ifdef IA_DYNAMIC
692 static inline void list_dyna_interface(FILE *, char *, char *);
693 ControlMode *dynamic_interface_module(int);
694 #endif
695 static inline int parse_opt_i(const char *);
696 static inline int parse_opt_verbose(const char *);
697 static inline int parse_opt_quiet(const char *);
698 static inline int parse_opt_trace(const char *);
699 static inline int parse_opt_loop(const char *);
700 static inline int parse_opt_random(const char *);
701 static inline int parse_opt_sort(const char *);
702 #ifdef IA_ALSASEQ
703 static inline int parse_opt_background(const char *);
704 static inline int parse_opt_rt_prio(const char *);
705 static inline int parse_opt_seq_ports(const char *);
706 #endif
707 #if defined(IA_WINSYN) || defined(IA_NPSYN) || defined(IA_PORTMIDI) || \
708     defined(IA_W32G_SYN) || defined(IA_W32GUI)
709 static inline int parse_opt_rtsyn_latency(const char*);
710 static inline int parse_opt_rtsyn_skip_aq(const char*);
711 static inline int parse_opt_rtsyn_print_ports(const char*);
712 #endif /* RTSYN || IA_W32GUI */
713 static inline int parse_opt_j(const char *);
714 static inline int parse_opt_K(const char *);
715 static inline int parse_opt_k(const char *);
716 static inline int parse_opt_L(const char *);
717 static inline int parse_opt_l(const char *);
718 static inline int parse_opt_resample_over_sampling(const char *);
719 static inline int parse_opt_pre_resample(const char *);
720 static inline int parse_opt_M(const char *);
721 static inline int parse_opt_m(const char *);
722 static inline int parse_opt_N(const char *);
723 static inline int parse_opt_n(const char *);
724 static inline int parse_opt_O(const char *);
725 static inline int parse_opt_output_stereo(const char *);
726 static inline int parse_opt_output_signed(const char *);
727 static inline int parse_opt_output_bitwidth(const char *);
728 static inline int parse_opt_output_format(const char *);
729 static inline int parse_opt_output_swab(const char *);
730 ///r
731 static inline int parse_opt_output_device_id(const char *);
732 #ifdef AU_W32
733 static inline int parse_opt_wmme_device_id(const char *);
734 static inline int parse_opt_wave_format_ext(const char *arg);
735 #endif
736 #ifdef AU_WDMKS
737 static inline int parse_opt_wdmks_device_id(const char *arg);
738 static inline int parse_opt_wdmks_latency(const char *arg);
739 static inline int parse_opt_wdmks_format_ext(const char *arg);
740 static inline int parse_opt_wdmks_polling(const char *arg);
741 static inline int parse_opt_wdmks_thread_priority(const char *arg);
742 static inline int parse_opt_wdmks_rt_priority(const char *arg);
743 static inline int parse_opt_wdmks_pin_priority(const char *arg);
744 #endif
745 #ifdef AU_WASAPI
746 static inline int parse_opt_wasapi_device_id(const char *arg);
747 static inline int parse_opt_wasapi_latency(const char *arg);
748 static inline int parse_opt_wasapi_format_ext(const char *arg);
749 static inline int parse_opt_wasapi_exclusive(const char *arg);
750 static inline int parse_opt_wasapi_polling(const char *arg);
751 static inline int parse_opt_wasapi_priority(const char *arg);
752 static inline int parse_opt_wasapi_stream_category(const char *arg);
753 static inline int parse_opt_wasapi_stream_option(const char *arg);
754 #endif
755 #ifdef AU_PORTAUDIO
756 static inline int parse_opt_pa_wmme_device_id(const char *);
757 static inline int parse_opt_pa_ds_device_id(const char *);
758 static inline int parse_opt_pa_asio_device_id(const char *);
759 #ifdef PORTAUDIO_V19
760 static inline int parse_opt_pa_wdmks_device_id(const char *);
761 static inline int parse_opt_pa_wasapi_device_id(const char *);
762 static inline int parse_opt_pa_wasapi_flag(const char *);
763 static inline int parse_opt_pa_wasapi_stream_category(const char *arg);
764 static inline int parse_opt_pa_wasapi_stream_option(const char *arg);
765 #endif
766 #endif
767
768 #if defined(AU_PORTAUDIO) || defined(AU_W32)
769 static inline int parse_opt_output_device(const char*);
770 #endif
771 static inline int parse_opt_wave_extensible(const char*);
772 static inline int parse_opt_wave_update_step(const char*);
773 #ifdef AU_FLAC
774 static inline int parse_opt_flac_verify(const char *);
775 static inline int parse_opt_flac_padding(const char *);
776 static inline int parse_opt_flac_complevel(const char *);
777 #ifdef AU_OGGFLAC
778 static inline int parse_opt_flac_oggflac(const char *);
779 #endif /* AU_OGGFLAC */
780 #endif /* AU_FLAC */
781 #ifdef AU_OPUS
782 static inline int parse_opt_opus_nframes(const char *);
783 static inline int parse_opt_opus_bitrate(const char *);
784 static inline int parse_opt_opus_complexity(const char *);
785 static inline int parse_opt_opus_vbr(const char *);
786 static inline int parse_opt_opus_cvbr(const char *);
787 #endif /* AU_OPUS */
788 #ifdef AU_SPEEX
789 static inline int parse_opt_speex_quality(const char *);
790 static inline int parse_opt_speex_vbr(const char *);
791 static inline int parse_opt_speex_abr(const char *);
792 static inline int parse_opt_speex_vad(const char *);
793 static inline int parse_opt_speex_dtx(const char *);
794 static inline int parse_opt_speex_complexity(const char *);
795 static inline int parse_opt_speex_nframes(const char *);
796 #endif /* AU_SPEEX */
797 static inline int parse_opt_o(const char *);
798 static inline int parse_opt_P(const char *);
799 static inline int parse_opt_p(const char *);
800 static inline int parse_opt_p1(const char *);
801 static inline int parse_opt_Q(const char *);
802 static inline int parse_opt_Q1(const char *);
803 static inline int parse_opt_preserve_silence(const char *);
804 static inline int parse_opt_q(const char *);
805 static inline int parse_opt_R(const char *);
806 ///r
807 static inline int parse_opt_r(const char *);
808
809 static inline int parse_opt_S(const char *);
810 static inline int parse_opt_s(const char *);
811 static inline int parse_opt_T(const char *);
812 static inline int parse_opt_t(const char *);
813 static inline int parse_opt_U(const char *);
814 #if defined(AU_VOLUME_CALC)
815 static inline int parse_opt_volume_calc_rms(const char *);
816 static inline int parse_opt_volume_calc_trim(const char *);
817 #endif /* AU_VOLUME_CALC */
818 static inline int parse_opt_volume_curve(const char *);
819 __attribute__((noreturn))
820 static inline int parse_opt_v(const char *);
821 static inline int parse_opt_W(const char *);
822 #ifdef __W32__
823 static inline int parse_opt_w(const char *);
824 #endif
825 static inline int parse_opt_x(const char *);
826 ///r
827 static inline int parse_opt_Y(const char *);
828 static inline void expand_escape_string(char *);
829 static inline int parse_opt_Z(const char *);
830 static inline int parse_opt_Z1(const char *);
831 static inline int parse_opt_default_module(const char *);
832 __attribute__((noreturn))
833 static inline int parse_opt_fail(const char *);
834 static inline int set_value(int *, int, int, int, const char *);
835 static inline int set_val_i32(int32 *, int32, int32, int32, const char *);
836 static int parse_val_float_t(FLOAT_T *param, const char *arg, FLOAT_T low, FLOAT_T high, const char *name);
837 static inline int set_val_float_t(FLOAT_T *param, FLOAT_T i, FLOAT_T low, FLOAT_T high, const char *name);
838 static inline int set_channel_flag(ChannelBitMask *, int32, const char *);
839 static inline int y_or_n_p(const char *);
840 static inline int set_flag(int32 *, int32, const char *);
841 static inline FILE *open_pager(void);
842 static inline void close_pager(FILE *);
843 static void interesting_message(void);
844 ///r
845 static inline int parse_opt_add_play_time(const char *arg);
846 static inline int parse_opt_add_silent_time(const char *arg);
847 static inline int parse_opt_emu_delay_time(const char *arg);
848 static inline int parse_opt_mix_envelope(const char *arg);
849 static inline int parse_opt_modulation_update(const char *arg);
850
851 static inline int parse_opt_cut_short_time(const char *arg);
852 static inline int parse_opt_limiter(const char *arg);
853 static inline int parse_opt_compute_thread_num(const char *arg);
854 static inline int parse_opt_trace_mode_update(const char *arg);
855 static inline int parse_opt_load_all_instrument(const char *arg);
856 static inline int parse_opt_int_synth_rate(const char *arg);
857 static inline int parse_opt_int_synth_sine(const char *arg);
858 static inline int parse_opt_int_synth_update(const char *arg);
859 #ifdef SUPPORT_LOOPEVENT
860 static inline int parse_opt_midi_loop_repeat(const char*);
861 static inline int parse_opt_midi_loop_filter(const char *arg);
862 #endif /* SUPPORT_LOOPEVENT */
863 static inline int parse_opt_od_level_gs(const char *arg);
864 static inline int parse_opt_od_drive_gs(const char *arg);
865 static inline int parse_opt_od_level_xg(const char *arg);
866 static inline int parse_opt_od_drive_xg(const char *arg);
867 static inline int parse_opt_od_level_sd(const char *arg);
868 static inline int parse_opt_od_drive_sd(const char *arg);
869
870 #if defined(__W32__)
871 static inline int parse_opt_process_priority(const char *arg);
872 #if !(defined(IA_W32G_SYN) || defined(WINDRV))
873 static inline int parse_opt_player_thread_priority(const char *arg);
874 #endif
875 static void w32_exit(void);
876 static int w32_atexit = 1;
877 #endif
878
879 extern StringTable wrd_read_opts;
880
881 extern int SecondMode;
882
883 extern struct URL_module URL_module_file;
884 #ifndef __MACOS__
885 extern struct URL_module URL_module_dir;
886 #endif /* __MACOS__ */
887 #ifdef SUPPORT_SOCKET
888 extern struct URL_module URL_module_http;
889 extern struct URL_module URL_module_ftp;
890 extern struct URL_module URL_module_news;
891 extern struct URL_module URL_module_newsgroup;
892 #endif /* SUPPORT_SOCKET */
893 #ifdef HAVE_POPEN
894 extern struct URL_module URL_module_pipe;
895 #endif /* HAVE_POPEN */
896
897 MAIN_INTERFACE struct URL_module *url_module_list[] =
898 {
899     &URL_module_file,
900 #ifndef __MACOS__
901     &URL_module_dir,
902 #endif /* __MACOS__ */
903 #ifdef SUPPORT_SOCKET
904     &URL_module_http,
905     &URL_module_ftp,
906     &URL_module_news,
907     &URL_module_newsgroup,
908 #endif /* SUPPORT_SOCKET */
909 #if !defined(__MACOS__) && defined(HAVE_POPEN)
910     &URL_module_pipe,
911 #endif
912 #if defined(main) || defined(ANOTHER_MAIN)
913     /* You can put some other modules */
914     NULL,
915     NULL,
916     NULL,
917     NULL,
918     NULL,
919     NULL,
920     NULL,
921     NULL,
922 #endif /* main */
923     NULL
924 };
925
926 #ifdef IA_DYNAMIC
927 #include "dlutils.h"
928 #ifndef SHARED_LIB_PATH
929 #define SHARED_LIB_PATH PKGLIBDIR
930 #endif /* SHARED_LIB_PATH */
931 static char *dynamic_lib_root = NULL;
932 #endif /* IA_DYNAMIC */
933
934 #ifndef MAXPATHLEN
935 #define MAXPATHLEN 1024
936 #endif /* MAXPATHLEN */
937
938 int free_instruments_afterwards = 1;
939 int def_prog = -1;
940 char def_instr_name[FILEPATH_MAX] = "";
941 VOLATILE int intr = 0;
942
943 #ifdef __W32__
944 CRITICAL_SECTION critSect;
945
946 #pragma argsused
947 static BOOL WINAPI handler(DWORD dw)
948 {
949 #if defined(IA_WINSYN) || defined(IA_PORTMIDISYN)
950         if (ctl->id_character == 'W' || ctl->id_character == 'P')
951                 rtsyn_midiports_close();
952 #endif /* IA_WINSYN || IA_PORTMIDISYN */
953 #if 0
954 #ifdef IA_NPSYN
955         if (ctl->id_character == 'N')
956                 return FALSE;   /* why FALSE need?  It must close by intr++; */
957 #endif /* IA_NPSYN */
958 #endif
959         printf ("***BREAK" NLS);
960         fflush(stdout);
961         intr++;
962         return TRUE;
963 }
964 #endif /* __W32__ */
965
966 int effect_lr_mode = -1;
967 /* 0: left delay
968  * 1: right delay
969  * 2: rotate
970  * -1: not use
971  */
972 int effect_lr_delay_msec = 25;
973
974 extern char* pcm_alternate_file;
975 /* NULL, "none": disabled (default)
976  * "auto":       automatically selected
977  * filename:     use the one.
978  */
979
980 #ifndef atof
981 extern double atof(const char *);
982 #endif
983
984 ///r
985 /*! copy bank and, if necessary, map appropriately */
986 static void copybank(ToneBank *to, ToneBank *from, int mapid, int bankmapfrom, int bankno)
987 {
988         ToneBankElement *toelm, *fromelm;
989         int i;
990         int elm;
991
992         if (from == NULL)
993                 return;
994         for(i = 0; i < 128; i++){
995                 if(from->tone[i][0] == NULL)
996                         continue;
997                 for(elm = 0; elm <= from->tone[i][0]->element_num; elm++) {
998                         toelm = to->tone[i][elm];
999                         fromelm = from->tone[i][elm];
1000                         if (fromelm->name == NULL)
1001                                 continue;
1002                         if(toelm == NULL){                                      
1003                                 if(alloc_tone_bank_element(&to->tone[i][elm])){
1004                                         ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "copybank ToneBankElement malloc error ",mapid, bankmapfrom, bankno, elm);
1005                                         break;
1006                                 }
1007                                 toelm = to->tone[i][elm];
1008                         }
1009                         copy_tone_bank_element(toelm, fromelm);
1010                         toelm->instrument = NULL;
1011                         if (mapid != INST_NO_MAP)
1012                                 set_instrument_map(mapid, bankmapfrom, i, bankno, i);
1013                 }
1014         }
1015 }
1016
1017 /*! copy the whole mapped bank. returns 0 if no error. */
1018 static int copymap(int mapto, int mapfrom, int isdrum)
1019 {
1020         ToneBank **tb = isdrum ? drumset : tonebank;
1021         int i, bankfrom, bankto;
1022         
1023         for(i = 0; i < 128; i++)
1024         {
1025                 bankfrom = find_instrument_map_bank(isdrum, mapfrom, i);
1026                 if (bankfrom <= 0) /* not mapped */
1027                         continue;
1028                 bankto = alloc_instrument_map_bank(isdrum, mapto, i);
1029                 if (bankto == -1) /* failed */
1030                         return 1;
1031                 copybank(tb[bankto], tb[bankfrom], mapto, i, bankto);
1032         }
1033         return 0;
1034 }
1035
1036 static float *config_parse_tune(const char *cp, int *num)
1037 {
1038         const char *p;
1039         float *tune_list;
1040         int i;
1041         
1042         /* count num */
1043         *num = 1, p = cp;
1044         while ((p = strchr(p, ',')) != NULL)
1045                 (*num)++, p++;
1046         /* alloc */
1047         tune_list = (float *) safe_malloc((*num) * sizeof(float));
1048         /* regist */
1049         for (i = 0, p = cp; i < *num; i++, p++) {
1050                 tune_list[i] = atof(p);
1051                 if (! (p = strchr(p, ',')))
1052                         break;
1053         }
1054         return tune_list;
1055 }
1056
1057 static int16 *config_parse_int16(const char *cp, int *num)
1058 {
1059         const char *p;
1060         int16 *list;
1061         int i;
1062         
1063         /* count num */
1064         *num = 1, p = cp;
1065         while ((p = strchr(p, ',')) != NULL)
1066                 (*num)++, p++;
1067         /* alloc */
1068         list = (int16 *) safe_malloc((*num) * sizeof(int16));
1069         /* regist */
1070         for (i = 0, p = cp; i < *num; i++, p++) {
1071                 list[i] = atoi(p);
1072                 if (! (p = strchr(p, ',')))
1073                         break;
1074         }
1075         return list;
1076 }
1077
1078 static int16 *config_parse_lfo_rate(const char *cp, int *num)
1079 {
1080         const char *p;
1081         int16 *list;
1082         int i;
1083         
1084         /* count num */
1085         *num = 1, p = cp;
1086         while ((p = strchr(p, ',')) != NULL)
1087                 (*num)++, p++;
1088         /* alloc */
1089         list = (int16 *) safe_malloc((*num) * sizeof(int16));
1090         /* regist */
1091         for (i = 0, p = cp; i < *num; i++, p++) {
1092                 if ((! strcmp(p, "off")) || (! strcmp(p, "-0")))
1093                         list[i] = -1;
1094                 else 
1095                         list[i] = atoi(p);
1096                 if (! (p = strchr(p, ',')))
1097                         break;
1098         }
1099         return list;
1100 }
1101
1102 static int **config_parse_envelope(const char *cp, int *num)
1103 {
1104         const char *p, *px;
1105         int **env_list;
1106         int i, j;
1107         
1108         /* count num */
1109         *num = 1, p = cp;
1110         while ((p = strchr(p, ',')) != NULL)
1111                 (*num)++, p++;
1112         /* alloc */
1113         env_list = (int **) safe_malloc((*num) * sizeof(int *));
1114         for (i = 0; i < *num; i++)
1115                 env_list[i] = (int *) safe_malloc(6 * sizeof(int));
1116         /* init */
1117         for (i = 0; i < *num; i++)
1118                 for (j = 0; j < 6; j++)
1119                         env_list[i][j] = -1;
1120         /* regist */
1121         for (i = 0, p = cp; i < *num; i++, p++) {
1122                 px = strchr(p, ',');
1123                 for (j = 0; j < 6; j++, p++) {
1124                         if (*p == ':')
1125                                 continue;
1126                         env_list[i][j] = atoi(p);
1127                         if (! (p = strchr(p, ':')))
1128                                 break;
1129                         if (px && p > px)
1130                                 break;
1131                 }
1132                 if (! (p = px))
1133                         break;
1134         }
1135         return env_list;
1136 }
1137
1138
1139 static int **config_parse_pitch_envelope(const char *cp, int *num)
1140 {
1141         const char *p, *px;
1142         int **env_list;
1143         int i, j;
1144         
1145         /* count num */
1146         *num = 1, p = cp;
1147         while ((p = strchr(p, ',')) != NULL)
1148                 (*num)++, p++;
1149         /* alloc */
1150         env_list = (int **) safe_malloc((*num) * sizeof(int *));
1151         for (i = 0; i < *num; i++)
1152                 env_list[i] = (int *) safe_malloc(9 * sizeof(int));
1153         /* init */
1154         for (i = 0; i < *num; i++)
1155                 for (j = 0; j < 9; j++)
1156                         env_list[i][j] = 0;
1157         /* regist */
1158         for (i = 0, p = cp; i < *num; i++, p++) {
1159                 px = strchr(p, ',');
1160                 for (j = 0; j < 9; j++, p++) {
1161                         if (*p == ':')
1162                                 continue;
1163                         env_list[i][j] = atoi(p);
1164                         if (! (p = strchr(p, ':')))
1165                                 break;
1166                         if (px && p > px)
1167                                 break;
1168                 }
1169                 if (! (p = px))
1170                         break;
1171         }
1172         return env_list;
1173 }
1174
1175 ///r
1176 static int **config_parse_hpfparam(const char *cp, int *num)
1177 {
1178         const char *p, *px;
1179         int **hpf_param;
1180         int i, j;
1181         
1182         /* count num */
1183         *num = 1, p = cp;
1184         while ((p = strchr(p, ',')) != NULL)
1185                 (*num)++, p++;
1186         /* alloc */
1187         hpf_param = (int **) safe_malloc((*num) * sizeof(int *));
1188         for (i = 0; i < *num; i++)
1189                 hpf_param[i] = (int *) safe_malloc(HPF_PARAM_NUM * sizeof(int));
1190         /* init */
1191         for (i = 0; i < *num; i++)
1192                 for (j = 0; j < HPF_PARAM_NUM; j++)
1193                         hpf_param[i][j] = 0;
1194         /* regist */
1195         for (i = 0, p = cp; i < *num; i++, p++) {
1196                 px = strchr(p, ',');
1197                 for (j = 0; j < HPF_PARAM_NUM; j++, p++) {
1198                         if (*p == ':')
1199                                 continue;
1200                         hpf_param[i][j] = atoi(p);
1201                         if (! (p = strchr(p, ':')))
1202                                 break;
1203                         if (px && p > px)
1204                                 break;
1205                 }
1206                 if (! (p = px))
1207                         break;
1208         }
1209         return hpf_param;
1210 }
1211
1212 static int **config_parse_vfxparam(const char *cp, int *num)
1213 {
1214         const char *p, *px;
1215         int **vfx_param;
1216         int i, j;
1217         
1218         /* count num */
1219         *num = 1, p = cp;
1220         while ((p = strchr(p, ',')) != NULL)
1221                 (*num)++, p++;
1222         /* alloc */
1223         vfx_param = (int **) safe_malloc((*num) * sizeof(int *));
1224         for (i = 0; i < *num; i++)
1225                 vfx_param[i] = (int *) safe_malloc(VOICE_EFFECT_PARAM_NUM * sizeof(int));
1226         /* init */
1227         for (i = 0; i < *num; i++)
1228                 for (j = 0; j < VOICE_EFFECT_PARAM_NUM; j++)
1229                         vfx_param[i][j] = 0; // init param
1230         /* regist */
1231         for (i = 0, p = cp; i < *num; i++, p++) {
1232                 px = strchr(p, ',');
1233                 for (j = 0; j < VOICE_EFFECT_PARAM_NUM; j++, p++) {
1234                         if (*p == ':')
1235                                 continue;
1236                         vfx_param[i][j] = atoi(p);
1237                         if (! (p = strchr(p, ':')))
1238                                 break;
1239                         if (px && p > px)
1240                                 break;
1241                 }
1242                 if (! (p = px))
1243                         break;
1244         }
1245         return vfx_param;
1246 }
1247
1248 static Quantity **config_parse_modulation(const char *name, int line, const char *cp, int *num, int mod_type)
1249 {
1250         const char *p, *px, *err;
1251         char buf[128], *delim;
1252         Quantity **mod_list;
1253         int i, j;
1254         static const char * qtypestr[] = {"tremolo", "vibrato"};
1255         static const uint16 qtypes[] = {
1256                 QUANTITY_UNIT_TYPE(LFO_SWEEP), QUANTITY_UNIT_TYPE(LFO_RATE), QUANTITY_UNIT_TYPE(TREMOLO_DEPTH),
1257                 QUANTITY_UNIT_TYPE(LFO_SWEEP), QUANTITY_UNIT_TYPE(LFO_RATE), QUANTITY_UNIT_TYPE(DIRECT_INT)
1258         };
1259         
1260         /* count num */
1261         *num = 1, p = cp;
1262         while ((p = strchr(p, ',')) != NULL)
1263                 (*num)++, p++;
1264         /* alloc */
1265         mod_list = (Quantity **) safe_malloc((*num) * sizeof(Quantity *));
1266         for (i = 0; i < *num; i++)
1267                 mod_list[i] = (Quantity *) safe_malloc(3 * sizeof(Quantity));
1268         /* init */
1269         for (i = 0; i < *num; i++)
1270                 for (j = 0; j < 3; j++)
1271                         INIT_QUANTITY(mod_list[i][j]);
1272         buf[sizeof buf - 1] = '\0';
1273         /* regist */
1274         for (i = 0, p = cp; i < *num; i++, p++) {
1275                 px = strchr(p, ',');
1276                 for (j = 0; j < 3; j++, p++) {
1277                         if (*p == ':')
1278                                 continue;
1279                         if ((delim = strpbrk(strncpy(buf, p, sizeof buf - 1), ":,")) != NULL)
1280                                 *delim = '\0';
1281                         if (*buf != '\0' && (err = string_to_quantity(buf, &mod_list[i][j], qtypes[mod_type * 3 + j])) != NULL) {
1282                                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: line %d: %s: parameter %d of item %d: %s (%s)",
1283                                                 name, line, qtypestr[mod_type], j+1, i+1, err, buf);
1284                                 free_ptr_list(mod_list, *num);
1285                                 mod_list = NULL;
1286                                 *num = 0;
1287                                 return NULL;
1288                         }
1289                         if (! (p = strchr(p, ':')))
1290                                 break;
1291                         if (px && p > px)
1292                                 break;
1293                 }
1294                 if (! (p = px))
1295                         break;
1296         }
1297         return mod_list;
1298 }
1299
1300 ///r
1301 extern int16 sd_mfx_patch_param[13][9][128][33];
1302 static int config_parse_mfx_patch(char *w[], int words, int mapid, int bank, int prog)
1303 {
1304     int i, num;
1305
1306         switch (mapid) {
1307         case SDXX_TONE80_MAP: num = 0; break; /* Special 1 */
1308         case SDXX_TONE81_MAP: num = 1; break; /* Special 2 */
1309         case SDXX_TONE87_MAP: num = 2; break; /* Usr Inst*/ /* SD-50 Preset Inst */
1310         case SDXX_TONE89_MAP: num = -1; break; /* SD-50 Solo */
1311         case SDXX_TONE96_MAP: num = 3; break; /* Clsc Inst */
1312         case SDXX_TONE97_MAP: num = 4; break; /* Cntn Inst */
1313         case SDXX_TONE98_MAP: num = 5; break; /* Solo Inst */
1314         case SDXX_TONE99_MAP: num = 6; break; /* Ehnc Inst */
1315         case SDXX_DRUM86_MAP: num = 7; break; /* Usr Drum */ /* SD-50 Preset Rhythm */
1316         case SDXX_DRUM104_MAP: num = 8; break; /* Clsc Drum */
1317         case SDXX_DRUM105_MAP: num = 9; break; /* Cntn Drum */
1318         case SDXX_DRUM106_MAP: num = 10; break; /* Solo Drum */
1319         case SDXX_DRUM107_MAP: num = 11; break; /* Ehnc Drum */
1320         //case GM2_TONE_MAP: num = 12; break; /* GM2 tone */ // test
1321         default: num = -1; break;
1322         }
1323         if(num < 0)
1324                 return 1;
1325         if(bank > 9)
1326                 return 1;
1327         memset(sd_mfx_patch_param[num][bank][prog], 0, sizeof(int16) * 33);
1328         for(i = 0; i < words && i <= 33; i++){
1329                 int32 tmp = atoi(w[i]);
1330                 if(tmp < INT16_MIN || tmp > INT16_MAX)
1331                         return 1;
1332                 sd_mfx_patch_param[num][bank][prog][i] = tmp;
1333         }
1334     return 0;
1335 }
1336
1337 ///r
1338 static int set_gus_patchconf_opts(const char *name,
1339                 int line, char *opts, ToneBankElement *tone)
1340 {
1341         char *cp;
1342         int i, k, dup;
1343
1344         
1345         if (! (cp = strchr(opts, '='))) {
1346                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
1347                                 "%s: line %d: bad patch option %s", name, line, opts);
1348                 return 1;
1349         }
1350         *cp++ = 0;
1351         if (! strcmp(opts, "amp")) {
1352 #if 1 // float (instrum.c apply_bank_parameter()
1353                 double fk = atof(cp);
1354                 if ((fk < 0 || fk > MAX_AMPLIFICATION) || (*cp < '0' || *cp > '9') && (*cp != '.')) {
1355                         ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
1356                                         "%s: line %d: amplification must be between 0 and %d",
1357                                         name, line, MAX_AMPLIFICATION);
1358                         return 1;
1359                 }
1360                 k = fk * DIV_100 * M_12BIT; // max 32768 = 8 * 4096
1361                 if(k > INT16_MAX)
1362                         k = INT16_MAX; // 32767
1363                 tone->amp = k;
1364 #else
1365                 if ((k < 0 || k > MAX_AMPLIFICATION) || (*cp < '0' || *cp > '9')) {
1366                         ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
1367                                         "%s: line %d: amplification must be between 0 and %d",
1368                                         name, line, MAX_AMPLIFICATION);
1369                         return 1;
1370                 }
1371                 tone->amp = k;
1372 #endif
1373         } else if (! strcmp(opts, "amp_normalize")) {
1374                 if (! strcmp(cp, "on"))
1375                         k = 1;
1376                 else if (! strcmp(cp, "off"))
1377                         k = 0;
1378                 else {
1379                         k = atoi(cp);
1380                         if ((k < 0 || k > 1)
1381                                         || (k == 0 && *cp != '-' && (*cp < '0' || *cp > '1'))) {
1382                                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
1383                                                 "%s: line %d: amp_normalize must be off, on, 0, 1",
1384                                                 name, line);
1385                                 return 1;
1386                         }
1387                 }
1388                 tone->amp_normalize = k;
1389         } else if (! strcmp(opts, "lokey")) {
1390                 k = atoi(cp);
1391                 if ((k < 0 || k > 127) || (*cp < '0' || *cp > '9')) {
1392                         ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
1393                                         "%s: line %d: lokey must be between 0 and 127",
1394                                         name, line);
1395                         return 1;
1396                 }
1397                 tone->lokey = k;
1398         } else if (! strcmp(opts, "hikey")) {
1399                 k = atoi(cp);
1400                 if ((k < 0 || k > 127) || (*cp < '0' || *cp > '9')) {
1401                         ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
1402                                         "%s: line %d: hikey must be between 0 and 127",
1403                                         name, line);
1404                         return 1;
1405                 }
1406                 tone->hikey = k;
1407         } else if (! strcmp(opts, "lovel")) {
1408                 k = atoi(cp);
1409                 if ((k < 0 || k > 127) || (*cp < '0' || *cp > '9')) {
1410                         ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
1411                                         "%s: line %d: lovel must be between 0 and 127",
1412                                         name, line);
1413                         return 1;
1414                 }
1415                 tone->lovel = k;
1416         } else if (! strcmp(opts, "hivel")) {
1417                 k = atoi(cp);
1418                 if ((k < 0 || k > 127) || (*cp < '0' || *cp > '9')) {
1419                         ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
1420                                         "%s: line %d: hivel must be between 0 and 127",
1421                                         name, line);
1422                         return 1;
1423                 }
1424                 tone->hivel = k;
1425         }else if (! strcmp(opts, "sample_lokey")){
1426                 if(tone->sample_lokeynum)
1427                         safe_free(tone->sample_lokey);
1428                 tone->sample_lokey = config_parse_int16(cp, &tone->sample_lokeynum);
1429         }else if (! strcmp(opts, "sample_hikey")){
1430                 if(tone->sample_hikeynum)
1431                         safe_free(tone->sample_hikey);
1432                 tone->sample_hikey = config_parse_int16(cp, &tone->sample_hikeynum);
1433         }else if (! strcmp(opts, "sample_lovel")){
1434                 if(tone->sample_lovelnum)
1435                         safe_free(tone->sample_lovel);
1436                 tone->sample_lovel = config_parse_int16(cp, &tone->sample_lovelnum);
1437         }else if (! strcmp(opts, "sample_hivel")){
1438                 if(tone->sample_hivelnum)
1439                         safe_free(tone->sample_hivel);
1440                 tone->sample_hivel = config_parse_int16(cp, &tone->sample_hivelnum);
1441         } else if (! strcmp(opts, "note")) {
1442                 k = atoi(cp);
1443                 if ((k < 0 || k > 127) || (*cp < '0' || *cp > '9')) {
1444                         ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
1445                                         "%s: line %d: note must be between 0 and 127",
1446                                         name, line);
1447                         return 1;
1448                 }
1449                 tone->note = k;
1450                 if(tone->scltunenum)
1451                         safe_free(tone->scltune);
1452                 tone->scltune = config_parse_int16("100", &tone->scltunenum);
1453 ///r
1454         } else if (! strcmp(opts, "pan")) {
1455                 if (! strcmp(cp, "center"))
1456                         k = 64;
1457                 else if (! strcmp(cp, "left"))
1458                         k = 0;
1459                 else if (! strcmp(cp, "right"))
1460                         k = 127;
1461                 else {
1462                         int tmp = atoi(cp);
1463                         if(tmp == 0)
1464                                 k = 0x40;
1465                         else if(tmp == 100)
1466                                 k = 0x7F;
1467                         else if(tmp == -100)
1468                                 k = 0x00;
1469                         else if(tmp > 0)
1470                                 k = 0x40 + (tmp * 63) / 100;
1471                         else // tmp < 0
1472                                 k = 0x40 + (tmp * 64) / 100;
1473                         if ((k < 0 || k > 127)
1474                                         || (k == 0 && *cp != '-' && (*cp < '0' || *cp > '9'))) {
1475                                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
1476                                                 "%s: line %d: panning must be left, right, "
1477                                                 "center, or between -100 and 100",
1478                                                 name, line);
1479                                 return 1;
1480                         }
1481                 }       
1482                 tone->def_pan = k;
1483 ///r
1484         } else if (! strcmp(opts, "sample_pan")) {
1485                 if (! strcmp(cp, "left"))
1486                         k = -200;
1487                 else if (! strcmp(cp, "right"))
1488                         k = 200;
1489                 else {
1490                         k = atoi(cp);
1491                         if ((k < -200 || k > 200)
1492                                         || (k == 0 && *cp != '-' && (*cp < '0' || *cp > '9'))) {
1493                                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
1494                                                 "%s: line %d: sample_pan must be left, right, "
1495                                                 "or between -200 and 200",
1496                                                 name, line);
1497                                 return 1;
1498                         }
1499                 }
1500                 tone->sample_pan = k + 200; // offset +200
1501         } else if (! strcmp(opts, "sample_width")) {            
1502                 if (! strcmp(cp, "center"))
1503                         k = 0;
1504                 else if (! strcmp(cp, "reverse"))
1505                         k = -100;
1506                 else{
1507                         k = atoi(cp);
1508                         if ((k < -800 || k > 800)
1509                                         || (k == 0 && *cp != '-' && (*cp < '0' || *cp > '9'))) {
1510                                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
1511                                                 "%s: line %d: sample_width must be center, reverse, "
1512                                                 "or between -800 and 800",
1513                                                 name, line);
1514                                 return 1;
1515                         }
1516                 }
1517                 tone->sample_width = k + 800; // offset +800
1518         } else if (! strcmp(opts, "tune")){
1519                 if(tone->tunenum)
1520                         safe_free(tone->tune);
1521                 tone->tune = config_parse_tune(cp, &tone->tunenum);
1522         }else if (! strcmp(opts, "rate")){
1523                 if(tone->envratenum)
1524                         free_ptr_list(tone->envrate, tone->envratenum);
1525                 tone->envrate = config_parse_envelope(cp, &tone->envratenum);
1526         }else if (! strcmp(opts, "offset")){
1527                 if(tone->envofsnum)
1528                         free_ptr_list(tone->envofs, tone->envofsnum);
1529                 tone->envofs = config_parse_envelope(cp, &tone->envofsnum);
1530         }else if (! strcmp(opts, "keep")) {
1531                 if (! strcmp(cp, "env"))
1532                         tone->strip_envelope = 0;
1533                 else if (! strcmp(cp, "loop"))
1534                         tone->strip_loop = 0;
1535                 else if (! strcmp(cp, "voice"))
1536                         tone->keep_voice = 1;
1537                 else {
1538                         ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
1539                                         "%s: line %d: keep must be env or loop or voice", name, line);
1540                         return 1;
1541                 }
1542         } else if (! strcmp(opts, "strip")) {
1543                 if (! strcmp(cp, "env"))
1544                         tone->strip_envelope = 1;
1545                 else if (! strcmp(cp, "loop"))
1546                         tone->strip_loop = 1;
1547                 else if (! strcmp(cp, "tail"))
1548                         tone->strip_tail = 1;
1549                 else {
1550                         ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
1551                                         "%s: line %d: strip must be env, loop, or tail",
1552                                         name, line);
1553                         return 1;
1554                 }
1555         } else if (! strcmp(opts, "tremolo")){
1556                 if(tone->tremnum)
1557                         free_ptr_list(tone->trem, tone->tremnum);
1558                 if ((tone->trem = config_parse_modulation(name,
1559                                 line, cp, &tone->tremnum, 0)) == NULL)
1560                         return 1;
1561 ///r
1562         }else if (! strcmp(opts, "tremdelay")){
1563                 if(tone->tremdelaynum)
1564                         safe_free(tone->tremdelay);
1565                 tone->tremdelay = config_parse_int16(cp, &tone->tremdelaynum);
1566         }else if (! strcmp(opts, "tremsweep")){
1567                 if(tone->tremsweepnum)
1568                         safe_free(tone->tremsweep);
1569                 tone->tremsweep = config_parse_lfo_rate(cp, &tone->tremsweepnum);
1570         }else if (! strcmp(opts, "tremfreq")){
1571                 if(tone->tremfreqnum)
1572                         safe_free(tone->tremfreq);
1573                 tone->tremfreq = config_parse_lfo_rate(cp, &tone->tremfreqnum);         
1574         }else if (! strcmp(opts, "tremamp")){
1575                 if(tone->tremampnum)
1576                         safe_free(tone->tremamp);
1577                 tone->tremamp = config_parse_int16(cp, &tone->tremampnum);
1578         }else if (! strcmp(opts, "trempitch")){
1579                 if(tone->trempitchnum)
1580                         safe_free(tone->trempitch);
1581                 tone->trempitch = config_parse_int16(cp, &tone->trempitchnum);
1582         }else if (! strcmp(opts, "tremfc")){
1583                 if(tone->tremfcnum)
1584                         safe_free(tone->tremfc);
1585                 tone->tremfc = config_parse_int16(cp, &tone->tremfcnum);
1586         } else if (! strcmp(opts, "vibrato")){
1587                 if(tone->vibnum)
1588                         free_ptr_list(tone->vib, tone->vibnum);
1589                 if ((tone->vib = config_parse_modulation(name,
1590                                 line, cp, &tone->vibnum, 1)) == NULL)
1591                         return 1;       
1592 ///r                    
1593         }else if (! strcmp(opts, "vibdelay")){
1594                 if(tone->vibdelaynum)
1595                         safe_free(tone->vibdelay);
1596                 tone->vibdelay = config_parse_int16(cp, &tone->vibdelaynum);            
1597         }else if (! strcmp(opts, "vibsweep")){
1598                 if(tone->vibsweepnum)
1599                         safe_free(tone->vibsweep);
1600                 tone->vibsweep = config_parse_lfo_rate(cp, &tone->vibsweepnum);
1601         }else if (! strcmp(opts, "vibfreq")){
1602                 if(tone->vibfreqnum)
1603                         safe_free(tone->vibfreq);
1604                 tone->vibfreq = config_parse_lfo_rate(cp, &tone->vibfreqnum);   
1605         }else if (! strcmp(opts, "vibamp")){
1606                 if(tone->vibampnum)
1607                         safe_free(tone->vibamp);
1608                 tone->vibamp = config_parse_int16(cp, &tone->vibampnum);
1609         }else if (! strcmp(opts, "vibpitch")){
1610                 if(tone->vibpitchnum)
1611                         safe_free(tone->vibpitch);
1612                 tone->vibpitch = config_parse_int16(cp, &tone->vibpitchnum);
1613         }else if (! strcmp(opts, "vibfc")){
1614                 if(tone->vibfcnum)
1615                         safe_free(tone->vibfc);
1616                 tone->vibfc = config_parse_int16(cp, &tone->vibfcnum);
1617         } else if (! strcmp(opts, "sclnote")){
1618                 if(tone->sclnotenum)
1619                         safe_free(tone->sclnote);
1620                 tone->sclnote = config_parse_int16(cp, &tone->sclnotenum);
1621         }else if (! strcmp(opts, "scltune")){
1622                 if(tone->scltunenum)
1623                         safe_free(tone->scltune);
1624                 tone->scltune = config_parse_int16(cp, &tone->scltunenum);
1625         }else if (! strcmp(opts, "comm")) {
1626                 char *p;
1627                 
1628                 if (!tone->comment)
1629                         tone->comment = (char *)safe_malloc(sizeof(char) * MAX_TONEBANK_COMM_LEN);
1630                 strncpy(tone->comment, cp, MAX_TONEBANK_COMM_LEN - 1);
1631                 tone->comment[MAX_TONEBANK_COMM_LEN - 1] = '\0';
1632                 p = tone->comment;
1633                 while (*p) {
1634                         if (*p == ',')
1635                                 *p = ' ';
1636                         p++;
1637                 }
1638         } else if (! strcmp(opts, "modrate")){
1639                 if(tone->modenvratenum)
1640                         free_ptr_list(tone->modenvrate, tone->modenvratenum);
1641                 tone->modenvrate = config_parse_envelope(cp, &tone->modenvratenum);
1642         }else if (! strcmp(opts, "modoffset")){
1643                 if(tone->modenvofsnum)
1644                         free_ptr_list(tone->modenvofs, tone->modenvofsnum);
1645                 tone->modenvofs = config_parse_envelope(cp, &tone->modenvofsnum);
1646         }else if (! strcmp(opts, "envkeyf")){
1647                 if(tone->envkeyfnum)
1648                         free_ptr_list(tone->envkeyf, tone->envkeyfnum);
1649                 tone->envkeyf = config_parse_envelope(cp, &tone->envkeyfnum);
1650         }else if (! strcmp(opts, "envvelf")){
1651                 if(tone->envvelfnum)
1652                         free_ptr_list(tone->envvelf, tone->envvelfnum);
1653                 tone->envvelf = config_parse_envelope(cp, &tone->envvelfnum);
1654         }else if (! strcmp(opts, "modkeyf")){
1655                 if(tone->modenvkeyfnum)
1656                         free_ptr_list(tone->modenvkeyf, tone->modenvkeyfnum);
1657                 tone->modenvkeyf = config_parse_envelope(cp, &tone->modenvkeyfnum);
1658         }else if (! strcmp(opts, "modvelf")){
1659                 if(tone->modenvvelfnum)
1660                         free_ptr_list(tone->modenvvelf, tone->modenvvelfnum);
1661                 tone->modenvvelf = config_parse_envelope(cp, &tone->modenvvelfnum);
1662         }else if (! strcmp(opts, "modpitch")){
1663                 if(tone->modpitchnum)
1664                         safe_free(tone->modpitch);
1665                 tone->modpitch = config_parse_int16(cp, &tone->modpitchnum);
1666         }else if (! strcmp(opts, "modfc")){
1667                 if(tone->modfcnum)
1668                         safe_free(tone->modfc);
1669                 tone->modfc = config_parse_int16(cp, &tone->modfcnum);
1670         }else if (! strcmp(opts, "fc")){
1671                 if(tone->fcnum)
1672                         safe_free(tone->fc);
1673                 tone->fc = config_parse_int16(cp, &tone->fcnum);
1674         }else if (! strcmp(opts, "q")){
1675                 if(tone->resonum)
1676                         safe_free(tone->reso);
1677                 tone->reso = config_parse_int16(cp, &tone->resonum);
1678 ///r
1679         }else if (! strcmp(opts, "fclow")){
1680                 if(tone->fclownum)
1681                         safe_free(tone->fclow);
1682                 tone->fclow = config_parse_int16(cp, &tone->fclownum);
1683         }else if (! strcmp(opts, "fcmul")){
1684                 if(tone->fcmulnum)
1685                         safe_free(tone->fcmul);
1686                 tone->fcmul = config_parse_int16(cp, &tone->fcmulnum);
1687         }else if (! strcmp(opts, "fcadd")){
1688                 if(tone->fcaddnum)
1689                         safe_free(tone->fcadd);
1690                 tone->fcadd = config_parse_int16(cp, &tone->fcaddnum);
1691         } else if (! strcmp(opts, "pitenv")){
1692                 if(tone->pitenvnum)
1693                         free_ptr_list(tone->pitenv, tone->pitenvnum);
1694                 tone->pitenv = config_parse_pitch_envelope(cp, &tone->pitenvnum);
1695         }else if (! strcmp(opts, "fckeyf"))             /* filter key-follow */
1696                 tone->key_to_fc = atoi(cp);
1697         else if (! strcmp(opts, "fcvelf"))              /* filter velocity-follow */
1698                 tone->vel_to_fc = atoi(cp);
1699         else if (! strcmp(opts, "qvelf"))               /* resonance velocity-follow */
1700                 tone->vel_to_resonance = atoi(cp);
1701 ///r
1702         else if (! strcmp(opts, "perc"))
1703                 tone->rx_note_off = atoi(cp) ? 0 : 1;
1704         else if (! strcmp(opts, "rxnoteoff"))
1705                 tone->rx_note_off = atoi(cp);
1706         else if (! strcmp(opts, "lpf"))         /* lpf type */
1707                 tone->lpf_type = atoi(cp);
1708         else if (! strcmp(opts, "hpf")){                /* hpf */
1709                 if(tone->hpfnum)
1710                         free_ptr_list(tone->hpf, tone->hpfnum);
1711                 tone->hpf = config_parse_hpfparam(cp, &tone->hpfnum);
1712 #ifdef VOICE_EFFECT
1713         }else if (! strcmp(opts, "vfx")){               /* voice effect*/
1714                 cfg_flg_vfx = 1;
1715                 for (i = 0; i < VOICE_EFFECT_NUM; i++){
1716                         if(tone->vfxnum[i]) // already use
1717                                 continue;
1718                         if(tone->vfx[i]){
1719                                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,"VFX ERROR: read cfg. vfx pointer already exist.");
1720                                 continue; // error check
1721                         }
1722                         tone->vfx[i] = config_parse_vfxparam(cp, &tone->vfxnum[i]);
1723                         tone->vfxe_num = i + 1;
1724                         break;
1725                 }
1726 #endif
1727 ///r
1728         }else {
1729                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
1730                                 "%s: line %d: bad patch option %s",
1731                                 name, line, opts);
1732                 return 1;
1733         }
1734         return 0;
1735 }
1736
1737 ///r
1738 #define SET_GUS_PATCHCONF_COMMENT
1739 static int set_gus_patchconf(const char *name, int line,
1740                              ToneBankElement *tone, char *pat, char **opts)
1741 {
1742     int j;
1743 #ifdef SET_GUS_PATCHCONF_COMMENT
1744                 char *old_name = NULL;
1745
1746                 if(tone != NULL && tone->name != NULL)
1747                         old_name = safe_strdup(tone->name);
1748 #endif
1749 //    reinit_tone_bank_element(tone);
1750
1751     if(strcmp(pat, "%font") == 0 || strcmp(pat, "%sf2") == 0 || strcmp(pat, "%sbk") == 0) /* Font extention */
1752     {
1753         /* %font filename bank prog [note-to-use]
1754          * %font filename 128 bank key
1755          */
1756
1757                 if(opts[0] == NULL || opts[1] == NULL || opts[2] == NULL ||
1758                    (atoi(opts[1]) == 128 && opts[3] == NULL))
1759                 {
1760                         ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
1761                                   "%s: line %d: Syntax error", name, line);
1762                         return 1;
1763                 }
1764                 tone->name = safe_strdup(opts[0]);
1765                 tone->instype = 1; // sf2
1766                 if(atoi(opts[1]) == 128) /* drum */
1767                 {
1768                         tone->font_bank = 128;
1769                         tone->font_preset = atoi(opts[2]);
1770                         tone->font_keynote = atoi(opts[3]);
1771                         opts += 4;
1772                 }
1773                 else
1774                 {
1775                         tone->font_bank = atoi(opts[1]);
1776                         tone->font_preset = atoi(opts[2]);
1777
1778                         if(opts[3] && isdigit(opts[3][0]))
1779                         {
1780                         tone->font_keynote = atoi(opts[3]);
1781                         opts += 4;
1782                         }
1783                         else
1784                         {
1785                         tone->font_keynote = -1;
1786                         opts += 3;
1787                         }
1788                 }
1789     }
1790     else if(strcmp(pat, "%sample") == 0) /* Sample extention */
1791     {
1792         /* %sample filename */
1793                 if(opts[0] == NULL)
1794                 {
1795                         ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
1796                                   "%s: line %d: Syntax error", name, line);
1797                         return 1;
1798                 }
1799                 tone->name = safe_strdup(opts[0]);
1800                 tone->instype = 2; // wav
1801                 opts++;
1802     }
1803 ///r
1804 #ifdef INT_SYNTH
1805     else if(strcmp(pat, "%mms") == 0) /* mms extention */
1806     {
1807         /* %mms filename num */
1808                 if(opts[0] == NULL || opts[1] == NULL)
1809                 {
1810                         ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
1811                                   "%s: line %d: Syntax error", name, line);
1812                         return 1;
1813                 }
1814                 tone->name = safe_strdup(opts[0]);
1815                 tone->instype = 3; // mms
1816                 tone->is_preset = atoi(opts[1]);
1817                 cfg_flg_int_synth_mms = 1;
1818                 opts += 2;
1819     }
1820     else if(strcmp(pat, "%scc") == 0) /* scc extention */
1821     {
1822         /* %scc filename num */
1823                 if(opts[0] == NULL || opts[1] == NULL)
1824                 {
1825                         ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
1826                                   "%s: line %d: Syntax error", name, line);
1827                         return 1;
1828                 }
1829                 tone->name = safe_strdup(opts[0]);
1830                 tone->instype = 4; // scc
1831                 tone->is_preset = atoi(opts[1]);
1832                 cfg_flg_int_synth_scc = 1;
1833                 opts += 2;
1834     }
1835 #endif
1836 #ifdef ENABLE_SFZ
1837         else if(strcmp(pat, "%sfz") == 0) /* sfz extension */
1838         {
1839                 /* %sfz filename */
1840                 if (opts[0] == NULL)
1841                 {
1842                         ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
1843                                 "%s: line %d: Syntax error", name, line);
1844                         return 1;
1845                 }
1846                 tone->name = safe_strdup(opts[0]);
1847                 tone->instype = 5; // sfz
1848                 opts++;
1849         }
1850 #endif
1851 #ifdef ENABLE_DLS
1852         else if (strcmp(pat, "%dls") == 0) /* dls extension */
1853         {
1854                 /* %dls filename bank prog [note-to-use]
1855                  * %dls filename 128 bank key
1856                  */
1857
1858                 if (opts[0] == NULL || opts[1] == NULL || opts[2] == NULL ||
1859                         (atoi(opts[1]) == 128 && opts[3] == NULL))
1860                 {
1861                         ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
1862                                 "%s: line %d: Syntax error", name, line);
1863                         return 1;
1864                 }
1865                 tone->name = safe_strdup(opts[0]);
1866                 tone->instype = 6; // dls
1867                 if (atoi(opts[1]) == 128) /* drum */
1868                 {
1869                         tone->font_bank = 128;
1870                         tone->font_preset = atoi(opts[2]);
1871                         tone->font_keynote = atoi(opts[3]);
1872                         opts += 4;
1873                 }
1874                 else
1875                 {
1876                         tone->font_bank = atoi(opts[1]);
1877                         tone->font_preset = atoi(opts[2]);
1878
1879                         if (opts[3] && isdigit(opts[3][0]))
1880                         {
1881                                 tone->font_keynote = atoi(opts[3]);
1882                                 opts += 4;
1883                         }
1884                         else
1885                         {
1886                                 tone->font_keynote = -1;
1887                                 opts += 3;
1888                         }
1889                 }
1890         }
1891 #endif
1892         else if(strcmp(pat, "%pat") == 0) /* pat extention */
1893         {
1894                 tone->instype = 0; // pat
1895                 tone->name = safe_strdup(pat);
1896         }
1897     else
1898     {
1899                 tone->instype = 0; // pat
1900                 tone->name = safe_strdup(pat);
1901     }
1902
1903     for(j = 0; opts[j] != NULL; j++)
1904     {
1905         int err;
1906         if((err = set_gus_patchconf_opts(name, line, opts[j], tone)) != 0)
1907             return err;
1908     }
1909 #if 0 // c210 CFG comm
1910 /*
1911 \82±\82±\82Åtone->comment\82ð\83Z\83b\83g\82·\82é\97\9d\97R\82ª\95s\96¾
1912 CFG\82Ìcomm\82Å\83Z\83b\83g\82µ\82½\82©\82Ç\82¤\82©\82ª\8cã\82Å\82í\82©\82ç\82È\82­\82È\82é 
1913 CFG\82Ìcomm\82ð\97D\90æ\82·\82é\82×\82«
1914 \83T\83\93\83v\83\8b\83\8d\81[\83h\82Ì\8e\9e\82É instrum.c *load_instrument() \82Å\83Z\83b\83g\82·\82é
1915 */
1916 #ifdef SET_GUS_PATCHCONF_COMMENT
1917         if(tone->comment == NULL ||
1918                 (old_name != NULL && strcmp(old_name,tone->comment) == 0))
1919         {
1920                 tone->comment = (char *)safe_malloc(sizeof(char) * MAX_TONEBANK_COMM_LEN);
1921                 strncpy(tone->comment, tone->name, MAX_TONEBANK_COMM_LEN - 1);
1922                 tone->comment[MAX_TONEBANK_COMM_LEN - 1] = '\0';
1923         }
1924         safe_free(old_name);
1925
1926 #else
1927     if(tone->comment == NULL)
1928     {
1929         //tone->comment = safe_strdup(tone->name);
1930         tone->comment = (char *)safe_malloc(sizeof(char) * MAX_TONEBANK_COMM_LEN);
1931         strncpy(tone->comment, tone->name, MAX_TONEBANK_COMM_LEN - 1);
1932         tone->comment[MAX_TONEBANK_COMM_LEN - 1] = '\0';
1933     }
1934 #endif
1935 #endif
1936     return 0;
1937 }
1938 ///r
1939 static int set_patchconf(const char *name, int line, ToneBank *bank, char *w[], int dr, int mapid, int bankmapfrom, int bankno, int add)
1940 {
1941     int i;
1942         int elm;
1943     
1944     i = atoi(w[0]);
1945     if(!dr)
1946         i -= progbase;
1947     if(i < 0 || i > 127)
1948     {
1949         if(dr)
1950             ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
1951                       "%s: line %d: Drum number must be between "
1952                       "0 and 127",
1953                       name, line);
1954         else
1955             ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
1956                       "%s: line %d: Program must be between "
1957                       "%d and %d",
1958                       name, line, progbase, 127 + progbase);
1959         return 1;
1960     }
1961     if(!bank)
1962     {
1963         ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
1964                   "%s: line %d: Must specify tone bank or drum set "
1965                   "before assignment", name, line);
1966         return 1;
1967     }
1968         if(add){
1969                 if(bank->tone[i][0] == NULL){
1970                         ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: line %d: undeine normal program" "xx: add ", name, line);
1971                         return 1;
1972                 }
1973                 elm = bank->tone[i][0]->element_num + 1; // next add_elm
1974                 if(elm >= MAX_ELEMENT){
1975                         ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: line %d: Too much program" "%d: add ", name, line, elm);
1976                         return 1;
1977                 }
1978                 if(bank->tone[i][elm] == NULL){
1979                         if(alloc_tone_bank_element(&bank->tone[i][elm])){
1980                                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: line %d: ToneBankElement malloc error" "%d: add ", name, line, elm);
1981                                 return 1;
1982                         }
1983                 }
1984                 reinit_tone_bank_element(bank->tone[i][elm]);
1985                 bank->tone[i][0]->element_num = elm;
1986                 if(set_gus_patchconf(name, line, bank->tone[i][elm], w[1], w + 2))
1987                         return 1;
1988         }else{
1989                 if(bank->tone[i][0] == NULL){
1990                         if(alloc_tone_bank_element(&bank->tone[i][0])){
1991                                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: line %d: ToneBankElement malloc error.", name, line);
1992                                 return 1;
1993                         }
1994                 }
1995                 reinit_tone_bank_element(bank->tone[i][0]);
1996                 for(elm = 1; elm < MAX_ELEMENT; elm++){ // delete add_elm
1997                         if(bank->tone[i][elm]){
1998                                 free_tone_bank_element(bank->tone[i][elm]);
1999                                 safe_free(bank->tone[i][elm]);
2000                                 bank->tone[i][elm] = NULL;
2001                         }
2002                 }
2003                 if(set_gus_patchconf(name, line, bank->tone[i][0], w[1], w + 2)){
2004                         return 1;
2005         }
2006     if (mapid != INST_NO_MAP)
2007                 set_instrument_map(mapid, bankmapfrom, i, bankno, i);
2008         }
2009     return 0;
2010 }
2011
2012 typedef struct {
2013         const char *name;
2014         int mapid, isdrum;
2015 } MapNameEntry;
2016
2017 static int mapnamecompare(const void *name, const void *entry)
2018 {
2019         return strcmp((const char *)name, ((const MapNameEntry *)entry)->name);
2020 }
2021 ///r
2022 static int mapname2id(char *name, int *isdrum)
2023 {
2024         static const MapNameEntry data[] = {
2025                 /* sorted in alphabetical order */
2026                 {"cm32drum",    CM32_DRUM_MAP, 1},
2027                 {"cm32l",               CM32L_TONE_MAP, 0},
2028                 {"cm32p",               CM32P_TONE_MAP, 0},
2029                 {"gm2",         GM2_TONE_MAP, 0},
2030                 {"gm2drum",     GM2_DRUM_MAP, 1},
2031                 {"gm2drum",     GM2_DRUM_MAP, 1},
2032                 {"k05rw000",    K05RW_TONE0_MAP, 0},
2033                 {"k05rw056",    K05RW_TONE56_MAP, 0},
2034                 {"k05rw057",    K05RW_TONE57_MAP, 0},
2035                 {"k05rwdrum062",K05RW_DRUM62_MAP, 1},
2036                 {"mt32",                MT32_TONE_MAP, 0},
2037                 {"mt32drum",    MT32_DRUM_MAP, 1},
2038                 {"nx5r000",             NX5R_TONE0_MAP, 0},
2039                 {"nx5r001",             NX5R_TONE1_MAP, 0},
2040                 {"nx5r002",             NX5R_TONE2_MAP, 0},
2041                 {"nx5r003",             NX5R_TONE3_MAP, 0},
2042                 {"nx5r004",             NX5R_TONE4_MAP, 0},
2043                 {"nx5r005",             NX5R_TONE5_MAP, 0},
2044                 {"nx5r006",             NX5R_TONE6_MAP, 0},
2045                 {"nx5r007",             NX5R_TONE7_MAP, 0},
2046                 {"nx5r008",             NX5R_TONE8_MAP, 0},
2047                 {"nx5r009",             NX5R_TONE9_MAP, 0},
2048                 {"nx5r010",             NX5R_TONE10_MAP, 0},
2049                 {"nx5r011",             NX5R_TONE11_MAP, 0},
2050                 {"nx5r016",             NX5R_TONE16_MAP, 0},
2051                 {"nx5r017",             NX5R_TONE17_MAP, 0},
2052                 {"nx5r018",             NX5R_TONE18_MAP, 0},
2053                 {"nx5r019",             NX5R_TONE19_MAP, 0},
2054                 {"nx5r024",             NX5R_TONE24_MAP, 0},
2055                 {"nx5r025",             NX5R_TONE25_MAP, 0},
2056                 {"nx5r026",             NX5R_TONE26_MAP, 0},
2057                 {"nx5r032",             NX5R_TONE32_MAP, 0},
2058                 {"nx5r033",             NX5R_TONE33_MAP, 0},
2059                 {"nx5r040",             NX5R_TONE40_MAP, 0},
2060                 {"nx5r056",             NX5R_TONE56_MAP, 0},
2061                 {"nx5r057",             NX5R_TONE57_MAP, 0},
2062                 {"nx5r064",             NX5R_TONE64_MAP, 0}, 
2063                 {"nx5r080",             NX5R_TONE80_MAP, 0},
2064                 {"nx5r081",             NX5R_TONE81_MAP, 0},
2065                 {"nx5r082",             NX5R_TONE82_MAP, 0},
2066                 {"nx5r083",             NX5R_TONE83_MAP, 0},
2067                 {"nx5r088",             NX5R_TONE88_MAP, 0},
2068                 {"nx5r089",             NX5R_TONE89_MAP, 0},
2069                 {"nx5r090",             NX5R_TONE90_MAP, 0},
2070                 {"nx5r091",             NX5R_TONE91_MAP, 0},
2071                 {"nx5r125",             NX5R_TONE125_MAP, 0},
2072                 {"nx5rdrum061", NX5R_DRUM61_MAP, 1},
2073                 {"nx5rdrum062", NX5R_DRUM61_MAP, 1},
2074                 {"nx5rdrum126", NX5R_DRUM126_MAP, 1},
2075                 {"nx5rdrum127", NX5R_DRUM127_MAP, 1},
2076                 {"sc55",        SC_55_TONE_MAP, 0},
2077                 {"sc55drum",    SC_55_DRUM_MAP, 1},
2078                 {"sc88",        SC_88_TONE_MAP, 0},
2079                 {"sc8850",      SC_8850_TONE_MAP, 0},
2080                 {"sc8850drum",  SC_8850_DRUM_MAP, 1},
2081                 {"sc88drum",    SC_88_DRUM_MAP, 1},
2082                 {"sc88pro",     SC_88PRO_TONE_MAP, 0},
2083                 {"sc88prodrum", SC_88PRO_DRUM_MAP, 1},
2084                 {"sd080",               SDXX_TONE80_MAP, 0},
2085                 {"sd081",               SDXX_TONE81_MAP, 0},
2086                 {"sd087",               SDXX_TONE87_MAP, 0},
2087                 {"sd089",               SDXX_TONE89_MAP, 0},
2088                 {"sd096",               SDXX_TONE96_MAP, 0},
2089                 {"sd097",               SDXX_TONE97_MAP, 0},
2090                 {"sd098",               SDXX_TONE98_MAP, 0},
2091                 {"sd099",               SDXX_TONE99_MAP, 0},
2092                 {"sddrum104",   SDXX_DRUM104_MAP, 1}, 
2093                 {"sddrum105",   SDXX_DRUM105_MAP, 1}, 
2094                 {"sddrum106",   SDXX_DRUM106_MAP, 1}, 
2095                 {"sddrum107",   SDXX_DRUM107_MAP, 1}, 
2096                 {"sddrum86",    SDXX_DRUM86_MAP, 1}, 
2097                 {"sn01",                SN01_TONE_MAP, 0},
2098                 {"sn02",                SN02_TONE_MAP, 0},
2099                 {"sn02drum",    SN02_DRUM_MAP, 1},
2100                 {"sn03",                SN03_TONE_MAP, 0},
2101                 {"sn04",                SN04_TONE_MAP, 0},
2102                 {"sn05",                SN05_TONE_MAP, 0},
2103                 {"sn06",                SN06_TONE_MAP, 0},
2104                 {"sn07",                SN07_TONE_MAP, 0},
2105                 {"sn08",                SN08_TONE_MAP, 0},
2106                 {"sn09",                SN09_TONE_MAP, 0},
2107                 {"sn10drum",    SN10_DRUM_MAP, 1},
2108                 {"sn11",                SN11_TONE_MAP, 0},
2109                 {"sn12",                SN12_TONE_MAP, 0},
2110                 {"sn13",                SN13_TONE_MAP, 0},
2111                 {"sn14",                SN14_TONE_MAP, 0},
2112                 {"sn15",                SN15_TONE_MAP, 0},
2113                 {"xg",          XG_NORMAL_MAP, 0},
2114                 {"xg000",       XG_NORMAL_MAP, 0},
2115                 {"xg016",       XG_SAMPLING16_MAP, 0}, 
2116                 {"xg032",       XG_PCM_USER_MAP, 0}, 
2117                 {"xg033",       XG_VA_USER_MAP, 0}, 
2118                 {"xg034",       XG_SG_USER_MAP, 0}, 
2119                 {"xg035",       XG_FM_USER_MAP, 0}, 
2120                 {"xg048",       XG_MU100EXC_MAP, 0}, 
2121                 {"xg063",       XG_FREE_MAP, 0}, 
2122                 {"xg064",       XG_PCM_SFX_MAP, 0}, 
2123                 {"xg065",       XG_VA_SFX_MAP, 0}, 
2124                 {"xg066",       XG_SG_SFX_MAP, 0}, 
2125                 {"xg067",       XG_FM_SFX_MAP, 0}, 
2126                 {"xg080",       XG_PCM_A_MAP, 0}, 
2127                 {"xg081",       XG_VA_A_MAP, 0}, 
2128                 {"xg082",       XG_SG_A_MAP, 0}, 
2129                 {"xg083",       XG_FM_A_MAP, 0}, 
2130                 {"xg096",       XG_PCM_B_MAP, 0}, 
2131                 {"xg097",       XG_VA_B_MAP, 0}, 
2132                 {"xg098",       XG_SG_B_MAP, 0}, 
2133                 {"xg099",       XG_FM_B_MAP, 0},
2134                 {"xg126",       XG_SFX_KIT_MAP, 1},
2135                 {"xg127",       XG_DRUM_KIT_MAP, 1},
2136                 {"xgdrum",      XG_DRUM_KIT_MAP, 1},
2137                 {"xgexclusive48", XG_MU100EXC_MAP, 0}, 
2138                 {"xgfma",       XG_FM_A_MAP, 0},
2139                 {"xgfmb",       XG_FM_B_MAP, 0},
2140                 {"xgfmsfx",     XG_FM_SFX_MAP, 0}, 
2141                 {"xgfmuser",    XG_FM_USER_MAP, 0},
2142                 {"xgfree",      XG_FREE_MAP, 0}, 
2143                 {"xgpcma",      XG_PCM_A_MAP, 0},
2144                 {"xgpcmb",      XG_PCM_B_MAP, 0},
2145                 {"xgpcmsfx",    XG_PCM_SFX_MAP, 0}, 
2146                 {"xgpcmuser",   XG_PCM_USER_MAP, 0}, 
2147                 {"xgsampling126",XG_SAMPLING126_MAP, 1}, 
2148                 {"xgsampling16",XG_SAMPLING16_MAP, 0}, 
2149                 {"xgsfx126",    XG_SFX_KIT_MAP, 1},
2150                 {"xgsfx64",     XG_PCM_SFX_MAP, 0},  // = xgpcmsfx
2151                 {"xgsga",       XG_SG_A_MAP, 0},
2152                 {"xgsgb",       XG_SG_B_MAP, 0},
2153                 {"xgsgsfx",     XG_SG_SFX_MAP, 0}, 
2154                 {"xgsguser",    XG_SG_USER_MAP, 0}, 
2155                 {"xgvaa",       XG_VA_A_MAP, 0},
2156                 {"xgvab",       XG_VA_B_MAP, 0},
2157                 {"xgvasfx",     XG_VA_SFX_MAP, 0}, 
2158                 {"xgvauser",    XG_VA_USER_MAP, 0}, 
2159         };
2160         const MapNameEntry *found;
2161         
2162         found = (MapNameEntry *)bsearch(name, data, sizeof data / sizeof data[0], sizeof data[0], mapnamecompare);
2163         if (found != NULL)
2164         {
2165                 *isdrum = found->isdrum;
2166                 return found->mapid;
2167         }
2168         return -1;
2169 }
2170
2171 /* string[0] should not be '#' */
2172 static int strip_trailing_comment(char *string, int next_token_index)
2173 {
2174     if (string[next_token_index - 1] == '#'     /* strip \1 in /^\S+(#*[ \t].*)/ */
2175         && (string[next_token_index] == ' ' || string[next_token_index] == '\t'))
2176     {
2177         string[next_token_index] = '\0';        /* new c-string terminator */
2178         while(string[--next_token_index - 1] == '#')
2179             ;
2180     }
2181     return next_token_index;
2182 }
2183
2184 static char *expand_variables(char *string, MBlockList *varbuf, const char *basedir)
2185 {
2186         char *p, *expstr;
2187         const char *copystr;
2188         int limlen, copylen, explen, varlen, braced;
2189         
2190         if ((p = strchr(string, '$')) == NULL)
2191                 return string;
2192         varlen = strlen(basedir);
2193         explen = limlen = 0;
2194         expstr = NULL;
2195         copystr = string;
2196         copylen = p - string;
2197         string = p;
2198         for(;;)
2199         {
2200                 if (explen + copylen + 1 > limlen)
2201                 {
2202                         limlen += copylen + 128;
2203                         expstr = memcpy(new_segment(varbuf, limlen), expstr, explen);
2204                 }
2205                 memcpy(&expstr[explen], copystr, copylen);
2206                 explen += copylen;
2207                 if (*string == '\0')
2208                         break;
2209                 else if (*string == '$')
2210                 {
2211                         braced = *++string == '{';
2212                         if (braced)
2213                         {
2214                                 if ((p = strchr(string + 1, '}')) == NULL)
2215                                         p = string;     /* no closing brace */
2216                                 else
2217                                         string++;
2218                         }
2219                         else
2220                                 for(p = string; isalnum(*p) || *p == '_'; p++) ;
2221                         if (p == string)        /* empty */
2222                         {
2223                                 copystr = "${";
2224                                 copylen = 1 + braced;
2225                         }
2226                         else
2227                         {
2228                                 if (p - string == 7 && memcmp(string, "basedir", 7) == 0)
2229                                 {
2230                                         copystr = basedir;
2231                                         copylen = varlen;
2232                                 }
2233                                 else    /* undefined variable */
2234                                         copylen = 0;
2235                                 string = p + braced;
2236                         }
2237                 }
2238                 else    /* search next */
2239                 {
2240                         p = strchr(string, '$');
2241                         if (p == NULL)
2242                                 copylen = strlen(string);
2243                         else
2244                                 copylen = p - string;
2245                         copystr = string;
2246                         string += copylen;
2247                 }
2248         }
2249         expstr[explen] = '\0';
2250         return expstr;
2251 }
2252
2253 #define MAXWORDS 130
2254 #define CHECKERRLIMIT \
2255   if(++errcnt >= 10) { \
2256     ctl->cmsg(CMSG_ERROR, VERB_NORMAL, \
2257       "Too many errors... Give up read %s", name); \
2258     reuse_mblock(&varbuf); \
2259     close_file(tf); return 1; }
2260
2261 #define READ_CONFIG_SUCCESS        0
2262 #define READ_CONFIG_ERROR          1
2263 #define READ_CONFIG_RECURSION      2 /* Too much recursion */
2264 #define READ_CONFIG_FILE_NOT_FOUND 3 /* Returned only w. allow_missing_file */
2265
2266
2267 MAIN_INTERFACE int read_config_file(const char *name, int self, int allow_missing_file) // changed elion static int -> int
2268 {
2269     struct timidity_file *tf = NULL;
2270     char buf[2048] = "", *tmp = NULL, *w[MAXWORDS + 1] = { NULL }, *cp = NULL;
2271     ToneBank *bank = NULL;
2272     int i, j, k, line = 0, words, errcnt = 0, elm;
2273     static int rcf_count = 0;
2274     int dr = 0, bankno = 0, mapid = INST_NO_MAP, origbankno = 0x7FFFFFFF;
2275     int extension_flag, param_parse_err;
2276     MBlockList varbuf;
2277     char *basedir = NULL, *sep = NULL;
2278     char *onmemory = NULL;
2279
2280         if(rcf_count == 0){
2281 #ifdef VOICE_EFFECT
2282                 cfg_flg_vfx = 0;
2283 #endif
2284 #ifdef INT_SYNTH
2285                 cfg_flg_int_synth_mms = 0;
2286                 cfg_flg_int_synth_scc = 0;
2287 #endif
2288         }
2289
2290     if (rcf_count > 50)
2291     {
2292         ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2293                   "Probable source loop in configuration files");
2294         return READ_CONFIG_RECURSION;
2295     }
2296
2297     if (self)
2298     {
2299         tf = open_with_constmem(name, strlen(name), OF_VERBOSE);
2300         name = "(configuration)";
2301     }
2302     else if (check_file_extension(name, ".sf2", 0) == 1 || check_file_extension(name, ".sf3", 0) == 1)
2303     {
2304         const char fmt[] = "soundfont \"%s\"\n";
2305         onmemory = (char*) safe_malloc(strlen(fmt) + strlen(name) + 1);
2306         snprintf(onmemory, strlen(fmt) + strlen(name), fmt, name);
2307         tf = open_with_mem(onmemory, strlen(onmemory), OF_VERBOSE);
2308         name = "(configuration)";
2309     }
2310     else
2311         tf = open_file(name, 1, allow_missing_file ? OF_NORMAL : OF_VERBOSE);
2312     if (!tf)
2313         return allow_missing_file ? READ_CONFIG_FILE_NOT_FOUND :
2314                                     READ_CONFIG_ERROR;
2315
2316         init_mblock(&varbuf);
2317         if (!self)
2318         {
2319                 basedir = strdup_mblock(&varbuf, current_filename);
2320                 if (is_url_prefix(basedir))
2321                         sep = strrchr(basedir, '/');
2322                 else
2323                         sep = pathsep_strrchr(basedir);
2324         }
2325         else
2326                 sep = NULL;
2327         if (!sep)
2328         {
2329                 #ifndef __MACOS__
2330                 basedir = ".";
2331                 #else
2332                 basedir = "";
2333                 #endif
2334         }
2335         else
2336         {
2337                 if ((cp = strchr(sep, '#')) != NULL)
2338                         sep = cp + 1;   /* inclusive of '#' */
2339                 *sep = '\0';
2340         }
2341
2342     errno = 0;
2343     while (tf_gets(buf, sizeof(buf), tf))
2344     {
2345         line++;
2346         
2347         if (!strncmp(buf, "#@extension", 11)) {
2348             extension_flag = 1;
2349             i = 11;
2350         }else if (!strncmp(buf, "#extension", 10)) {
2351             extension_flag = 1;
2352             i = 10;
2353         }
2354         else
2355         {
2356             extension_flag = 0;
2357             i = 0;
2358         }
2359
2360         while (isspace(buf[i]))                 /* skip /^\s*(?#)/ */
2361             i++;
2362         if (buf[i] == '#' || buf[i] == '\0')    /* /^#|^$/ */
2363             continue;
2364         tmp = expand_variables(buf, &varbuf, basedir);
2365         j = strcspn(tmp + i, " \t\r\n\240");
2366         if (j == 0)
2367                 j = strlen(tmp + i);
2368         j = strip_trailing_comment(tmp + i, j);
2369         tmp[i + j] = '\0';                      /* terminate the first token */
2370         w[0] = tmp + i;
2371         i += j + 1;
2372         words = param_parse_err = 0;
2373         while (words < MAXWORDS - 1)            /* -1 : next arg */
2374         {
2375             char *terminator;
2376
2377             while (isspace(tmp[i]))             /* skip /^\s*(?#)/ */
2378                 i++;
2379             if (tmp[i] == '\0'
2380                     || tmp[i] == '#')           /* /\s#/ */
2381                 break;
2382             if ((tmp[i] == '"' || tmp[i] == '\'')
2383                     && (terminator = strchr(tmp + i + 1, tmp[i])) != NULL)
2384             {
2385                 if (!isspace(terminator[1]) && terminator[1] != '\0')
2386                 {
2387                     ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2388                         "%s: line %d: there must be at least one whitespace between "
2389                         "string terminator (%c) and the next parameter", name, line, tmp[i]);
2390                     CHECKERRLIMIT;
2391                     param_parse_err = 1;
2392                     break;
2393                 }
2394                 w[++words] = tmp + i + 1;
2395                 i = terminator - tmp + 1;
2396                 *terminator = '\0';
2397             }
2398             else        /* not terminated */
2399             {
2400                 j = strcspn(tmp + i, " \t\r\n\240");
2401                 if (j > 0)
2402                     j = strip_trailing_comment(tmp + i, j);
2403                 w[++words] = tmp + i;
2404                 i += j;
2405                 if (tmp[i] != '\0')             /* unless at the end-of-string (i.e. EOF) */
2406                     tmp[i++] = '\0';            /* terminate the token */
2407             }
2408         }
2409         if (param_parse_err)
2410             continue;
2411         w[++words] = NULL;
2412
2413         /*
2414          * #extension [something...]
2415          */
2416
2417         /* #extension comm program comment */
2418         if (!strcmp(w[0], "comm"))
2419         {
2420             char *p;
2421
2422             if (words != 3)
2423             {
2424                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2425                           "%s: line %d: syntax error", name, line);
2426                 CHECKERRLIMIT;
2427                 continue;
2428             }
2429             if (!bank)
2430             {
2431                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2432                           "%s: line %d: Must specify tone bank or drum "
2433                           "set before assignment", name, line);
2434                 CHECKERRLIMIT;
2435                 continue;
2436             }
2437             i = atoi(w[1]);
2438             if (i < 0 || i > 127)
2439             {
2440                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2441                           "%s: line %d: extension comm must be "
2442                           "between 0 and 127", name, line);
2443                 CHECKERRLIMIT;
2444                 continue;
2445             }           
2446                 if(bank->tone[i][0] == NULL){
2447                         if(alloc_tone_bank_element(&bank->tone[i][0])){
2448                                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: line %d: ToneBankElement malloc error.", name, line);
2449                                 return 1;
2450                         }
2451                 }
2452                 if (!bank->tone[i][0]->comment)
2453                 bank->tone[i][0]->comment = (char*) safe_malloc(sizeof(char) * MAX_TONEBANK_COMM_LEN);
2454                 strncpy(bank->tone[i][0]->comment, w[2], MAX_TONEBANK_COMM_LEN - 1);
2455                 bank->tone[i][0]->comment[MAX_TONEBANK_COMM_LEN - 1] = '\0';
2456                 p = bank->tone[i][0]->comment;
2457                 while (*p)
2458                 {
2459                 if (*p == ',') *p = ' ';
2460                 p++;
2461                 }
2462         }
2463         /* #extension timeout program sec */
2464         else if (!strcmp(w[0], "timeout"))
2465         {
2466             if (words != 3)
2467             {
2468                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2469                           "%s: line %d: syntax error", name, line);
2470                 CHECKERRLIMIT;
2471                 continue;
2472             }
2473             if (!bank)
2474             {
2475                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2476                           "%s: line %d: Must specify tone bank or drum set "
2477                           "before assignment", name, line);
2478                 CHECKERRLIMIT;
2479                 continue;
2480             }
2481             i = atoi(w[1]);
2482             if (i < 0 || i > 127)
2483             {
2484                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2485                           "%s: line %d: extension timeout "
2486                           "must be between 0 and 127", name, line);
2487                 CHECKERRLIMIT;
2488                 continue;
2489             }
2490                 if(bank->tone[i][0])
2491                         bank->tone[i][0]->loop_timeout = atoi(w[2]) & 0xFF;
2492         }
2493         /* #extension copydrumset drumset */
2494         else if (!strcmp(w[0], "copydrumset"))
2495         {
2496             if (words < 2)
2497             {
2498                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2499                           "%s: line %d: No copydrumset number given",
2500                           name, line);
2501                 CHECKERRLIMIT;
2502                 continue;
2503             }
2504             i = atoi(w[1]);
2505             if (i < 0 || i > 127)
2506             {
2507                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2508                           "%s: line %d: extension copydrumset "
2509                           "must be between 0 and 127", name, line);
2510                 CHECKERRLIMIT;
2511                 continue;
2512             }
2513             if (!bank)
2514             {
2515                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2516                           "%s: line %d: Must specify tone bank or "
2517                           "drum set before assignment", name, line);
2518                 CHECKERRLIMIT;
2519                 continue;
2520             }
2521             copybank(bank, drumset[i], mapid, origbankno, bankno);
2522         }
2523         /* #extension copybank bank */
2524         else if (!strcmp(w[0], "copybank"))
2525         {
2526             if (words < 2)
2527             {
2528                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2529                           "%s: line %d: No copybank number given",
2530                           name, line);
2531                 CHECKERRLIMIT;
2532                 continue;
2533             }
2534             i = atoi(w[1]);
2535             if (i < 0 || i > 127)
2536             {
2537                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2538                           "%s: line %d: extension copybank "
2539                           "must be between 0 and 127", name, line);
2540                 CHECKERRLIMIT;
2541                 continue;
2542             }
2543             if (!bank)
2544             {
2545                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2546                           "%s: line %d: Must specify tone bank or "
2547                           "drum set before assignment", name, line);
2548                 CHECKERRLIMIT;
2549                 continue;
2550             }
2551             copybank(bank, tonebank[i], mapid, origbankno, bankno);
2552         }
2553         /* #extension copymap tomapid frommapid */
2554         else if (!strcmp(w[0], "copymap"))
2555         {
2556             int mapto, mapfrom;
2557             int toisdrum, fromisdrum;
2558
2559             if (words != 3)
2560             {
2561                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2562                           "%s: line %d: syntax error", name, line);
2563                 CHECKERRLIMIT;
2564                 continue;
2565             }
2566             if ((mapto = mapname2id(w[1], &toisdrum)) == -1)
2567             {
2568                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2569                           "%s: line %d: Invalid map name: %s", name, line, w[1]);
2570                 CHECKERRLIMIT;
2571                 continue;
2572             }
2573             if ((mapfrom = mapname2id(w[2], &fromisdrum)) == -1)
2574             {
2575                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2576                           "%s: line %d: Invalid map name: %s", name, line, w[2]);
2577                 CHECKERRLIMIT;
2578                 continue;
2579             }
2580             if (toisdrum != fromisdrum)
2581             {
2582                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2583                           "%s: line %d: Map type should be matched", name, line);
2584                 CHECKERRLIMIT;
2585                 continue;
2586             }
2587             if (copymap(mapto, mapfrom, toisdrum))
2588             {
2589                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2590                           "%s: line %d: No free %s available to map",
2591                           name, line, toisdrum ? "drum set" : "tone bank");
2592                 CHECKERRLIMIT;
2593                 continue;
2594             }
2595         }
2596         /* #extension HTTPproxy hostname:port */
2597         else if (!strcmp(w[0], "HTTPproxy"))
2598         {
2599             char r_bracket, l_bracket;
2600
2601             if (words < 2)
2602             {
2603                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2604                           "%s: line %d: No proxy name given",
2605                           name, line);
2606                 CHECKERRLIMIT;
2607                 continue;
2608             }
2609             /* If network is not supported, this extension is ignored. */
2610 #ifdef SUPPORT_SOCKET
2611             url_http_proxy_host = safe_strdup(w[1]);
2612             if ((cp = strrchr(url_http_proxy_host, ':')) == NULL)
2613             {
2614                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2615                           "%s: line %d: Syntax error", name, line);
2616                 CHECKERRLIMIT;
2617                 continue;
2618             }
2619             *cp++ = '\0';
2620             if ((url_http_proxy_port = atoi(cp)) <= 0)
2621             {
2622                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2623                           "%s: line %d: Port number must be "
2624                           "positive number", name, line);
2625                 CHECKERRLIMIT;
2626                 continue;
2627             }
2628
2629             l_bracket = url_http_proxy_host[0];
2630             r_bracket = url_http_proxy_host[strlen(url_http_proxy_host) - 1];
2631
2632             if (l_bracket == '[' || r_bracket == ']')
2633             {
2634                 if (l_bracket != '[' || r_bracket != ']')
2635                 {
2636                     ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2637                               "%s: line %d: Malformed IPv6 address",
2638                               name, line);
2639                     CHECKERRLIMIT;
2640                     continue;
2641                 }
2642                 url_http_proxy_host++;
2643                 url_http_proxy_host[strlen(url_http_proxy_host) - 1] = '\0';
2644             }
2645 #endif /* SUPPORT_SOCKET */
2646         }
2647         /* #extension FTPproxy hostname:port */
2648         else if (!strcmp(w[0], "FTPproxy"))
2649         {
2650             char l_bracket, r_bracket;
2651
2652             if (words < 2)
2653             {
2654                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2655                           "%s: line %d: No proxy name given",
2656                           name, line);
2657                 CHECKERRLIMIT;
2658                 continue;
2659             }
2660             /* If network is not supported, this extension is ignored. */
2661 #ifdef SUPPORT_SOCKET
2662             url_ftp_proxy_host = safe_strdup(w[1]);
2663             if ((cp = strrchr(url_ftp_proxy_host, ':')) == NULL)
2664             {
2665                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2666                           "%s: line %d: Syntax error", name, line);
2667                 CHECKERRLIMIT;
2668                 continue;
2669             }
2670             *cp++ = '\0';
2671             if ((url_ftp_proxy_port = atoi(cp)) <= 0)
2672             {
2673                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2674                           "%s: line %d: Port number "
2675                           "must be positive number", name, line);
2676                 CHECKERRLIMIT;
2677                 continue;
2678             }
2679
2680             l_bracket = url_ftp_proxy_host[0];
2681             r_bracket = url_ftp_proxy_host[strlen(url_ftp_proxy_host) - 1];
2682
2683             if (l_bracket == '[' || r_bracket == ']')
2684             {
2685                 if (l_bracket != '[' || r_bracket != ']')
2686                 {
2687                     ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2688                               "%s: line %d: Malformed IPv6 address",
2689                               name, line);
2690                     CHECKERRLIMIT;
2691                     continue;
2692                 }
2693                 url_ftp_proxy_host++;
2694                 url_ftp_proxy_host[strlen(url_ftp_proxy_host) - 1] = '\0';
2695             }
2696 #endif /* SUPPORT_SOCKET */
2697         }
2698         /* #extension mailaddr somebody@someware.domain.com */
2699         else if (!strcmp(w[0], "mailaddr"))
2700         {
2701             if (words < 2)
2702             {
2703                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2704                           "%s: line %d: No mail address given",
2705                           name, line);
2706                 CHECKERRLIMIT;
2707                 continue;
2708             }
2709             if (strchr(w[1], '@') == NULL) {
2710                 ctl->cmsg(CMSG_WARNING, VERB_NOISY,
2711                           "%s: line %d: Warning: Mail address %s is not valid",
2712                           name, line);
2713             }
2714
2715             /* If network is not supported, this extension is ignored. */
2716 #ifdef SUPPORT_SOCKET
2717             user_mailaddr = safe_strdup(w[1]);
2718 #endif /* SUPPORT_SOCKET */
2719         }
2720         /* #extension opt [-]{option}[optarg] */
2721         else if (!strcmp(w[0], "opt")) {
2722                 int c, longind, err;
2723                 char *p, *cmd, *arg;
2724
2725                 if (words != 2 && words != 3) {
2726                         ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2727                                         "%s: line %d: Syntax error", name, line);
2728                         CHECKERRLIMIT;
2729                         continue;
2730                 }
2731                 if (*w[1] == '-') {
2732                         int optind_save = optind;
2733                         optind = 0;
2734                         c = getopt_long(words, w, optcommands, longopts, &longind);
2735                         err = set_tim_opt_long(c, optarg, longind);
2736                         optind = optind_save;
2737                 } else {
2738                         /* backward compatibility */
2739                         if ((p = strchr(optcommands, c = *(cmd = w[1]))) == NULL)
2740                                 err = 1;
2741                         else {
2742                                 if (*(p + 1) == ':')
2743                                         arg = (words == 2) ? cmd + 1 : w[2];
2744                                 else
2745                                         arg = "";
2746                                 err = set_tim_opt_short(c, arg);
2747                         }
2748                 }
2749                 if (err) {
2750                         /* error */
2751                         ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2752                                         "%s: line %d: Invalid command line option",
2753                                         name, line);
2754                         errcnt += err - 1;
2755                         CHECKERRLIMIT;
2756                         continue;
2757                 }
2758         }
2759         /* #extension undef program */
2760         else if (!strcmp(w[0], "undef"))
2761         {
2762             if (words < 2)
2763             {
2764                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2765                           "%s: line %d: No undef number given",
2766                           name, line);
2767                 CHECKERRLIMIT;
2768                 continue;
2769             }
2770             i = atoi(w[1]);
2771             if (i < 0 || i > 127)
2772             {
2773                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2774                           "%s: line %d: extension undef "
2775                           "must be between 0 and 127", name, line);
2776                 CHECKERRLIMIT;
2777                 continue;
2778             }
2779             if (!bank)
2780             {
2781                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2782                           "%s: line %d: Must specify tone bank or "
2783                           "drum set before assignment", name, line);
2784                 CHECKERRLIMIT;
2785                 continue;
2786             }
2787                 
2788             for (elm = 0; elm < MAX_ELEMENT; elm++)
2789                         if(bank->tone[i][elm])
2790                                 free_tone_bank_element(bank->tone[i][elm]);
2791         }
2792         /* #extension altassign numbers... */
2793         else if (!strcmp(w[0], "altassign"))
2794         {
2795             ToneBank *bk;
2796
2797             if (!bank)
2798             {
2799                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2800                           "%s: line %d: Must specify tone bank or drum set "
2801                           "before altassign", name, line);
2802                 CHECKERRLIMIT;
2803                 continue;
2804             }
2805             if (words < 2)
2806             {
2807                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2808                           "%s: line %d: No alternate assignment", name, line);
2809                 CHECKERRLIMIT;
2810                 continue;
2811             }
2812
2813             if (!dr) {
2814                 ctl->cmsg(CMSG_WARNING, VERB_NORMAL,
2815                           "%s: line %d: Warning: Not a drumset altassign"
2816                           " (ignored)",
2817                           name, line);
2818                 continue;
2819             }
2820
2821             bk = drumset[bankno];
2822             bk->alt = add_altassign_string(bk->alt, w + 1, words - 1);
2823         }
2824         /* #extension legato [program] [0 or 1] */
2825         else if (!strcmp(w[0], "legato"))
2826         {
2827             if (words != 3)
2828             {
2829                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2830                           "%s: line %d: syntax error", name, line);
2831                 CHECKERRLIMIT;
2832                 continue;
2833             }
2834             if (!bank)
2835             {
2836                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2837                           "%s: line %d: Must specify tone bank or drum set "
2838                           "before assignment", name, line);
2839                 CHECKERRLIMIT;
2840                 continue;
2841             }
2842             i = atoi(w[1]);
2843             if (i < 0 || i > 127)
2844             {
2845                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2846                           "%s: line %d: extension legato "
2847                           "must be between 0 and 127", name, line);
2848                 CHECKERRLIMIT;
2849                 continue;
2850             }
2851 ///r
2852                 if(bank->tone[i][0] == NULL){
2853                         if(alloc_tone_bank_element(&bank->tone[i][0])){
2854                                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: line %d: ToneBankElement malloc error.", name, line);
2855                                 return READ_CONFIG_ERROR;
2856                         }
2857                 }
2858                 bank->tone[i][0]->legato = atoi(w[2]);
2859
2860         }       /* #extension damper [program] [0 or 1] */
2861         else if (!strcmp(w[0], "damper"))
2862         {
2863             if (words != 3)
2864             {
2865                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2866                           "%s: line %d: syntax error", name, line);
2867                 CHECKERRLIMIT;
2868                 continue;
2869             }
2870             if (!bank)
2871             {
2872                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2873                           "%s: line %d: Must specify tone bank or drum set "
2874                           "before assignment", name, line);
2875                 CHECKERRLIMIT;
2876                 continue;
2877             }
2878             i = atoi(w[1]);
2879             if (i < 0 || i > 127)
2880             {
2881                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2882                           "%s: line %d: extension damper "
2883                           "must be between 0 and 127", name, line);
2884                 CHECKERRLIMIT;
2885                 continue;
2886             }
2887 ///r
2888                 if(bank->tone[i][0] == NULL){
2889                         if(alloc_tone_bank_element(&bank->tone[i][0])){
2890                                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: line %d: ToneBankElement malloc error.", name, line);
2891                                 return READ_CONFIG_ERROR;
2892                         }
2893                 }
2894                 bank->tone[i][0]->damper_mode = atoi(w[2]);
2895         }       /* #extension rnddelay [program] [0 or 1] */
2896         else if (!strcmp(w[0], "rnddelay"))
2897         {
2898             if (words != 3)
2899             {
2900                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2901                           "%s: line %d: syntax error", name, line);
2902                 CHECKERRLIMIT;
2903                 continue;
2904             }
2905             if (!bank)
2906             {
2907                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2908                           "%s: line %d: Must specify tone bank or drum set "
2909                           "before assignment", name, line);
2910                 CHECKERRLIMIT;
2911                 continue;
2912             }
2913             i = atoi(w[1]);
2914             if (i < 0 || i > 127)
2915             {
2916                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2917                           "%s: line %d: extension rnddelay "
2918                           "must be between 0 and 127", name, line);
2919                 CHECKERRLIMIT;
2920                 continue;
2921             }
2922 ///r
2923                 if(bank->tone[i][0] == NULL){
2924                         if(alloc_tone_bank_element(&bank->tone[i][0])){
2925                                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: line %d: ToneBankElement malloc error.", name, line);
2926                                 return READ_CONFIG_ERROR;
2927                         }
2928                 }
2929                 bank->tone[i][0]->rnddelay = atoi(w[2]);
2930         }       /* #extension level program tva_level */
2931         else if (!strcmp(w[0], "level"))
2932         {
2933             if (words != 3)
2934             {
2935                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: line %d: syntax error", name, line);
2936                 CHECKERRLIMIT;
2937                 continue;
2938             }
2939             if (!bank)
2940             {
2941                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2942                           "%s: line %d: Must specify tone bank or drum set "
2943                           "before assignment", name, line);
2944                 CHECKERRLIMIT;
2945                 continue;
2946             }
2947             i = atoi(w[2]);
2948             if (i < 0 || i > 127)
2949             {
2950                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2951                           "%s: line %d: extension level "
2952                           "must be between 0 and 127", name, line);
2953                 CHECKERRLIMIT;
2954                 continue;
2955             }
2956             cp = w[1];
2957             do {
2958                 if (string_to_7bit_range(cp, &j, &k))
2959                 {
2960                     while (j <= k) {
2961                                 if(bank->tone[j][0] == NULL){
2962                                         if(alloc_tone_bank_element(&bank->tone[j][0])){
2963                                                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: line %d: ToneBankElement malloc error.", name, line);
2964                                                 return READ_CONFIG_ERROR;
2965                                         }
2966                                 }
2967                                 bank->tone[j][0]->tva_level = i;
2968                                 j++;
2969                     }
2970                 }
2971                 cp = strchr(cp, ',');
2972             } while (cp++);
2973         }       /* #extension reverbsend */
2974         else if (!strcmp(w[0], "reverbsend"))
2975         {
2976             if (words != 3)
2977             {
2978                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: line %d: syntax error", name, line);
2979                 CHECKERRLIMIT;
2980                 continue;
2981             }
2982             if (!bank)
2983             {
2984                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2985                           "%s: line %d: Must specify tone bank or drum set "
2986                           "before assignment", name, line);
2987                 CHECKERRLIMIT;
2988                 continue;
2989             }
2990             i = atoi(w[2]);
2991             if (i < 0 || i > 127)
2992             {
2993                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
2994                           "%s: line %d: extension reverbsend "
2995                           "must be between 0 and 127", name, line);
2996                 CHECKERRLIMIT;
2997                 continue;
2998             }
2999             cp = w[1];
3000             do {
3001                 if (string_to_7bit_range(cp, &j, &k))
3002                 {
3003                     while (j <= k) {
3004                                 if(bank->tone[j][0] == NULL){
3005                                         if(alloc_tone_bank_element(&bank->tone[j][0])){
3006                                                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: line %d: ToneBankElement malloc error.", name, line);
3007                                                 return READ_CONFIG_ERROR;
3008                                         }
3009                                 }
3010                                 bank->tone[j][0]->reverb_send = i;
3011                                 j++;
3012                     }
3013                 }
3014                 cp = strchr(cp, ',');
3015             } while (cp++);
3016         }       /* #extension chorussend */
3017         else if (!strcmp(w[0], "chorussend"))
3018         {
3019             if (words != 3)
3020             {
3021                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: line %d: syntax error", name, line);
3022                 CHECKERRLIMIT;
3023                 continue;
3024             }
3025             if (!bank)
3026             {
3027                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3028                           "%s: line %d: Must specify tone bank or drum set "
3029                           "before assignment", name, line);
3030                 CHECKERRLIMIT;
3031                 continue;
3032             }
3033             i = atoi(w[2]);
3034             if (i < 0 || i > 127)
3035             {
3036                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3037                           "%s: line %d: extension chorussend "
3038                           "must be between 0 and 127", name, line);
3039                 CHECKERRLIMIT;
3040                 continue;
3041             }
3042             cp = w[1];
3043             do {
3044                 if (string_to_7bit_range(cp, &j, &k))
3045                 {
3046                     while (j <= k) {
3047                                 if(bank->tone[j][0] == NULL){
3048                                         if(alloc_tone_bank_element(&bank->tone[j][0])){
3049                                                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: line %d: ToneBankElement malloc error.", name, line);
3050                                                 return READ_CONFIG_ERROR;
3051                                         }
3052                                 }
3053                                 bank->tone[j][0]->chorus_send = i;
3054                                 j++;
3055                     }
3056                 }
3057                 cp = strchr(cp, ',');
3058             } while (cp++);
3059         }       /* #extension delaysend */
3060         else if (!strcmp(w[0], "delaysend"))
3061         {
3062             if (words != 3)
3063             {
3064                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: line %d: syntax error", name, line);
3065                 CHECKERRLIMIT;
3066                 continue;
3067             }
3068             if (!bank)
3069             {
3070                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3071                           "%s: line %d: Must specify tone bank or drum set "
3072                           "before assignment", name, line);
3073                 CHECKERRLIMIT;
3074                 continue;
3075             }
3076
3077             i = atoi(w[2]);
3078             if (i < 0 || i > 127)
3079             {
3080                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3081                           "%s: line %d: extension delaysend "
3082                           "must be between 0 and 127", name, line);
3083                 CHECKERRLIMIT;
3084                 continue;
3085             }
3086             cp = w[1];
3087             do {
3088                 if (string_to_7bit_range(cp, &j, &k))
3089                 {
3090                     while (j <= k) {
3091                                 if(bank->tone[j][0] == NULL){
3092                                         if(alloc_tone_bank_element(&bank->tone[j][0])){
3093                                                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: line %d: ToneBankElement malloc error.", name, line);
3094                                                 return READ_CONFIG_ERROR;
3095                                         }
3096                                 }
3097                                 bank->tone[j][0]->delay_send = i;
3098                                 j++;
3099                     }
3100                 }
3101                 cp = strchr(cp, ',');
3102             } while (cp++);
3103         }       
3104         /* #extension rxnoteoff numbers... */
3105         else if (!strcmp(w[0], "rxnoteoff"))
3106         {               
3107             if (words != 3)
3108             {
3109                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: line %d: syntax error", name, line);
3110                 CHECKERRLIMIT;
3111                 continue;
3112             }
3113             if (!bank)
3114             {
3115                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3116                           "%s: line %d: Must specify tone bank or drum set "
3117                           "before assignment", name, line);
3118                 CHECKERRLIMIT;
3119                 continue;
3120             }
3121             if (!dr) {
3122                 ctl->cmsg(CMSG_WARNING, VERB_NORMAL,
3123                           "%s: line %d: Warning: Not a drumset rxnoteoff"
3124                           " (ignored)",
3125                           name, line);
3126                 continue;
3127             }           
3128             i = atoi(w[2]);
3129             if (i < 0 || i > 1)
3130             {
3131                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3132                           "%s: line %d: extension rxnoteoff "
3133                           "must be 0 or 1", name, line);
3134                 CHECKERRLIMIT;
3135                 continue;
3136             }
3137             cp = w[1];
3138             do {
3139                 if (string_to_7bit_range(cp, &j, &k))
3140                 {
3141                     while (j <= k) {
3142                                 if(bank->tone[j][0] == NULL){
3143                                         if(alloc_tone_bank_element(&bank->tone[j][0])){
3144                                                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: line %d: ToneBankElement malloc error.", name, line);
3145                                                 return READ_CONFIG_ERROR;
3146                                         }
3147                                 }
3148                                 bank->tone[j][0]->rx_note_off = i;
3149                                 j++;
3150                     }
3151                 }
3152                 cp = strchr(cp, ',');
3153             } while (cp++);
3154         }
3155         /* #extension playnote */
3156         else if (!strcmp(w[0], "playnote"))
3157         {
3158             if (words != 3)
3159             {
3160                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: line %d: syntax error", name, line);
3161                 CHECKERRLIMIT;
3162                 continue;
3163             }
3164             if (!bank)
3165             {
3166                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3167                           "%s: line %d: Must specify tone bank or drum set "
3168                           "before assignment", name, line);
3169                 CHECKERRLIMIT;
3170                 continue;
3171             }
3172             i = atoi(w[2]);
3173             if (i < 0 || i > 127)
3174             {
3175                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3176                           "%s: line %d: extension playnote"
3177                           "must be between 0 and 127", name, line);
3178                 CHECKERRLIMIT;
3179                 continue;
3180             }
3181             cp = w[1];
3182             do {
3183                 if (string_to_7bit_range(cp, &j, &k))
3184                 {
3185                     while (j <= k) {
3186                                 if(bank->tone[j][0] == NULL){
3187                                         if(alloc_tone_bank_element(&bank->tone[j][0])){
3188                                                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: line %d: ToneBankElement malloc error.", name, line);
3189                                                 return READ_CONFIG_ERROR;
3190                                         }
3191                                 }
3192                                 bank->tone[j][0]->play_note = i;
3193                                 j++;
3194                     }
3195                 }
3196                 cp = strchr(cp, ',');
3197             } while (cp++);
3198         }       /* #extension fc */
3199         else if (!strcmp(w[0], "fc"))
3200         {
3201             if (words != 3)
3202             {
3203                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3204                           "%s: line %d: syntax error", name, line);
3205                 CHECKERRLIMIT;
3206                 continue;
3207             }
3208             if (!bank)
3209             {
3210                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3211                           "%s: line %d: Must specify tone bank or drum set "
3212                           "before assignment", name, line);
3213                 CHECKERRLIMIT;
3214                 continue;
3215             }
3216             i = atoi(w[1]);
3217             if (i < 0 || i > 127)
3218             {
3219                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3220                           "%s: line %d: extension fc "
3221                           "must be between 0 and 127", name, line);
3222                 CHECKERRLIMIT;
3223                 continue;
3224             }
3225 ///r
3226                 for (elm = 0; elm < MAX_ELEMENT; elm++)
3227                         if(bank->tone[i][elm])
3228                                 bank->tone[i][elm]->fc = config_parse_int16(w[2], &bank->tone[i][elm]->fcnum);
3229         }       /* #extension q */
3230         else if (!strcmp(w[0], "q"))
3231         {
3232             if (words != 3)
3233             {
3234                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3235                           "%s: line %d: syntax error", name, line);
3236                 CHECKERRLIMIT;
3237                 continue;
3238             }
3239             if (!bank)
3240             {
3241                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3242                           "%s: line %d: Must specify tone bank or drum set "
3243                           "before assignment", name, line);
3244                 CHECKERRLIMIT;
3245                 continue;
3246             }
3247             i = atoi(w[1]);
3248             if (i < 0 || i > 127)
3249             {
3250                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3251                           "%s: line %d: extension fc "
3252                           "must be between 0 and 127", name, line);
3253                 CHECKERRLIMIT;
3254                 continue;
3255             }
3256 ///r
3257                 for (elm = 0; elm < MAX_ELEMENT; elm++)
3258                         if(bank->tone[i][elm])
3259                                 bank->tone[i][elm]->reso = config_parse_int16(w[2], &bank->tone[i][elm]->resonum);
3260         }
3261 ///r
3262         else if(!strcmp(w[0], "mfx_patch"))
3263         {
3264             int prog;
3265             words--;
3266             memmove(&w[0], &w[1], sizeof(w[0]) * words);
3267             w[words] = '\0';            /* terminate the token */
3268             if (words < 2 || *w[0] < '0' || *w[0] > '9')
3269             {
3270                 if (extension_flag)
3271                     continue;
3272                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3273                           "%s: line %d: syntax error", name, line);
3274                 CHECKERRLIMIT;
3275                 continue;
3276             }
3277             prog = atoi(w[0]);
3278             words--;
3279             memmove(&w[0], &w[1], sizeof(w[0]) * words);
3280             w[words] = '\0';            /* terminate the token */
3281             if (!dr)
3282                 prog -= progbase;
3283             if (prog < 0 || prog > 127){
3284                 if (dr)
3285                     ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3286                               "%s: line %d: Drum number must be between ""0 and 127", name, line);
3287                 else
3288                     ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3289                               "%s: line %d: Program must be between ""%d and %d", name, line, progbase, 127 + progbase);
3290                 CHECKERRLIMIT;
3291                 continue;
3292             }
3293             if (!bank) {
3294                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3295                           "%s: line %d: Must specify tone bank or drum set ""before assignment", name, line);
3296                 CHECKERRLIMIT;
3297                 continue;
3298             }
3299             if (config_parse_mfx_patch(w, words, mapid, origbankno, prog)) // mfx
3300             {
3301                 CHECKERRLIMIT;
3302                 continue;
3303             }
3304         }       /* #extension amp_normalize */
3305         else if (!strcmp(w[0], "amp_normalize"))
3306         {
3307             if (words != 2)
3308             {
3309                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3310                           "%s: line %d: syntax error", name, line);
3311                 CHECKERRLIMIT;
3312                 continue;
3313             }
3314             if (!bank)
3315             {
3316                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3317                           "%s: line %d: Must specify tone bank or drum set "
3318                           "before amp_normalize", name, line);
3319                 CHECKERRLIMIT;
3320                 continue;
3321             }
3322                 if (! strcmp(w[1], "on"))
3323                         k = 1;
3324                 else if (! strcmp(w[1], "off"))
3325                         k = 0;
3326             else {
3327                         k = atoi(w[1]);
3328                         if (k < 0 || k > 1)     {
3329                         ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3330                                   "%s: line %d: extension amp_normalize "
3331                                   "must be off, on, 0, 1", name, line);
3332                         CHECKERRLIMIT;
3333                         continue;
3334                         }
3335                 }
3336                 for(i = 0; i < (128 + MAP_BANK_COUNT); i++){
3337                         ToneBank *tmpbank = tonebank[i];
3338                         if (!tmpbank)
3339                                 continue;
3340                         for (j = 0; j < 128; j++)
3341                                 for (elm = 0; elm < MAX_ELEMENT; elm++)
3342                                         if(tmpbank->tone[j][elm])
3343                                                 tmpbank->tone[j][elm]->amp_normalize = k;
3344                 }
3345                 for(i = 0; i < (128 + MAP_BANK_COUNT); i++){
3346                         ToneBank *tmpbank = drumset[i];
3347                         if (!tmpbank)
3348                                 continue;
3349                         for (j = 0; j < 128; j++)
3350                                 for (elm = 0; elm < MAX_ELEMENT; elm++)
3351                                         if(tmpbank->tone[i][elm])
3352                                                 tmpbank->tone[i][elm]->amp_normalize = k;
3353                 }
3354         }
3355         else if (!strcmp(w[0], "soundfont"))
3356         {
3357             int order, cutoff, isremove, reso, amp;
3358             char *sf_file;
3359
3360             if (words < 2)
3361             {
3362                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3363                           "%s: line %d: No soundfont file given",
3364                           name, line);
3365                 CHECKERRLIMIT;
3366                 continue;
3367             }
3368
3369             sf_file = w[1];
3370             order = cutoff = reso = amp = -1;
3371             isremove = 0;
3372             for (j = 2; j < words; j++)
3373             {
3374                 if (!strcmp(w[j], "remove"))
3375                 {
3376                     isremove = 1;
3377                     break;
3378                 }
3379                 if (!(cp = strchr(w[j], '=')))
3380                 {
3381                     ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3382                               "%s: line %d: bad patch option %s",
3383                               name, line, w[j]);
3384                     CHECKERRLIMIT;
3385                     break;
3386                 }
3387                 *cp++ = 0;
3388                 k = atoi(cp);
3389                 if (!strcmp(w[j], "order"))
3390                 {
3391                     if (k < 0 || (*cp < '0' || *cp > '9'))
3392                     {
3393                         ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3394                                   "%s: line %d: order must be a digit",
3395                                   name, line);
3396                         CHECKERRLIMIT;
3397                         break;
3398                     }
3399                     order = k;
3400                 }
3401                 else if (!strcmp(w[j], "cutoff"))
3402                 {
3403                     if (k < 0 || (*cp < '0' || *cp > '9'))
3404                     {
3405                         ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3406                                   "%s: line %d: cutoff must be a digit",
3407                                   name, line);
3408                         CHECKERRLIMIT;
3409                         break;
3410                     }
3411                     cutoff = k;
3412                 }
3413                 else if (!strcmp(w[j], "reso"))
3414                 {
3415                     if (k < 0 || (*cp < '0' || *cp > '9'))
3416                     {
3417                         ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3418                                   "%s: line %d: reso must be a digit",
3419                                   name, line);
3420                         CHECKERRLIMIT;
3421                         break;
3422                     }
3423                     reso = k;
3424                 }
3425                 else if (!strcmp(w[j], "amp"))
3426                 {
3427                     amp = k;
3428                 }
3429             }
3430             if (isremove)
3431                 remove_soundfont(sf_file);
3432             else
3433                 add_soundfont(sf_file, order, cutoff, reso, amp);
3434                 // init tonebank
3435                 for(i = 0; i < 128; i++){
3436                         ToneBank *tmpbank = tonebank[i];
3437                         if (!tmpbank){
3438                                 tonebank[i] = (ToneBank *)safe_malloc(sizeof(ToneBank));
3439                                 memset(tonebank[i], 0, sizeof(ToneBank));
3440                                 tmpbank = tonebank[i];
3441                         }
3442                         for (j = 0; j < 128; j++){                              
3443                                 if(tmpbank->tone[j][0] == NULL)
3444                                 {
3445                                         if(alloc_tone_bank_element(&tmpbank->tone[j][0])){
3446                                                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "read_config_file: soundfont ToneBankElement malloc error.");
3447                                                 return READ_CONFIG_ERROR;
3448                                         }
3449                                 }
3450                                 for(elm = 0; elm < MAX_ELEMENT; elm++)
3451                                         if(tmpbank->tone[j][elm])
3452                                         reinit_tone_bank_element(tmpbank->tone[j][elm]); // need instrum.c apply_bank_parameter()
3453                         }
3454                 }
3455                 for(i = 0; i < 128; i++){
3456                         ToneBank *tmpbank = drumset[i];
3457                         if (!tmpbank){
3458                                 drumset[i] = (ToneBank *)safe_malloc(sizeof(ToneBank));
3459                                 memset(drumset[i], 0, sizeof(ToneBank));
3460                                 tmpbank = drumset[i];
3461                         }
3462                         for (j = 0; j < 128; j++){                      
3463                                 if(tmpbank->tone[j][0] == NULL)
3464                                 {
3465                                         if(alloc_tone_bank_element(&tmpbank->tone[j][0])){
3466                                                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "read_config_file: soundfont ToneBankElement malloc error.");
3467                                                 return READ_CONFIG_ERROR;
3468                                         }
3469                                 }
3470                                 for(elm = 0; elm < MAX_ELEMENT; elm++)
3471                                         if(tmpbank->tone[j][elm])
3472                                         reinit_tone_bank_element(tmpbank->tone[j][elm]); // need instrum.c apply_bank_parameter()
3473                         }
3474                 }
3475         }
3476         else if (!strcmp(w[0], "font"))
3477         {
3478             int bank, preset, keynote;
3479             if (words < 2)
3480             {
3481                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3482                           "%s: line %d: no font command", name, line);
3483                 CHECKERRLIMIT;
3484                 continue;
3485             }
3486             if (!strcmp(w[1], "exclude"))
3487             {
3488                 if (words < 3)
3489                 {
3490                     ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3491                               "%s: line %d: No bank/preset/key is given",
3492                               name, line);
3493                     CHECKERRLIMIT;
3494                     continue;
3495                 }
3496                 bank = atoi(w[2]);
3497                 if (words >= 4)
3498                     preset = atoi(w[3]) - progbase;
3499                 else
3500                     preset = -1;
3501                 if (words >= 5)
3502                     keynote = atoi(w[4]);
3503                 else
3504                     keynote = -1;
3505                 if (exclude_soundfont(bank, preset, keynote))
3506                 {
3507                     ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3508                               "%s: line %d: No soundfont is given",
3509                               name, line);
3510                     CHECKERRLIMIT;
3511                 }
3512             }
3513             else if (!strcmp(w[1], "order"))
3514             {
3515                 int order;
3516                 if (words < 4)
3517                 {
3518                     ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3519                               "%s: line %d: No order/bank is given",
3520                               name, line);
3521                     CHECKERRLIMIT;
3522                     continue;
3523                 }
3524                 order = atoi(w[2]);
3525                 bank = atoi(w[3]);
3526                 if (words >= 5)
3527                     preset = atoi(w[4]) - progbase;
3528                 else
3529                     preset = -1;
3530                 if (words >= 6)
3531                     keynote = atoi(w[5]);
3532                 else
3533                     keynote = -1;
3534                 if (order_soundfont(bank, preset, keynote, order))
3535                 {
3536                     ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3537                               "%s: line %d: No soundfont is given",
3538                               name, line);
3539                     CHECKERRLIMIT;
3540                 }
3541             }
3542         }
3543         else if (!strcmp(w[0], "progbase"))
3544         {
3545             if (words < 2 || *w[1] < '0' || *w[1] > '9')
3546             {
3547                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3548                           "%s: line %d: syntax error", name, line);
3549                 CHECKERRLIMIT;
3550                 continue;
3551             }
3552             progbase = atoi(w[1]);
3553         }
3554         else if (!strcmp(w[0], "map")) /* map <name> set1 elem1 set2 elem2 */
3555         {
3556             int arg[5], isdrum;
3557
3558             if (words != 6)
3559             {
3560                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3561                           "%s: line %d: syntax error", name, line);
3562                 CHECKERRLIMIT;
3563                 continue;
3564             }
3565             if ((arg[0] = mapname2id(w[1], &isdrum)) == -1)
3566             {
3567                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3568                           "%s: line %d: Invalid map name: %s", name, line, w[1]);
3569                 CHECKERRLIMIT;
3570                 continue;
3571             }
3572             for (i = 2; i < 6; i++)
3573                 arg[i - 1] = atoi(w[i]);
3574             if (isdrum)
3575             {
3576                 arg[1] -= progbase;
3577                 arg[3] -= progbase;
3578             }
3579             else
3580             {
3581                 arg[2] -= progbase;
3582                 arg[4] -= progbase;
3583             }
3584
3585             for (i = 1; i < 5; i++)
3586                 if (arg[i] < 0 || arg[i] > 127)
3587                     break;
3588             if (i != 5)
3589             {
3590                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3591                           "%s: line %d: Invalid parameter", name, line);
3592                 CHECKERRLIMIT;
3593                 continue;
3594             }
3595             set_instrument_map(arg[0], arg[1], arg[2], arg[3], arg[4]);
3596         }
3597
3598         /*
3599          * Standard configurations
3600          */
3601         else if (!strcmp(w[0], "dir"))
3602         {
3603             if (words < 2)
3604             {
3605                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3606                           "%s: line %d: No directory given", name, line);
3607                 CHECKERRLIMIT;
3608                 continue;
3609             }
3610             for (i = 1; i < words; i++)
3611                 add_to_pathlist(w[i]);
3612         }
3613         else if (!strcmp(w[0], "source") || !strcmp(w[0], "trysource"))
3614         {
3615             if (words < 2)
3616             {
3617                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3618                           "%s: line %d: No file name given", name, line);
3619                 CHECKERRLIMIT;
3620                 continue;
3621             }
3622             for (i = 1; i < words; i++)
3623             {
3624                 int status;
3625                 rcf_count++;
3626                 status = read_config_file(w[i], 0, !strcmp(w[0], "trysource"));
3627                 rcf_count--;
3628                 switch (status) {
3629                 case READ_CONFIG_SUCCESS:
3630                     break;
3631                 case READ_CONFIG_ERROR:
3632                     CHECKERRLIMIT;
3633                     continue;
3634                 case READ_CONFIG_RECURSION:
3635                     reuse_mblock(&varbuf);
3636                     close_file(tf);
3637                     return READ_CONFIG_RECURSION;
3638                 case READ_CONFIG_FILE_NOT_FOUND:
3639                     break;
3640                 }
3641             }
3642         }
3643         else if (!strcmp(w[0], "default"))
3644         {
3645             if (words != 2)
3646             {
3647                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3648                           "%s: line %d: Must specify exactly one patch name",
3649                           name, line);
3650                 CHECKERRLIMIT;
3651                 continue;
3652             }
3653             strncpy(def_instr_name, w[1], FILEPATH_MAX - 1);
3654             def_instr_name[FILEPATH_MAX - 1] = '\0';
3655             default_instrument_name = def_instr_name;
3656         }
3657         /* drumset [mapid] num */
3658         else if (!strcmp(w[0], "drumset"))
3659         {
3660             int newmapid, isdrum, newbankno;
3661
3662             if (words < 2)
3663             {
3664                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3665                           "%s: line %d: No drum set number given", name, line);
3666                 CHECKERRLIMIT;
3667                 continue;
3668             }
3669             if (words != 2 && !isdigit(*w[1]))
3670             {
3671                 if ((newmapid = mapname2id(w[1], &isdrum)) == -1 || !isdrum)
3672                 {
3673                     ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3674                           "%s: line %d: Invalid drum set map name: %s", name, line, w[1]);
3675                     CHECKERRLIMIT;
3676                     continue;
3677                 }
3678                 words--;
3679                 memmove(&w[1], &w[2], sizeof w[0] * words);
3680             }
3681             else
3682                 newmapid = INST_NO_MAP;
3683             i = atoi(w[1]) - progbase;
3684             if (i < 0 || i > 127)
3685             {
3686                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3687                           "%s: line %d: Drum set must be between %d and %d",
3688                           name, line,
3689                           progbase, progbase + 127);
3690                 CHECKERRLIMIT;
3691                 continue;
3692             }
3693
3694             newbankno = i;
3695             i = alloc_instrument_map_bank(1, newmapid, i);
3696             if (i == -1)
3697             {
3698                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3699                           "%s: line %d: No free drum set available to map",
3700                           name, line);
3701                 CHECKERRLIMIT;
3702                 continue;
3703             }
3704
3705             if (words == 2)
3706             {
3707                 bank = drumset[i];
3708                 bankno = i;
3709                 mapid = newmapid;
3710                 origbankno = newbankno;
3711                 dr = 1;
3712             }
3713             else
3714             {
3715                 if (words < 4 || *w[2] < '0' || *w[2] > '9')
3716                 {
3717                     ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3718                               "%s: line %d: syntax error", name, line);
3719                     CHECKERRLIMIT;
3720                     continue;
3721                 }
3722                 if (set_patchconf(name, line, drumset[i], &w[2], 1, newmapid, newbankno, i, 0))
3723                 {
3724                     CHECKERRLIMIT;
3725                     continue;
3726                 }
3727             }
3728         }
3729         /* bank [mapid] num */
3730         else if (!strcmp(w[0], "bank"))
3731         {
3732             int newmapid, isdrum, newbankno;
3733
3734             if (words < 2)
3735             {
3736                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3737                           "%s: line %d: No bank number given", name, line);
3738                 CHECKERRLIMIT;
3739                 continue;
3740             }
3741             if (words != 2 && !isdigit(*w[1]))
3742             {
3743                 if ((newmapid = mapname2id(w[1], &isdrum)) == -1 || isdrum)
3744                 {
3745                     ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3746                           "%s: line %d: Invalid bank map name: %s", name, line, w[1]);
3747                     CHECKERRLIMIT;
3748                     continue;
3749                 }
3750                 words--;
3751                 memmove(&w[1], &w[2], sizeof w[0] * words);
3752             }
3753             else
3754                 newmapid = INST_NO_MAP;
3755             i = atoi(w[1]);
3756             if (i < 0 || i > 127)
3757             {
3758                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3759                           "%s: line %d: Tone bank must be between 0 and 127",
3760                           name, line);
3761                 CHECKERRLIMIT;
3762                 continue;
3763             }
3764
3765             newbankno = i;
3766             i = alloc_instrument_map_bank(0, newmapid, i);
3767             if (i == -1)
3768             {
3769                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3770                           "%s: line %d: No free tone bank available to map",
3771                           name, line);
3772                 CHECKERRLIMIT;
3773                 continue;
3774             }
3775
3776             if (words == 2)
3777             {
3778                 bank = tonebank[i];
3779                 bankno = i;
3780                 mapid = newmapid;
3781                 origbankno = newbankno;
3782                 dr = 0;
3783             }
3784             else
3785             {
3786                 if (words < 4 || *w[2] < '0' || *w[2] > '9')
3787                 {
3788                     ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3789                               "%s: line %d: syntax error", name, line);
3790                     CHECKERRLIMIT;
3791                     continue;
3792                 }
3793                 if (set_patchconf(name, line, tonebank[i], &w[2], 0, newmapid, newbankno, i, 0))
3794                 {
3795                     CHECKERRLIMIT;
3796                     continue;
3797                 }
3798             }
3799         }
3800 ///r
3801         else if(!strcmp(w[0], "add"))
3802         {
3803                 words--;
3804                 memmove(&w[0], &w[1], sizeof w[0] * words);
3805                 w[words] = '\0';                /* terminate the token */
3806             if (words < 2 || *w[0] < '0' || *w[0] > '9')
3807             {
3808                 if (extension_flag)
3809                     continue;
3810                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3811                           "%s: line %d: syntax error", name, line);
3812                 CHECKERRLIMIT;
3813                 continue;
3814             }
3815             if (set_patchconf(name, line, bank, w, dr, mapid, origbankno, bankno, 1))
3816             {
3817                 CHECKERRLIMIT;
3818                 continue;
3819             }
3820         }
3821         else
3822         {
3823             if (words < 2 || *w[0] < '0' || *w[0] > '9')
3824             {
3825                 if (extension_flag)
3826                     continue;
3827                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3828                           "%s: line %d: syntax error", name, line);
3829                 CHECKERRLIMIT;
3830                 continue;
3831             }
3832             if (set_patchconf(name, line, bank, w, dr, mapid, origbankno, bankno, 0))
3833             {
3834                 CHECKERRLIMIT;
3835                 continue;
3836             }
3837         }
3838     }
3839
3840     if (errno)
3841     {
3842         ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3843                   "Can't read %s: %s", name, strerror(errno));
3844         errcnt++;
3845     }
3846     reuse_mblock(&varbuf);
3847     close_file(tf);
3848     return (errcnt == 0) ? READ_CONFIG_SUCCESS : READ_CONFIG_ERROR;
3849 }
3850
3851 #ifdef SUPPORT_SOCKET
3852
3853 #if defined(__W32__) && !defined(MAIL_NAME)
3854 #define MAIL_NAME "anonymous"
3855 #endif /* __W32__ */
3856
3857 #ifdef MAIL_NAME
3858 #define get_username() MAIL_NAME
3859 #else /* MAIL_NAME */
3860 #include <pwd.h>
3861 static char *get_username(void)
3862 {
3863     char *p;
3864     struct passwd *pass;
3865
3866     /* USER
3867      * LOGIN
3868      * LOGNAME
3869      * getpwnam()
3870      */
3871
3872     if((p = getenv("USER")) != NULL)
3873         return p;
3874     if((p = getenv("LOGIN")) != NULL)
3875         return p;
3876     if((p = getenv("LOGNAME")) != NULL)
3877         return p;
3878
3879     pass = getpwuid(getuid());
3880     if(pass == NULL)
3881         return "nobody";
3882     return pass->pw_name;
3883 }
3884 #endif /* MAIL_NAME */
3885
3886 static void init_mail_addr(void)
3887 {
3888     char addr[BUFSIZ];
3889
3890     sprintf(addr, "%s%s", get_username(), MAIL_DOMAIN);
3891     user_mailaddr = safe_strdup(addr);
3892 }
3893 #endif /* SUPPORT_SOCKET */
3894
3895 static int read_user_config_file(void)
3896 {
3897     char *home;
3898     char path[BUFSIZ];
3899     int status;
3900
3901     home = getenv("HOME");
3902 #ifdef __W32__
3903 /* HOME or home */
3904     if(home == NULL)
3905         home = getenv("home");
3906 #endif
3907     if(home == NULL)
3908     {
3909         ctl->cmsg(CMSG_INFO, VERB_NOISY,
3910                   "Warning: HOME environment is not defined.");
3911         return 0;
3912     }
3913
3914 #ifdef __W32__
3915 /* timidity.cfg or _timidity.cfg or .timidity.cfg*/
3916     sprintf(path, "%s" PATH_STRING "timidity.cfg", home);
3917     status = read_config_file(path, 0, 1);
3918     if (status != READ_CONFIG_FILE_NOT_FOUND)
3919         return status;
3920
3921     sprintf(path, "%s" PATH_STRING "_timidity.cfg", home);
3922     status = read_config_file(path, 0, 1);
3923     if (status != READ_CONFIG_FILE_NOT_FOUND)
3924         return status;
3925 #endif
3926
3927     sprintf(path, "%s" PATH_STRING ".timidity.cfg", home);
3928     status = read_config_file(path, 0, 1);
3929     if (status != READ_CONFIG_FILE_NOT_FOUND)
3930         return status;
3931         return 0;
3932 }
3933
3934 MAIN_INTERFACE void tmdy_free_config(void)
3935 {
3936         free_tone_bank();
3937         free_instrument_map();
3938         clean_up_pathlist();
3939 }
3940
3941 int set_extension_modes(char *flag)
3942 {
3943         return parse_opt_E(flag);
3944 }
3945
3946 int set_ctl(char *cp)
3947 {
3948         return parse_opt_i(cp);
3949 }
3950
3951 int set_play_mode(char *cp)
3952 {
3953         return parse_opt_O(cp);
3954 }
3955
3956 int set_wrd(char *w)
3957 {
3958         return parse_opt_W(w);
3959 }
3960
3961
3962
3963 #ifdef __W32__
3964 int opt_evil_mode = 0;
3965 #ifdef SMFCONV
3966 int opt_rcpcv_dll = 0;
3967 #endif  /* SMFCONV */
3968 #endif  /* __W32__ */
3969 static int   try_config_again = 0;
3970 ///r tim13-
3971 #ifdef FAST_DECAY
3972 int opt_fast_decay = 1;
3973 #else
3974 int opt_fast_decay = 0;
3975 #endif /* FAST_DECAY */
3976 int32 opt_output_rate = 0;
3977 static char *opt_output_name = NULL;
3978 static StringTable opt_config_string;
3979 #ifdef SUPPORT_SOUNDSPEC
3980 double spectrogram_update_sec = 0.0;
3981 #endif /* SUPPORT_SOUNDSPEC */
3982 #if defined(AU_VOLUME_CALC)
3983 int opt_volume_calc_rms = 0;
3984 int opt_volume_calc_trim = 0;
3985 #endif /* AU_VOLUME_CALC */
3986 int opt_buffer_fragments = -1;
3987 int opt_audio_buffer_bits = -1;
3988 ///r
3989 int opt_compute_buffer_bits = -128;
3990
3991 MAIN_INTERFACE int set_tim_opt_short(int c, const char *optarg)
3992 {
3993         int err = 0;
3994         
3995         switch (c) {
3996         case '4':
3997                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
3998                                 "-4 option is obsoleted.  Please use -N");
3999                 return 1;
4000         case 'A':
4001                 if (*optarg != ',' && *optarg != 'a')
4002                         err += parse_opt_A(optarg);
4003                 if (strchr(optarg, ','))
4004                         err += parse_opt_drum_power(strchr(optarg, ',') + 1);
4005                 if (strchr(optarg, 'a'))
4006                         opt_amp_compensation = 1;
4007                 return err;
4008         case 'a':
4009                 antialiasing_allowed = 1;
4010                 break;
4011         case 'B':
4012                 return parse_opt_B(optarg);
4013         case 'C':
4014                 return parse_opt_C(optarg);
4015         case 'c':
4016                 return parse_opt_c(optarg);
4017         case 'D':
4018                 return parse_opt_D(optarg);
4019         case 'd':
4020                 return parse_opt_d(optarg);
4021         case 'E':
4022                 return parse_opt_E(optarg);
4023         case 'e':
4024                 return parse_opt_e(optarg);
4025         case 'F':
4026                 adjust_panning_immediately = (adjust_panning_immediately) ? 0 : 1;
4027                 break;
4028         case 'f':
4029                 fast_decay = (fast_decay) ? 0 : 1;
4030                 break;
4031         case 'G':
4032                 return parse_opt_G(optarg);
4033         case 'g':
4034                 return parse_opt_g(optarg);
4035         case 'H':
4036                 return parse_opt_H(optarg);
4037         case 'h':
4038                 return parse_opt_h(optarg);
4039         case 'I':
4040                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
4041                                 "-I option is obsoleted.  Please use -Ei");
4042                 return 1;
4043         case 'i':
4044                 return parse_opt_i(optarg);
4045         case 'j':
4046                 opt_realtime_playing = (opt_realtime_playing) ? 0 : 1;
4047                 break;
4048         case 'K':
4049                 return parse_opt_K(optarg);
4050         case 'k':
4051                 return parse_opt_k(optarg);
4052         case 'L':
4053                 return parse_opt_L(optarg);
4054         case 'M':
4055                 return parse_opt_M(optarg);
4056         case 'm':
4057                 return parse_opt_m(optarg);
4058         case 'N':
4059                 return parse_opt_N(optarg);
4060         case 'n':
4061                 return parse_opt_n(optarg);
4062         case 'O':
4063                 return parse_opt_O(optarg);
4064         case 'o':
4065                 return parse_opt_o(optarg);
4066         case 'P':
4067                 return parse_opt_P(optarg);
4068         case 'p':
4069                 if (*optarg != 'a')
4070                         err += parse_opt_p(optarg);
4071                 if (strchr(optarg, 'a'))
4072                         auto_reduce_polyphony = (auto_reduce_polyphony) ? 0 : 1;
4073                 return err;
4074         case 'Q':
4075                 return parse_opt_Q(optarg);
4076         case 'q':
4077                 return parse_opt_q(optarg);
4078         case 'R':
4079                 return parse_opt_R(optarg);
4080 ///r
4081         case 'r':
4082                 return parse_opt_r(optarg);
4083         case 'S':
4084                 return parse_opt_S(optarg);
4085         case 's':
4086                 return parse_opt_s(optarg);
4087         case 'T':
4088                 return parse_opt_T(optarg);
4089         case 't':
4090                 return parse_opt_t(optarg);
4091         case 'U':
4092                 free_instruments_afterwards = 1;
4093                 break;
4094         case 'V':
4095                 return parse_opt_volume_curve(optarg);
4096         case 'v':
4097                 return parse_opt_v(optarg);
4098         case 'W':
4099                 return parse_opt_W(optarg);
4100 #ifdef __W32__
4101         case 'w':
4102                 return parse_opt_w(optarg);
4103 #endif
4104         case 'x':
4105                 return parse_opt_x(optarg);
4106         case 'Y':
4107                 return parse_opt_Y(optarg);
4108         case 'Z':
4109                 if (strncmp(optarg, "pure", 4))
4110                         return parse_opt_Z(optarg);
4111                 else
4112                         return parse_opt_Z1(optarg + 4);
4113         default:
4114                 return 1;
4115         }
4116         return 0;
4117 }
4118
4119 #ifdef __W32__
4120 MAIN_INTERFACE int set_tim_opt_short_cfg(int c, const char *optarg)
4121 {
4122         switch (c) {
4123         case 'c':
4124                 return parse_opt_c(optarg);
4125         }
4126         return 0;
4127 }
4128 #endif
4129
4130 /* -------- getopt_long -------- */
4131 MAIN_INTERFACE int set_tim_opt_long(int c, const char *optarg, int index)
4132 {
4133         const struct option *the_option = &(longopts[index]);
4134         char *arg = NULL;
4135         
4136         if (c == '?')   /* getopt_long failed parsing */
4137                 parse_opt_fail(optarg);
4138         else if (c < TIM_OPT_FIRST)
4139                 return set_tim_opt_short(c, optarg);
4140         if (! strncmp(the_option->name, "no-", 3))
4141                 arg = "no";             /* `reverse' switch */
4142         else
4143                 arg = optarg;
4144         switch (c) {
4145         case TIM_OPT_VOLUME:
4146                 return parse_opt_A(arg);
4147         case TIM_OPT_MASTER_VOLUME:
4148                 return parse_opt_master_volume(arg);
4149         case TIM_OPT_DRUM_POWER:
4150                 return parse_opt_drum_power(arg);
4151         case TIM_OPT_VOLUME_COMP:
4152                 return parse_opt_volume_comp(arg);
4153         case TIM_OPT_ANTI_ALIAS:
4154                 return parse_opt_a(arg);
4155         case TIM_OPT_BUFFER_FRAGS:
4156                 return parse_opt_B(arg);
4157         case TIM_OPT_CONTROL_RATIO:
4158                 return parse_opt_C(arg);
4159         case TIM_OPT_CONFIG_FILE:
4160                 return parse_opt_c(arg);
4161         case TIM_OPT_DRUM_CHANNEL:
4162                 return parse_opt_D(arg);
4163         case TIM_OPT_IFACE_PATH:
4164                 return parse_opt_d(arg);
4165         case TIM_OPT_EXT:
4166                 return parse_opt_E(arg);
4167         case TIM_OPT_MOD_WHEEL:
4168                 return parse_opt_mod_wheel(arg);
4169         case TIM_OPT_PORTAMENTO:
4170                 return parse_opt_portamento(arg);
4171         case TIM_OPT_VIBRATO:
4172                 return parse_opt_vibrato(arg);
4173         case TIM_OPT_CH_PRESS:
4174                 return parse_opt_ch_pressure(arg);
4175         case TIM_OPT_MOD_ENV:
4176                 return parse_opt_mod_env(arg);
4177         case TIM_OPT_TRACE_TEXT:
4178                 return parse_opt_trace_text(arg);
4179         case TIM_OPT_OVERLAP:
4180                 return parse_opt_overlap_voice(arg);
4181 ///r
4182         case TIM_OPT_OVERLAP_COUNT:
4183                 return parse_opt_overlap_voice_count(arg);
4184         case TIM_OPT_MAX_CHANNEL_VOICES:
4185                 return parse_opt_max_channel_voices(arg);
4186
4187         case TIM_OPT_TEMPER_CTRL:
4188                 return parse_opt_temper_control(arg);
4189         case TIM_OPT_DEFAULT_MID:
4190                 return parse_opt_default_mid(arg);
4191         case TIM_OPT_SYSTEM_MID:
4192                 return parse_opt_system_mid(arg);
4193         case TIM_OPT_DEFAULT_BANK:
4194                 return parse_opt_default_bank(arg);
4195         case TIM_OPT_FORCE_BANK:
4196                 return parse_opt_force_bank(arg);
4197         case TIM_OPT_DEFAULT_PGM:
4198                 return parse_opt_default_program(arg);
4199         case TIM_OPT_FORCE_PGM:
4200                 return parse_opt_force_program(arg);
4201         case TIM_OPT_DELAY:
4202                 return parse_opt_delay(arg);
4203         case TIM_OPT_CHORUS:
4204                 return parse_opt_chorus(arg);
4205         case TIM_OPT_REVERB:
4206                 return parse_opt_reverb(arg);
4207         case TIM_OPT_VOICE_LPF:
4208                 return parse_opt_voice_lpf(arg);
4209         case TIM_OPT_VOICE_HPF:
4210                 return parse_opt_voice_hpf(arg);
4211         case TIM_OPT_NS:
4212                 return parse_opt_noise_shaping(arg);
4213 #ifndef FIXED_RESAMPLATION
4214         case TIM_OPT_RESAMPLE:
4215                 return parse_opt_resample(arg);
4216 #endif
4217         case TIM_OPT_EVIL:
4218                 return parse_opt_e(arg);
4219         case TIM_OPT_FAST_PAN:
4220                 return parse_opt_F(arg);
4221         case TIM_OPT_FAST_DECAY:
4222                 return parse_opt_f(arg);
4223         case TIM_OPT_SEGMENT:
4224                 return parse_opt_G(arg);
4225         case TIM_OPT_SPECTROGRAM:
4226                 return parse_opt_g(arg);
4227         case TIM_OPT_KEYSIG:
4228                 return parse_opt_H(arg);
4229         case TIM_OPT_HELP:
4230                 return parse_opt_h(arg);
4231         case TIM_OPT_INTERFACE:
4232                 return parse_opt_i(arg);
4233         case TIM_OPT_VERBOSE:
4234                 return parse_opt_verbose(arg);
4235         case TIM_OPT_QUIET:
4236                 return parse_opt_quiet(arg);
4237         case TIM_OPT_TRACE:
4238                 return parse_opt_trace(arg);
4239         case TIM_OPT_LOOP:
4240                 return parse_opt_loop(arg);
4241         case TIM_OPT_RANDOM:
4242                 return parse_opt_random(arg);
4243         case TIM_OPT_SORT:
4244                 return parse_opt_sort(arg);
4245 #ifdef IA_ALSASEQ
4246         case TIM_OPT_BACKGROUND:
4247                 return parse_opt_background(arg);
4248         case TIM_OPT_RT_PRIO:
4249                 return parse_opt_rt_prio(arg);
4250         case TIM_OPT_SEQ_PORTS:
4251                 return parse_opt_seq_ports(arg);
4252 #endif
4253 #if defined(IA_WINSYN) || defined(IA_NPSYN) || defined(IA_PORTMIDI) || \
4254     defined(IA_W32G_SYN) || defined(IA_W32GUI)
4255     case TIM_OPT_RTSYN_LATENCY:
4256         return parse_opt_rtsyn_latency(arg);
4257     case TIM_OPT_RTSYN_SKIP_AQ:
4258         return parse_opt_rtsyn_skip_aq(arg);
4259     case TIM_OPT_RTSYN_PRINT_PORTS:
4260         return parse_opt_rtsyn_print_ports(arg);
4261 #endif /* RTSYN || IA_W32GUI */
4262         case TIM_OPT_REALTIME_LOAD:
4263                 return parse_opt_j(arg);
4264         case TIM_OPT_ADJUST_KEY:
4265                 return parse_opt_K(arg);
4266         case TIM_OPT_VOICE_QUEUE:
4267                 return parse_opt_k(arg);
4268         case TIM_OPT_PATCH_PATH:
4269                 return parse_opt_L(arg);
4270         case TIM_OPT_PCM_FILE:
4271                 return parse_opt_M(arg);
4272 ///r
4273         case TIM_OPT_RESAMPLE_FILTER:
4274                 return parse_opt_l(arg);
4275         case TIM_OPT_RESAMPLE_OVER_SAMPLING:
4276                 return parse_opt_resample_over_sampling(arg);
4277         case TIM_OPT_PRE_RESAMPLE:
4278                 return parse_opt_pre_resample(arg);
4279         case TIM_OPT_DECAY_TIME:
4280                 return parse_opt_m(arg);
4281         case TIM_OPT_INTERPOLATION:
4282                 return parse_opt_N(arg);
4283         case TIM_OPT_POLYPHONY_QUEUE:
4284                 return parse_opt_n(arg);
4285         case TIM_OPT_OUTPUT_MODE:
4286                 return parse_opt_O(arg);
4287         case TIM_OPT_OUTPUT_STEREO:
4288                 if (! strcmp(the_option->name, "output-mono"))
4289                         /* --output-mono == --output-stereo=no */
4290                         arg = "no";
4291                 return parse_opt_output_stereo(arg);
4292         case TIM_OPT_OUTPUT_SIGNED:
4293                 if (! strcmp(the_option->name, "output-unsigned"))
4294                         /* --output-unsigned == --output-signed=no */
4295                         arg = "no";
4296                 return parse_opt_output_signed(arg);
4297 ///r
4298         case TIM_OPT_OUTPUT_BITWIDTH:
4299                 if (! strcmp(the_option->name, "output-16bit"))
4300                         arg = "16bit";
4301                 else if (! strcmp(the_option->name, "output-24bit"))
4302                         arg = "24bit";
4303                 else if (! strcmp(the_option->name, "output-32bit"))
4304                         arg = "32bit";
4305                 else if (! strcmp(the_option->name, "output-64bit"))
4306                         arg = "64bit";
4307                 else if (! strcmp(the_option->name, "output-8bit"))
4308                         arg = "8bit";
4309                 else if (! strcmp(the_option->name, "output-f32bit"))
4310                         arg = "f32bit";
4311                 else if (! strcmp(the_option->name, "output-float32bit"))
4312                         arg = "f";
4313                 else if (! strcmp(the_option->name, "output-f64bit"))
4314                         arg = "D64bit";
4315                 else if (! strcmp(the_option->name, "output-float64bit"))
4316                         arg = "D";
4317                 return parse_opt_output_bitwidth(arg);
4318         case TIM_OPT_OUTPUT_FORMAT:
4319                 if (! strcmp(the_option->name, "output-linear"))
4320                         arg = "linear";
4321                 else if (! strcmp(the_option->name, "output-ulaw"))
4322                         arg = "ulaw";
4323                 else if (! strcmp(the_option->name, "output-alaw"))
4324                         arg = "alaw";
4325                 return parse_opt_output_format(arg);
4326         case TIM_OPT_OUTPUT_SWAB:
4327                 return parse_opt_output_swab(arg);
4328 ///r
4329         case TIM_OPT_OUTPUT_DEVICE_ID:
4330                 return parse_opt_output_device_id(arg);
4331 #ifdef AU_W32
4332         case TIM_OPT_WMME_DEVICE_ID:
4333                 return parse_opt_wmme_device_id(arg);
4334         case TIM_OPT_WAVE_FORMAT_EXT:
4335                 return parse_opt_wave_format_ext(arg);
4336 #endif
4337 #ifdef AU_WDMKS
4338         case TIM_OPT_WDMKS_DEVICE_ID:
4339                 return parse_opt_wdmks_device_id(arg);
4340         case TIM_OPT_WDMKS_LATENCY:
4341                 return parse_opt_wdmks_latency(arg);
4342         case TIM_OPT_WDMKS_FORMAT_EXT:
4343                 return parse_opt_wdmks_format_ext(arg);
4344         case TIM_OPT_WDMKS_POLLING:
4345                 return parse_opt_wdmks_polling(arg);
4346         case TIM_OPT_WDMKS_THREAD_PRIORITY:
4347                 return parse_opt_wdmks_thread_priority(arg);
4348         case TIM_OPT_WDMKS_RT_PRIORITY:
4349                 return parse_opt_wdmks_rt_priority(arg);
4350         case TIM_OPT_WDMKS_PIN_PRIORITY:
4351                 return parse_opt_wdmks_pin_priority(arg);
4352 #endif
4353 #ifdef AU_WASAPI
4354         case TIM_OPT_WASAPI_DEVICE_ID:
4355                 return parse_opt_wasapi_device_id(arg);
4356         case TIM_OPT_WASAPI_LATENCY:
4357                 return parse_opt_wasapi_latency(arg);
4358         case TIM_OPT_WASAPI_FORMAT_EXT:
4359                 return parse_opt_wasapi_format_ext(arg);
4360         case TIM_OPT_WASAPI_EXCLUSIVE:
4361                 return parse_opt_wasapi_exclusive(arg);
4362         case TIM_OPT_WASAPI_POLLING:
4363                 return parse_opt_wasapi_polling(arg);
4364         case TIM_OPT_WASAPI_PRIORITY:
4365                 return parse_opt_wasapi_priority(arg);
4366         case TIM_OPT_WASAPI_STREAM_CATEGORY:
4367                 return parse_opt_wasapi_stream_category(arg);
4368         case TIM_OPT_WASAPI_STREAM_OPTION:
4369                 return parse_opt_wasapi_stream_option(arg);
4370 #endif
4371 #ifdef AU_PORTAUDIO
4372         case TIM_OPT_PA_WMME_DEVICE_ID:
4373                 return parse_opt_pa_wmme_device_id(arg);
4374         case TIM_OPT_PA_DS_DEVICE_ID:
4375                 return parse_opt_pa_ds_device_id(arg);
4376         case TIM_OPT_PA_ASIO_DEVICE_ID:
4377                 return parse_opt_pa_asio_device_id(arg);
4378 #ifdef PORTAUDIO_V19
4379         case TIM_OPT_PA_WDMKS_DEVICE_ID:
4380                 return parse_opt_pa_wdmks_device_id(arg);
4381         case TIM_OPT_PA_WASAPI_DEVICE_ID:
4382                 return parse_opt_pa_wasapi_device_id(arg);
4383         case TIM_OPT_PA_WASAPI_FLAG:
4384                 return parse_opt_pa_wasapi_flag(arg);
4385         case TIM_OPT_PA_WASAPI_STREAM_CATEGORY:
4386                 return parse_opt_pa_wasapi_stream_category(arg);
4387         case TIM_OPT_PA_WASAPI_STREAM_OPTION:
4388                 return parse_opt_pa_wasapi_stream_option(arg);
4389 #endif
4390 #endif
4391                 
4392         case TIM_OPT_WAVE_EXTENSIBLE:
4393                 return parse_opt_wave_extensible(arg);
4394         case TIM_OPT_WAVE_UPDATE_STEP:
4395                 return parse_opt_wave_update_step(arg);
4396 #ifdef AU_FLAC
4397         case TIM_OPT_FLAC_VERIFY:
4398                 return parse_opt_flac_verify(arg);
4399         case TIM_OPT_FLAC_PADDING:
4400                 return parse_opt_flac_padding(arg);
4401         case TIM_OPT_FLAC_COMPLEVEL:
4402                 return parse_opt_flac_complevel(arg);
4403 #ifdef AU_OGGFLAC
4404         case TIM_OPT_FLAC_OGGFLAC:
4405                 return parse_opt_flac_oggflac(arg);
4406 #endif /* AU_OGGFLAC */
4407 #endif /* AU_FLAC */
4408 #ifdef AU_OPUS
4409         case TIM_OPT_OPUS_NFRAMES:
4410                 return parse_opt_opus_nframes(arg);
4411         case TIM_OPT_OPUS_BITRATE:
4412                 return parse_opt_opus_bitrate(arg);
4413         case TIM_OPT_OPUS_COMPLEXITY:
4414                 return parse_opt_opus_complexity(arg);
4415         case TIM_OPT_OPUS_VBR:
4416                 return parse_opt_opus_vbr(arg);
4417         case TIM_OPT_OPUS_CVBR:
4418                 return parse_opt_opus_cvbr(arg);
4419 #endif /* AU_OPUS */
4420 #ifdef AU_SPEEX
4421         case TIM_OPT_SPEEX_QUALITY:
4422                 return parse_opt_speex_quality(arg);
4423         case TIM_OPT_SPEEX_VBR:
4424                 return parse_opt_speex_vbr(arg);
4425         case TIM_OPT_SPEEX_ABR:
4426                 return parse_opt_speex_abr(arg);
4427         case TIM_OPT_SPEEX_VAD:
4428                 return parse_opt_speex_vad(arg);
4429         case TIM_OPT_SPEEX_DTX:
4430                 return parse_opt_speex_dtx(arg);
4431         case TIM_OPT_SPEEX_COMPLEXITY:
4432                 return parse_opt_speex_complexity(arg);
4433         case TIM_OPT_SPEEX_NFRAMES:
4434                 return parse_opt_speex_nframes(arg);
4435 #endif /* AU_SPEEX */
4436         case TIM_OPT_OUTPUT_FILE:
4437                 return parse_opt_o(arg);
4438         case TIM_OPT_PATCH_FILE:
4439                 return parse_opt_P(arg);
4440         case TIM_OPT_POLYPHONY:
4441                 return parse_opt_p(arg);
4442         case TIM_OPT_POLY_REDUCE:
4443                 return parse_opt_p1(arg);
4444         case TIM_OPT_MUTE:
4445                 return parse_opt_Q(arg);
4446         case TIM_OPT_TEMPER_MUTE:
4447                 return parse_opt_Q1(arg);
4448         case TIM_OPT_PRESERVE_SILENCE:
4449                 return parse_opt_preserve_silence(arg);
4450         case TIM_OPT_AUDIO_BUFFER:
4451                 return parse_opt_q(arg);
4452 ///r
4453         case TIM_OPT_RESAMPLE_QUEUE:
4454                 return parse_opt_r(arg);
4455
4456         case TIM_OPT_CACHE_SIZE:
4457                 return parse_opt_S(arg);
4458         case TIM_OPT_SAMPLE_FREQ:
4459                 return parse_opt_s(arg);
4460         case TIM_OPT_ADJUST_TEMPO:
4461                 return parse_opt_T(arg);
4462         case TIM_OPT_CHARSET:
4463                 return parse_opt_t(arg);
4464         case TIM_OPT_UNLOAD_INST:
4465                 return parse_opt_U(arg);
4466 #if defined(AU_VOLUME_CALC)
4467         case TIM_OPT_VOLUME_CALC_RMS:
4468                 return parse_opt_volume_calc_rms(arg);
4469         case TIM_OPT_VOLUME_CALC_TRIM:
4470                 return parse_opt_volume_calc_trim(arg);
4471 #endif /* AU_VOLUME_CALC */
4472         case TIM_OPT_VOLUME_CURVE:
4473                 return parse_opt_volume_curve(arg);
4474         case TIM_OPT_VERSION:
4475                 return parse_opt_v(arg);
4476         case TIM_OPT_WRD:
4477                 return parse_opt_W(arg);
4478 #ifdef __W32__
4479         case TIM_OPT_RCPCV_DLL:
4480                 return parse_opt_w(arg);
4481 #endif
4482         case TIM_OPT_CONFIG_STR:
4483                 return parse_opt_x(arg);
4484 ///r
4485         case TIM_OPT_COMPUTE_BUFFER:
4486                 return parse_opt_Y(arg);
4487         case TIM_OPT_FREQ_TABLE:
4488                 return parse_opt_Z(arg);
4489         case TIM_OPT_PURE_INT:
4490                 return parse_opt_Z1(arg);
4491         case TIM_OPT_MODULE:
4492                 return parse_opt_default_module(arg);
4493 ///r
4494         case TIM_OPT_ADD_PLAY_TIME:
4495                 return parse_opt_add_play_time(arg);
4496         case TIM_OPT_ADD_SILENT_TIME:
4497                 return parse_opt_add_silent_time(arg);
4498         case TIM_OPT_EMU_DELAY_TIME:
4499                 return parse_opt_emu_delay_time(arg);
4500         case TIM_OPT_LIMITER:
4501                 return parse_opt_limiter(arg);          
4502         case TIM_OPT_MIX_ENV:
4503                 return parse_opt_mix_envelope(arg);
4504         case TIM_OPT_MOD_UPDATE:
4505                 return parse_opt_modulation_update(arg);                
4506         case TIM_OPT_CUT_SHORT:
4507                 return parse_opt_cut_short_time(arg);
4508         case TIM_OPT_COMPUTE_THREAD_NUM:
4509                 return parse_opt_compute_thread_num(arg);       
4510         case TIM_OPT_TRACE_MODE_UPDATE:
4511                 return parse_opt_trace_mode_update(arg);
4512         case TIM_OPT_LOAD_ALL_INSTRUMENT:
4513                 return parse_opt_load_all_instrument(arg);
4514 #ifdef SUPPORT_LOOPEVENT
4515         case TIM_OPT_LOOP_REPEAT:
4516                 return parse_opt_midi_loop_repeat(arg);
4517         case TIM_OPT_LOOP_FILTER:
4518         return parse_opt_midi_loop_filter(arg);         
4519 #endif /* SUPPORT_LOOPEVENT */
4520         case TIM_OPT_OD_LEVEL_GS:
4521                 return parse_opt_od_level_gs(arg);
4522         case TIM_OPT_OD_DRIVE_GS:
4523                 return parse_opt_od_drive_gs(arg);
4524         case TIM_OPT_OD_LEVEL_XG:
4525                 return parse_opt_od_level_xg(arg);
4526         case TIM_OPT_OD_DRIVE_XG:
4527                 return parse_opt_od_drive_xg(arg);
4528     case TIM_OPT_OD_LEVEL_SD:
4529         return parse_opt_od_level_sd(arg);
4530     case TIM_OPT_OD_DRIVE_SD:
4531         return parse_opt_od_drive_sd(arg);
4532                                 
4533 #if defined(__W32__)
4534         case TIM_OPT_PROCESS_PRIORITY:
4535                 return parse_opt_process_priority(arg);
4536 #if !(defined(IA_W32G_SYN) || defined(WINDRV))
4537         case TIM_OPT_PLAYER_THREAD_PRIORITY:
4538                 return parse_opt_player_thread_priority(arg);
4539 #endif
4540 #endif
4541         case TIM_OPT_DUMMY_SETTING:
4542                 return 0; //dummy call
4543                 
4544         case TIM_OPT_INT_SYNTH_RATE:
4545                 return parse_opt_int_synth_rate(arg);
4546         case TIM_OPT_INT_SYNTH_SINE:
4547                 return parse_opt_int_synth_sine(arg);
4548         case TIM_OPT_INT_SYNTH_UPDATE:
4549                 return parse_opt_int_synth_update(arg);
4550
4551         default:
4552                 ctl->cmsg(CMSG_FATAL, VERB_NORMAL,
4553                                 "[BUG] Inconceivable case branch %d", c);
4554                 abort();
4555                 return 0; //dummy call
4556         }
4557 }
4558
4559 #ifdef __W32__
4560 MAIN_INTERFACE int set_tim_opt_long_cfg(int c, const char *optarg, int index)
4561 {
4562         const struct option *the_option = &(longopts[index]);
4563         char *arg;
4564         
4565         if (c == '?')   /* getopt_long failed parsing */
4566                 parse_opt_fail(optarg);
4567         else if (c < TIM_OPT_FIRST)
4568                 return set_tim_opt_short_cfg(c, optarg);
4569         if (! strncmp(the_option->name, "no-", 3))
4570                 arg = "no";             /* `reverse' switch */
4571         else
4572                 arg = optarg;
4573         switch (c) {
4574         case TIM_OPT_CONFIG_FILE:
4575                 return parse_opt_c(arg);
4576         default:
4577                 return 0; //dummy call
4578         }
4579 }
4580 #endif
4581
4582 static inline int parse_opt_A(const char *arg)
4583 {
4584         /* amplify volume by n percent */
4585         if (!arg) return amplification;
4586         return set_val_i32(&amplification, atoi(arg), 0, MAX_AMPLIFICATION,
4587                         "Amplification");
4588 }
4589
4590 static inline int parse_opt_master_volume(const char *arg)
4591 {
4592         /* amplify volume by n percent */
4593         if (!arg) return output_amplification;
4594         return set_val_i32(&output_amplification, atoi(arg), 0, MAX_AMPLIFICATION,
4595                         "Output Amplification");
4596 }
4597
4598 static inline int parse_opt_drum_power(const char *arg)
4599 {
4600         /* --drum-power */
4601         if (!arg) return opt_drum_power;
4602         return set_val_i32(&opt_drum_power, atoi(arg), 0, MAX_AMPLIFICATION,
4603                         "Drum power");
4604 }
4605
4606 static inline int parse_opt_volume_comp(const char *arg)
4607 {
4608         /* --[no-]volume-compensation */
4609         if (!arg) return 0;
4610         opt_amp_compensation = y_or_n_p(arg);
4611         return 0;
4612 }
4613
4614 static inline int parse_opt_a(const char *arg)
4615 {
4616         if (!arg) return 0;
4617         antialiasing_allowed = y_or_n_p(arg);
4618         return 0;
4619 }
4620
4621 static inline int parse_opt_B(const char *arg)
4622 {
4623         /* --buffer-fragments */
4624         const char *p;
4625         if (!arg) return 0;
4626         
4627         /* num */
4628         if (*arg != ',') {
4629                 if (set_value(&opt_buffer_fragments, atoi(arg), 0, 4096,
4630                                 "Buffer Fragments (num)"))
4631                         return 1;
4632         }
4633         /* bits */
4634         if ((p = strchr(arg, ',')) != NULL) {
4635                 if (set_value(&opt_audio_buffer_bits, atoi(++p), 1, AUDIO_BUFFER_BITS,
4636                                 "Buffer Fragments (bit)"))
4637                         return 1;
4638         }
4639         return 0;
4640 }
4641
4642 static inline int parse_opt_C(const char *arg)
4643 {
4644         if (!arg) return 0;
4645         if (set_val_i32(&control_ratio, atoi(arg), 0, MAX_CONTROL_RATIO,
4646                         "Control ratio"))
4647                 return 1;
4648         opt_control_ratio = control_ratio;
4649         return 0;
4650 }
4651
4652 static inline int parse_opt_c(const char *arg)
4653 {
4654         if (!arg) return 0;
4655 #ifdef __W32__
4656         if (got_a_configuration == 1)
4657                 return 0;
4658 #endif
4659         if (read_config_file(arg, 0, 0))
4660                 return 1;
4661         got_a_configuration = 1;
4662         return 0;
4663 }
4664
4665 static inline int parse_opt_D(const char *arg)
4666 {
4667         if (!arg) return 0;
4668         return set_channel_flag(&default_drumchannels, atoi(arg), "Drum channel");
4669 }
4670
4671 static inline int parse_opt_d(const char *arg)
4672 {
4673         /* dynamic lib root */
4674 #ifdef IA_DYNAMIC
4675         safe_free(dynamic_lib_root);
4676         if (!arg) arg = ".";
4677         dynamic_lib_root = safe_strdup(arg);
4678         return 0;
4679 #else
4680         ctl->cmsg(CMSG_WARNING, VERB_NOISY, "-d option is not supported");
4681         return 1;
4682 #endif  /* IA_DYNAMIC */
4683 }
4684
4685 static inline int parse_opt_E(const char *arg)
4686 {
4687         /* undocumented option --ext */
4688         int err = 0;
4689         if (!arg) return err;
4690         
4691         while (*arg) {
4692                 switch (*arg) {
4693                 case 'w':
4694                         opt_modulation_wheel = 1;
4695                         break;
4696                 case 'W':
4697                         opt_modulation_wheel = 0;
4698                         break;
4699                 case 'p':
4700                         opt_portamento = 1;
4701                         break;
4702                 case 'P':
4703                         opt_portamento = 0;
4704                         break;
4705                 case 'v':
4706                         opt_nrpn_vibrato = 1;
4707                         break;
4708                 case 'V':
4709                         opt_nrpn_vibrato = 0;
4710                         break;
4711                 case 's':
4712                         opt_channel_pressure = 1;
4713                         break;
4714                 case 'S':
4715                         opt_channel_pressure = 0;
4716                         break;
4717                 case 'e':
4718                         opt_modulation_envelope = 1;
4719                         break;
4720                 case 'E':
4721                         opt_modulation_envelope = 0;
4722                         break;
4723                 case 't':
4724                         opt_trace_text_meta_event = 1;
4725                         break;
4726                 case 'T':
4727                         opt_trace_text_meta_event = 0;
4728                         break;
4729                 case 'o':
4730                         opt_overlap_voice_allow = 1;
4731                         break;
4732                 case 'O':
4733                         opt_overlap_voice_allow = 0;
4734                         break;
4735                 case 'z':
4736                         opt_temper_control = 1;
4737                         break;
4738                 case 'Z':
4739                         opt_temper_control = 0;
4740                         break;
4741 //elion
4742                 case 'd':
4743                         opt_drum_effect = 1;
4744                         break;
4745                 case 'D':
4746                         opt_drum_effect = 0;
4747                         break;
4748                 case 'j':
4749                         opt_insertion_effect = 1;
4750                         break;
4751                 case 'J':
4752                         opt_insertion_effect = 0;
4753                         break;
4754                 case 'q':
4755                         opt_eq_control = 1;
4756                         break;
4757                 case 'Q':
4758                         opt_eq_control = 0;
4759                         break;
4760                 case 'x':
4761                         opt_tva_attack = 1;
4762                         opt_tva_decay = 1;
4763                         opt_tva_release = 1;
4764                         break;
4765                 case 'X':
4766                         opt_tva_attack = 0;
4767                         opt_tva_decay = 0;
4768                         opt_tva_release = 0;
4769                         break;
4770                 case 'c':
4771                         opt_delay_control = 1;
4772                         break;
4773                 case 'C':
4774                         opt_delay_control = 0;
4775                         break;
4776
4777                 case 'm':
4778                         if (parse_opt_default_mid(arg + 1))
4779                                 err++;
4780                         arg += 2;
4781                         break;
4782                 case 'M':
4783                         if (parse_opt_system_mid(arg + 1))
4784                                 err++;
4785                         arg += 2;
4786                         break;
4787                 case 'b':
4788                         if (parse_opt_default_bank(arg + 1))
4789                                 err++;
4790                         while (isdigit(*(arg + 1)))
4791                                 arg++;
4792                         break;
4793                 case 'B':
4794                         if (parse_opt_force_bank(arg + 1))
4795                                 err++;
4796                         while (isdigit(*(arg + 1)))
4797                                 arg++;
4798                         break;
4799                 case 'i':
4800                         if (parse_opt_default_program(arg + 1))
4801                                 err++;
4802                         while (isdigit(*(arg + 1)) || *(arg + 1) == '/')
4803                                 arg++;
4804                         break;
4805                 case 'I':
4806                         if (parse_opt_force_program(arg + 1))
4807                                 err++;
4808                         while (isdigit(*(arg + 1)) || *(arg + 1) == '/')
4809                                 arg++;
4810                         break;
4811                 case 'F':
4812                         if (strncmp(arg + 1, "delay=", 6) == 0) {
4813                                 if (parse_opt_delay(arg + 7))
4814                                         err++;
4815                         } else if (strncmp(arg + 1, "chorus=", 7) == 0) {
4816                                 if (parse_opt_chorus(arg + 8))
4817                                         err++;
4818                         } else if (strncmp(arg + 1, "reverb=", 7) == 0) {
4819                                 if (parse_opt_reverb(arg + 8))
4820                                         err++;
4821                         } else if (strncmp(arg + 1, "ns=", 3) == 0) {
4822                                 if (parse_opt_noise_shaping(arg + 4))
4823                                         err++;
4824                         } else if (strncmp(arg + 1, "vlpf=", 5) == 0) {
4825                                 if (parse_opt_voice_lpf(arg + 6))
4826                                         err++;
4827 #ifndef FIXED_RESAMPLATION
4828                         } else if (strncmp(arg + 1, "resamp=", 7) == 0) {
4829                                 if (parse_opt_resample(arg + 8))
4830                                         err++;
4831 #endif
4832                         }
4833                         if (err) {
4834                                 ctl->cmsg(CMSG_ERROR,
4835                                                 VERB_NORMAL, "-E%s: unsupported effect", arg);
4836                                 return err;
4837                         }
4838                         return err;
4839                 default:
4840                         ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
4841                                         "-E: Illegal mode `%c'", *arg);
4842                         err++;
4843                         break;
4844                 }
4845                 arg++;
4846         }
4847         return err;
4848 }
4849
4850 static inline int parse_opt_mod_wheel(const char *arg)
4851 {
4852         /* --[no-]mod-wheel */
4853         if (!arg) return 0;
4854         opt_modulation_wheel = y_or_n_p(arg);
4855         return 0;
4856 }
4857
4858 static inline int parse_opt_portamento(const char *arg)
4859 {
4860         /* --[no-]portamento */
4861         if (!arg) return 0;
4862         opt_portamento = y_or_n_p(arg);
4863         return 0;
4864 }
4865
4866 static inline int parse_opt_vibrato(const char *arg)
4867 {
4868         /* --[no-]vibrato */
4869         if (!arg) return 0;
4870         opt_nrpn_vibrato = y_or_n_p(arg);
4871         return 0;
4872 }
4873
4874 static inline int parse_opt_ch_pressure(const char *arg)
4875 {
4876         /* --[no-]ch-pressure */
4877         if (!arg) return 0;
4878         opt_channel_pressure = y_or_n_p(arg);
4879         return 0;
4880 }
4881
4882 static inline int parse_opt_mod_env(const char *arg)
4883 {
4884         /* --[no-]mod-envelope */
4885         if (!arg) return 0;
4886         opt_modulation_envelope = y_or_n_p(arg);
4887         return 0;
4888 }
4889
4890 static inline int parse_opt_trace_text(const char *arg)
4891 {
4892         /* --[no-]trace-text-meta */
4893         if (!arg) return 0;
4894         opt_trace_text_meta_event = y_or_n_p(arg);
4895         return 0;
4896 }
4897
4898 static inline int parse_opt_overlap_voice(const char *arg)
4899 {
4900         /* --[no-]overlap-voice */
4901         if (!arg) return 0;
4902         opt_overlap_voice_allow = y_or_n_p(arg);
4903         return 0;
4904 }
4905 ///r
4906 static inline int parse_opt_overlap_voice_count(const char *arg)
4907 {
4908         /* --overlap-voice-count */
4909         int val;
4910         if (!arg) return 0;
4911         val = atoi(arg);
4912
4913         if (! val || val < 0) {
4914                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Voice Overlap Count: Illegal value");
4915                 return 1;
4916         }
4917         opt_overlap_voice_count = val;
4918         return 0;
4919 }
4920
4921 static inline int parse_opt_max_channel_voices(const char *arg)
4922 {
4923         /* --max_channel_voices */
4924         int val;
4925         if (!arg) return 0;
4926         val = atoi(arg);
4927
4928         if (! val || val < 4 || val > 512) {
4929                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Max Channel Voices: Illegal value");
4930                 return 1;
4931         }
4932         opt_max_channel_voices = val;
4933         return 0;
4934 }
4935
4936 static inline int parse_opt_temper_control(const char *arg)
4937 {
4938         /* --[no-]temper-control */
4939         if (!arg) return 0;
4940         opt_temper_control = y_or_n_p(arg);
4941         return 0;
4942 }
4943
4944 static inline int parse_opt_default_mid(const char *arg)
4945 {
4946         /* --default-mid */
4947         int val;
4948         if (!arg) return 0;
4949         val = str2mID(arg);
4950         
4951         if (! val) {
4952                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Manufacture ID: Illegal value");
4953                 return 1;
4954         }
4955         opt_default_mid = val;
4956         return 0;
4957 }
4958
4959 static inline int parse_opt_system_mid(const char *arg)
4960 {
4961         /* --system-mid */
4962         int val;
4963         if (!arg) return 0;
4964         val = str2mID(arg);
4965         
4966         if (! val) {
4967                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Manufacture ID: Illegal value");
4968                 return 1;
4969         }
4970         opt_system_mid = val;
4971         return 0;
4972 }
4973
4974 static inline int parse_opt_default_bank(const char *arg)
4975 {
4976         /* --default-bank */
4977         if (!arg) return 0;
4978         if (set_value(&default_tonebank, atoi(arg), 0, 0x7f, "Bank number"))
4979                 return 1;
4980         special_tonebank = -1;
4981         return 0;
4982 }
4983
4984 static inline int parse_opt_force_bank(const char *arg)
4985 {
4986         /* --force-bank */
4987         if (!arg) return 0;
4988         if (set_value(&special_tonebank, atoi(arg), 0, 0x7f, "Bank number"))
4989                 return 1;
4990         return 0;
4991 }
4992
4993 static inline int parse_opt_default_program(const char *arg)
4994 {
4995         /* --default-program */
4996         int prog, i;
4997         const char *p;
4998         if (!arg) return 0;
4999         
5000         if (set_value(&prog, atoi(arg), 0, 0x7f, "Program number"))
5001                 return 1;
5002         if ((p = strchr(arg, '/')) != NULL) {
5003                 if (set_value(&i, atoi(++p), 1, MAX_CHANNELS, "Program channel"))
5004                         return 1;
5005                 default_program[i - 1] = prog;
5006         } else
5007                 for (i = 0; i < MAX_CHANNELS; i++)
5008                         default_program[i] = prog;
5009         return 0;
5010 }
5011
5012 static inline int parse_opt_force_program(const char *arg)
5013 {
5014         /* --force-program */
5015         const char *p;
5016         int i;
5017         if (!arg) return 0;
5018         
5019         if (set_value(&def_prog, atoi(arg), 0, 0x7f, "Program number"))
5020                 return 1;
5021         if (ctl->opened)
5022                 set_default_program(def_prog);
5023         if ((p = strchr(arg, '/')) != NULL) {
5024                 if (set_value(&i, atoi(++p), 1, MAX_CHANNELS, "Program channel"))
5025                         return 1;
5026                 default_program[i - 1] = SPECIAL_PROGRAM;
5027         } else
5028                 for (i = 0; i < MAX_CHANNELS; i++)
5029                         default_program[i] = SPECIAL_PROGRAM;
5030         return 0;
5031 }
5032 ///r
5033 static inline int set_default_program(int prog)
5034 {
5035         int bank;
5036         Instrument *ip;
5037         
5038         bank = (special_tonebank >= 0) ? special_tonebank : default_tonebank;
5039         if ((ip = play_midi_load_instrument(0, bank, prog, 0, NULL)) == NULL) // elm=0
5040                 return 1;
5041         default_instrument = ip;
5042         return 0;
5043 }
5044 ///r
5045 static inline int parse_opt_delay(const char *arg)
5046 {
5047         /* --delay */
5048         const char *p;
5049         if (!arg) return 0;
5050         
5051         switch (*arg) {
5052         case '0':
5053         case 'd':       /* disable */
5054                 opt_delay_control = 0;
5055                 return 0;
5056         case '1':
5057         case 'D':       /* normal */
5058                 opt_delay_control = 1;
5059                 return 0;
5060         default:
5061                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Invalid delay parameter.");
5062                 return 1;
5063         }
5064         return 0;
5065 }
5066
5067 static inline int parse_opt_chorus(const char *arg)
5068 {
5069         /* --chorus */
5070         const char *p;
5071         if (!arg) return 0;
5072         
5073         switch (*arg) {
5074         case '0':
5075         case 'd':       /* disable */
5076                 opt_chorus_control = 0;
5077                 opt_surround_chorus = 0;
5078                 break;
5079         case '1':
5080         case 'n':       /* normal */
5081         case '2':
5082         case 's':       /* surround */
5083                 opt_surround_chorus = (*arg == '2' || *arg == 's') ? 1 : 0;
5084                 if ((p = strchr(arg, ',')) != NULL) {
5085                         if (set_value(&opt_chorus_control, atoi(++p), 0, 0x7f,
5086                                         "Chorus level"))
5087                                 return 1;
5088                         opt_chorus_control = -opt_chorus_control;
5089                         opt_normal_chorus_plus = 0;
5090                 } else {
5091                         opt_chorus_control = 1;
5092                         opt_normal_chorus_plus = 0;
5093                 }
5094                 break;
5095         case '3':
5096         case 'w':
5097         case '4':
5098         case 'W':
5099                 opt_surround_chorus = (*arg == '4' || *arg == 'W') ? 1 : 0;
5100                 if ((p = strchr(arg, ',')) != NULL) {
5101                         if (set_value(&opt_chorus_control, atoi(++p), 0, 0x7f,
5102                                         "Chorus level"))
5103                                 return 1;
5104                         opt_chorus_control = -opt_chorus_control;
5105                         opt_normal_chorus_plus = 1;
5106                 } else {
5107                         opt_chorus_control = 1;
5108                         opt_normal_chorus_plus = 1;
5109                 }
5110                 break;
5111         case '5':
5112         case 'b':
5113         case '6':
5114         case 'B':
5115                 opt_surround_chorus = (*arg == '6' || *arg == 'B') ? 1 : 0;
5116                 if ((p = strchr(arg, ',')) != NULL) {
5117                         if (set_value(&opt_chorus_control, atoi(++p), 0, 0x7f,
5118                                         "Chorus level"))
5119                                 return 1;
5120                         opt_chorus_control = -opt_chorus_control;
5121                         opt_normal_chorus_plus = 2;
5122                 } else {
5123                         opt_chorus_control = 1;
5124                         opt_normal_chorus_plus = 2;
5125                 }
5126                 break;
5127         case '7':
5128         case 't':
5129         case '8':
5130         case 'T':
5131                 opt_surround_chorus = (*arg == '8' || *arg == 'T') ? 1 : 0;
5132                 if ((p = strchr(arg, ',')) != NULL) {
5133                         if (set_value(&opt_chorus_control, atoi(++p), 0, 0x7f,
5134                                         "Chorus level"))
5135                                 return 1;
5136                         opt_chorus_control = -opt_chorus_control;
5137                         opt_normal_chorus_plus = 3;
5138                 } else {
5139                         opt_chorus_control = 1;
5140                         opt_normal_chorus_plus = 3;
5141                 }
5142                 break;
5143         case '9':
5144         case 'h':
5145         case 'H':
5146                 opt_surround_chorus = ( *arg == 'H') ? 1 : 0;
5147                 if ((p = strchr(arg, ',')) != NULL) {
5148                         if (set_value(&opt_chorus_control, atoi(++p), 0, 0x7f,
5149                                         "Chorus level"))
5150                                 return 1;
5151                         opt_chorus_control = -opt_chorus_control;
5152                         opt_normal_chorus_plus = 4;
5153                 } else {
5154                         opt_chorus_control = 1;
5155                         opt_normal_chorus_plus = 4;
5156                 }
5157                 break;
5158         case 'e':
5159         case 'E':
5160                 opt_surround_chorus = ( *arg == 'E') ? 1 : 0;
5161                 if ((p = strchr(arg, ',')) != NULL) {
5162                         if (set_value(&opt_chorus_control, atoi(++p), 0, 0x7f,
5163                                         "Chorus level"))
5164                                 return 1;
5165                         opt_chorus_control = -opt_chorus_control;
5166                         opt_normal_chorus_plus = 5;
5167                 } else {
5168                         opt_chorus_control = 1;
5169                         opt_normal_chorus_plus = 5;
5170                 }
5171                 break;
5172 #if VSTWRAP_EXT
5173         case 'v':       /* chorus VST */
5174         case 'V':
5175                 opt_surround_chorus = ( *arg == 'V') ? 1 : 0;
5176                 if ((p = strchr(arg, ',')) != NULL) {
5177                         if (set_value(&opt_chorus_control, atoi(++p), 0, 0x7f,
5178                                         "Chorus level"))
5179                                 return 1;
5180                         opt_chorus_control = -opt_chorus_control;
5181                         opt_normal_chorus_plus = 6;
5182                 } else {
5183                         opt_chorus_control = 1;
5184                         opt_normal_chorus_plus = 6;
5185                 }
5186                 break;
5187 #endif
5188         default:
5189                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Invalid chorus parameter.");
5190                 return 1;
5191         }
5192         return 0;
5193 }
5194
5195 static inline int parse_opt_reverb(const char *arg)
5196 {
5197         /* --reverb */
5198         const char *p;
5199         if (!arg) return 0;
5200         
5201         /* option       action                  opt_reverb_control
5202          * reverb=0     no reverb                 0
5203          * reverb=1     old reverb                1
5204          * reverb=1,n   set reverb level to n   (-1 to -127)
5205          * reverb=2     "global" old reverb       2
5206          * reverb=2,n   set reverb level to n   (-1 to -127) - 128
5207          * reverb=3     new reverb                3
5208          * reverb=3,n   set reverb level to n   (-1 to -127) - 256
5209          * reverb=4     "global" new reverb       4
5210          * reverb=4,n   set reverb level to n   (-1 to -127) - 384
5211          * 
5212          * I think "global" was meant to apply a single global reverb,
5213          * without applying any reverb to the channels.  The do_effects()
5214          * function in effects.c looks like a good way to do this.
5215          * 
5216          * This is NOT the "correct" way to implement global reverb, we should
5217          * really make a new variable just for that.  But if opt_reverb_control
5218          * is already used in a similar fashion, rather than creating a new
5219          * variable for setting the channel reverb levels, then I guess
5220          * maybe this isn't so bad....  It would be nice to create new
5221          * variables for both global reverb and channel reverb level settings
5222          * in the future, but this will do for now.
5223          */
5224         
5225         switch (*arg) {
5226         case '0':
5227         case 'd':       /* disable */
5228                 opt_reverb_control = 0;
5229                 break;
5230         case '1':
5231         case 'n':       /* normal */
5232                 if ((p = strchr(arg, ',')) != NULL) {
5233                         if (set_value(&opt_reverb_control, atoi(++p), 1, 0x7f,
5234                                         "Reverb level"))
5235                                 return 1;
5236                         opt_reverb_control = -opt_reverb_control;
5237                 } else
5238                         opt_reverb_control = 1;
5239                 break;
5240         case '2':
5241         case 'g':       /* global */
5242                 if ((p = strchr(arg, ',')) != NULL) {
5243                         if (set_value(&opt_reverb_control, atoi(++p), 1, 0x7f,
5244                                         "Reverb level"))
5245                                 return 1;
5246                         opt_reverb_control = -opt_reverb_control - 128;
5247                 } else
5248                         opt_reverb_control = 2;
5249                 break;
5250         case '3':
5251         case 'f':       /* freeverb */
5252                 return parse_opt_reverb_freeverb(arg, 'f');
5253         case '4':
5254         case 'G':       /* global freeverb */
5255                 return parse_opt_reverb_freeverb(arg, 'G');
5256         case '5':       /* reverb ex */
5257                 if ((p = strchr(arg, ',')) != NULL) {
5258                         if (set_value(&opt_reverb_control, atoi(++p), 1, 0x7f,
5259                                         "Reverb level"))
5260                                 return 1;
5261                         opt_reverb_control = -opt_reverb_control - 512;
5262                 } else
5263                         opt_reverb_control = 5;         
5264                 break;
5265         case '6':       /* global reverb ex */
5266                 if ((p = strchr(arg, ',')) != NULL) {
5267                         if (set_value(&opt_reverb_control, atoi(++p), 1, 0x7f,
5268                                         "Reverb level"))
5269                                 return 1;
5270                         opt_reverb_control = -opt_reverb_control - 640;
5271                 } else
5272                         opt_reverb_control = 6;
5273                 break;
5274         case '7':       /* reverb ex2 */
5275                 if ((p = strchr(arg, ',')) != NULL) {
5276                         if (set_value(&opt_reverb_control, atoi(++p), 1, 0x7f,
5277                                         "Reverb level"))
5278                                 return 1;
5279                         opt_reverb_control = -opt_reverb_control - 768;
5280                 } else
5281                         opt_reverb_control = 7;         
5282                 break;
5283         case '8':       /* global reverb ex2 */
5284                 if ((p = strchr(arg, ',')) != NULL) {
5285                         if (set_value(&opt_reverb_control, atoi(++p), 1, 0x7f,
5286                                         "Reverb level"))
5287                                 return 1;
5288                         opt_reverb_control = -opt_reverb_control - 896;
5289                 } else
5290                         opt_reverb_control = 8;
5291                 break;
5292 #if VSTWRAP_EXT
5293         case 'v': /* reverb VST */
5294                 if ((p = strchr(arg, ',')) != NULL) {
5295                         if (set_value(&opt_reverb_control, atoi(++p), 1, 0x7f,
5296                                         "Reverb level"))
5297                                 return 1;
5298                         opt_reverb_control = -opt_reverb_control - 1024;
5299                 } else
5300                         opt_reverb_control = 9;         
5301                 break;
5302         case 'V': /* global reverb VST */
5303                 if ((p = strchr(arg, ',')) != NULL) {
5304                         if (set_value(&opt_reverb_control, atoi(++p), 1, 0x7f,
5305                                         "Reverb level"))
5306                                 return 1;
5307                         opt_reverb_control = -opt_reverb_control - 1152;
5308                 } else
5309                         opt_reverb_control = 10;
5310                 break;
5311 #endif
5312         default:
5313                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Invalid reverb parameter.");
5314                 return 1;
5315         }
5316         return 0;
5317 }
5318
5319 static int parse_opt_reverb_freeverb(const char *arg, char type)
5320 {
5321         const char *p;
5322         if (!arg) return 0;
5323         
5324         if ((p = strchr(arg, ',')) != NULL)
5325                 p++;
5326         else
5327                 p = "";
5328         /* reverb level */
5329         if (*p && *p != ',') {
5330                 if (set_value(&opt_reverb_control, atoi(p), 1, 0x7f,
5331                                 "Reverb level"))
5332                         return 1;
5333                 if (type == 'f')
5334                         opt_reverb_control = -opt_reverb_control - 256;
5335                 else
5336                         opt_reverb_control = -opt_reverb_control - 384;
5337         } else
5338                 opt_reverb_control = (type == 'f') ? 3 : 4;
5339         if ((p = strchr(p, ',')) == NULL)
5340                 return 0;
5341         p++;
5342         /* ranges 0..10 below determined just to reject an extreme value */
5343         /* scaleroom */
5344         if (*p && *p != ',') {
5345                 if (parse_val_float_t(&freeverb_scaleroom, p, 0, 10,
5346                                 "Freeverb scaleroom"))
5347                         return 1;
5348         }
5349         if ((p = strchr(p, ',')) == NULL)
5350                 return 0;
5351         p++;
5352         /* offsetroom */
5353         if (*p && *p != ',') {
5354                 if (parse_val_float_t(&freeverb_offsetroom, p, 0, 10,
5355                                 "Freeverb offsetroom"))
5356                         return 1;
5357         }
5358         if ((p = strchr(p, ',')) == NULL)
5359                 return 0;
5360         p++;
5361         /* predelay factor */
5362         if (*p && *p != ',') {
5363                 int value;
5364
5365                 if (set_val_i32(&value, atoi(p), 0, 1000,
5366                                 "Freeverb predelay factor"))
5367                         return 1;
5368                 reverb_predelay_factor = value * DIV_100;
5369         }
5370         return 0;
5371 }
5372
5373 static inline int parse_opt_voice_lpf(const char *arg)
5374 {
5375         /* --voice-lpf */
5376         if (!arg) return 0;
5377         switch (*arg) {
5378         case '0':
5379         case 'd':       /* disable */
5380                 opt_lpf_def = 0;
5381                 break;
5382         case '1':
5383         case 'c':       /* chamberlin */
5384                 opt_lpf_def = 1;
5385                 break;
5386         case '2':
5387         case 'm':       /* moog */
5388                 opt_lpf_def = 2;
5389                 break;
5390         case '3':
5391         case 'b':       /* ButterworthFilter */
5392                 opt_lpf_def = 3;
5393                 break;
5394         case '4':       /* Resonant IIR */
5395         case 'i':
5396                 opt_lpf_def = 4;
5397                 break;
5398         case '5':       /* amSynth */
5399         case 'a':
5400                 opt_lpf_def = 5;
5401                 break;
5402         case '6':       /* 1 pole 6db/oct */
5403         case 'o':
5404                 opt_lpf_def = 6;
5405                 break;
5406         case '7':       /* LPF18 18db/oct */
5407         case 'e':
5408                 opt_lpf_def = 7;
5409                 break;
5410         case '8':       /* two first order low-pass filter */
5411         case 't':
5412                 opt_lpf_def = 8;
5413                 break;
5414         case '9':       /* HPF ButterworthFilter */
5415         case 'h':
5416                 opt_lpf_def = 9;
5417                 break;
5418         case 'B':       /* BPF ButterworthFilter */
5419                 opt_lpf_def = 10;
5420                 break;
5421         default:
5422                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Invalid voice LPF type %s", arg);
5423                 return 1;
5424         }
5425         return 0;
5426 }
5427
5428 static inline int parse_opt_voice_hpf(const char *arg)
5429 {
5430         /* --voice-hpf */
5431         if (!arg) return 0;
5432         switch (*arg) {
5433         case '0':
5434         case 'd':       /* disable */
5435                 opt_hpf_def = 0;
5436                 break;
5437         case '1':
5438         case 'b':       /* ButterworthFilter hpf */
5439                 opt_hpf_def = 1;
5440                 break;
5441         case '2':
5442         case 'c':       /* lpf12-3 */
5443                 opt_hpf_def = 2;
5444                 break;
5445         case '3':
5446         case 'o':       /* 1 pole 6db/oct */
5447                 opt_hpf_def = 3;
5448                 break;
5449         default:
5450                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Invalid voice HPF type %s", arg);
5451                 return 1;
5452         }
5453         return 0;
5454 }
5455
5456 /* Noise Shaping filter from
5457  * Kunihiko IMAI <imai@leo.ec.t.kanazawa-u.ac.jp>
5458  */
5459 static inline int parse_opt_noise_shaping(const char *arg)
5460 {
5461         /* --noise-shaping */
5462         if (!arg) return 0;
5463         if (set_value(&noise_sharp_type, atoi(arg), 0, 4, "Noise shaping type"))
5464                 return 1;
5465         return 0;
5466 }
5467
5468 static inline int parse_opt_resample(const char *arg)
5469 {
5470         int num;
5471         /* --resample */
5472         if (!arg) return 0;
5473         num = atoi(arg);
5474         if(num > RESAMPLE_MAX - 1){
5475                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Invalid resample type %s", arg);
5476                 opt_resample_type = DEFAULT_RESAMPLATION_NUM;
5477                 return 1;
5478         }else if(num > 1){
5479                 opt_resample_type = num;
5480                 return 0;
5481         }
5482         switch (*arg) {
5483         case '0':
5484         case 'd':       /* disable */
5485                 opt_resample_type = RESAMPLE_NONE;
5486                 break;
5487         case '1':
5488         case 'l':       /* linear */
5489                 opt_resample_type = RESAMPLE_LINEAR;
5490                 break;
5491         case '2':
5492         case 'c':       /* cspline */
5493                 opt_resample_type = RESAMPLE_CSPLINE;
5494                 break;
5495         case '3':
5496         case 'L':       /* lagrange */
5497                 opt_resample_type = RESAMPLE_LAGRANGE;
5498                 break;
5499         case '4':
5500         case 'n':       /* newton */
5501                 opt_resample_type = RESAMPLE_NEWTON;
5502                 break;
5503         case '5':
5504         case 'g':       /* guass */
5505                 opt_resample_type = RESAMPLE_GAUSS;
5506                 break;
5507 ///r
5508         case '6':
5509         case 's':       /* sharp */
5510                 opt_resample_type = RESAMPLE_SHARP;
5511                 break;
5512         case '7':
5513         case 'p':       /* linear % */
5514                 opt_resample_type = RESAMPLE_LINEAR_P;
5515                 break;
5516         case '8':       /* sine */
5517                 opt_resample_type = RESAMPLE_SINE;
5518                 break;
5519         case '9':       /* square */
5520                 opt_resample_type = RESAMPLE_SQUARE;
5521                 break;
5522         default:
5523                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Invalid resample type %s", arg);
5524                 opt_resample_type = DEFAULT_RESAMPLATION_NUM;
5525                 return 1;
5526         }
5527         return 0;
5528 }
5529
5530 static inline int parse_opt_e(const char *arg)
5531 {
5532         /* evil */
5533 #ifdef __W32__
5534         opt_evil_mode = 1;
5535         return 0;
5536 #else
5537         ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "-e option is not supported");
5538         return 1;
5539 #endif /* __W32__ */
5540 }
5541
5542 static inline int parse_opt_F(const char *arg)
5543 {
5544         if (!arg) return 0;
5545         adjust_panning_immediately = y_or_n_p(arg);
5546         return 0;
5547 }
5548
5549 static inline int parse_opt_f(const char *arg)
5550 {
5551         if (!arg) return 0;
5552         fast_decay = y_or_n_p(arg);
5553         return 0;
5554 }
5555
5556 static inline int parse_opt_G(const char *arg)
5557 {
5558         /* play just sub-segment(s) (seconds) */
5559         TimeSegment *sp;
5560         const char *p = arg;
5561         int prev_end;
5562         
5563         if (strchr(arg, 'm'))
5564                 return parse_opt_G1(arg);
5565         if (time_segments == NULL) {
5566                 time_segments = (TimeSegment *) safe_malloc(sizeof(TimeSegment));
5567                 time_segments->type = 0;
5568                 if (parse_segment(time_segments, p)) {
5569                         free_time_segments();
5570                         return 1;
5571                 }
5572                 time_segments->prev = time_segments->next = NULL, sp = time_segments;
5573         } else {
5574                 for (sp = time_segments; sp->next != NULL; sp = sp->next)
5575                         ;
5576                 sp->next = (TimeSegment *) safe_malloc(sizeof(TimeSegment));
5577                 sp->next->type = 0;
5578                 if (parse_segment(sp->next, p)) {
5579                         free_time_segments();
5580                         return 1;
5581                 }
5582                 sp->next->prev = sp, sp->next->next = NULL, sp = sp->next;
5583         }
5584         while ((p = strchr(p, ',')) != NULL) {
5585                 sp->next = (TimeSegment *) safe_malloc(sizeof(TimeSegment));
5586                 sp->next->type = 0;
5587                 if (parse_segment(sp->next, ++p)) {
5588                         free_time_segments();
5589                         return 1;
5590                 }
5591                 sp->next->prev = sp, sp->next->next = NULL, sp = sp->next;
5592         }
5593         prev_end = -1;
5594         for (sp = time_segments; sp != NULL; sp = sp->next) {
5595                 if (sp->type != 0)
5596                         continue;
5597                 if (sp->begin.s <= prev_end) {
5598                         ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Segments must be ordered");
5599                         free_time_segments();
5600                         return 1;
5601                 } else if (sp->end.s != -1 && sp->begin.s >= sp->end.s) {
5602                         ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Segment time must be ordered");
5603                         free_time_segments();
5604                         return 1;
5605                 }
5606                 prev_end = sp->end.s;
5607         }
5608         return 0;
5609 }
5610
5611 static inline int parse_opt_G1(const char *arg)
5612 {
5613         /* play just sub-segment(s) (measure) */
5614         TimeSegment *sp;
5615         const char *p = arg;
5616         int prev_end_meas, prev_end_beat;
5617         
5618         if (time_segments == NULL) {
5619                 time_segments = (TimeSegment *) safe_malloc(sizeof(TimeSegment));
5620                 time_segments->type = 1;
5621                 if (parse_segment2(time_segments, p)) {
5622                         free_time_segments();
5623                         return 1;
5624                 }
5625                 time_segments->prev = time_segments->next = NULL, sp = time_segments;
5626         } else {
5627                 for (sp = time_segments; sp->next != NULL; sp = sp->next)
5628                         ;
5629                 sp->next = (TimeSegment *) safe_malloc(sizeof(TimeSegment));
5630                 sp->next->type = 1;
5631                 if (parse_segment2(sp->next, p)) {
5632                         free_time_segments();
5633                         return 1;
5634                 }
5635                 sp->next->prev = sp, sp->next->next = NULL, sp = sp->next;
5636         }
5637         while ((p = strchr(p, ',')) != NULL) {
5638                 sp->next = (TimeSegment *) safe_malloc(sizeof(TimeSegment));
5639                 sp->next->type = 1;
5640                 if (parse_segment2(sp->next, ++p)) {
5641                         free_time_segments();
5642                         return 1;
5643                 }
5644                 sp->next->prev = sp, sp->next->next = NULL, sp = sp->next;
5645         }
5646         prev_end_meas = prev_end_beat = -1;
5647         for (sp = time_segments; sp != NULL; sp = sp->next) {
5648                 if (sp->type != 1)
5649                         continue;
5650                 if (sp->begin.m.meas * 16 + sp->begin.m.beat
5651                                 <= prev_end_meas * 16 + prev_end_beat) {
5652                         ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Segments must be ordered");
5653                         free_time_segments();
5654                         return 1;
5655                 } else if (sp->end.m.meas != -1 && sp->end.m.beat != -1
5656                                 && sp->begin.m.meas * 16 + sp->begin.m.beat
5657                                 >= sp->end.m.meas * 16 + sp->end.m.beat) {
5658                         ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Segment time must be ordered");
5659                         free_time_segments();
5660                         return 1;
5661                 }
5662                 prev_end_meas = sp->end.m.meas, prev_end_beat = sp->end.m.beat;
5663         }
5664         return 0;
5665 }
5666
5667 static int parse_segment(TimeSegment *seg, const char *p)
5668 {
5669         const char *q;
5670         
5671         if (*p == '-')
5672                 seg->begin.s = 0;
5673         else if (parse_time(&seg->begin.s, p))
5674                 return 1;
5675         p = ((q = strchr(p, '-')) == NULL) ? p + strlen(p) : q + 1;
5676         if (*p == ',' || *p == '\0')
5677                 seg->end.s = -1;
5678         else if (parse_time(&seg->end.s, p))
5679                 return 1;
5680         return 0;
5681 }
5682
5683 static int parse_segment2(TimeSegment *seg, const char *p)
5684 {
5685         const char *q;
5686         
5687         if (*p == '-')
5688                 seg->begin.m.meas = seg->begin.m.beat = 1;
5689         else if (parse_time2(&seg->begin.m, p))
5690                 return 1;
5691         p = ((q = strchr(p, '-')) == NULL) ? p + strlen(p) : q + 1;
5692         if (*p == ',' || *p == 'm')
5693                 seg->end.m.meas = seg->end.m.beat = -1;
5694         else if (parse_time2(&seg->end.m, p))
5695                 return 1;
5696         return 0;
5697 }
5698
5699 static int parse_time(FLOAT_T *param, const char *p)
5700 {
5701         const char *p1, *p2, *p3;
5702         int min;
5703         FLOAT_T sec;
5704         
5705         p1 = ((p1 = strchr(p, ':')) == NULL) ? p + strlen(p) : p1;
5706         p2 = ((p2 = strchr(p, '-')) == NULL) ? p + strlen(p) : p2;
5707         p3 = ((p3 = strchr(p, ',')) == NULL) ? p + strlen(p) : p3;
5708         if ((p1 < p2 && p2 <= p3) || (p1 < p3 && p3 <= p2)) {
5709                 if (set_value(&min, atoi(p), 0, 59, "Segment time (min part)"))
5710                         return 1;
5711                 if (parse_val_float_t(&sec, p1 + 1, 0, 59.999,
5712                                 "Segment time (sec+frac part)"))
5713                         return 1;
5714                 *param = min * 60 + sec;
5715         } else if (parse_val_float_t(param, p, 0, 3599.999, "Segment time"))
5716                 return 1;
5717         return 0;
5718 }
5719
5720 static int parse_time2(Measure *param, const char *p)
5721 {
5722         const char *p1, *p2, *p3;
5723         
5724         if (set_value(&param->meas, atoi(p), 0, 999, "Segment time (measure)"))
5725                 return 1;
5726         p1 = ((p1 = strchr(p, '.')) == NULL) ? p + strlen(p) : p1;
5727         p2 = ((p2 = strchr(p, '-')) == NULL) ? p + strlen(p) : p2;
5728         p3 = ((p3 = strchr(p, ',')) == NULL) ? p + strlen(p) : p3;
5729         if ((p1 < p2 && p2 <= p3) || (p1 < p3 && p3 <= p2)) {
5730                 if (set_value(&param->beat, atoi(p1 + 1), 1, 15,
5731                                 "Segment time (beat)"))
5732                         return 1;
5733         } else
5734                 param->beat = 1;
5735         return 0;
5736 }
5737
5738 static inline int parse_opt_g(const char *arg)
5739 {
5740 #ifdef SUPPORT_SOUNDSPEC
5741         if (!arg) return 0;
5742         spectrogram_update_sec = atof(arg);
5743         if (spectrogram_update_sec <= 0) {
5744                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
5745                                 "Invalid -g argument: `%s'", arg);
5746                 return 1;
5747         }
5748         view_soundspec_flag = 1;
5749         return 0;
5750 #else
5751         ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "-g option is not supported");
5752         return 1;
5753 #endif  /* SUPPORT_SOUNDSPEC */
5754 }
5755
5756 static inline int parse_opt_H(const char *arg)
5757 {
5758         /* force keysig (number of sharp/flat) */
5759         int keysig;
5760         if (!arg) return 0;
5761         
5762         if (set_value(&keysig, atoi(arg), -7, 7,
5763                         "Force keysig (number of sHarp(+)/flat(-))"))
5764                 return 1;
5765         opt_force_keysig = keysig;
5766         return 0;
5767 }
5768
5769 __attribute__((noreturn))
5770 #ifndef __BORLANDC__
5771 static inline int parse_opt_h(const char *arg)
5772 #else
5773 static int parse_opt_h(const char *arg)
5774 #endif
5775 {
5776         static char *help_list[] = {
5777 "TiMidity++ %s (C) 1999-2018 Masanao Izumo <iz@onicos.co.jp>",
5778 "The original version (C) 1995 Tuukka Toivonen <tt@cgs.fi>",
5779 "TiMidity is free software and comes with ABSOLUTELY NO WARRANTY.",
5780 "",
5781 #ifdef __W32__
5782 "Win32 version by Davide Moretti <dave@rimini.com>",
5783 "              and Daisuke Aoki <dai@y7.net>",
5784 "",
5785 #endif /* __W32__ */
5786 "Usage:",
5787 "  %s [options] filename [...]",
5788 "",
5789 #ifndef __W32__         /*does not work in Windows */
5790 "  Use \"-\" as filename to read a MIDI file from stdin",
5791 "",
5792 #endif
5793 "Options:",
5794 "  -A n,m     --volume=n, --drum-power=m",
5795 "               Amplify volume by n percent (may cause clipping),",
5796 "                 and amplify drum power by m percent",
5797 "     (a)     --[no-]volume-compensation",
5798 "               Toggle amplify compensation (disabled by default)",
5799 "  -a         --[no-]anti-alias",
5800 "               Enable the anti-aliasing filter",
5801 "  -B n,m     --buffer-fragments=n,m",
5802 "               Set number of buffer fragments(n), and buffer size(2^m)",
5803 "  -C n       --control-ratio=n",
5804 "               Set ratio of sampling and control frequencies (0...255)",
5805 "  -c file    --config-file=file",
5806 "               Read extra configuration file",
5807 "  -D n       --drum-channel=n",
5808 "               Play drums on channel n",
5809 #ifdef IA_DYNAMIC
5810 "  -d path    --interface-path=path",
5811 "               Set dynamic interface module directory",
5812 #endif /* IA_DYNAMIC */
5813 "  -E mode    --ext=mode",
5814 "               TiMidity sequencer extensional modes:",
5815 "                 mode = w/W : Enable/Disable Modulation wheel",
5816 "                        p/P : Enable/Disable Portamento",
5817 "                        v/V : Enable/Disable NRPN Vibrato",
5818 "                        s/S : Enable/Disable Channel pressure",
5819 "                        e/E : Enable/Disable Modulation Envelope",
5820 "                        t/T : Enable/Disable Trace Text Meta Event at playing",
5821 "                        o/O : Enable/Disable Overlapped voice",
5822 "                        z/Z : Enable/Disable Temperament control",
5823 "                        j/J : Enable/Disable Insertion effect",
5824 "                        q/Q : Enable/Disable EQ",
5825 "                        d/D : Enable/Disable Drumpart effect",
5826 "                        x/X : Enable/Disable TVA envelope control",
5827 "                        c/C : Enable/Disable CC#94 delay(celeste) effect",
5828 "                        m<HH>: Define default Manufacture ID <HH> in two hex",
5829 "                        M<HH>: Define system Manufacture ID <HH> in two hex",
5830 "                        b<n>: Use tone bank <n> as the default",
5831 "                        B<n>: Always use tone bank <n>",
5832 "                        i<n/m>: Use program <n> on channel <m> as the default",
5833 "                        I<n/m>: Always use program <n> on channel <m>",
5834 "                        F<args>: For effect.  See below for effect options",
5835 "                   default: -E "
5836 #ifdef MODULATION_WHEEL_ALLOW
5837 "w"
5838 #else
5839 "W"
5840 #endif /* MODULATION_WHEEL_ALLOW */
5841 #ifdef PORTAMENTO_ALLOW
5842 "p"
5843 #else
5844 "P"
5845 #endif /* PORTAMENTO_ALLOW */
5846 #ifdef NRPN_VIBRATO_ALLOW
5847 "v"
5848 #else
5849 "V"
5850 #endif /* NRPN_VIBRATO_ALLOW */
5851 #ifdef GM_CHANNEL_PRESSURE_ALLOW
5852 "s"
5853 #else
5854 "S"
5855 #endif /* GM_CHANNEL_PRESSURE_ALLOW */
5856 #ifdef MODULATION_ENVELOPE_ALLOW
5857 "e"
5858 #else
5859 "E"
5860 #endif /* MODULATION_ENVELOPE_ALLOW */
5861 #ifdef ALWAYS_TRACE_TEXT_META_EVENT
5862 "t"
5863 #else
5864 "T"
5865 #endif /* ALWAYS_TRACE_TEXT_META_EVENT */
5866 #ifdef OVERLAP_VOICE_ALLOW
5867 "o"
5868 #else
5869 "O"
5870 #endif /* OVERLAP_VOICE_ALLOW */
5871 #ifdef TEMPER_CONTROL_ALLOW
5872 "z"
5873 #else
5874 "Z"
5875 #endif /* TEMPER_CONTROL_ALLOW */
5876 "J"
5877 "Q"
5878 "D"
5879 "X"
5880 "C"
5881 ,
5882 #ifdef __W32__
5883 "  -e         --evil",
5884 "               Increase thread priority (evil) - be careful!",
5885 #endif
5886 "  -F         --[no-]fast-panning",
5887 "               Disable/Enable fast panning (toggle on/off, default is on)",
5888 "  -f         --[no-]fast-decay",
5889 "               "
5890 #ifdef FAST_DECAY
5891 "Disable "
5892 #else
5893 "Enable "
5894 #endif
5895 "fast decay mode (toggle)",
5896 "               "
5897 #ifdef FAST_DECAY
5898 "(default is on)",
5899 #else
5900 "(default is off)",
5901 #endif
5902 #ifdef SUPPORT_SOUNDSPEC
5903 "  -g sec     --spectrogram=sec",
5904 "               Open Sound-Spectrogram Window",
5905 #endif /* SUPPORT_SOUNDSPEC */
5906 "  -H n       --force-keysig=n",
5907 "               Force keysig number of sHarp(+)/flat(-) (-7..7)",
5908 "  -h         --help",
5909 "               Display this help message",
5910 "  -i mode    --interface=mode",
5911 "               Select user interface (see below for list)",
5912 #ifdef IA_ALSASEQ
5913 "             --realtime-priority=n (for alsaseq only)",
5914 "               Set the realtime priority (0-100)",
5915 "             --sequencer-ports=n (for alsaseq only)",
5916 "               Set the number of opened sequencer ports (default is 4)",
5917 #endif
5918 #if defined(IA_WINSYN) || defined(IA_PORTMIDISYN) || defined(IA_NPSYN) || defined(IA_W32G_SYN)
5919 "             --rtsyn-latency=sec (for rtsyn only)",
5920 "               Set the realtime latency (sec)",
5921 "                 (default is 0.2 sec, minimum is 0.04 sec)",
5922 #endif
5923 "  -j         --[no-]realtime-load",
5924 "               Realtime load instrument (toggle on/off)",
5925 "  -K n       --adjust-key=n",
5926 "               Adjust key by n half tone (-24..24)",
5927 "  -k msec    --voice-queue=msec",
5928 "               Specify audio queue time limit to reduce voice",
5929 "  -L path    --patch-path=path",
5930 "               Append dir to search path",
5931 "  -l n       --resample-filter=n",
5932 "                 n=0  : Disable filtration",
5933 #if !defined(DEFAULT_RESAMPLATION_FILTER) || (DEFAULT_RESAMPLATION_FILTER == 0)
5934 "                        (default)",
5935 #endif
5936 "                   1  : LPFBW (Butterworth) x1",
5937 #if defined(DEFAULT_RESAMPLATION_FILTER) && (DEFAULT_RESAMPLATION_FILTER == 1)
5938 "                        (default)",
5939 #endif
5940 "                   2  : LPFBW (Butterworth) x2",
5941 #if defined(DEFAULT_RESAMPLATION_FILTER) && (DEFAULT_RESAMPLATION_FILTER == 2)
5942 "                        (default)",
5943 #endif
5944 "                   3  : LPFBW (Butterworth) x3",
5945 #if defined(DEFAULT_RESAMPLATION_FILTER) && (DEFAULT_RESAMPLATION_FILTER == 3)
5946 "                        (default)",
5947 #endif
5948 "                   4  : LPFBW (Butterworth) x4",
5949 #if defined(DEFAULT_RESAMPLATION_FILTER) && (DEFAULT_RESAMPLATION_FILTER == 4)
5950 "                        (default)",
5951 #endif
5952 "                   5  : LPFAM (24dB/oct)-2 x1",
5953 #if defined(DEFAULT_RESAMPLATION_FILTER) && (DEFAULT_RESAMPLATION_FILTER == 5)
5954 "                        (default)",
5955 #endif
5956 "                   6  : LPFAM (24dB/oct)-2 x2",
5957 #if defined(DEFAULT_RESAMPLATION_FILTER) && (DEFAULT_RESAMPLATION_FILTER == 6)
5958 "                        (default)",
5959 #endif
5960 "                   7  : LPFAU x1",
5961 #if defined(DEFAULT_RESAMPLATION_FILTER) && (DEFAULT_RESAMPLATION_FILTER == 7)
5962 "                        (default)",
5963 #endif
5964 "  -M name    --pcm-file=name",
5965 "               Specify PCM filename (*.wav or *.aiff) to be played or:",
5966 "                 \"auto\" : Play *.mid.wav or *.mid.aiff",
5967 "                 \"none\" : Disable this feature (default)",
5968 "  -m msec    --decay-time=msec",
5969 "               Minimum time for a full volume sustained note to decay,",
5970 "                 0 disables",
5971 "  -N n       --interpolation=n",
5972 "               Set the interpolation parameter (depends on -EFresamp option)",
5973 "                 Linear interpolation is used if audio queue < 99%s",
5974 "                 cspline, lagrange:",
5975 "                   Toggle 4-point interpolation (default on)",
5976 "                   (off: 0, on: 1)",
5977 "                 newton:",
5978 "                   n'th order Newton polynomial interpolation, n=1-45 odd",
5979 "                   (default 11)",
5980 "                 gauss:",
5981 "                   n+1 point Gauss-like interpolation, n=2-32 (default 24)",
5982 "                 sharp:",
5983 "                   n+1 point Sharp interpolation, n=2-8 (default 6)",
5984 "                 linearP:",
5985 "                   n=0-100 (default 100)",
5986 "  -O mode    --output-mode=mode",
5987 "               Select output mode and format (see below for list)",
5988 "             --wave-extensible (for RIFF WAVE file only)",
5989 "               Enable WAVE_FORMAT_EXTENSIBLE tag (GUID)",
5990 "             --wave-update-step=n (for RIFF WAVE file only)",
5991 "               Update RIFF to n KBytes per. n=0-9999 (default is 512)",
5992 #ifdef AU_FLAC
5993 "             --flac-verify (for FLAC / OggFLAC only)",
5994 "               Verify a correct encoding",
5995 "             --flac-padding=n (for FLAC / OggFLAC only)",
5996 "               Write a PADDING block of length n",
5997 "             --flac-complevel=n (for FLAC / OggFLAC only)",
5998 "               Set compression level n:[0..8]",
5999 #ifdef AU_OGGFLAC
6000 "             --oggflac (for Ogg FLAC only)",
6001 "               Output OggFLAC stream (experimental)",
6002 #endif /* AU_OGGFLAC */
6003 #endif /* AU_FLAC */
6004 #ifdef AU_OPUS
6005 "             --opus-nframes=n (for Ogg Opus only)",
6006 "               Number of frames per Opus packet",
6007 "               n:[120, 240, 480, 960, 1920, 2880] (default is 960)",
6008 "             --opus-bitrate=n (for Ogg Opus only)",
6009 "               Encoding average bit-rate n:[5-512,513-512000] (default is 128)",
6010 "             --opus-complexity=n (for Ogg Opus only)",
6011 "               Set encoding complexity n:[0..10] (default is 10)",
6012 "             --[no-]opus-vbr (for Ogg Opus only)",
6013 "               Enable variable bit-rate (VBR) (default on)",
6014 "             --[no-]opus-cvbr (for Ogg Opus only)",
6015 "               Enable constrained variable bit-rate (CVBR) (default on)",
6016 #endif /* AU_OPUS */
6017 #ifdef AU_SPEEX
6018 "             --speex-quality=n (for Ogg Speex only)",
6019 "               Encoding quality n:[0..10]",
6020 "             --speex-vbr (for Ogg Speex only)",
6021 "               Enable variable bit-rate (VBR)",
6022 "             --speex-abr=n (for Ogg Speex only)",
6023 "               Enable average bit-rate (ABR) at rate bps",
6024 "             --speex-vad (for Ogg Speex only)",
6025 "               Enable voice activity detection (VAD)",
6026 "             --speex-dtx (for Ogg Speex only)",
6027 "               Enable file-based discontinuous transmission (DTX)",
6028 "             --speex-complexity=n (for Ogg Speex only)",
6029 "               Set encoding complexity n:[0-10]",
6030 "             --speex-nframes=n (for Ogg Speex only)",
6031 "               Number of frames per Ogg packet n:[0-10]",
6032 #endif
6033 "             --output-device-id=n",
6034 #ifdef AU_W32
6035 "             --wmme-device-id=n (for Windows only)",
6036 "               Number of WMME device ID (-1: Default device, 0..19: other)",
6037 "             --wave-format-ext=n , --wmme-format-ext=n (for Windows only)",
6038 "               WMME Enable WAVE_FORMAT_EXTENSIBLE (default is 1)",
6039 #endif
6040 #ifdef AU_WDMKS
6041 "             --wdmks-device-id=n (for Windows only)",
6042 "               Number of WDMKS device ID (0..19:)",
6043 "             --wdmks-latency=n (for Windows only)",
6044 "               WDMKS Latency ms n=1-9999 depend device (default is 20)",
6045 "             --wdmks-format-ext=n (for Windows only)",
6046 "               WDMKS Enable WAVE_FORMAT_EXTENSIBLE (default is 1)",
6047 "             --wdmks-realtime=n (for Windows only)",
6048 "               WDMKS 0:WaveCyclic 1:WaveRT (default is 0)",
6049 "               supported since Windows Vista",
6050 "             --wdmks-polling=n (for Windows only)",
6051 "               WDMKS Flags RT 0:Event 1:Polling (default is 0)",
6052 "             --wdmks_priority=n (for Windows only)",
6053 "               WDMKS ThreadPriority (default is 1)",
6054 "               0:Low , 1:Normal , 2:High , 3:Exclusive",
6055 "             --wdmks_priority-rt=n (for Windows only)",
6056 "               WDMKS ThreadPriority RT (default is 0)",
6057 "               0:Auto(Audio) , 1:Audio , 2:Capture , 3:Distribution , 4:Games , 5:playback , 6:ProAudio , 7:WindowManager",
6058 #endif
6059 #ifdef AU_WASAPI
6060 "             --wasapi-device-id=n (for Windows only)",
6061 "               Number of WASAPI device ID (-1: Default device, 0..19: other)",
6062 "             --wasapi-latency=n (for Windows only)",
6063 "               WASAPI Latency ms n=1-9999 depend device (default is 10)",
6064 "             --wasapi-format-ext=n (for Windows only)",
6065 "               WASAPI Enable WAVE_FORMAT_EXTENSIBLE (default is 1)",
6066 "             --wasapi-exclusive=n (for Windows only)",
6067 "               WASAPI 0:Shared mode 1:Exclusive mode (default is 0)",
6068 "             --wasapi-polling=n (for Windows only)",
6069 "               WASAPI Flags 0:Event 1:Polling (default is 0)",
6070 "             --wasapi_priority=n (for Windows only)",
6071 "               WASAPI ThreadPriority (default is 0)",
6072 "               0:None , 1:Audio , 2:Capture , 3:Distribution , 4:Games , 5:playback , 6:ProAudio , 7:WindowManager",
6073 "             --wasapi-stream-category=n (for Windows only)",
6074 "               WASAPI StreamCategory (default is 0)",
6075 "               0:Other , 1:None , 2:None , 3:Communications , 4:Alerts , 5:SoundEffects ,",
6076 "               6:GameEffects , 7:GameMedia , 8:GameChat , 9:Speech , 10:Movie , 11:Media",
6077 "               values 1,2 are deprecated on Windows 10 and not included into enumeration",
6078 "             --wasapi-stream-option=n (for Windows only)",
6079 "               WASAPI StreamOption 0:None 1:Raw 2:MatchFormat (default is 0)",
6080 "               1:Raw bypass WASAPI Audio Engine DSP effects, supported since Windows 8.1",
6081 #endif
6082 #ifdef AU_PORTAUDIO
6083 "             --pa-asio-device-id=n",
6084 "               Number of PortAudio device ID (-2: Default device, 0..99: other)",
6085 #ifdef __W32__
6086 "             --pa-wmme-device-id=n (for Windows only)",
6087 "               Number of PortAudio device ID (-2: Default device, 0..99: other)",
6088 "             --pa-ds-device-id=n (for Windows only)",
6089 "               Number of PortAudio device ID (-2: Default device, 0..99: other)",
6090 #ifdef PORTAUDIO_V19
6091 "             --pa-wdmks-device-id=n (for Windows only)",
6092 "               Number of PortAudio device ID (-2: Default device, 0..99: other)",
6093 "             --pa-wasapi-device-id=n (for Windows only)",
6094 "               Number of PortAudio device ID (-2: Default device, 0..99: other)",
6095 "             --pa-wasapi-flag=n",
6096 #endif
6097 #endif
6098 #endif
6099 "             --add-play-time=sec (default is 0.5)",
6100 "             --add-silent-time=sec (default is 0.5)",
6101 "             --emu-delay-time=sec (default is 0.1)",
6102 #if defined(__W32__)
6103 "             --process-priority=n (for Windows only)",
6104 #if !defined(IA_W32G_SYN)
6105 "             --player-thread-priority=n (for Windows only)",
6106 #endif
6107 #endif
6108 "  -o file    --output-file=file",
6109 "               Output to another file (or device/server) (Use \"-\" for stdout)",
6110 #if defined(AU_PORTAUDIO) || defined(AU_WIN32)
6111 "               Set the output device no. (-1 shows available device no. list)",
6112 #endif
6113 "  -P file    --patch-file=file",
6114 "               Use patch file for all programs",
6115 "  -p n       --polyphony=n",
6116 "               Allow n-voice polyphony.  Optional auto polyphony reduction",
6117 "     (a)     --[no-]polyphony-reduction",
6118 "               Toggle automatic polyphony reduction.  Enabled by default",
6119 "  -Q n[,...] --mute=n[,...]",
6120 "               Ignore channel n (0: ignore all, -n: resume channel n)",
6121 "     (t)     --temper-mute=n[,...]",
6122 "               Quiet temperament type n (0..3: preset, 4..7: user-defined)",
6123 "             --preserve-silence",
6124 "               Do not drop initial silence.  Default: drop initial silence",
6125 "  -q sec/n   --audio-buffer=sec/n",
6126 "               Specify audio buffer in seconds",
6127 "                 sec: Maxmum buffer, n: Filled to start (default is 5.0/100%s)",
6128 "                 (size of 100%s equals device buffer size)",
6129 "  -R msec      Pseudo reveb effect (set every instrument's release to msec)",
6130 "                 if n=0, n is set to 800",
6131 "  -r n       --resample-queue=n",
6132 "               Specify audio queue time limit to reduce resample quality",
6133 "                 n: size of 100 equals device buffer size",
6134 "  -S n       --cache-size=n",
6135 "               Cache size (0 means no cache)",
6136 "  -s freq    --sampling-freq=freq",
6137 "               Set sampling frequency to freq (Hz or kHz)",
6138 "  -T n       --adjust-tempo=n",
6139 "               Adjust tempo to n%s,",
6140 "                 120=play MOD files with an NTSC Amiga's timing",
6141 "  -t code    --output-charset=code",
6142 "               Output text language code:",
6143 "                 code=auto  : Auto conversion by `LANG' environment variable",
6144 "                              (UNIX only)",
6145 "                      ascii : Convert unreadable characters to '.' (0x2e)",
6146 "                      nocnv : No conversion",
6147 "                      1251  : Convert from windows-1251 to koi8-r",
6148 #ifdef JAPANESE
6149 "                      euc   : EUC-japan",
6150 "                      jis   : JIS",
6151 "                      sjis  : shift JIS",
6152 #endif /* JAPANESE */
6153 "  -U         --[no-]unload-instruments",
6154 "               Unload instruments from memory between MIDI files",
6155 #ifdef AU_VOLUME_CALC
6156 "             --volume-calc-rms",
6157 "               Soundfont Volume Calc output to `root mean square' format",
6158 "             --volume-calc-trim",
6159 "               Trim silence samples",
6160 #endif /* AU_VOLUME_CALC */
6161 "  -V power   --volume-curve=power",
6162 "               Define the velocity/volume/expression curve",
6163 "                 amp = vol^power (auto: 0, linear: 1, ideal: ~1.661, GS: ~2)",
6164 "  -v         --version",
6165 "               Display TiMidity version information",
6166 "  -W mode    --wrd=mode",
6167 "               Select WRD interface (see below for list)",
6168 #ifdef __W32__
6169 "  -w mode    --rcpcv-dll=mode",
6170 "               Windows extensional modes:",
6171 "                 mode=r/R : Enable/Disable rcpcv.dll",
6172 #endif /* __W32__ */
6173 "  -x str     --config-string=str",
6174 "               Read configuration str from command line argument",
6175 "  -Z file    --freq-table=file",
6176 "               Load frequency table (Use \"pure\" for pure intonation)",
6177 "  pure<n>(m) --pure-intonation=n(m)",
6178 "               Initial keysig number <n> of sharp(+)/flat(-) (-7..7)",
6179 "                 'm' stands for minor mode",
6180 ///r
6181 "  --module=n",
6182 "               Simulate behavior of specific synthesizer module by n",
6183 "                 n=0       : TiMidity++ Default (default)",
6184 "                   1-4     : Roland SC family",
6185 "                   5-15    : GS family",
6186 "                   16-23   : Yamaha MU family",
6187 "                   24-31   : XG family",
6188 "                   32-33   : SoundBlaster family",
6189 "                   56-79   : LA family",
6190 "                   80-84   : KORG family",
6191 "                   96-98   : SD series",
6192 "                   99-111  : other systhesizer modules",
6193 "                   112-127 : TiMidity++ specification purposes",
6194 " --mix-envelope=n",
6195 " --cut-short-time=msec",
6196 " --limiter=n (gain per)",
6197 #ifdef SUPPORT_LOOPEVENT
6198 "  --loop-repeat=n",
6199 "               Set number of unwind repeat count",
6200 "  --loop-filter=types",
6201 "               Enable loop-event types:",
6202 "                 type=0 : Filter reset",
6203 "                      1 : CC#111 - Last End of Track",
6204 "                      m : Mark A - B",
6205 "                      l : Mark Loop_Start - Loop_End",
6206 "                      2 : CC#2 - CC#4",
6207 "                  All: --loop-filter=1ml2",
6208 #endif /* SUPPORT_LOOPEVENT */
6209 #ifdef ENABLE_THREAD
6210 "  --compute-thread-num=n",
6211 "               Set number of divide multi-threads (0..16)",
6212 "                 (0..1 means single-thread, default is 0)",
6213 #endif /* ENABLE_THREAD */
6214 "  --od-level-gs=n",
6215 "               Set GS overdrive-amplify-level (1..400:default=100)",
6216 "  --od-drive-gs=n",
6217 "               Set GS overdrive-drive-level (1..400:default=100)",
6218 "  --od-level-xg=n",
6219 "               Set XG overdrive-amplify-level (1..400:default=100)",
6220 "  --od-drive-xg=n",
6221 "               Set XG overdrive-drive-level (1..400:default=100)",
6222 #if defined(PRE_RESAMPLATION) || defined(DEFAULT_PRE_RESAMPLATION)
6223 "  --pre-resample=n",
6224 #endif
6225                 NULL
6226         };
6227         void show_ao_device_info(FILE *fp);
6228         FILE *fp;
6229         char version[64], *help_args[7], per_mark[2];
6230         int i, j;
6231         char *h;
6232         ControlMode *cmp, **cmpp;
6233         char mark[128];
6234         PlayMode *pmp, **pmpp;
6235         WRDTracer *wlp, **wlpp;
6236         
6237         fp = open_pager();
6238         strcpy(version, (!strstr(timidity_version, "current")) ? "version " : "");
6239         strcat(version, timidity_version);
6240         strcat(version, " ");
6241         strcat(version, arch_string);
6242         per_mark[0] = '%';
6243         per_mark[1] = '\0';
6244         help_args[0] = version;
6245         help_args[1] = program_name;
6246         help_args[2] = per_mark;
6247         help_args[3] = per_mark;
6248         help_args[4] = per_mark;
6249         help_args[5] = per_mark;
6250         help_args[6] = NULL;
6251         for (i = 0, j = 0; (h = help_list[i]) != NULL; i++) {
6252                 if (strchr(h, '%')) {
6253                         if (*(strchr(h, '%') + 1) != '%')
6254                                 fprintf(fp, h, help_args[j++]);
6255                         else
6256                                 fprintf(fp, "%s", h);
6257                 } else
6258                         fputs(h, fp);
6259                 fputs(NLS, fp);
6260         }
6261         fputs(NLS, fp);
6262         fputs("Effect options (-EF, --ext=F option):" NLS
6263 "  -EFdelay=d   Disable delay effect (default)" NLS
6264 "  -EFdelay=D   Enable delay effect" NLS
6265 "  -EFchorus=d  Disable MIDI chorus effect control" NLS
6266 "  -EFchorus=n  Enable Normal MIDI chorus effect control" NLS
6267 "    [,level]     `level' is optional to specify chorus level [0..127]" NLS
6268 "                 (default)" NLS
6269 "  -EFchorus=s  Surround sound, chorus detuned to a lesser degree" NLS
6270 "    [,level]     `level' is optional to specify chorus level [0..127]" NLS
6271 "  -EFreverb=d  Disable MIDI reverb effect control" NLS
6272 #if !defined(REVERB_CONTROL_ALLOW) && !defined(FREEVERB_CONTROL_ALLOW)
6273 "                 (default)" NLS
6274 #endif
6275 "  -EFreverb=n  Enable Normal MIDI reverb effect control" NLS
6276 "    [,level]     `level' is optional to specify reverb level [1..127]" NLS
6277 #if defined(REVERB_CONTROL_ALLOW)
6278 "                 (default)" NLS
6279 #endif
6280 "  -EFreverb=g  Global reverb effect" NLS
6281 "    [,level]     `level' is optional to specify reverb level [1..127]" NLS
6282 "  -EFreverb=f  Enable Freeverb MIDI reverb effect control" NLS
6283 "    [,level]     `level' is optional to specify reverb level [1..127]" NLS
6284 #if !defined(REVERB_CONTROL_ALLOW) && defined(FREEVERB_CONTROL_ALLOW)
6285 "                 (default)" NLS
6286 #endif
6287 "  -EFreverb=G  Global Freeverb effect" NLS
6288 "    [,level]     `level' is optional to specify reverb level [1..127]" NLS
6289 "  -EFvlpf=d    Disable voice LPF" NLS
6290 #if !defined(VOICE_MOOG_LPF_ALLOW) && !defined(VOICE_CHAMBERLIN_LPF_ALLOW)
6291 "                 (default)" NLS
6292 #endif
6293 "  -EFvlpf=c    Enable Chamberlin resonant LPF (12dB/oct)" NLS
6294 #if defined(VOICE_CHAMBERLIN_LPF_ALLOW)
6295 "                 (default)" NLS
6296 #endif
6297 "  -EFvlpf=m    Enable Moog resonant lowpass VCF (24dB/oct)" NLS
6298 #if defined(VOICE_MOOG_LPF_ALLOW) && !defined(VOICE_CHAMBERLIN_LPF_ALLOW)
6299 "                 (default)" NLS
6300 #endif
6301 "  -EFvlpf=b    Enable ButterworthFilter resonant lowpass (butterworth)" NLS
6302 "  -EFvlpf=i    Enable Resonant IIR lowpass VCF (12dB/oct)-2" NLS
6303 "  -EFvlpf=a    Enable amSynth resonant lowpass VCF (24dB/oct)-2" NLS
6304 "  -EFvlpf=o    Enable 1 pole 6db/oct resonant lowpass VCF (6dB/oct)" NLS
6305 "  -EFvlpf=e    Enable resonant 3 pole lowpass VCF (18dB/oct)" NLS
6306 "  -EFvlpf=t    Enable two first order lowpass VCF " NLS
6307 "  -EFvlpf=h    Enable HPF ButterworthFilter VCF (butterworth)" NLS
6308 "  -EFvlpf=B    Enable BPF ButterworthFilter VCF (butterworth)" NLS
6309 "  -EFns=n      Enable the n'th degree (type) noise shaping filter" NLS
6310 "                 n:[0..4] (for 8-bit linear encoding, default is 4)" NLS
6311 "                 n:[0..4] (for 16-bit linear encoding, default is 4)" NLS
6312 "                 n:[0] (for 24-bit linear encoding, default is 0)" NLS
6313 "                 n:[0] (for 32-bit linear encoding, default is 0)" NLS
6314 "                 n:[0] (for 64-bit linear encoding, default is 0)" NLS
6315 "                 n:[0] (for float 32-bit linear encoding, default is 0)" NLS
6316 "                 n:[0] (for float 64-bit linear encoding, default is 0)" NLS, fp);
6317 #ifndef FIXED_RESAMPLATION
6318 #ifdef HAVE_STRINGIZE
6319 #define tim_str_internal(x) #x
6320 #define tim_str(x) tim_str_internal(x)
6321 #else
6322 #define tim_str(x) "x"
6323 #endif
6324         fputs("  -EFresamp=d  Disable resamplation", fp);
6325         if (! strcmp(tim_str(DEFAULT_RESAMPLATION), "resample_none"))
6326                 fputs(" (default)", fp);
6327         fputs(NLS, fp);
6328         fputs("  -EFresamp=l  Enable Linear resample algorithm", fp);
6329         if (! strcmp(tim_str(DEFAULT_RESAMPLATION), "resample_linear"))
6330                 fputs(" (default)", fp);
6331         fputs(NLS, fp);
6332         fputs("  -EFresamp=c  Enable C-spline resample algorithm", fp);
6333         if (! strcmp(tim_str(DEFAULT_RESAMPLATION), "resample_cspline"))
6334                 fputs(" (default)", fp);
6335         fputs(NLS, fp);
6336         fputs("  -EFresamp=L  Enable Lagrange resample algorithm", fp);
6337         if (! strcmp(tim_str(DEFAULT_RESAMPLATION), "resample_lagrange"))
6338                 fputs(" (default)", fp);
6339         fputs(NLS, fp);
6340         fputs("  -EFresamp=n  Enable Newton resample algorithm", fp);
6341         if (! strcmp(tim_str(DEFAULT_RESAMPLATION), "resample_newton"))
6342                 fputs(" (default)", fp);
6343         fputs(NLS, fp);
6344         fputs("  -EFresamp=g  Enable Gauss-like resample algorithm", fp);
6345         if (! strcmp(tim_str(DEFAULT_RESAMPLATION), "resample_gauss"))
6346                 fputs(" (default)", fp);
6347 ///r
6348         fputs(NLS, fp);
6349         fputs("  -EFresamp=s  Enable Sharp resample algorithm", fp);
6350         if (! strcmp(tim_str(DEFAULT_RESAMPLATION), "resample_sharp"))
6351                 fputs(" (default)", fp);
6352         fputs(NLS, fp);
6353         fputs("  -EFresamp=p  Enable LinearP resample algorithm", fp);
6354         if (! strcmp(tim_str(DEFAULT_RESAMPLATION), "resample_linear_p"))
6355                 fputs(" (default)", fp);
6356         fputs(NLS
6357 "                 -EFresamp affects the behavior of -N option" NLS, fp);
6358 #endif
6359         fputs(NLS, fp);
6360         fputs("Alternative TiMidity sequencer extensional mode long options:" NLS
6361 "  --[no-]mod-wheel" NLS
6362 "  --[no-]portamento" NLS
6363 "  --[no-]vibrato" NLS
6364 "  --[no-]ch-pressure" NLS
6365 "  --[no-]mod-envelope" NLS
6366 "  --[no-]trace-text-meta" NLS
6367 "  --[no-]overlap-voice" NLS
6368 ///r
6369 "  --overlap-voice-count=n" NLS
6370 "  --[no-]temper-control" NLS
6371 "  --default-mid=<HH>" NLS
6372 "  --system-mid=<HH>" NLS
6373 "  --default-bank=n" NLS
6374 "  --force-bank=n" NLS
6375 "  --default-program=n/m" NLS
6376 "  --force-program=n/m" NLS
6377 "  --delay=(d|D)" NLS
6378 "  --chorus=(d|n|s|w|W|b|B|t|T|h|H|e|E)[,level]" NLS
6379 "  --reverb=(d|n|g|f|G)[,level]" NLS
6380 "  --reverb=(f|G)[,level[,scaleroom[,offsetroom[,predelay]]]]" NLS
6381 "  --voice-lpf=(d|c|m|b|i|a|o|e|t|h|B)" NLS
6382 "  --noise-shaping=n" NLS, fp);
6383 #ifndef FIXED_RESAMPLATION
6384         fputs("  --resample=(d|l|c|L|n|g|s|p)" NLS, fp);
6385 #endif
6386         fputs(NLS, fp);
6387         fputs("Available interfaces (-i, --interface option):" NLS, fp);
6388         for (cmpp = ctl_list; (cmp = *cmpp) != NULL; cmpp++)
6389                 fprintf(fp, "  -i%c          %s" NLS,
6390                                 cmp->id_character, cmp->id_name);
6391 #ifdef IA_DYNAMIC
6392         fprintf(fp, "Supported dynamic load interfaces (%s):" NLS,
6393                         dynamic_lib_root);
6394         memset(mark, 0, sizeof(mark));
6395         for (cmpp = ctl_list; (cmp = *cmpp) != NULL; cmpp++)
6396                 mark[(int) cmp->id_character] = 1;
6397         list_dyna_interface(fp, dynamic_lib_root, mark);
6398 #endif  /* IA_DYNAMIC */
6399         fputs(NLS, fp);
6400         fputs("Interface options (append to -i? option):" NLS
6401 "  `v'          more verbose (cumulative)" NLS
6402 "  `q'          quieter (cumulative)" NLS
6403 "  `t'          trace playing" NLS
6404 "  `l'          loop playing (some interface ignore this option)" NLS
6405 "  `r'          randomize file list arguments before playing" NLS
6406 "  `s'          sorting file list arguments before playing" NLS, fp);
6407 #ifdef IA_ALSASEQ
6408         fputs("  `D'          daemonize TiMidity++ in background "
6409                         "(for alsaseq only)" NLS, fp);
6410 #endif
6411         fputs(NLS, fp);
6412         fputs("Alternative interface long options:" NLS
6413 "  --verbose=n" NLS
6414 "  --quiet=n" NLS
6415 "  --[no-]trace" NLS
6416 "  --[no-]loop" NLS
6417 "  --[no-]random" NLS
6418 "  --[no-]sort" NLS, fp);
6419 #ifdef IA_ALSASEQ
6420         fputs("  --[no-]background" NLS, fp);
6421 #endif
6422         fputs(NLS, fp);
6423         fputs("Available output modes (-O, --output-mode option):" NLS, fp);
6424         for (pmpp = play_mode_list; (pmp = *pmpp) != NULL; pmpp++)
6425                 fprintf(fp, "  -O%c          %s" NLS,
6426                                 pmp->id_character, pmp->id_name);
6427         fputs(NLS, fp);
6428         fputs("Output format options (append to -O? option):" NLS
6429 "  `S'          stereo" NLS
6430 "  `M'          monophonic" NLS
6431 "  `s'          signed output" NLS
6432 "  `u'          unsigned output" NLS
6433 "  `1'          16-bit sample width" NLS
6434 "  `2'          24-bit sample width" NLS
6435 "  `3'          32-bit sample width" NLS
6436 "  `6'          64-bit sample width" NLS
6437 "  `8'          8-bit sample width" NLS
6438 "  `f'          float 32-bit sample width" NLS
6439 "  `D'          float 64-bit sample width" NLS
6440 "  `l'          linear encoding" NLS
6441 "  `U'          U-Law encoding" NLS
6442 "  `A'          A-Law encoding" NLS
6443 "  `x'          byte-swapped output" NLS, fp);
6444         fputs(NLS, fp);
6445         fputs("Alternative output format long options:" NLS
6446 "  --output-stereo" NLS
6447 "  --output-mono" NLS
6448 "  --output-signed" NLS
6449 "  --output-unsigned" NLS
6450 "  --output-16bit" NLS
6451 "  --output-24bit" NLS
6452 "  --output-32bit" NLS
6453 "  --output-64bit" NLS
6454 "  --output-8bit" NLS
6455 "  --output-f32bit" NLS
6456 "  --output-float32bit" NLS
6457 "  --output-f64bit" NLS
6458 "  --output-float64bit" NLS
6459 "  --output-linear" NLS
6460 "  --output-ulaw" NLS
6461 "  --output-alaw" NLS
6462 "  --[no-]output-swab" NLS, fp);
6463         fputs(NLS, fp);
6464         fputs("Available WRD interfaces (-W, --wrd option):" NLS, fp);
6465         for (wlpp = wrdt_list; (wlp = *wlpp) != NULL; wlpp++)
6466                 fprintf(fp, "  -W%c          %s" NLS, wlp->id, wlp->name);
6467         fputs(NLS, fp);
6468         close_pager(fp);
6469         exit(EXIT_SUCCESS);
6470         return 0; // dummy call
6471 }
6472
6473
6474 #ifdef IA_DYNAMIC
6475 static inline void list_dyna_interface(FILE *fp, char *path, char *mark)
6476 {
6477     URL dir;
6478     char fname[NAME_MAX];
6479     int cwd, dummy;
6480         if ((dir = url_dir_open(path)) == NULL)
6481                 return;
6482 #if defined(__W32__)
6483         cwd = -1;
6484 #else
6485         cwd = open(".", 0);
6486 #endif
6487         if(chdir(path) != 0)
6488                 return;
6489         while (url_gets(dir, fname, sizeof(fname)) != NULL)
6490                 if (strncmp(fname, "if_", 3) == 0) {
6491                         void* handle = NULL;
6492                         char path[NAME_MAX];
6493                         snprintf(path, NAME_MAX, ".%c%s", PATH_SEP, fname);
6494                         if((handle = dl_load_file(path))) {
6495                                 ControlMode *(* loader)(void) = NULL;
6496                                 char c;
6497                                 for (c = 'A'; c <= 'z'; c++) {
6498                                         char buf[20]; /* enough */
6499                                         if(mark[(int)c]) continue;
6500                                         sprintf(buf, "interface_%c_loader", c);
6501                                         if((loader = dl_find_symbol(handle, buf))) {
6502                                                 fprintf(fp, "  -i%c          %s" NLS, c, loader()->id_name);
6503                                                 mark[(int)c] = 1;
6504                                                 break;
6505                                         }
6506                                 }
6507                                 dl_free(handle);
6508                         }
6509                 }
6510         if(cwd != -1) {
6511                 dummy = fchdir(cwd);
6512                 dummy += close(cwd);
6513         }
6514         url_close(dir);
6515 }
6516
6517 ControlMode *dynamic_interface_module(int id_char)
6518 {
6519         URL url;
6520         char fname[FILEPATH_MAX];
6521         ControlMode *ctl = NULL;
6522         int cwd, dummy;
6523         void *handle;
6524         ControlMode *(* inferface_loader)(void);
6525
6526         if ((url = url_dir_open(dynamic_lib_root)) == NULL)
6527                 return NULL;
6528         cwd = open(".", 0);
6529         if(chdir(dynamic_lib_root) != 0)
6530                 return NULL;
6531         while (url_gets(url, fname, sizeof(fname)) != NULL) {
6532                 if (strncmp(fname, "if_", 3) == 0) {
6533                         char path[NAME_MAX], buff[20];
6534                         snprintf(path, NAME_MAX-1, ".%c%s", PATH_SEP, fname);
6535                         if((handle = dl_load_file(path)) == NULL)
6536                                 continue;
6537                         sprintf(buff, "interface_%c_loader", id_char);
6538                         if((inferface_loader = dl_find_symbol(handle, buff)) == NULL) {
6539                                 dl_free(handle);
6540                                 continue;
6541                         }
6542                         ctl = inferface_loader();
6543                         if(ctl->id_character == id_char)
6544                                 break;
6545                 }
6546         }
6547         dummy = fchdir(cwd);
6548         dummy += close(cwd);
6549         url_close(url);
6550         return ctl;
6551 }
6552 #endif  /* IA_DYNAMIC */
6553
6554 static inline int parse_opt_i(const char *arg)
6555 {
6556         /* interface mode */
6557         ControlMode *cmp, **cmpp;
6558         int found = 0;
6559         if (!arg) arg = "";
6560         
6561         for (cmpp = ctl_list; (cmp = *cmpp) != NULL; cmpp++) {
6562                 if (cmp->id_character == *arg) {
6563                         found = 1;
6564                         ctl = cmp;
6565 #if defined(IA_W32GUI) || defined(IA_W32G_SYN)
6566                         cmp->verbosity = 1;
6567                         cmp->trace_playing = 0;
6568                         cmp->flags = 0;
6569 #endif  /* IA_W32GUI */
6570                         break;
6571                 }
6572         }
6573 #ifdef IA_DYNAMIC
6574         if (! found) {
6575                 cmp = dynamic_interface_module(*arg);
6576                 if(cmp) {
6577                         ctl = cmp;
6578                         found = 1;
6579                 }
6580         }
6581 #endif  /* IA_DYNAMIC */
6582         if (! found) {
6583                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
6584                                 "Interface `%c' is not compiled in.", *arg);
6585                 return 1;
6586         }
6587         while (*(++arg))
6588                 switch (*arg) {
6589                 case 'v':
6590                         cmp->verbosity++;
6591                         break;
6592                 case 'q':
6593                         cmp->verbosity--;
6594                         break;
6595                 case 't':       /* toggle */
6596                         cmp->trace_playing = (cmp->trace_playing) ? 0 : 1;
6597                         break;
6598                 case 'l':
6599                         cmp->flags ^= CTLF_LIST_LOOP;
6600                         break;
6601                 case 'r':
6602                         cmp->flags ^= CTLF_LIST_RANDOM;
6603                         break;
6604                 case 's':
6605                         cmp->flags ^= CTLF_LIST_SORT;
6606                         break;
6607                 case 'a':
6608                         cmp->flags ^= CTLF_AUTOSTART;
6609                         break;
6610                 case 'x':
6611                         cmp->flags ^= CTLF_AUTOEXIT;
6612                         break;
6613                 case 'd':
6614                         cmp->flags ^= CTLF_DRAG_START;
6615                         break;
6616                 case 'u':
6617                         cmp->flags ^= CTLF_AUTOUNIQ;
6618                         break;
6619                 case 'R':
6620                         cmp->flags ^= CTLF_AUTOREFINE;
6621                         break;
6622                 case 'C':
6623                         cmp->flags ^= CTLF_NOT_CONTINUE;
6624                         break;
6625                 case 'D':
6626                         cmp->flags ^= CTLF_DAEMONIZE;
6627                         break;
6628                 default:
6629                         ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
6630                                         "Unknown interface option `%c'", *arg);
6631                         return 1;
6632                 }
6633         return 0;
6634 }
6635
6636 static inline int parse_opt_verbose(const char *arg)
6637 {
6638         /* --verbose */
6639         ctl->verbosity += (arg) ? atoi(arg) : 1;
6640         return 0;
6641 }
6642
6643 static inline int parse_opt_quiet(const char *arg)
6644 {
6645         /* --quiet */
6646         ctl->verbosity -= (arg) ? atoi(arg) : 1;
6647         return 0;
6648 }
6649
6650 static inline int parse_opt_trace(const char *arg)
6651 {
6652         /* --[no-]trace */
6653         if (!arg) return 0;
6654         ctl->trace_playing = y_or_n_p(arg);
6655         return 0;
6656 }
6657
6658 static inline int parse_opt_loop(const char *arg)
6659 {
6660         /* --[no-]loop */
6661         if (!arg) return 0;
6662         return set_flag(&(ctl->flags), CTLF_LIST_LOOP, arg);
6663 }
6664
6665 static inline int parse_opt_random(const char *arg)
6666 {
6667         /* --[no-]random */
6668         if (!arg) return 0;
6669         return set_flag(&(ctl->flags), CTLF_LIST_RANDOM, arg);
6670 }
6671
6672 static inline int parse_opt_sort(const char *arg)
6673 {
6674         /* --[no-]sort */
6675         if (!arg) return 0;
6676         return set_flag(&(ctl->flags), CTLF_LIST_SORT, arg);
6677 }
6678
6679 #ifdef IA_ALSASEQ
6680 static inline int parse_opt_background(const char *arg)
6681 {
6682         /* --[no-]background */
6683         if (!arg) return 0;
6684         return set_flag(&(ctl->flags), CTLF_DAEMONIZE, arg);
6685 }
6686
6687 static inline int parse_opt_rt_prio(const char *arg)
6688 {
6689         /* --realtime-priority */
6690         if (!arg) return 0;
6691         if (set_value(&opt_realtime_priority, atoi(arg), 0, 100,
6692                         "Realtime priority"))
6693                 return 1;
6694         return 0;
6695 }
6696
6697 static inline int parse_opt_seq_ports(const char *arg)
6698 {
6699         /* --sequencer-ports */
6700         if (!arg) return 0;
6701         if (set_value(&opt_sequencer_ports, atoi(arg), 1, 16,
6702                         "Number of sequencer ports"))
6703                 return 1;
6704         return 0;
6705 }
6706 #endif
6707
6708 #if defined(IA_WINSYN) || defined(IA_NPSYN) || defined(IA_PORTMIDI) || \
6709     defined(IA_W32G_SYN)
6710 static inline int parse_opt_rtsyn_latency(const char *arg)
6711 {
6712     /* --rtsyn-latency */
6713     double latency;
6714     if (!arg) arg = "";
6715
6716     if (sscanf(arg, "%lf", &latency) == EOF)
6717         latency = RTSYN_LATENCY;
6718     rtsyn_set_latency(latency);
6719     return 0;
6720 }
6721
6722 static inline int parse_opt_rtsyn_skip_aq(const char *arg)
6723 {
6724     /* --rtsyn-skip-aq */
6725     rtsyn_set_skip_aq(y_or_n_p(arg));
6726     return 0;
6727 }
6728
6729 __attribute__((noreturn))
6730 static inline int parse_opt_rtsyn_print_ports(const char *arg)
6731 {
6732     /* --rtsyn-print-ports */
6733     const char c = ctl->id_character;
6734     if (c == 'W' || c == 'P') {
6735         ctl->open(0, 0);
6736         rtsyn_get_port_list();
6737         rtsyn_print_port_list();
6738         ctl->close();
6739     }
6740     exit(EXIT_SUCCESS);
6741 }
6742 #elif defined(IA_W32GUI)
6743 static inline int parse_opt_rtsyn_latency(const char *arg)
6744 {
6745     /* --rtsyn-latency */
6746     return 0;
6747 }
6748 static inline int parse_opt_rtsyn_skip_aq(const char *arg)
6749 {
6750     /* --rtsyn-skip-aq */
6751     return 0;
6752 }
6753 static inline int parse_opt_rtsyn_print_ports(const char *arg)
6754 {
6755     /* --rtsyn-print-ports */
6756     return 0;
6757 }
6758 #endif
6759
6760 static inline int parse_opt_j(const char *arg)
6761 {
6762         if (!arg) return 0;
6763         opt_realtime_playing = y_or_n_p(arg);
6764         return 0;
6765 }
6766
6767 static inline int parse_opt_K(const char *arg)
6768 {
6769         /* key adjust */
6770         if (!arg) return 0;
6771         if (set_value(&key_adjust, atoi(arg), -24, 24, "Key adjust"))
6772                 return 1;
6773         return 0;
6774 }
6775
6776 static inline int parse_opt_k(const char *arg)
6777 {
6778         safe_free(opt_reduce_voice_threshold);
6779         opt_reduce_voice_threshold = safe_strdup(arg ? arg : DEFAULT_REDUCE_VOICE_THRESHOLD);
6780 //      reduce_voice_threshold = atoi(arg);
6781         return 0;
6782 }
6783
6784 static inline int parse_opt_L(const char *arg)
6785 {
6786         if (!arg) return 0;
6787         add_to_pathlist(arg);
6788         try_config_again = 1;
6789         return 0;
6790 }
6791 ///r
6792 static inline int parse_opt_l(const char *arg)
6793 {
6794         if (!arg) return 0;
6795         opt_resample_filter = atoi(arg);
6796         return 0;
6797 }
6798
6799 static inline int parse_opt_resample_over_sampling(const char *arg)
6800 {
6801         if (!arg) return 0;
6802         opt_resample_over_sampling = atoi(arg);
6803         return 0;
6804 }
6805
6806 static inline int parse_opt_pre_resample(const char *arg)
6807 {
6808         if (!arg) return 0;
6809         opt_pre_resamplation = atoi(arg);
6810         return 0;
6811 }
6812
6813 static inline int parse_opt_M(const char *arg)
6814 {
6815         safe_free(pcm_alternate_file);
6816         if (!arg) arg = "";
6817         pcm_alternate_file = safe_strdup(arg);
6818         return 0;
6819 }
6820
6821 static inline int parse_opt_m(const char *arg)
6822 {
6823         if (!arg) return 0;
6824         min_sustain_time = atoi(arg);
6825         if (min_sustain_time < 0)
6826                 min_sustain_time = 0;
6827         return 0;
6828 }
6829
6830 static inline int parse_opt_N(const char *arg)
6831 {
6832         if (!arg) return 0;
6833         opt_resample_param = atoi(arg);
6834         return 0;
6835 }
6836
6837 static inline int parse_opt_n(const char *arg)
6838 {
6839         safe_free(opt_reduce_polyphony_threshold);
6840         opt_reduce_polyphony_threshold = safe_strdup(arg ? arg : DEFAULT_REDUCE_POLYPHONY_THRESHOLD);
6841         return 0;
6842 }
6843
6844 static inline int parse_opt_O(const char *arg)
6845 {
6846         /* output mode */
6847         PlayMode *pmp, **pmpp, *prev_playmode;
6848         int found = 0;
6849         if (!arg) arg = "";
6850
6851         prev_playmode = play_mode;
6852         for (pmpp = play_mode_list; (pmp = *pmpp) != NULL; pmpp++)
6853                 if (pmp->id_character == *arg) {
6854                         found = 1;
6855                         play_mode = pmp;
6856                         break;
6857                 }
6858         if (!found) {
6859                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
6860                                 "Playmode `%c' is not compiled in.", *arg);
6861                 return 1;
6862         }
6863         if (prev_playmode) {
6864                 if (prev_playmode->close_output) {
6865                         prev_playmode->close_output();
6866                 }
6867                 safe_free(prev_playmode->name);
6868                 prev_playmode->name = NULL;
6869         }
6870         while (*(++arg))
6871                 switch (*arg) {
6872                 case 'S':       /* stereo */
6873                         pmp->encoding &= ~PE_MONO;
6874                         break;
6875                 case 'M':
6876                         pmp->encoding |= PE_MONO;
6877                         break;
6878                 case 's':
6879                         pmp->encoding |= PE_SIGNED;
6880                         pmp->encoding &= ~(PE_ULAW | PE_ALAW);
6881                         break;
6882 ///r
6883                 case 'u':
6884                         pmp->encoding &= ~PE_SIGNED;
6885 //                      pmp->encoding &= ~(PE_ULAW | PE_ALAW); // w32g can't use ULAW ALAW
6886                         break;
6887                 case 'D':       /* D for float 64-bit */
6888                         pmp->encoding |= PE_F64BIT;
6889                         pmp->encoding &= ~(PE_16BIT | PE_24BIT | PE_32BIT | PE_64BIT | PE_F32BIT | PE_ULAW | PE_ALAW);
6890                         break;
6891                 case '6':       /* 6 for 64-bit */
6892                         pmp->encoding |= PE_64BIT;
6893                         pmp->encoding &= ~(PE_16BIT | PE_24BIT | PE_32BIT | PE_F32BIT | PE_F64BIT | PE_ULAW | PE_ALAW);
6894                         break;
6895                 case 'f':       /* f for float 32-bit */
6896                         pmp->encoding |= PE_F32BIT;
6897                         pmp->encoding &= ~(PE_16BIT | PE_24BIT | PE_32BIT | PE_64BIT | PE_F64BIT | PE_ULAW | PE_ALAW);
6898                         break;
6899                 case '3':       /* 3 for 32-bit */
6900                         pmp->encoding |= PE_32BIT;
6901                         pmp->encoding &= ~(PE_16BIT | PE_24BIT | PE_64BIT | PE_F32BIT | PE_F64BIT | PE_ULAW | PE_ALAW);
6902                         break;
6903                 case '2':       /* 2 for 24-bit */
6904                         pmp->encoding |= PE_24BIT;
6905                         pmp->encoding &= ~(PE_16BIT | PE_32BIT | PE_64BIT | PE_F32BIT | PE_F64BIT | PE_ULAW | PE_ALAW);
6906                         break;
6907                 case '1':       /* 1 for 16-bit */
6908                         pmp->encoding |= PE_16BIT;
6909                         pmp->encoding &= ~(PE_24BIT | PE_32BIT | PE_64BIT | PE_F32BIT | PE_F64BIT | PE_ULAW | PE_ALAW);
6910                         break;
6911                 case '8':
6912                         pmp->encoding &= ~(PE_16BIT | PE_24BIT | PE_32BIT | PE_64BIT | PE_F32BIT | PE_F64BIT | PE_BYTESWAP);
6913                         break;
6914                 case 'l':       /* linear */
6915                         pmp->encoding &= ~(PE_ULAW | PE_ALAW);
6916                         break;
6917                 case 'U':       /* uLaw */
6918                         pmp->encoding |= PE_ULAW;
6919                         pmp->encoding &= ~(PE_SIGNED | PE_16BIT | PE_24BIT | PE_32BIT | PE_64BIT | PE_F32BIT | PE_F64BIT | PE_ALAW | PE_BYTESWAP);
6920                         break;
6921                 case 'A':       /* aLaw */
6922                         pmp->encoding |= PE_ALAW;
6923                         pmp->encoding &= ~(PE_SIGNED | PE_16BIT | PE_24BIT | PE_32BIT | PE_64BIT | PE_F32BIT | PE_F64BIT | PE_ULAW | PE_BYTESWAP);
6924                         break;
6925                 case 'x':
6926                         pmp->encoding ^= PE_BYTESWAP;   /* toggle */
6927                         pmp->encoding &= ~(PE_ULAW | PE_ALAW);
6928                         break;
6929                 default:
6930                         ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
6931                                         "Unknown format modifier `%c'", *arg);
6932                         return 1;
6933                 }
6934         return 0;
6935 }
6936
6937 static inline int parse_opt_output_stereo(const char *arg)
6938 {
6939         /* --output-stereo, --output-mono */
6940         if (!arg) return 0;
6941         if (y_or_n_p(arg))
6942                 /* I first thought --mono should be the syntax sugar to
6943                  * --stereo=no, but the source said stereo should be !PE_MONO,
6944                  * not mono should be !PE_STEREO.  Perhaps I took a wrong
6945                  * choice? -- mput
6946                  */
6947                 play_mode->encoding &= ~PE_MONO;
6948         else
6949                 play_mode->encoding |= PE_MONO;
6950         return 0;
6951 }
6952
6953 static inline int parse_opt_output_signed(const char *arg)
6954 {
6955         /* --output-singed, --output-unsigned */
6956         if (!arg) return 0;
6957         if (set_flag(&(play_mode->encoding), PE_SIGNED, arg))
6958                 return 1;
6959         play_mode->encoding &= ~(PE_ULAW | PE_ALAW);
6960         return 0;
6961 }
6962 ///r
6963 static inline int parse_opt_output_bitwidth(const char *arg)
6964 {
6965         /* --output-16bit, --output-24bit, --output-32bit, --output-64bit, --output-8bit, --output-float32bit, --output-float64bit */
6966         if (!arg) return 0;
6967         switch (*arg) {
6968         case 'f':       /* float32bit */
6969                 play_mode->encoding |= PE_F32BIT;
6970                 play_mode->encoding &= ~(PE_16BIT | PE_24BIT | PE_32BIT | PE_64BIT | PE_F64BIT | PE_ULAW | PE_ALAW);
6971                 return 0;
6972         case 'D':       /* float64bit */
6973                 play_mode->encoding |= PE_F64BIT;
6974                 play_mode->encoding &= ~(PE_16BIT | PE_24BIT | PE_32BIT | PE_64BIT | PE_F32BIT | PE_ULAW | PE_ALAW);
6975                 return 0;
6976         case '1':       /* 16bit */
6977                 play_mode->encoding |= PE_16BIT;
6978                 play_mode->encoding &= ~(PE_24BIT | PE_32BIT | PE_64BIT | PE_F32BIT | PE_F64BIT | PE_ULAW | PE_ALAW);
6979                 return 0;
6980         case '2':       /* 24bit */
6981                 play_mode->encoding |= PE_24BIT;
6982                 play_mode->encoding &= ~(PE_16BIT | PE_32BIT | PE_64BIT | PE_F32BIT | PE_F64BIT | PE_ULAW | PE_ALAW);
6983                 return 0;
6984         case '3':       /* 32bit */
6985                 play_mode->encoding |= PE_32BIT;
6986                 play_mode->encoding &= ~(PE_16BIT | PE_24BIT | PE_64BIT | PE_F32BIT | PE_F64BIT | PE_ULAW | PE_ALAW);
6987                 return 0;
6988         case '6':       /* 64bit */
6989                 play_mode->encoding |= PE_64BIT;
6990                 play_mode->encoding &= ~(PE_16BIT | PE_24BIT | PE_32BIT | PE_F32BIT | PE_F64BIT | PE_ULAW | PE_ALAW);
6991                 return 0;
6992         case '8':       /* 8bit */
6993                 play_mode->encoding &= ~(PE_16BIT | PE_24BIT | PE_32BIT | PE_64BIT | PE_F32BIT | PE_F64BIT);
6994                 return 0;
6995         default:
6996                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Invalid output bitwidth %s", arg);
6997                 return 1;
6998         }
6999 }
7000
7001 static inline int parse_opt_output_format(const char *arg)
7002 {
7003         /* --output-linear, --output-ulaw, --output-alaw */
7004         if (!arg) return 0;
7005         switch (*arg) {
7006         case 'l':       /* linear */
7007                 play_mode->encoding &= ~(PE_ULAW | PE_ALAW);
7008                 return 0;
7009         case 'u':       /* uLaw */
7010                 play_mode->encoding |= PE_ULAW;
7011                 play_mode->encoding &=
7012                                 ~(PE_SIGNED | PE_16BIT | PE_24BIT | PE_32BIT | PE_64BIT | PE_F32BIT | PE_F64BIT | PE_ALAW | PE_BYTESWAP);
7013                 return 0;
7014         case 'a':       /* aLaw */
7015                 play_mode->encoding |= PE_ALAW;
7016                 play_mode->encoding &=
7017                                 ~(PE_SIGNED | PE_16BIT | PE_24BIT | PE_32BIT | PE_64BIT | PE_F32BIT | PE_F64BIT | PE_ULAW | PE_BYTESWAP);
7018                 return 0;
7019         default:
7020                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Invalid output format %s", arg);
7021                 return 1;
7022         }
7023 }
7024
7025 static inline int parse_opt_output_swab(const char *arg)
7026 {
7027         /* --[no-]output-swab */
7028         if (!arg) return 0;
7029         if (set_flag(&(play_mode->encoding), PE_BYTESWAP, arg))
7030                 return 1;
7031         play_mode->encoding &= ~(PE_ULAW | PE_ALAW);
7032         return 0;
7033 }
7034 ///r
7035 static inline int parse_opt_output_device_id(const char *arg)
7036 {
7037         /* --opt-output-device-id */
7038         if (!arg) return 0;
7039         opt_output_device_id = atoi(arg);
7040         return 0;
7041 }
7042 #ifdef AU_W32
7043 static inline int parse_opt_wmme_device_id(const char *arg)
7044 {
7045         /* --opt-wmme-device-id */
7046         if (!arg) return 0;
7047         opt_wmme_device_id = atoi(arg);
7048         return 0;
7049 }
7050 static inline int parse_opt_wave_format_ext(const char *arg)
7051 {
7052         /* --opt-wave_format_ext */
7053         if (!arg) return 0;
7054         opt_wave_format_ext = atoi(arg);
7055         return 0;
7056 }
7057 static inline int parse_opt_wmme_buffer_bit(const char *arg)
7058 {
7059         /* --opt_wmme_buffer_bit */
7060         if (!arg) return 0;
7061         opt_wmme_buffer_bits = atoi(arg);
7062         return 0;
7063 }
7064 static inline int parse_opt_wmme_buffer_num(const char *arg)
7065 {
7066         /* --opt_wmme_buffer_num */
7067         if (!arg) return 0;
7068         opt_wmme_buffer_num = atoi(arg);
7069         return 0;
7070 }
7071 #endif
7072 #ifdef AU_WDMKS
7073 static inline int parse_opt_wdmks_device_id(const char *arg)
7074 {
7075         /* --wdmks-device-id */
7076         if (!arg) return 0;
7077         opt_wdmks_device_id = atoi(arg);
7078         return 0;
7079 }
7080 static inline int parse_opt_wdmks_latency(const char *arg)
7081 {
7082         /* --wdmks-latency */
7083         if (!arg) return 0;
7084         opt_wdmks_latency = atoi(arg);
7085         return 0;
7086 }
7087 static inline int parse_opt_wdmks_format_ext(const char *arg)
7088 {
7089         /* --wdmks-format_ext */
7090         if (!arg) return 0;
7091         opt_wdmks_format_ext = atoi(arg);
7092         return 0;
7093 }
7094 static inline int parse_opt_wdmks_polling(const char *arg)
7095 {
7096         /* --wdmks-polling */
7097         if (!arg) return 0;
7098         opt_wdmks_polling = atoi(arg);
7099         return 0;
7100 }
7101 static inline int parse_opt_wdmks_thread_priority(const char *arg)
7102 {
7103         /* --wdmks-thread-priority */
7104         if (!arg) return 0;
7105         opt_wdmks_thread_priority = atoi(arg);
7106         return 0;
7107 }
7108 static inline int parse_opt_wdmks_pin_priority(const char *arg)
7109 {
7110         /* --wdmks-pin-priority */
7111         if (!arg) return 0;
7112         opt_wdmks_pin_priority = atoi(arg);
7113         return 0;
7114 }
7115 static inline int parse_opt_wdmks_rt_priority(const char *arg)
7116 {
7117         /* --wdmks-rt-priority */
7118         if (!arg) return 0;
7119         opt_wdmks_rt_priority = atoi(arg);
7120         return 0;
7121 }
7122 #endif
7123 #ifdef AU_WASAPI
7124 static inline int parse_opt_wasapi_device_id(const char *arg)
7125 {
7126         /* --wasapi-device-id */
7127         if (!arg) return 0;
7128         opt_wasapi_device_id = atoi(arg);
7129         return 0;
7130 }
7131 static inline int parse_opt_wasapi_latency(const char *arg)
7132 {
7133         /* --wasapi-latency */
7134         if (!arg) return 0;
7135         opt_wasapi_latency = atoi(arg);
7136         return 0;
7137 }
7138 static inline int parse_opt_wasapi_format_ext(const char *arg)
7139 {
7140         /* --wasapi-format_ext */
7141         if (!arg) return 0;
7142         opt_wasapi_format_ext = atoi(arg);
7143         return 0;
7144 }
7145 static inline int parse_opt_wasapi_exclusive(const char *arg)
7146 {
7147         /* --wasapi-exclusive */
7148         if (!arg) return 0;
7149         opt_wasapi_exclusive = atoi(arg);
7150         return 0;
7151 }
7152 static inline int parse_opt_wasapi_polling(const char *arg)
7153 {
7154         /* --wasapi-polling */
7155         if (!arg) return 0;
7156         opt_wasapi_polling = atoi(arg);
7157         return 0;
7158 }
7159 static inline int parse_opt_wasapi_priority(const char *arg)
7160 {
7161         /* --wasapi-priority */
7162         if (!arg) return 0;
7163         opt_wasapi_priority = atoi(arg);
7164         return 0;
7165 }
7166 static inline int parse_opt_wasapi_stream_category(const char *arg)
7167 {
7168         /* --wasapi-stream-category */
7169         if (!arg) return 0;
7170         opt_wasapi_stream_category = atoi(arg);
7171         return 0;
7172 }
7173 static inline int parse_opt_wasapi_stream_option(const char *arg)
7174 {
7175         /* --wasapi-stream-option */
7176         if (!arg) return 0;
7177         opt_wasapi_stream_option = atoi(arg);
7178         return 0;
7179 }
7180 #endif
7181 ///r
7182 #ifdef AU_PORTAUDIO
7183 static inline int parse_opt_pa_wmme_device_id(const char *arg)
7184 {
7185         /* --pa-wmme-device-id */
7186         if (!arg) return 0;
7187         opt_pa_wmme_device_id = atoi(arg);
7188         return 0;
7189 }
7190 static inline int parse_opt_pa_ds_device_id(const char *arg)
7191 {
7192         /* --pa-ds-device */
7193         if (!arg) return 0;
7194         opt_pa_ds_device_id = atoi(arg);
7195         return 0;
7196 }
7197 static inline int parse_opt_pa_asio_device_id(const char *arg)
7198 {
7199         /* --pa-asio-device-id */
7200         if (!arg) return 0;
7201         opt_pa_asio_device_id = atoi(arg);
7202         return 0;
7203 }
7204 #ifdef PORTAUDIO_V19
7205 static inline int parse_opt_pa_wdmks_device_id(const char *arg)
7206 {
7207         /* --pa-wdmks-device */
7208         if (!arg) return 0;
7209         opt_pa_wdmks_device_id = atoi(arg);
7210         return 0;
7211 }
7212 static inline int parse_opt_pa_wasapi_device_id(const char *arg)
7213 {
7214         /* --pa-wasapi-device-id */
7215         if (!arg) return 0;
7216         opt_pa_wasapi_device_id = atoi(arg);
7217         return 0;
7218 }
7219 static inline int parse_opt_pa_wasapi_flag(const char *arg)
7220 {
7221         /* --pa-wasapi-flag */
7222         if (!arg) return 0;
7223         opt_pa_wasapi_flag = atoi(arg);
7224         return 0;
7225 }
7226 static inline int parse_opt_pa_wasapi_stream_category(const char *arg)
7227 {
7228         /* --pa-wasapi-stream-category */
7229         if (!arg) return 0;
7230         opt_pa_wasapi_stream_category = atoi(arg);
7231         return 0;
7232 }
7233 static inline int parse_opt_pa_wasapi_stream_option(const char *arg)
7234 {
7235         /* --pa-wasapi-stream-option */
7236         if (!arg) return 0;
7237         opt_pa_wasapi_stream_option = atoi(arg);
7238         return 0;
7239 }
7240 #endif
7241 #endif
7242
7243
7244 extern void wave_set_option_extensible(int);
7245 extern void wave_set_option_update_step(int);
7246
7247 static inline int parse_opt_wave_extensible(const char *arg)
7248 {
7249         wave_set_option_extensible(y_or_n_p(arg));
7250         return 0;
7251 }
7252
7253 static inline int parse_opt_wave_update_step(const char *arg)
7254 {
7255         wave_set_option_update_step(arg ? atoi(arg) : 0);
7256         return 0;
7257 }
7258
7259
7260 #ifdef AU_FLAC
7261
7262 static inline int parse_opt_flac_verify(const char *arg)
7263 {
7264         flac_set_option_verify(1);
7265         return 0;
7266 }
7267
7268 static inline int parse_opt_flac_padding(const char *arg)
7269 {
7270         if (!arg) return 0;
7271         flac_set_option_padding(atoi(arg));
7272         return 0;
7273 }
7274
7275 static inline int parse_opt_flac_complevel(const char *arg)
7276 {
7277         if (!arg) return 0;
7278         flac_set_compression_level(atoi(arg));
7279         return 0;
7280 }
7281
7282 #ifdef AU_OGGFLAC
7283 extern void flac_set_option_oggflac(int);
7284
7285 static inline int parse_opt_flac_oggflac(const char *arg)
7286 {
7287         flac_set_option_oggflac(1);
7288         return 0;
7289 }
7290 #endif /* AU_OGGFLAC */
7291 #endif /* AU_FLAC */
7292
7293 #ifdef AU_OPUS
7294 extern void opus_set_nframes(int);
7295 extern void opus_set_bitrate(int);
7296 extern void opus_set_complexity(int);
7297 extern void opus_set_vbr(int);
7298 extern void opus_set_cvbr(int);
7299
7300 static inline int parse_opt_opus_nframes(const char *arg)
7301 {
7302         opus_set_nframes(arg ? atoi(arg) : 1920);
7303         return 0;
7304 }
7305
7306 static inline int parse_opt_opus_bitrate(const char *arg)
7307 {
7308         opus_set_bitrate(arg ? atoi(arg) : 128);
7309         return 0;
7310 }
7311
7312 static inline int parse_opt_opus_complexity(const char *arg)
7313 {
7314         opus_set_complexity(arg ? atoi(arg) : 10);
7315         return 0;
7316 }
7317
7318 static inline int parse_opt_opus_vbr(const char *arg)
7319 {
7320         opus_set_vbr(y_or_n_p(arg));
7321         return 0;
7322 }
7323
7324 static inline int parse_opt_opus_cvbr(const char *arg)
7325 {
7326         opus_set_cvbr(y_or_n_p(arg));
7327         return 0;
7328 }
7329 #endif /* AU_OPUS */
7330
7331 #ifdef AU_SPEEX
7332 extern void speex_set_option_quality(int);
7333 extern void speex_set_option_vbr(int);
7334 extern void speex_set_option_abr(int);
7335 extern void speex_set_option_vad(int);
7336 extern void speex_set_option_dtx(int);
7337 extern void speex_set_option_complexity(int);
7338 extern void speex_set_option_nframes(int);
7339
7340 static inline int parse_opt_speex_quality(const char *arg)
7341 {
7342         if (!arg) return 0;
7343         speex_set_option_quality(atoi(arg));
7344         return 0;
7345 }
7346
7347 static inline int parse_opt_speex_vbr(const char *arg)
7348 {
7349         speex_set_option_vbr(1);
7350         return 0;
7351 }
7352
7353 static inline int parse_opt_speex_abr(const char *arg)
7354 {
7355         if (!arg) return 0;
7356         speex_set_option_abr(atoi(arg));
7357         return 0;
7358 }
7359
7360 static inline int parse_opt_speex_vad(const char *arg)
7361 {
7362         speex_set_option_vad(1);
7363         return 0;
7364 }
7365
7366 static inline int parse_opt_speex_dtx(const char *arg)
7367 {
7368         speex_set_option_dtx(1);
7369         return 0;
7370 }
7371
7372 static inline int parse_opt_speex_complexity(const char *arg)
7373 {
7374         if (!arg) return 0;
7375         speex_set_option_complexity(atoi(arg));
7376         return 0;
7377 }
7378
7379 static inline int parse_opt_speex_nframes(const char *arg)
7380 {
7381         if (!arg) return 0;
7382         speex_set_option_nframes(atoi(arg));
7383         return 0;
7384 }
7385 #endif /* AU_SPEEX */
7386
7387 static inline int parse_opt_o(const char *arg)
7388 {
7389         if (!arg) return 0;
7390         safe_free(opt_output_name);
7391         opt_output_name = safe_strdup(url_expand_home_dir(arg));
7392         return 0;
7393 }
7394
7395 static inline int parse_opt_P(const char *arg)
7396 {
7397         /* set overriding instrument */
7398         if (!arg) return 0;
7399         strncpy(def_instr_name, arg, sizeof(def_instr_name) - 1);
7400         def_instr_name[sizeof(def_instr_name) - 1] = '\0';
7401         return 0;
7402 }
7403
7404 static inline int parse_opt_p(const char *arg)
7405 {
7406         if (!arg) return 0;
7407 #if 1 //def __W32__
7408         // safe_calloc() sizeof(Voice) < MAX_SAFE_MALLOC_SIZE \82Å\82 \82ê\82Î\82¢\82¢\82Í\82¸ (win\88È\8aO\82Í\95s\96¾
7409         if (set_value(&voices, atoi(arg), 1, MAX_VOICES, "Polyphony"))
7410                 return 1;
7411 #else
7412         if (set_value(&voices, atoi(arg), 1,
7413                         MAX_SAFE_MALLOC_SIZE / sizeof(Voice), "Polyphony"))
7414                 return 1;
7415 #endif
7416         max_voices = voices;
7417         return 0;
7418 }
7419
7420 static inline int parse_opt_p1(const char *arg)
7421 {
7422         /* --[no-]polyphony-reduction */
7423         if (!arg) arg = "n";
7424         auto_reduce_polyphony = y_or_n_p(arg);
7425         return 0;
7426 }
7427
7428 static inline int parse_opt_Q(const char *arg)
7429 {
7430         const char *p = arg;
7431         if (!arg) return 0;
7432         
7433         if (strchr(arg, 't'))
7434                 /* backward compatibility */
7435                 return parse_opt_Q1(arg);
7436         if (set_channel_flag(&quietchannels, atoi(arg), "Quiet channel"))
7437                 return 1;
7438         while ((p = strchr(p, ',')) != NULL)
7439                 if (set_channel_flag(&quietchannels, atoi(++p), "Quiet channel"))
7440                         return 1;
7441         return 0;
7442 }
7443
7444 static inline int parse_opt_Q1(const char *arg)
7445 {
7446         /* --temper-mute */
7447         int prog;
7448         const char *p = arg;
7449         if (!arg) return 0;
7450         
7451         if (set_value(&prog, atoi(arg), 0, 7, "Temperament program number"))
7452                 return 1;
7453         temper_type_mute |= 1 << prog;
7454         while ((p = strchr(p, ',')) != NULL) {
7455                 if (set_value(&prog, atoi(++p), 0, 7, "Temperament program number"))
7456                         return 1;
7457                 temper_type_mute |= 1 << prog;
7458         }
7459         return 0;
7460 }
7461
7462 static inline int parse_opt_preserve_silence(const char *arg)
7463 {
7464         opt_preserve_silence = 1;
7465         return 0;
7466 }
7467
7468 static inline int parse_opt_q(const char *arg)
7469 {
7470         char *max_buff = NULL;
7471         char *fill_buff = NULL;
7472         if (!arg) return 0;
7473         max_buff = safe_strdup(arg);
7474         fill_buff = strchr(max_buff, '/');
7475         
7476         if (fill_buff != max_buff) {
7477                 safe_free(opt_aq_max_buff);
7478                 opt_aq_max_buff = max_buff;
7479         }
7480         if (fill_buff) {
7481                 safe_free(opt_aq_fill_buff);
7482                 *fill_buff = '\0';
7483                 opt_aq_fill_buff = safe_strdup(++fill_buff);
7484         }
7485         return 0;
7486 }
7487
7488 static inline int parse_opt_R(const char *arg)
7489 {
7490         /* I think pseudo reverb can now be retired... Computers are
7491          * enough fast to do a full reverb, don't they?
7492          */
7493         if (!arg) arg = "-1";
7494         if (atoi(arg) == -1)    /* reset */
7495                 modify_release = 0;
7496         else {
7497                 if (set_val_i32(&modify_release, atoi(arg), 0, MAX_MREL,
7498                                 "Modify Release"))
7499                         return 1;
7500                 if (modify_release == 0)
7501                         modify_release = DEFAULT_MREL;
7502         }
7503         return 0;
7504 }
7505 ///r
7506 static inline int parse_opt_r(const char *arg)
7507 {
7508         safe_free(opt_reduce_quality_threshold);
7509         opt_reduce_quality_threshold = safe_strdup((arg) ? arg : DEFAULT_REDUCE_QUALITY_THRESHOLD);
7510 //    reduce_quality_threshold = atoi(arg);
7511         return 0;
7512 }
7513
7514 static inline int parse_opt_add_play_time(const char *arg)
7515 {
7516         if (!arg) return 0;
7517         add_play_time = atoi(arg);
7518         if(add_play_time < 0)
7519                 add_play_time = 0;
7520         return 0;
7521 }
7522
7523 static inline int parse_opt_add_silent_time(const char *arg)
7524 {
7525         if (!arg) return 0;
7526         add_silent_time = atoi(arg);
7527         if(add_silent_time < 0)
7528                 add_silent_time = 0;
7529         return 0;
7530 }
7531
7532 static inline int parse_opt_emu_delay_time(const char *arg)
7533 {
7534         if (!arg) return 0;
7535         emu_delay_time = atoi(arg);
7536         if(emu_delay_time < 0)
7537                 emu_delay_time = 0;
7538         return 0;
7539 }
7540 ///r
7541 static inline int parse_opt_mix_envelope(const char *arg)
7542 {
7543         if (!arg) return 0;
7544         opt_mix_envelope = atoi(arg);
7545         if (opt_mix_envelope < 0)
7546                 opt_mix_envelope = 0;
7547         return 0;
7548 }
7549
7550 static inline int parse_opt_modulation_update(const char *arg)
7551 {
7552         if (!arg) return 0;
7553         opt_modulation_update = atoi(arg);
7554         if (opt_modulation_update < 0)
7555                 opt_modulation_update = 0;
7556         return 0;
7557 }
7558
7559 static inline int parse_opt_cut_short_time(const char *arg)
7560 {
7561         if (!arg) return 0;
7562         opt_cut_short_time = atoi(arg);
7563         if (opt_cut_short_time < 0)
7564                 opt_cut_short_time = 0;
7565         return 0;
7566 }
7567
7568 static inline int parse_opt_limiter(const char *arg)
7569 {
7570         if (!arg) return 0;
7571         opt_limiter = atoi(arg);
7572         if (opt_limiter < 0)
7573                 opt_limiter = 0;
7574         return 0;
7575 }
7576
7577 static inline int parse_opt_compute_thread_num(const char *arg)
7578 {
7579         if (!arg) return 0;
7580         compute_thread_num = atoi(arg);
7581         return 0;
7582 }
7583
7584 static inline int parse_opt_trace_mode_update(const char *arg)
7585 {
7586 #ifdef USE_TRACE_TIMER  
7587         if (!arg) return 0;
7588         trace_mode_update_time = atoi(arg);
7589         set_trace_mode_update_time();
7590 #endif
7591         return 0;
7592 }
7593
7594 static inline int parse_opt_load_all_instrument(const char *arg)
7595 {
7596         if (!arg) return 0;
7597         opt_load_all_instrument = atoi(arg);
7598         return 0;
7599 }
7600
7601 static inline int parse_opt_int_synth_rate(const char *arg)
7602 {
7603         if (!arg) return 0;
7604         opt_int_synth_rate = atoi(arg);
7605         return 0;
7606 }
7607 static inline int parse_opt_int_synth_sine(const char *arg)
7608 {
7609         if (!arg) return 0;
7610         opt_int_synth_sine = atoi(arg);
7611         return 0;
7612 }
7613 static inline int parse_opt_int_synth_update(const char *arg)
7614 {
7615         if (!arg) return 0;
7616         opt_int_synth_update = atoi(arg);
7617         return 0;
7618 }
7619
7620 #ifdef SUPPORT_LOOPEVENT
7621 static inline int parse_opt_midi_loop_repeat(const char *arg)
7622 {
7623         opt_midi_loop_repeat = (arg) ? atoi(arg) : 0;
7624         opt_use_midi_loop_repeat = (opt_midi_loop_repeat >= 1) ? 1 : 0;
7625         return 0;
7626 }
7627
7628 static inline int parse_opt_midi_loop_filter(const char *arg)
7629 {
7630     int err = 0;
7631     if (!arg) return err;
7632     opt_use_midi_loop_repeat = 0;
7633
7634     while (*arg) {
7635         switch (*arg) {
7636         case '0':
7637             /* clear */
7638             opt_use_midi_loop_repeat = 0;
7639             break;
7640
7641         case '1':
7642             /* Control change #111 */
7643             opt_use_midi_loop_repeat |= LF_CC111_TO_EOT;
7644             break;
7645
7646         case 'm':
7647             /* Mark `A'-`B'*/
7648             opt_use_midi_loop_repeat |= LF_MARK_A_TO_B;
7649             break;
7650
7651         case 'l':
7652             /* Mark `Loop_Start'-`Loop_End'*/
7653             opt_use_midi_loop_repeat |= LF_MARK_S_TO_E;
7654             break;
7655
7656         case '2':
7657             /* Control change #2 - CC#4 */
7658             opt_use_midi_loop_repeat |= LF_CC2_TO_CC4;
7659             break;
7660
7661         default:
7662             ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
7663                       "--loop-filter: Illegal mode `%c'", *arg);
7664             err++;
7665             break;
7666         }
7667         arg++;
7668     }
7669     return err;
7670 }
7671 #endif /* SUPPORT_LOOPEVENT */
7672
7673 static inline int parse_opt_od_level_gs(const char *arg)
7674 {
7675         int level;
7676         if (set_value(&level, arg ? atoi(arg) : 100, 1, 400, "GS OD Level adjust"))
7677                 return 1;
7678         otd.gsefx_CustomODLv = level / 100.0;
7679         return 0;
7680 }
7681
7682 static inline int parse_opt_od_drive_gs(const char *arg)
7683 {
7684         int level;
7685         if (set_value(&level, arg ? atoi(arg) : 100, 1, 400, "GS OD Drive adjust"))
7686                 return 1;
7687         otd.gsefx_CustomODDrive = level / 100.0;
7688         return 0;
7689 }
7690
7691 static inline int parse_opt_od_level_xg(const char *arg)
7692 {
7693         int level;
7694         if (set_value(&level, arg ? atoi(arg) : 100, 1, 400, "XG OD Level adjust"))
7695                 return 1;
7696         otd.xgefx_CustomODLv = level / 100.0;
7697         return 0;
7698 }
7699
7700 static inline int parse_opt_od_drive_xg(const char *arg)
7701 {
7702         int level;
7703         if (set_value(&level, arg ? atoi(arg) : 100, 1, 400, "XG OD Drive adjust"))
7704                 return 1;
7705         otd.xgefx_CustomODDrive = level / 100.0;
7706         return 0;
7707 }
7708
7709 static inline int parse_opt_od_level_sd(const char *arg)
7710 {
7711     int level;
7712     if (set_value(&level, arg ? atoi(arg) : 100, 1, 400, "SD OD Level adjust"))
7713         return 1;
7714     otd.sdefx_CustomODLv = level / 100.0;
7715     return 0;
7716 }
7717
7718 static inline int parse_opt_od_drive_sd(const char *arg)
7719 {
7720     int level;
7721     if (set_value(&level, arg ? atoi(arg) : 100, 1, 400, "SD OD Drive adjust"))
7722         return 1;
7723     otd.sdefx_CustomODDrive = level / 100.0;
7724     return 0;
7725 }
7726
7727 #ifdef __W32__
7728 static inline int parse_opt_process_priority(const char *arg)
7729 {
7730         processPriority = (arg) ? (DWORD)atoi(arg) : NORMAL_PRIORITY_CLASS;
7731         return 0;
7732 }
7733
7734 #if !(defined(IA_W32G_SYN) || defined(WINDRV))
7735 static inline int parse_opt_player_thread_priority(const char *arg)
7736 {
7737         PlayerThreadPriority = (arg) ? (DWORD)atoi(arg) : THREAD_PRIORITY_NORMAL;
7738         return 0;
7739 }
7740
7741 static inline int parse_opt_GUI_thread_priority(const char *arg)
7742 {
7743         GUIThreadPriority = (arg) ? (DWORD)atoi(arg) : THREAD_PRIORITY_NORMAL;
7744         return 0;
7745 }
7746 #endif /* !IA_W32G_SYN */
7747 #endif /* __W32__ */
7748
7749 static inline int parse_opt_S(const char *arg)
7750 {
7751         int suffix;
7752         int32 figure;
7753         if (!arg) return 0;
7754         suffix = arg[strlen(arg) - 1];
7755         
7756         switch (suffix) {
7757         case 'M':
7758         case 'm':
7759                 figure = 1 << 20;
7760                 break;
7761         case 'K':
7762         case 'k':
7763                 figure = 1 << 10;
7764                 break;
7765         default:
7766                 figure = 1;
7767                 break;
7768         }
7769         allocate_cache_size = atof(arg) * figure;
7770         return 0;
7771 }
7772
7773 static inline int parse_opt_s(const char *arg)
7774 {
7775         /* sampling rate */
7776         int32 freq;
7777
7778         if (!arg) arg = "44.1";
7779         if ((freq = atoi(arg)) < 1000)
7780                 freq = atof(arg) * 1000 + 0.5;
7781         return set_val_i32(&opt_output_rate, freq, MIN_OUTPUT_RATE, MAX_OUTPUT_RATE, "Resampling frequency");
7782 }
7783
7784 static inline int parse_opt_T(const char *arg)
7785 {
7786         /* tempo adjust */
7787         int adjust;
7788         
7789         if (!arg) arg = "100";
7790         if (set_value(&adjust, atoi(arg), 10, 400, "Tempo adjust"))
7791                 return 1;
7792         tempo_adjust = 100.0 / adjust;
7793         return 0;
7794 }
7795
7796 static inline int parse_opt_t(const char *arg)
7797 {
7798         if (!arg) arg = OUTPUT_TEXT_CODE;
7799         safe_free(output_text_code);
7800         output_text_code = safe_strdup(arg);
7801         return 0;
7802 }
7803
7804 static inline int parse_opt_U(const char *arg)
7805 {
7806         if (!arg) arg = "n";
7807         free_instruments_afterwards = y_or_n_p(arg);
7808         return 0;
7809 }
7810
7811 #if defined(AU_VOLUME_CALC)
7812 static inline int parse_opt_volume_calc_rms(const char *arg)
7813 {
7814         opt_volume_calc_rms = 1;
7815         return 0;
7816 }
7817
7818 static inline int parse_opt_volume_calc_trim(const char *arg)
7819 {
7820         opt_volume_calc_trim = 1;
7821         return 0;
7822 }
7823 #endif /* AU_VOLUME_CALC */
7824
7825 ///r
7826 static inline int parse_opt_volume_curve(const char *arg)
7827 {
7828         if (!arg) return 0;
7829         if (!(atof(arg) >= 0)) {
7830                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Volume curve power must be >= 0", *arg);
7831                 return 1;
7832         }
7833         if (atof(arg) != 0) {
7834                 init_user_vol_table(atof(arg));
7835                 opt_user_volume_curve = atof(arg);
7836         }else
7837                 opt_user_volume_curve = 0;
7838         return 0;
7839 }
7840
7841 __attribute__((noreturn))
7842 static inline int parse_opt_v(const char *arg)
7843 {
7844         const char *version_list[] = {
7845 #if defined(__BORLANDC__) || defined(__MRC__) || defined(__WATCOMC__) ||  defined(__DMC__)
7846                 "TiMidity++ ",
7847                                 "",
7848                                 NULL, NLS,
7849                 NLS,
7850 #else
7851                 "TiMidity++ ",
7852                                 (strcmp(timidity_version, "current")) ? "version " : "",
7853                                 timidity_version, " ", arch_string, NLS,
7854                 NLS,
7855 #endif
7856                 "Copyright (C) 1999-2018 Masanao Izumo <iz@onicos.co.jp>", NLS,
7857                 "Copyright (C) 1995 Tuukka Toivonen <tt@cgs.fi>", NLS,
7858                 NLS,
7859 #ifdef __W32__
7860                 "Win32 version by Davide Moretti <dmoretti@iper.net>", NLS,
7861                 "              and Daisuke Aoki <dai@y7.net>", NLS,
7862                 NLS,
7863 #endif  /* __W32__ */
7864                 "This program is distributed in the hope that it will be useful,", NLS,
7865                 "but WITHOUT ANY WARRANTY; without even the implied warranty of", NLS,
7866                 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the", NLS,
7867                 "GNU General Public License for more details.", NLS,
7868         };
7869         FILE *fp = open_pager();
7870         int i;
7871
7872 #if defined(__BORLANDC__) || defined(__MRC__)
7873         if (strcmp(timidity_version, "current"))
7874                 version_list[1] = "version ";
7875         version_list[2] = timidity_version;
7876 #endif
7877         for (i = 0; i < sizeof(version_list) / sizeof(char *); i++)
7878                 fputs(version_list[i], fp);
7879         close_pager(fp);
7880         exit(EXIT_SUCCESS);
7881 }
7882
7883 static inline int parse_opt_W(const char *arg)
7884 {
7885         WRDTracer *wlp, **wlpp;
7886         
7887         if (!arg) arg = "";
7888         if (*arg == 'R') {      /* for WRD reader options */
7889                 put_string_table(&wrd_read_opts, arg + 1, strlen(arg + 1));
7890                 return 0;
7891         }
7892         for (wlpp = wrdt_list; (wlp = *wlpp) != NULL; wlpp++)
7893                 if (wlp->id == *arg) {
7894                         wrdt = wlp;
7895                         safe_free(wrdt_open_opts);
7896                         wrdt_open_opts = safe_strdup(arg + 1);
7897                         return 0;
7898                 }
7899         ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
7900                         "WRD Tracer `%c' is not compiled in.", *arg);
7901         return 1;
7902 }
7903
7904 #ifdef __W32__
7905 static inline int parse_opt_w(const char *arg)
7906 {
7907         if (!arg) arg = "";
7908         switch (*arg) {
7909 #ifdef SMFCONV
7910         case 'r':
7911                 opt_rcpcv_dll = 1;
7912                 return 0;
7913         case 'R':
7914                 opt_rcpcv_dll = 0;
7915                 return 0;
7916 #else
7917         case 'r':
7918         case 'R':
7919                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
7920                                 "-w%c option is not supported", *arg);
7921                 return 1;
7922 #endif  /* SMFCONV */
7923         default:
7924                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "-w: Illegal mode `%c'", *arg);
7925                 return 1;
7926         }
7927 }
7928 #endif  /* __W32__ */
7929
7930 static inline int parse_opt_x(const char *arg)
7931 {
7932         StringTableNode *st;
7933         
7934         if (!arg) arg = "";
7935         if ((st = put_string_table(&opt_config_string,
7936                         arg, strlen(arg))) != NULL)
7937                 expand_escape_string(st->string);
7938         return 0;
7939 }
7940 ///r
7941 static inline int parse_opt_Y(const char *arg)
7942 {
7943         /* --compute-buffer */
7944         if (!arg) return 0;
7945         if (set_value(&opt_compute_buffer_bits, atoi(arg), -5, AUDIO_BUFFER_BITS - 1,
7946                         "Compute Buffer (bit)"))
7947                 return 1;
7948         return 0;
7949 }
7950
7951 static inline void expand_escape_string(char *s)
7952 {
7953         char *t = s;
7954         
7955         if (s == NULL)
7956                 return;
7957         for (t = s; *s; s++)
7958                 if (*s == '\\') {
7959                         switch (*++s) {
7960                         case 'a':
7961                                 *t++ = '\a';
7962                                 break;
7963                         case 'b':
7964                                 *t++ = '\b';
7965                                 break;
7966                         case 't':
7967                                 *t++ = '\t';
7968                                 break;
7969                         case 'n':
7970                                 *t++ = '\n';
7971                                 break;
7972                         case 'f':
7973                                 *t++ = '\f';
7974                                 break;
7975                         case 'v':
7976                                 *t++ = '\v';
7977                                 break;
7978                         case 'r':
7979                                 *t++ = '\r';
7980                                 break;
7981                         case '\\':
7982                                 *t++ = '\\';
7983                                 break;
7984                         default:
7985                                 if (! (*t++ = *s))
7986                                         return;
7987                                 break;
7988                         }
7989                 } else
7990                         *t++ = *s;
7991         *t = *s;
7992 }
7993
7994 static inline int parse_opt_Z(const char *arg)
7995 {
7996         /* load frequency table */
7997         return load_table(arg);
7998 }
7999
8000 static inline int parse_opt_Z1(const char *arg)
8001 {
8002         /* --pure-intonation */
8003         int keysig;
8004         
8005         opt_pure_intonation = 1;
8006         if (*arg) {
8007                 if (set_value(&keysig, atoi(arg), -7, 7,
8008                                 "Initial keysig (number of #(+)/b(-)[m(minor)])"))
8009                         return 1;
8010                 opt_init_keysig = keysig;
8011                 if (strchr(arg, 'm'))
8012                         opt_init_keysig += 16;
8013         }
8014         return 0;
8015 }
8016
8017 static inline int parse_opt_default_module(const char *arg)
8018 {
8019         if (!arg) return 0;
8020         opt_default_module = atoi(arg);
8021         if (opt_default_module < 0)
8022                 opt_default_module = 0;
8023         return 0;
8024 }
8025
8026
8027 __attribute__((noreturn))
8028 static inline int parse_opt_fail(const char *arg)
8029 {
8030         /* getopt_long failed to recognize any options */
8031         ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
8032                         "Could not understand option : try --help");
8033         exit(EXIT_FAILURE);
8034 }
8035
8036 static inline int set_value(int *param, int i, int low, int high, const char *name)
8037 {
8038         int32 val;
8039         
8040         if (set_val_i32(&val, i, low, high, name))
8041                 return 1;
8042         *param = val;
8043         return 0;
8044 }
8045
8046 static inline int set_val_i32(int32 *param,
8047                 int32 i, int32 low, int32 high, const char *name)
8048 {
8049         if (i < low || i > high) {
8050                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
8051                                 "%s must be between %ld and %ld", name, low, high);
8052                 return 1;
8053         }
8054         *param = i;
8055         return 0;
8056 }
8057
8058 static int parse_val_float_t(FLOAT_T *param,
8059                 const char *arg, FLOAT_T low, FLOAT_T high, const char *name)
8060 {
8061         FLOAT_T value;
8062         char *errp;
8063
8064         value = strtod(arg, &errp);
8065         if (arg == errp) {
8066                 /* only when nothing was parsed */
8067                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Invalid %s", name);
8068                 return 1;
8069         }
8070         return set_val_float_t(param, value, low, high, name);
8071 }
8072
8073 static inline int set_val_float_t(FLOAT_T *param,
8074                 FLOAT_T i, FLOAT_T low, FLOAT_T high, const char *name)
8075 {
8076         if (i < low || i > high) {
8077                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
8078                                 "%s must be between %.1f and %.1f", name, low, high);
8079                 return 1;
8080         }
8081         *param = i;
8082         return 0;
8083 }
8084
8085 static inline int set_channel_flag(ChannelBitMask *flags, int32 i, const char *name)
8086 {
8087         if (i == 0) {
8088                 FILL_CHANNELMASK(*flags);
8089                 return 0;
8090         } else if (abs(i) > MAX_CHANNELS) {
8091                 ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
8092                                 "%s must be between (-)1 and (-)%d, or 0",
8093                                                 name, MAX_CHANNELS);
8094                 return 1;
8095         }
8096         if (i > 0)
8097                 SET_CHANNELMASK(*flags, i - 1);
8098         else
8099                 UNSET_CHANNELMASK(*flags, -i - 1);
8100         return 0;
8101 }
8102
8103 static inline int y_or_n_p(const char *arg)
8104 {
8105         if (arg) {
8106                 switch (arg[0]) {
8107                 case 'y':
8108                 case 'Y':
8109                 case 't':
8110                 case 'T':
8111                         return 1;
8112                 case 'n':
8113                 case 'N':
8114                 case 'f':
8115                 case 'F':
8116                 default:
8117                         return 0;
8118                 }
8119         } else
8120                 return 1;
8121 }
8122
8123 static inline int set_flag(int32 *fields, int32 bitmask, const char *arg)
8124 {
8125         if (y_or_n_p(arg))
8126                 *fields |= bitmask;
8127         else
8128                 *fields &= ~bitmask;
8129         return 0;
8130 }
8131
8132 static inline FILE *open_pager(void)
8133 {
8134 #if ! defined(__MACOS__) && defined(HAVE_POPEN) && defined(HAVE_ISATTY) \
8135                 && ! defined(IA_W32GUI) && ! defined(IA_W32G_SYN)
8136         char *pager;
8137         
8138         if (isatty(1) && (pager = getenv("PAGER")) != NULL)
8139                 return popen(pager, "w");
8140 #endif
8141         return stdout;
8142 }
8143
8144 static inline void close_pager(FILE *fp)
8145 {
8146 #if ! defined(__MACOS__) && defined(HAVE_POPEN) && defined(HAVE_ISATTY) \
8147                 && ! defined(IA_W32GUI) && ! defined(IA_W32G_SYN)
8148         if (fp != stdout)
8149                 pclose(fp);
8150 #endif
8151 }
8152
8153 static void interesting_message(void)
8154 {
8155         printf(
8156 "TiMidity++ %s%s %s -- MIDI to WAVE converter and player" NLS
8157 "Copyright (C) 1999-2018 Masanao Izumo <iz@onicos.co.jp>" NLS
8158 "Copyright (C) 1995 Tuukka Toivonen <tt@cgs.fi>" NLS
8159                         NLS
8160 #ifdef __W32__
8161 "Win32 version by Davide Moretti <dmoretti@iper.net>" NLS
8162 "              and Daisuke Aoki <dai@y7.net>" NLS
8163                         NLS
8164 #endif /* __W32__ */
8165 "This program is free software; you can redistribute it and/or modify" NLS
8166 "it under the terms of the GNU General Public License as published by" NLS
8167 "the Free Software Foundation; either version 2 of the License, or" NLS
8168 "(at your option) any later version." NLS
8169                         NLS
8170 "This program is distributed in the hope that it will be useful," NLS
8171 "but WITHOUT ANY WARRANTY; without even the implied warranty of" NLS
8172 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the" NLS
8173 "GNU General Public License for more details." NLS
8174                         NLS
8175 "You should have received a copy of the GNU General Public License" NLS
8176 "along with this program; if not, write to the Free Software" NLS
8177 "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA" NLS
8178                         NLS, (strcmp(timidity_version, "current")) ? "version " : "",
8179                         timidity_version,
8180                         arch_string
8181                 );
8182 }
8183
8184 /* -------- functions for getopt_long ends here --------- */
8185
8186 #ifdef HAVE_SIGNAL
8187 static RETSIGTYPE sigterm_exit(int sig)
8188 {
8189     char s[4];
8190     size_t dummy;
8191
8192     /* NOTE: Here, fprintf is dangerous because it is not re-enterance
8193      * function.  It is possible coredump if the signal is called in printf's.
8194      */
8195
8196     dummy = write(2, "Terminated sig=0x", 17);
8197     s[0] = "0123456789abcdef"[(sig >> 4) & 0xf];
8198     s[1] = "0123456789abcdef"[sig & 0xf];
8199     s[2] = '\n';
8200     dummy += write(2, s, 3);
8201
8202     safe_exit(1);
8203         return 0;
8204 }
8205 #endif /* HAVE_SIGNAL */
8206
8207 static void timidity_arc_error_handler(const char *error_message)
8208 {
8209     extern int open_file_noise_mode;
8210     if(open_file_noise_mode)
8211         ctl->cmsg(CMSG_WARNING, VERB_NORMAL, "%s", error_message);
8212 }
8213
8214 static PlayMode null_play_mode = {
8215     0,                          /* rate */
8216     0,                          /* encoding */
8217     0,                          /* flag */
8218     -1,                         /* fd */
8219     {0,0,0,0,0},                /* extra_param */
8220     "Null output device",       /* id_name */
8221     '\0',                       /* id_character */
8222     NULL,                       /* open_output */
8223     NULL,                       /* close_output */
8224     NULL,                       /* output_data */
8225     NULL,                       /* acntl */
8226     NULL                        /* detect */
8227 };
8228
8229 MAIN_INTERFACE void timidity_start_initialize(void)
8230 {
8231     int i;
8232     static int drums[] = DEFAULT_DRUMCHANNELS;
8233     static int is_first = 1;
8234 #if defined(__FreeBSD__) && !defined(__alpha__)
8235     fp_except_t fpexp;
8236 #elif defined(__NetBSD__) || defined(__OpenBSD__)
8237     fp_except fpexp;
8238 #endif
8239
8240 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
8241     fpexp = fpgetmask();
8242     fpsetmask(fpexp & ~(FP_X_INV|FP_X_DZ));
8243 #endif
8244
8245     if(!output_text_code)
8246         output_text_code = safe_strdup(OUTPUT_TEXT_CODE);
8247     if(!opt_aq_max_buff)
8248         opt_aq_max_buff = safe_strdup("20.0");
8249     if(!opt_aq_fill_buff)
8250         opt_aq_fill_buff = safe_strdup("200%");
8251 ///r    
8252     if(!opt_reduce_voice_threshold)
8253         opt_reduce_voice_threshold = safe_strdup("0");
8254     if(!opt_reduce_quality_threshold)
8255         opt_reduce_quality_threshold = safe_strdup("0");
8256     if(!opt_reduce_polyphony_threshold)
8257         opt_reduce_polyphony_threshold = safe_strdup("0");
8258
8259     /* Check the byte order */
8260     i = 1;
8261 #ifdef LITTLE_ENDIAN
8262     if(*(char *)&i != 1)
8263 #else
8264     if(*(char *)&i == 1)
8265 #endif
8266     {
8267         fprintf(stderr, "Byte order is miss configured.\n");
8268         exit(1);
8269     }
8270
8271     for(i = 0; i < MAX_CHANNELS; i++)
8272     {
8273         memset(&(channel[i]), 0, sizeof(Channel));
8274     }
8275
8276     CLEAR_CHANNELMASK(quietchannels);
8277     CLEAR_CHANNELMASK(default_drumchannels);
8278
8279     for(i = 0; drums[i] > 0; i++)
8280         SET_CHANNELMASK(default_drumchannels, drums[i] - 1);
8281 #if MAX_CHANNELS > 16
8282     for(i = 16; i < MAX_CHANNELS; i++)
8283         if(IS_SET_CHANNELMASK(default_drumchannels, i & 0xF))
8284             SET_CHANNELMASK(default_drumchannels, i);
8285 #endif
8286
8287     if(program_name == NULL)
8288         program_name = "TiMidity";
8289     uudecode_unquote_html = 1;
8290 ///r
8291     for(i = 0; i < MAX_CHANNELS; i++)
8292     {
8293         default_program[i] = DEFAULT_PROGRAM;
8294         special_program[i] = -1;
8295         memset(channel[i].drums, 0, sizeof(channel[i].drums));
8296     }
8297     arc_error_handler = timidity_arc_error_handler;
8298
8299     if(play_mode == NULL) play_mode = &null_play_mode;
8300
8301     if(is_first) /* initialize once time */
8302     {
8303         got_a_configuration = 0;
8304
8305 #ifdef SUPPORT_SOCKET
8306         init_mail_addr();
8307         if(url_user_agent == NULL)
8308         {
8309             url_user_agent =
8310                 (char *)safe_malloc(10 + strlen(timidity_version));
8311             strcpy(url_user_agent, "TiMidity-");
8312             strcat(url_user_agent, timidity_version);
8313         }
8314 #endif /* SUPPORT_SOCKET */
8315
8316         for(i = 0; url_module_list[i]; i++)
8317             url_add_module(url_module_list[i]);
8318         init_string_table(&opt_config_string);
8319         init_freq_table();
8320         init_freq_table_tuning();
8321         init_freq_table_pytha();
8322         init_freq_table_meantone();
8323         init_freq_table_pureint();
8324         init_freq_table_user();
8325         init_bend_fine();
8326         init_bend_coarse();
8327         init_tables();
8328         init_gm2_pan_table();
8329         init_attack_vol_table();
8330         init_sb_vol_table();
8331         init_modenv_vol_table();
8332         init_def_vol_table();
8333         init_gs_vol_table();
8334         init_perceived_vol_table();
8335         init_gm2_vol_table();
8336 #ifdef SUPPORT_SOCKET
8337         url_news_connection_cache(URL_NEWS_CONN_CACHE);
8338 #endif /* SUPPORT_SOCKET */
8339         for(i = 0; i < NSPECIAL_PATCH; i++)
8340             special_patch[i] = NULL;
8341         init_midi_trace();
8342         int_rand(-1);   /* initialize random seed */
8343         int_rand(42);   /* the 1st number generated is not very random */
8344         ML_RegisterAllLoaders ();
8345     }
8346
8347     is_first = 0;
8348 }
8349
8350 MAIN_INTERFACE int timidity_pre_load_configuration(void)
8351 {
8352 #ifdef __W32__
8353     /* Windows */
8354     char *strp;
8355     int check;
8356     char local[FILEPATH_MAX] = { 0 };
8357     const char cfgname[] = CONFIG_FILE_NAME_P;
8358
8359 #if defined(IA_W32GUI) || defined(IA_W32G_SYN)
8360     extern char *ConfigFile;
8361     int firstStartup = 0;
8362     if (!ConfigFile[0]) {
8363         firstStartup = 1;
8364         GetWindowsDirectoryA(ConfigFile, FILEPATH_MAX - strlen(cfgname) - 1);
8365         strlcat(ConfigFile, cfgname, sizeof(local));
8366     }
8367     strlcpy(local, ConfigFile, sizeof(local) - 1);
8368     if ((check = open(local, 0)) >= 0)
8369     {
8370         close(check);
8371         if (!read_config_file(local, 0, 0)) {
8372             got_a_configuration = 1;
8373             return 0;
8374         }
8375     }
8376 #endif
8377
8378     /* First, try read configuration file which is in the
8379      * TiMidity directory.
8380      */
8381     if (GetModuleFileNameA(NULL, local, FILEPATH_MAX - strlen(cfgname) - 1))
8382     {
8383         local[FILEPATH_MAX - strlen(cfgname) - 1] = '\0';
8384         if ((strp = pathsep_strrchr(local)) != NULL)
8385         {
8386             *(strp) = '\0';
8387             strlcat(local, cfgname, sizeof(local));
8388             if ((check = open(local, 0)) >= 0)
8389             {
8390                 close(check);
8391                 if (!read_config_file(local, 0, 0)) {
8392 #if defined(IA_W32GUI) || defined(IA_W32G_SYN)
8393                     if (firstStartup) {
8394                         strlcpy(ConfigFile, local, FILEPATH_MAX - 1);
8395                     }
8396 #endif
8397                     got_a_configuration = 1;
8398                     return 0;
8399                 }
8400             }
8401         }
8402 #if !defined(IA_W32GUI) && !defined(IA_W32G_SYN)
8403         /* Next, try read system configuration file.
8404          * Default is C:\WINDOWS\TIMIDITY.CFG
8405          */
8406         GetWindowsDirectoryA(local, FILEPATH_MAX - strlen(cfgname) - 1);
8407         strlcat(local, cfgname, sizeof(local));
8408         if ((check = open(local, 0)) >= 0)
8409         {
8410             close(check);
8411             if (!read_config_file(local, 0, 0)) {
8412                 got_a_configuration = 1;
8413                 return 0;
8414             }
8415         }
8416 #endif
8417     }
8418 #else
8419     /* UNIX */
8420     if (!read_config_file(CONFIG_FILE, 0, 0))
8421         got_a_configuration = 1;
8422 #endif /* __W32__ */
8423
8424     /* Try read configuration file which is in the
8425      * $HOME (or %HOME% for DOS) directory.
8426      * Please setup each user preference in $HOME/.timidity.cfg
8427      * (or %HOME%/timidity.cfg for DOS)
8428      */
8429     if (read_user_config_file()) {
8430         ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
8431                   "Error: Syntax error in ~/." CONFIG_FILE_NAME);
8432         return 1;
8433     }
8434
8435     return 0;
8436 }
8437
8438 MAIN_INTERFACE int timidity_post_load_configuration(void)
8439 {
8440     int i, cmderr = 0;
8441
8442     /* If we're going to fork for daemon mode, we need to fork now, as
8443        certain output libraries (pulseaudio) become unhappy if initialized
8444        before forking and then being used from the child. */
8445 #ifndef WIN32
8446     if (ctl->id_character == 'A' && (ctl->flags & CTLF_DAEMONIZE))
8447     {
8448         int pid = (-1);
8449         FILE *pidf;
8450         switch (pid)
8451         {
8452             case 0:             // child is the daemon
8453                 break;
8454             case -1:            // error status return
8455                 exit(7);
8456             default:            // no error, doing well
8457                 if ((pidf = fopen( "/var/run/timidity.pid", "w" )) != NULL )
8458                 {
8459                     fprintf( pidf, "%d\n", pid );
8460                     fclose( pidf );
8461                 }
8462                 exit(0);
8463         }
8464     }
8465 #endif
8466     if(play_mode == &null_play_mode)
8467     {
8468         char *output_id;
8469
8470         output_id = getenv("TIMIDITY_OUTPUT_ID");
8471 #ifdef TIMIDITY_OUTPUT_ID
8472         if(output_id == NULL)
8473             output_id = TIMIDITY_OUTPUT_ID;
8474 #endif /* TIMIDITY_OUTPUT_ID */
8475         if(output_id != NULL)
8476         {
8477             for(i = 0; play_mode_list[i]; i++)
8478                 if(play_mode_list[i]->id_character == *output_id)
8479                 {
8480                     if (! play_mode_list[i]->detect ||
8481                         play_mode_list[i]->detect()) {
8482                         play_mode = play_mode_list[i];
8483                         break;
8484                     }
8485                 }
8486         }
8487     }
8488
8489     if (play_mode == &null_play_mode) {
8490         /* try to detect the first available device */
8491         for(i = 0; play_mode_list[i]; i++) {
8492             /* check only the devices with detect callback */
8493             if (play_mode_list[i]->detect) {
8494                 if (play_mode_list[i]->detect()) {
8495                     play_mode = play_mode_list[i];
8496                     break;
8497                 }
8498             }
8499         }
8500     }
8501
8502     if (play_mode == &null_play_mode) {
8503         fprintf(stderr, "Couldn't open output device" NLS);
8504         exit(1);
8505     }
8506     else {
8507         /* apply changes made for null play mode to actual play mode */
8508         if(null_play_mode.encoding != 0) {
8509             play_mode->encoding = apply_encoding(play_mode->encoding, null_play_mode.encoding);
8510         }
8511         if(null_play_mode.rate != 0) {
8512             play_mode->rate = null_play_mode.rate;
8513         }
8514     }
8515
8516     if(!got_a_configuration)
8517     {
8518         if(try_config_again && !read_config_file(CONFIG_FILE, 0, 0))
8519             got_a_configuration = 1;
8520     }
8521
8522     if(opt_config_string.nstring > 0)
8523     {
8524         char **config_string_list;
8525
8526         config_string_list = make_string_array(&opt_config_string);
8527         if(config_string_list != NULL)
8528         {
8529             for(i = 0; config_string_list[i]; i++)
8530             {
8531                 if(!read_config_file(config_string_list[i], 1, 0))
8532                     got_a_configuration = 1;
8533                 else
8534                     cmderr++;
8535             }
8536             free(config_string_list[0]);
8537             free(config_string_list);
8538         }
8539     }
8540
8541     if(!got_a_configuration)
8542         cmderr++;
8543     return cmderr;
8544 }
8545
8546
8547 MAIN_INTERFACE void timidity_init_player(void)
8548 {
8549     initialize_resampler_coeffs();
8550
8551     /* Allocate voice[] */
8552         free_voice_pointer();
8553         init_voice_pointer();
8554
8555     /* Set play mode parameters */
8556     if(opt_output_rate != 0)
8557                 play_mode->rate = opt_output_rate;
8558     else if(play_mode->rate == 0)
8559                 play_mode->rate = DEFAULT_RATE;
8560
8561     /* save defaults */
8562     COPY_CHANNELMASK(drumchannels, default_drumchannels);
8563     COPY_CHANNELMASK(drumchannel_mask, default_drumchannel_mask);
8564
8565 ///r //tim13-
8566     if(opt_buffer_fragments != -1)
8567     {
8568                 if(play_mode->flag & PF_BUFF_FRAGM_OPT)
8569                         play_mode->extra_param[0] = opt_buffer_fragments;
8570                 else
8571                         ctl->cmsg(CMSG_WARNING, VERB_NORMAL,
8572                                   "%s: -B option is ignored", play_mode->id_name);
8573     }
8574         else
8575                 opt_buffer_fragments = 64;
8576
8577 ///r //tim13-
8578     if(opt_audio_buffer_bits != -1)
8579     {
8580         audio_buffer_bits = opt_audio_buffer_bits;
8581     }
8582     else
8583         opt_audio_buffer_bits = audio_buffer_bits;
8584
8585     if(opt_compute_buffer_bits != -128)
8586     {
8587         compute_buffer_bits = opt_compute_buffer_bits;
8588     }
8589     else
8590         opt_compute_buffer_bits = compute_buffer_bits;
8591
8592         init_output(); // div_playmode_rate
8593         init_playmidi();
8594         init_mix_c();   
8595 #ifdef INT_SYNTH
8596         init_int_synth();
8597 #endif // INT_SYNTH
8598 #ifdef ENABLE_DLS
8599         init_dls();
8600 #endif
8601 #ifdef ENABLE_SFZ
8602         init_sfz();
8603 #endif
8604
8605 #ifdef SUPPORT_SOUNDSPEC
8606     if(view_soundspec_flag)
8607     {
8608         open_soundspec();
8609         soundspec_setinterval(spectrogram_update_sec);
8610     }
8611 #endif /* SOUNDSPEC */
8612 }
8613
8614
8615 ///r
8616 void timidity_init_reduce_queue(void)
8617 {
8618     double time1,time2,time3,base;
8619
8620         base = aq_get_dev_queuesize() / 2;
8621         if(base <= 0)
8622                 base = AUDIO_BUFFER_SIZE;
8623
8624 #ifdef REDUCE_QUALITY_TIME_TUNING
8625         time1 = atof(opt_reduce_quality_threshold);
8626         if(time1 == 0)
8627                 time1 = 0;
8628         else if(time1 < 0)
8629                 time1 = REDUCE_QUALITY_TIME_TUNING;
8630         else if(!strchr(opt_reduce_quality_threshold, '%'))
8631                 time1 = time1 * play_mode->rate / base * 100;
8632         reduce_quality_threshold = (int)time1;
8633 #endif
8634 #ifdef REDUCE_VOICE_TIME_TUNING
8635         time2 = atof(opt_reduce_voice_threshold);
8636         if(time2 == 0)
8637                 time2 = 0;
8638         else if(time2 < 0)
8639                 time2 = REDUCE_VOICE_TIME_TUNING;
8640         else if(!strchr(opt_reduce_voice_threshold, '%'))
8641                 time2 = time2 * play_mode->rate / base * 100;
8642         reduce_voice_threshold = (int)time2;
8643 #endif
8644 #ifdef REDUCE_POLYPHONY_TIME_TUNING
8645         time3 = atof(opt_reduce_polyphony_threshold);
8646         if(time3 == 0)
8647                 time3 = 0;
8648         else if(time3 < 0)
8649                 time3 = REDUCE_POLYPHONY_TIME_TUNING;
8650         else if(!strchr(opt_reduce_polyphony_threshold, '%'))
8651                 time3 = time3 * play_mode->rate / base * 100;
8652         reduce_polyphony_threshold = (int)time3;
8653 #endif
8654 }
8655
8656 void timidity_init_aq_buff(void)
8657 {
8658     double time1, /* max buffer */
8659            time2, /* init filled */
8660            base;  /* buffer of device driver */
8661
8662     if(!IS_STREAM_TRACE)
8663         return; /* Ignore */
8664
8665     time1 = atof(opt_aq_max_buff);
8666     time2 = atof(opt_aq_fill_buff);
8667     base  = (double)aq_get_dev_queuesize() * div_playmode_rate;
8668
8669     if(strchr(opt_aq_max_buff, '%'))
8670     {
8671                 time1 = base * (time1 - 100) * DIV_100;
8672                 if(time1 < 0) time1 = 0;
8673     }
8674     if(strchr(opt_aq_fill_buff, '%'))
8675                 time2 = base * time2 * DIV_100;
8676
8677     aq_set_soft_queue(time1, time2);
8678         timidity_init_reduce_queue();
8679 }
8680
8681 MAIN_INTERFACE int timidity_play_main(int nfiles, char **files)
8682 {
8683     int need_stdin = 0, need_stdout = 0;
8684     int i;
8685     int output_fail = 0;
8686     int retval;
8687 #if (defined(__W32__) && !defined(__W32G__)) && defined(FORCE_TIME_PERIOD)
8688     TIMECAPS tcaps;
8689 #endif /* (__W32__ && !__W32G__) && FORCE_TIME_PERIOD */
8690
8691     if(nfiles == 0 && !strchr(INTERACTIVE_INTERFACE_IDS, ctl->id_character))
8692         return 0;
8693
8694     if(opt_output_name)
8695     {
8696         play_mode->name = opt_output_name;
8697     if(!strcmp(opt_output_name, "-")){
8698             need_stdout = 1;
8699 #ifdef __W32__
8700         setmode( fileno(stdout), O_BINARY );
8701 #endif
8702     }
8703     }
8704
8705     for(i = 0; i < nfiles; i++)
8706         if (!strcmp(files[i], "-")){
8707             need_stdin = 1;
8708 #ifdef __W32__
8709         setmode( fileno(stdin), O_BINARY );
8710 #endif
8711         }
8712
8713     if(ctl->open(need_stdin, need_stdout))
8714     {
8715         fprintf(stderr, "Couldn't open %s (`%c')" NLS,
8716                 ctl->id_name, ctl->id_character);
8717         if(play_mode->close_output)
8718         {
8719                 play_mode->close_output();
8720         }
8721         safe_free(play_mode->name);
8722         play_mode->name = NULL;
8723         return 3;
8724     }
8725
8726     if(wrdt->open(wrdt_open_opts))
8727     {
8728         fprintf(stderr, "Couldn't open WRD Tracer: %s (`%c')" NLS,
8729                 wrdt->name, wrdt->id);
8730         if(play_mode->close_output)
8731         {
8732                 play_mode->close_output();
8733         }
8734         safe_free(play_mode->name);
8735         play_mode->name = NULL;
8736         ctl->close();
8737         return 1;
8738     }
8739
8740 #ifdef BORLANDC_EXCEPTION
8741     __try
8742     {
8743 #endif /* BORLANDC_EXCEPTION */
8744 #ifdef __W32__
8745
8746 #ifdef HAVE_SIGNAL
8747         signal(SIGTERM, sigterm_exit);
8748 #endif
8749         SetConsoleCtrlHandler(handler, TRUE);
8750
8751         ctl->cmsg(CMSG_INFO, VERB_DEBUG_SILLY,
8752                   "Initialize for Critical Section");
8753         InitializeCriticalSection(&critSect);
8754
8755         if(opt_evil_mode)
8756             if(!SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_ABOVE_NORMAL))
8757                         ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Error raising thread priority");
8758 ///r
8759 #if defined(__W32__)
8760 #ifndef BELOW_NORMAL_PRIORITY_CLASS     /* VC6.0 doesn't support them. */
8761 #define BELOW_NORMAL_PRIORITY_CLASS 0x4000
8762 #define ABOVE_NORMAL_PRIORITY_CLASS 0x8000
8763 #endif /* BELOW_NORMAL_PRIORITY_CLASS */
8764         if( processPriority == IDLE_PRIORITY_CLASS ||
8765                 processPriority == BELOW_NORMAL_PRIORITY_CLASS ||
8766                 processPriority == NORMAL_PRIORITY_CLASS ||
8767                 processPriority == ABOVE_NORMAL_PRIORITY_CLASS ||
8768                 processPriority == HIGH_PRIORITY_CLASS ||
8769                 processPriority == REALTIME_PRIORITY_CLASS)
8770             if(!SetPriorityClass(GetCurrentProcess(), processPriority))
8771                         ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Error changing process priority");
8772 #if !(defined(IA_W32G_SYN) || defined(WINDRV))
8773         if( PlayerThreadPriority == THREAD_PRIORITY_IDLE ||
8774                 PlayerThreadPriority == THREAD_PRIORITY_LOWEST ||
8775                 PlayerThreadPriority == THREAD_PRIORITY_BELOW_NORMAL ||
8776                 PlayerThreadPriority == THREAD_PRIORITY_NORMAL ||
8777                 PlayerThreadPriority == THREAD_PRIORITY_ABOVE_NORMAL ||
8778                 PlayerThreadPriority == THREAD_PRIORITY_HIGHEST ||
8779                 PlayerThreadPriority == THREAD_PRIORITY_TIME_CRITICAL)
8780             if(!SetThreadPriority(GetCurrentThread(), PlayerThreadPriority))
8781                         ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Error changing thread priority");
8782 #endif
8783 #endif
8784 #else
8785         /* UNIX */
8786 #ifdef HAVE_SIGNAL
8787         signal(SIGINT, sigterm_exit);
8788         signal(SIGTERM, sigterm_exit);
8789 #ifdef SIGPIPE
8790         signal(SIGPIPE, sigterm_exit);    /* Handle broken pipe */
8791 #endif /* SIGPIPE */
8792 #endif /* HAVE_SIGNAL */
8793
8794 #endif
8795
8796         /* Open output device */
8797         ctl->cmsg(CMSG_INFO, VERB_DEBUG_SILLY,
8798                   "Open output: %c, %s",
8799                   play_mode->id_character,
8800                   play_mode->id_name);
8801
8802         if (play_mode->flag & PF_PCM_STREAM) {
8803             play_mode->extra_param[1] = aq_calc_fragsize();
8804             ctl->cmsg(CMSG_INFO, VERB_DEBUG_SILLY,
8805                       "requesting fragment size: %d",
8806                       play_mode->extra_param[1]);
8807         }
8808 #if !defined ( IA_W32GUI ) && !defined ( IA_W32G_SYN )
8809         if(play_mode->open_output() < 0)
8810         {
8811             ctl->cmsg(CMSG_FATAL, VERB_NORMAL,
8812                       "Couldn't open %s (`%c')",
8813                       play_mode->id_name, play_mode->id_character);
8814             output_fail = 1;
8815             ctl->close();
8816             return 2;
8817         }
8818 #endif /* IA_W32GUI */
8819         if(!control_ratio)
8820         {
8821             control_ratio = play_mode->rate / CONTROLS_PER_SECOND;
8822             if(control_ratio < 1)
8823                 control_ratio = 1;
8824             else if (control_ratio > MAX_CONTROL_RATIO)
8825                 control_ratio = MAX_CONTROL_RATIO;
8826         }
8827         if(!opt_load_all_instrument)
8828                 init_load_soundfont();
8829         if(!output_fail)
8830         {
8831             aq_setup();
8832             timidity_init_aq_buff();
8833         }
8834         if(allocate_cache_size > 0)
8835             resamp_cache_reset();
8836
8837         if (def_prog >= 0)
8838                 set_default_program(def_prog);
8839         if (*def_instr_name)
8840                 set_default_instrument(def_instr_name);
8841
8842         if(ctl->flags & CTLF_LIST_RANDOM)
8843             randomize_string_list(files, nfiles);
8844         else if(ctl->flags & CTLF_LIST_SORT)
8845             sort_pathname(files, nfiles);
8846
8847 #if (defined(__W32__) && !defined(__W32G__)) && defined(FORCE_TIME_PERIOD)
8848         if (timeGetDevCaps(&tcaps, sizeof(TIMECAPS)) != TIMERR_NOERROR)
8849             tcaps.wPeriodMin = 10;
8850         timeBeginPeriod(tcaps.wPeriodMin);
8851 #endif /* (__W32__ && !__W32G__) && FORCE_TIME_PERIOD */
8852
8853         /* Return only when quitting */
8854         ctl->cmsg(CMSG_INFO, VERB_DEBUG_SILLY,
8855                   "pass_playing_list() nfiles=%d", nfiles);
8856
8857         retval=ctl->pass_playing_list(nfiles, files);
8858
8859 #if (defined(__W32__) && !defined(__W32G__)) && defined(FORCE_TIME_PERIOD)
8860         timeEndPeriod(tcaps.wPeriodMin);
8861 #endif /* (__W32__ && !__W32G__) && FORCE_TIME_PERIOD */
8862
8863         if(intr)
8864             aq_flush(1);
8865
8866 #ifdef XP_UNIX
8867         return 0;
8868 #endif /* XP_UNIX */
8869         
8870         if (play_mode->close_output)
8871                 play_mode->close_output();
8872         safe_free(play_mode->name);
8873         play_mode->name = NULL;
8874         ctl->close();
8875         wrdt->close();
8876 #ifdef __W32__
8877         DeleteCriticalSection (&critSect);
8878 #endif
8879
8880 #ifdef BORLANDC_EXCEPTION
8881     } __except(1) {
8882         fprintf(stderr, "\nError!!!\nUnexpected Exception Occured!\n");
8883         if(play_mode->fd != -1)
8884         {
8885                 play_mode->purge_output();
8886                 play_mode->close_output();
8887         }
8888         ctl->close();
8889         wrdt->close();
8890         DeleteCriticalSection (&critSect);
8891         exit(EXIT_FAILURE);
8892     }
8893 #endif /* BORLANDC_EXCEPTION */
8894
8895 #ifdef SUPPORT_SOUNDSPEC
8896     if(view_soundspec_flag)
8897         close_soundspec();
8898 #endif /* SUPPORT_SOUNDSPEC */
8899
8900     free_archive_files();
8901 #ifdef SUPPORT_SOCKET
8902     url_news_connection_cache(URL_NEWS_CLOSE_CACHE);
8903 #endif /* SUPPORT_SOCKET */
8904
8905     return retval;
8906 }
8907
8908 #ifdef IA_W32GUI
8909 int w32gSecondTiMidity(int opt, int argc, char **argv);
8910 int w32gSecondTiMidityExit(void);
8911 int w32gLoadDefaultPlaylist(void);
8912 int w32gSaveDefaultPlaylist(void);
8913 extern int volatile save_playlist_once_before_exit_flag;
8914 #endif /* IA_W32GUI */
8915
8916
8917 #if defined ( IA_W32GUI ) || defined ( IA_W32G_SYN )
8918 static int CoInitializeOK = 0;
8919 #endif
8920
8921
8922 #if !defined(KBTIM_SETUP) && !defined(WINDRV_SETUP)
8923
8924 #if !defined(ANOTHER_MAIN) || defined(__W32__)
8925 #ifdef __W32__ /* Windows */
8926 #if defined(IA_W32GUI) && !defined(__CYGWIN32__) && !defined(__MINGW32__) \
8927                 || defined(IA_W32G_SYN) /* _MSC_VER, _BORLANDC_, __WATCOMC__ */
8928 int win_main(int argc, char **argv)
8929 #else /* Cygwin, MinGW or console */
8930 int __cdecl main(int argc, char **argv)
8931 #endif
8932 #else /* UNIX */
8933 int main(int argc, char **argv)
8934 #endif
8935 {
8936         int c, err = 0, i;
8937         int nfiles;
8938         char **files;
8939         char *files_nbuf = NULL;
8940         int main_ret;
8941         int longind;
8942 #if defined(DANGEROUS_RENICE) && !defined(__W32__) && !defined(main)
8943         /*
8944          * THIS CODES MUST EXECUTE BEGINNING OF MAIN FOR SECURITY.
8945          * DON'T PUT ANY CODES ABOVE.
8946          */
8947 #include <sys/resource.h>
8948         int uid;
8949 #ifdef sun
8950         extern int setpriority(int which, id_t who, int prio);
8951         extern int setreuid(int ruid, int euid);
8952 #endif
8953         
8954         uid = getuid();
8955         if (setpriority(PRIO_PROCESS, 0, DANGEROUS_RENICE) < 0) {
8956                 perror("setpriority");
8957                 fprintf(stderr, "Couldn't set priority to %d.", DANGEROUS_RENICE);
8958         }
8959         setreuid(uid, uid);
8960 #endif
8961
8962 #if defined(REDIRECT_STDOUT)
8963         memcpy(stdout, fopen(REDIRECT_STDOUT, "a+"), sizeof(FILE));
8964         printf("TiMidity++ start\n");
8965         fflush(stdout);
8966 #endif
8967 ///r
8968 #ifdef __W32__
8969 #if defined(IA_W32GUI) && !defined(WIN32GCC) || defined(IA_W32G_SYN)
8970         /* WinMain */
8971 #elif defined(TIMIDITY_LEAK_CHECK)
8972         _CrtSetDbgFlag(CRTDEBUGFLAGS);
8973 #endif
8974         atexit(w32_exit);
8975
8976 #ifdef __W32__
8977         {
8978                 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
8979
8980                 if (hStdOut != INVALID_HANDLE_VALUE)
8981                 {
8982                         DWORD mode;
8983
8984                         if (GetConsoleMode(hStdOut, &mode))
8985                         {
8986                                 SetConsoleMode(hStdOut, mode | 0x0004 /* ENABLE_VIRTUAL_TERMINAL_PROCESSING */);
8987                         }
8988                 }
8989         }
8990 #endif
8991
8992 #endif /* __W32__ */
8993 #if !defined(KBTIM) && !defined(WINDRV)
8994         OverrideSFSettingLoad();
8995 #endif /* KBTIM WINVSTI */
8996 #if defined(VST_LOADER_ENABLE) && defined(TIM_CUI) && defined(TWSYNSRV)
8997         if (hVSTHost == NULL) {
8998 #ifdef _WIN64
8999                 hVSTHost = LoadLibrary("timvstwrap_x64.dll");
9000 #else
9001                 hVSTHost = LoadLibrary("timvstwrap.dll");
9002 #endif
9003                 if (hVSTHost != NULL) {
9004                         ((vst_open)GetProcAddress(hVSTHost, "vstOpen"))();
9005                 }
9006         }
9007 #endif
9008
9009 #if defined(__W32__) && !defined(WINDRV)
9010         (void)w32_reset_dll_directory();
9011 #endif
9012 #ifdef main
9013 {
9014         static int maincnt = 0;
9015         
9016         if (maincnt++ > 0) {
9017                 do {
9018                         argc--, argv++;
9019                 } while (argv[0][0] == '-');
9020                 ctl->pass_playing_list(argc, argv);
9021                 return 0;
9022         }
9023 }
9024 #endif
9025 #ifdef IA_DYNAMIC
9026 {
9027         dynamic_lib_root = safe_strdup(SHARED_LIB_PATH);
9028         dl_init(argc, argv);
9029 }
9030 #endif /* IA_DYNAMIC */
9031         if ((program_name = pathsep_strrchr(argv[0])))
9032                 program_name++;
9033         else
9034                 program_name = argv[0];
9035     if (strncmp(program_name, "timidity", 8) == 0)
9036         ;
9037 #ifdef IA_KMIDI
9038     else if (strncasecmp(program_name, "kmidi", 5) == 0)
9039         set_ctl("q");
9040 #endif /* IA_KMIDI */
9041 #ifdef IA_TCLTK
9042     else if (strncasecmp(program_name, "tkmidi", 6) == 0)
9043         set_ctl("k");
9044 #endif /* IA_TCLTK */
9045 #ifdef IA_GTK
9046     else if (strncasecmp(program_name, "gtkmidi", 6) == 0)
9047         set_ctl("g");
9048 #endif /* IA_GTK */
9049 #ifdef IA_MOTIF
9050     else if (strncasecmp(program_name, "xmmidi", 6) == 0)
9051         set_ctl("m");
9052 #endif /* IA_MOTIF */
9053 #ifdef IA_XAW
9054     else if (strncasecmp(program_name, "xawmidi", 7) == 0)
9055         set_ctl("a");
9056 #endif /* IA_XAW */
9057 #ifdef IA_XSKIN
9058     else if (strncasecmp(program_name, "xskinmidi", 9) == 0)
9059         set_ctl("i");
9060 #endif /* IA_XSKIN */
9061 #ifdef IA_WINSYN
9062     else if (strncasecmp(program_name, "twsync", 6) == 0) /* twsync*.exe */
9063         set_ctl("W");
9064     else if (strncasecmp(program_name, "tmidisyn", 8) == 0) /* tmidisyn*.exe */
9065         set_ctl("W");
9066     else if (strncasecmp(program_name, "tmidis~", 7) == 0) /* tmidis~1.exe */
9067         set_ctl("W");
9068 #endif /* IA_WINSYN */
9069         if (argc == 1 && !strchr(INTERACTIVE_INTERFACE_IDS, ctl->id_character)) {
9070                 interesting_message();
9071                 return 0;
9072         }
9073         timidity_start_initialize();
9074 #if defined (IA_W32GUI) || defined (IA_W32G_SYN)
9075         if (CoInitialize(NULL) == S_OK)
9076                 CoInitializeOK = 1;
9077         w32g_initialize();
9078 #ifdef IA_W32GUI
9079         /* Secondary TiMidity Execute */
9080         /*      FirstLoadIniFile(); */
9081         if (w32gSecondTiMidity(SecondMode, argc, argv) == FALSE) {
9082                 w32gSecondTiMidityExit();
9083                 if (CoInitializeOK)
9084                         CoUninitialize();
9085                 w32g_free_playlist();
9086                 w32g_uninitialize();
9087                 w32g_free_doc();
9088                 return 0;
9089         }
9090 #endif
9091         for (c = 1; c < argc; c++)
9092                 if (is_directory(argv[c])) {
9093                         char *p;
9094                         
9095                         p = (char *) safe_malloc(strlen(argv[c]) + 2);
9096                         strcpy(p, argv[c]);
9097                         directory_form(p);
9098                         argv[c] = p;
9099                 }
9100 #endif /* IA_W32GUI || IA_W32G_SYN */
9101 #if defined(IA_WINSYN) || defined(IA_PORTMIDISYN) || defined(IA_NPSYN) || defined(IA_W32G_SYN)
9102         opt_sf_close_each_file = 0;
9103 #endif 
9104         optind = longind = 0;
9105 #ifdef __W32__
9106         while ((c = getopt_long(argc, argv, optcommands, longopts, &longind)) > 0)
9107                 if ((err = set_tim_opt_long_cfg(c, optarg, longind)) != 0)
9108                         break;
9109 #endif
9110         if (got_a_configuration != 1){  
9111                 if ((err = timidity_pre_load_configuration()) != 0)
9112                         return err;
9113         }       
9114         optind = longind = 0;
9115         while ((c = getopt_long(argc, argv, optcommands, longopts, &longind)) > 0)
9116                 if ((err = set_tim_opt_long(c, optarg, longind)) != 0)
9117                         break;
9118         err += timidity_post_load_configuration();
9119         /* If there were problems, give up now */
9120         if (err || (optind >= argc && !strchr(INTERACTIVE_INTERFACE_IDS, ctl->id_character))) {
9121                 if (!got_a_configuration) {
9122 #ifdef __W32__
9123                         char config1[1024], config2[1024];
9124                         
9125                         memset(config1, 0, sizeof(config1));
9126                         memset(config2, 0, sizeof(config2));
9127 #if defined(IA_W32GUI) || defined(IA_W32G_SYN)
9128 {
9129                         extern char *ConfigFile;
9130                         
9131                         strncpy(config1, ConfigFile, sizeof(config1) - 1);
9132 }
9133 #else /* !IA_W32GUI && !IA_W32G_SYN */
9134                         GetWindowsDirectory(config1, 1023 - 13);
9135                         strcat(config1, "\\TIMIDITY.CFG");
9136 #endif
9137                         if (GetModuleFileName(NULL, config2, 1023)) {
9138                                 char *strp;
9139                                 
9140                                 config2[1023] = '\0';
9141                                 if (strp = strrchr(config2, '\\')) {
9142                                         *(++strp) = '\0';
9143                                         strncat(config2, "TIMIDITY.CFG",
9144                                                         sizeof(config2) - strlen(config2) - 1);
9145                                 }
9146                         }
9147                         ctl->cmsg(CMSG_FATAL, VERB_NORMAL,
9148                                         "%s: Can't read any configuration file.\n"
9149                                         "Please check %s or %s", program_name, config1, config2);
9150 #else
9151                         ctl->cmsg(CMSG_FATAL, VERB_NORMAL,
9152                                         "%s: Can't read any configuration file.\n"
9153                                         "Please check " CONFIG_FILE, program_name);
9154 #endif /* __W32__ */
9155                 } else
9156                         ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Try %s -h for help", program_name);
9157 /* Try to continue if it is Windows version */
9158 #if !defined(IA_W32GUI) && !defined(IA_W32G_SYN)
9159                 return 1; /* problems with command line */
9160 #endif
9161         }
9162         timidity_init_player();
9163 ///r
9164         load_all_instrument();
9165
9166 #ifdef MULTI_THREAD_COMPUTE     
9167 #if defined(__W32__) && ( defined(IA_W32G_SYN) )
9168         set_compute_thread_priority(syn_ThreadPriority);
9169 #endif
9170         begin_compute_thread();
9171 #endif
9172         nfiles = argc - optind;
9173         files  = argv + optind;
9174         if (nfiles > 0
9175                         && ctl->id_character != 'r' && ctl->id_character != 'A'
9176                         && ctl->id_character != 'W' && ctl->id_character != 'N'
9177                         && ctl->id_character != 'P')
9178                 files = expand_file_archives(files, &nfiles);
9179         if (nfiles > 0)
9180                 files_nbuf = files[0];
9181 #if !defined(IA_W32GUI) && !defined(IA_W32G_SYN) && !defined(IA_WINSYN)
9182         if (dumb_error_count)
9183                 sleep(1);
9184 #endif
9185 #ifdef IA_W32GUI
9186         w32gLoadDefaultPlaylist();
9187         main_ret = timidity_play_main(nfiles, files);
9188         w32_atexit = 0;
9189         if (save_playlist_once_before_exit_flag) {
9190                 save_playlist_once_before_exit_flag = 0;
9191                 w32gSaveDefaultPlaylist();
9192         }
9193         w32gSecondTiMidityExit();
9194         if (CoInitializeOK)
9195                 CoUninitialize();
9196         w32g_free_playlist();
9197         w32g_uninitialize();
9198         w32g_free_doc();
9199 #elif defined(__W32__)
9200         /* CUI, SYN */
9201         main_ret = timidity_play_main(nfiles, files);
9202         w32_atexit = 0;
9203 #ifdef IA_W32G_SYN
9204         if (CoInitializeOK)
9205                 CoUninitialize();
9206         w32g_uninitialize();
9207 #endif /* IA_W32G_SYN */
9208 #else
9209         /* UNIX */
9210         main_ret = timidity_play_main(nfiles, files);
9211 #endif /* IA_W32GUI */
9212 ///r
9213 #ifdef MULTI_THREAD_COMPUTE
9214         terminate_compute_thread();
9215 #endif
9216 #ifdef SUPPORT_SOCKET
9217         safe_free(url_user_agent);
9218         url_user_agent = NULL;
9219         safe_free(url_http_proxy_host);
9220         url_http_proxy_host = NULL;
9221         safe_free(url_ftp_proxy_host);
9222         url_ftp_proxy_host = NULL;
9223         safe_free(user_mailaddr);
9224         user_mailaddr = NULL;
9225 #endif
9226 #ifdef IA_DYNAMIC
9227         safe_free(dynamic_lib_root);
9228         dynamic_lib_root = NULL;
9229 #endif
9230         safe_free(pcm_alternate_file);
9231         pcm_alternate_file = NULL;
9232         safe_free(opt_output_name);
9233         opt_output_name = NULL;
9234         safe_free(opt_aq_max_buff);
9235         opt_aq_max_buff = NULL;
9236         safe_free(opt_aq_fill_buff);
9237         opt_aq_fill_buff = NULL;
9238         safe_free(opt_reduce_voice_threshold);
9239         opt_reduce_voice_threshold = NULL;
9240         safe_free(opt_reduce_quality_threshold);
9241         opt_reduce_quality_threshold = NULL;
9242         safe_free(opt_reduce_polyphony_threshold);
9243         opt_reduce_polyphony_threshold = NULL;
9244         safe_free(output_text_code);
9245         output_text_code = NULL;
9246         safe_free(wrdt_open_opts);
9247         wrdt_open_opts = NULL;
9248         if (nfiles > 0
9249                         && ctl->id_character != 'r' && ctl->id_character != 'A'
9250                         && ctl->id_character != 'W' && ctl->id_character != 'N'
9251                         && ctl->id_character != 'P') {
9252                 safe_free(files_nbuf);
9253                 files_nbuf = NULL;
9254                 safe_free(files);
9255                 files = NULL;
9256         }
9257         free_soft_queue();
9258         free_audio_bucket();
9259         free_instruments(0);
9260         free_soundfonts();
9261         free_cache_data();
9262         free_freq_data();
9263         free_wrd();
9264         free_readmidi();
9265 ///r
9266         free_playmidi();
9267         free_mix_c();
9268         free_global_mblock();
9269         tmdy_free_config();
9270         //free_reverb_buffer();
9271         free_effect_buffers();
9272 ///r
9273 #ifdef ENABLE_SFZ
9274         free_sfz();
9275 #endif
9276 #ifdef ENABLE_DLS
9277         free_dls();
9278 #endif
9279 #ifdef INT_SYNTH
9280         free_int_synth();
9281 #endif // INT_SYNTH
9282         free_voice_pointer();
9283         uninitialize_resampler_coeffs();
9284         for (i = 0; i < MAX_CHANNELS; i++)
9285                 free_drum_effect(i);
9286 #if defined(VST_LOADER_ENABLE) && defined(TIM_CUI) && defined(TWSYNSRV)
9287         if (hVSTHost) {
9288         // only load , no save
9289         //      (vst_close)GetProcAddress(hVSTHost, "vstClose")();
9290                 FreeLibrary(hVSTHost);
9291                 hVSTHost = NULL;
9292         }
9293 #endif
9294 #if (defined(IA_W32GUI) || defined(IA_W32G_SYN)) && \
9295     defined(__GNUC__) && !defined(SUPPORT_WINMAIN)
9296     if (RestartTimidity) {
9297         PROCESS_INFORMATION pi;
9298         STARTUPINFOA si;
9299         char path[MAX_PATH];
9300         char cmdline[4096];
9301         char *params;
9302         RestartTimidity = 0;
9303         ZeroMemory(&pi, sizeof(pi));
9304         ZeroMemory(&si, sizeof(si));
9305         si.cb = sizeof(si);
9306         path[0] = '\0';
9307         GetModuleFileNameA(NULL, path, MAX_PATH);
9308         cmdline[0] = '\0';
9309         //ArgvToCmdLine(); // ToDo
9310         params = (char*) safe_malloc(strlen(path) + 3 + strlen(cmdline) + 1);
9311         strcpy(params, "\"");
9312         strcat(params, path);
9313         strcat(params, "\"" " ");
9314         strcat(params, cmdline);
9315         if (CreateProcessA(NULL, params, NULL, NULL, TRUE,
9316                            CREATE_DEFAULT_ERROR_MODE, NULL, NULL, &si, &pi) == FALSE)
9317             MessageBoxA(NULL, "Restart Error.", "TiMidity++ Win32GUI", MB_OK | MB_ICONEXCLAMATION);
9318         CloseHandle(pi.hProcess);
9319         CloseHandle(pi.hThread);
9320         safe_free(params);
9321     }
9322 #endif /* (IA_W32GUI || IA_W32G_SYN) && __GNUC__ && !SUPPORT_WINMAIN */
9323         return main_ret;
9324 }
9325 #endif /* !ANOTHER_MAIN || __W32__ */
9326
9327 #ifdef __W32__
9328 static void w32_exit(void)
9329 {
9330         int i;
9331         if (!w32_atexit)
9332                 return;
9333         
9334 #if defined(IA_W32GUI) || defined(IA_W32G_SYN) || defined(TIM_CUI) || defined(TWSYNSRV)
9335 #ifdef MULTI_THREAD_COMPUTE
9336         terminate_compute_thread();
9337 #endif
9338 #endif
9339 #ifdef SUPPORT_SOCKET
9340         safe_free(url_user_agent);
9341         url_user_agent = NULL;
9342         safe_free(url_http_proxy_host);
9343         url_http_proxy_host = NULL;
9344         safe_free(url_ftp_proxy_host);
9345         url_ftp_proxy_host = NULL;
9346         safe_free(user_mailaddr);
9347         user_mailaddr = NULL;
9348 #endif
9349 #ifdef IA_DYNAMIC
9350         safe_free(dynamic_lib_root);
9351         dynamic_lib_root = NULL;
9352 #endif
9353         safe_free(pcm_alternate_file);
9354         pcm_alternate_file = NULL;
9355         safe_free(opt_output_name);
9356         opt_output_name = NULL;
9357         safe_free(opt_aq_max_buff);
9358         opt_aq_max_buff = NULL;
9359         safe_free(opt_aq_fill_buff);
9360         opt_aq_fill_buff = NULL;
9361         safe_free(opt_reduce_voice_threshold);
9362         opt_reduce_voice_threshold = NULL;
9363         safe_free(opt_reduce_quality_threshold);
9364         opt_reduce_quality_threshold = NULL;
9365         safe_free(opt_reduce_polyphony_threshold);
9366         opt_reduce_polyphony_threshold = NULL;
9367         safe_free(output_text_code);
9368         output_text_code = NULL;
9369         safe_free(wrdt_open_opts);
9370         wrdt_open_opts = NULL;
9371
9372         free_soft_queue();
9373         free_audio_bucket();
9374         free_instruments(0);
9375         free_soundfonts();
9376         free_cache_data();
9377         free_freq_data();
9378         free_wrd();
9379         free_readmidi();
9380         free_playmidi();
9381         free_mix_c();
9382         free_global_mblock();
9383         tmdy_free_config();
9384         //free_reverb_buffer();
9385         free_effect_buffers();
9386 ///r
9387 #ifdef ENABLE_SFZ
9388         free_sfz();
9389 #endif
9390 #ifdef ENABLE_DLS
9391         free_dls();
9392 #endif
9393 #ifdef INT_SYNTH
9394         free_int_synth();
9395 #endif // INT_SYNTH
9396         free_voice_pointer();
9397         uninitialize_resampler_coeffs();
9398         for (i = 0; i < MAX_CHANNELS; i++)
9399                 free_drum_effect(i);
9400 ///r
9401 #ifdef VST_LOADER_ENABLE
9402         if (hVSTHost) {
9403         // only load , no save
9404         //      ((vst_close) GetProcAddress(hVSTHost, "vstClose"))();
9405                 FreeLibrary(hVSTHost);
9406                 hVSTHost = NULL;
9407         }
9408 #endif
9409 }
9410 #endif /* __W32__ */
9411
9412
9413 #endif /* KBTIM_SETUP */