~ubuntu-branches/ubuntu/hoary/kdemultimedia/hoary

« back to all changes in this revision

Viewing changes to kmidi/timidity.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Martin Schulze
  • Date: 2003-01-22 15:00:51 UTC
  • Revision ID: james.westby@ubuntu.com-20030122150051-uihwkdoxf15mi1tn
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 
 
3
    TiMidity -- Experimental MIDI to WAVE converter
 
4
    Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>
 
5
 
 
6
         This program is free software; you can redistribute it and/or modify
 
7
         it under the terms of the GNU General Public License as published by
 
8
    the Free Software Foundation; either version 2 of the License, or
 
9
         (at your option) any later version.
 
10
 
 
11
    This program is distributed in the hope that it will be useful,
 
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
         GNU General Public License for more details.
 
15
 
 
16
    You should have received a copy of the GNU General Public License
 
17
    along with this program; if not, write to the Free Software
 
18
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
19
 
 
20
*/
 
21
#include <stdio.h>
 
22
#include <stdlib.h>
 
23
 
 
24
#include <string.h>
 
25
 
 
26
#if defined(__linux__) || defined(__FreeBSD__) || defined(__bsdi__)
 
27
#ifndef AU_LINUX
 
28
#define AU_LINUX
 
29
#endif
 
30
#endif
 
31
 
 
32
#ifdef __WIN32__
 
33
#include <windows.h>
 
34
#define rindex strrchr
 
35
extern int optind;
 
36
extern char *optarg;
 
37
int getopt(int, char **, char *);
 
38
#else
 
39
#include <unistd.h>
 
40
#endif
 
41
 
 
42
#include <errno.h>
 
43
#include "config.h"
 
44
#include "common.h"
 
45
#include "instrum.h"
 
46
#include "playmidi.h"
 
47
#include "readmidi.h"
 
48
#include "output.h"
 
49
#include "controls.h"
 
50
#include "tables.h"
 
51
#include "version.h"
 
52
 
 
53
int free_instruments_afterwards=0;
 
54
static char def_instr_name[256]="";
 
55
int cfg_select = 0;
 
56
#ifdef CHANNEL_EFFECT
 
57
extern void effect_activate( int iSwitch ) ;
 
58
extern int init_effect(void) ;
 
59
#endif /*CHANNEL_EFFECT*/
 
60
 
 
61
extern PlayMode arts_play_mode;
 
62
 
 
63
int have_commandline_midis = 0;
 
64
int output_device_open = 0;
 
65
 
 
66
#ifdef __WIN32__
 
67
int intr;
 
68
CRITICAL_SECTION critSect;
 
69
 
 
70
#pragma argsused
 
71
static BOOL WINAPI handler (DWORD dw)
 
72
        {
 
73
        printf ("***BREAK\n");
 
74
        intr = TRUE;
 
75
        play_mode->purge_output ();
 
76
        return TRUE;
 
77
        }
 
78
#endif
 
79
 
 
80
static void help(void)
 
81
{
 
82
  PlayMode **pmp=play_mode_list;
 
83
  ControlMode **cmp=ctl_list;
 
84
  cmp = cmp;
 
85
  /*
 
86
   * glibc headers break this code because printf is a macro there, and
 
87
   * you cannot use a preprocessing instruction like #ifdef inside a macro.
 
88
   * current workaround is not using printf ( Dirk )
 
89
   */
 
90
  fprintf(stdout, " TiMidity version " TIMID_VERSION " (C) 1995 Tuukka Toivonen "
 
91
         "<toivonen@clinet.fi>\n"
 
92
         " TiMidity is free software and comes with ABSOLUTELY NO WARRANTY.\n"
 
93
         "\n"
 
94
#ifdef __WIN32__
 
95
         " Win32 version by Davide Moretti <dmoretti@iper.net>\n"
 
96
#endif
 
97
#ifdef KMIDI
 
98
         " KMidi version " KMIDIVERSION "  MIDI to WAVE player/converter\n"
 
99
         " Copyright (C) 1997 Bernd Johannes Wuebben <wuebben@math.cornell.edu>\n"
 
100
         " \n"
 
101
         " KMidi is free software and comes with ABSOLUTELY NO WARRANTY.\n"
 
102
         "\n"
 
103
#endif
 
104
         "Usage:\n"
 
105
         "  %s [options] filename [...]\n"
 
106
         "\n"
 
107
#ifndef __WIN32__
 
108
        /*does not work in Win32 */
 
109
         "  Use \"-\" as filename to read a MIDI file from stdin\n"
 
110
         "\n"
 
111
#endif
 
112
         "Options:\n"
 
113
#if defined(AU_HPUX) || defined(hpux) || defined(__hpux)
 
114
         "  -o file Output to another file (or audio server) (Use \"-\" for stdout)\n"
 
115
#elif defined (AU_LINUX)
 
116
         "  -o file Output to another file (or device) (Use \"-\" for stdout)\n"
 
117
#else
 
118
         "  -o file Output to another file (Use \"-\" for stdout)\n"
 
119
#endif
 
120
         "  -O mode Select output mode and format (see below for list)\n"
 
121
         "  -s f    Set sampling frequency to f (Hz or kHz)\n"
 
122
         "  -a      Enable the antialiasing filter\n"
 
123
         "  -f      "
 
124
#ifdef FAST_DECAY
 
125
                                        "Disable"
 
126
#else
 
127
                                        "Enable"
 
128
#endif
 
129
                                                          " fast decay mode\n"
 
130
         "  -d      dry mode\n"
 
131
         "  -p n    Allow n-voice polyphony\n"
 
132
         "  -A n    Amplify volume by n percent (may cause clipping)\n"
 
133
         "  -C n    Set ratio of sampling and control frequencies\n"
 
134
         "\n"
 
135
         "  -# n    Select patch set\n"
 
136
#ifdef CHANNEL_EFFECT
 
137
         "  -E      Effects filter\n"
 
138
#endif /*CHANNEL_EFFECT*/
 
139
#ifdef tplus
 
140
         "  -m      Disable portamento\n"
 
141
#endif
 
142
         "  -L dir  Append dir to search path\n"
 
143
         "  -c file Read extra configuration file\n"
 
144
         "  -I n    Use program n as the default\n"
 
145
         "  -P file Use patch file for all programs\n"
 
146
         "  -D n    Play drums on channel n\n"
 
147
         "  -Q n    Ignore channel n\n"
 
148
         "  -R n    Reverb options (1)(+2)(+4) [7]\n"
 
149
         "  -k n    Resampling interpolation option (0-3) [1]\n"
 
150
         "  -r n    Max ram to hold patches in megabytes [60]\n"
 
151
         "  -X n    Midi expression is linear (0) or exponential (1-2) [1]\n"
 
152
         "  -V n    Midi volume is linear (0) or exponential (1-2) [1]\n"
 
153
 
 
154
         "  -F      Enable fast panning\n"
 
155
         "  -U      Unload instruments from memory between MIDI files\n"
 
156
         "\n"
 
157
         "  -i mode Select user interface (see below for list)\n"
 
158
#if defined(AU_LINUX) || defined(AU_WIN32)
 
159
         "  -B n    Set number of buffer fragments\n"
 
160
#endif
 
161
#ifdef __WIN32__
 
162
         "  -e      Increase thread priority (evil) - be careful!\n"
 
163
#endif
 
164
         "  -h      Display this help message\n"
 
165
         "\n"
 
166
         , program_name);
 
167
 
 
168
  printf("Available output modes (-O option):\n\n");
 
169
  while (*pmp)
 
170
         {
 
171
                printf("  -O%c     %s\n", (*pmp)->id_character, (*pmp)->id_name);
 
172
                pmp++;
 
173
         }
 
174
  printf("\nOutput format options (append to -O? option):\n\n"
 
175
         "   `8'    8-bit sample width\n"
 
176
         "   `1'    16-bit sample width\n"
 
177
         "   `U'    uLaw encoding\n"
 
178
         "   `l'    linear encoding\n"
 
179
         "   `M'    monophonic\n"
 
180
         "   `S'    stereo\n"
 
181
         "   `s'    signed output\n"
 
182
         "   `u'    unsigned output\n"
 
183
         "   `x'    byte-swapped output\n");
 
184
 
 
185
  printf("\nAvailable interfaces (-i option):\n\n");
 
186
  while (*cmp)
 
187
         {
 
188
      printf("  -i%c     %s\n", (*cmp)->id_character, (*cmp)->id_name);
 
189
      cmp++;
 
190
         }
 
191
  printf("\nInterface options (append to -i? option):\n\n"
 
192
         "   `v'    more verbose (cumulative)\n"
 
193
         "   `q'    quieter (cumulative)\n"
 
194
         "   `t'    trace playing\n\n");
 
195
}
 
196
 
 
197
#ifndef KMIDI
 
198
static void interesting_message(void)
 
199
{
 
200
  fprintf(stdout,
 
201
"\n"
 
202
" TiMidity version " TIMID_VERSION " -- Experimental MIDI to WAVE converter\n"
 
203
" Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>\n"
 
204
" \n"
 
205
#ifdef __WIN32__
 
206
" Win32 version by Davide Moretti <dmoretti@iper.net>\n"
 
207
"\n"
 
208
#endif
 
209
" This program is free software; you can redistribute it and/or modify\n"
 
210
" it under the terms of the GNU General Public License as published by\n"
 
211
" the Free Software Foundation; either version 2 of the License, or\n"
 
212
" (at your option) any later version.\n"
 
213
" \n"
 
214
" This program is distributed in the hope that it will be useful,\n"
 
215
" but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
 
216
" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
 
217
" GNU General Public License for more details.\n"
 
218
" \n"
 
219
" You should have received a copy of the GNU General Public License\n"
 
220
" along with this program; if not, write to the Free Software\n"
 
221
" Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n"
 
222
"\n"
 
223
);
 
224
 
 
225
}
 
226
#endif
 
227
 
 
228
static int set_channel_flag(int32 *flags, int32 i, const char *name)
 
229
{
 
230
  if (i==0) *flags=0;
 
231
  else if ((i<1 || i>16) && (i<-16 || i>-1))
 
232
    {
 
233
                fprintf(stderr,
 
234
              "%s must be between 1 and 16, or between -1 and -16, or 0\n",
 
235
              name);
 
236
      return -1;
 
237
         }
 
238
  else
 
239
    {
 
240
      if (i>0) *flags |= (1<<(i-1));
 
241
                else *flags &= ~ ((1<<(-1-i)));
 
242
    }
 
243
  return 0;
 
244
}
 
245
 
 
246
static int set_value(int32 *param, int32 i, int32 low, int32 high, const char *name)
 
247
{
 
248
  if (i<low || i > high)
 
249
    {
 
250
      fprintf(stderr, "%s must be between %ld and %ld\n", name, low, high);
 
251
      return -1;
 
252
    }
 
253
  else *param=i;
 
254
  return 0;
 
255
}
 
256
 
 
257
int set_play_mode(char *cp)
 
258
{
 
259
  PlayMode *pmp, **pmpp=play_mode_list;
 
260
 
 
261
  while ((pmp=*pmpp++))
 
262
    {
 
263
      if (pmp->id_character == *cp)
 
264
        {
 
265
          play_mode=pmp;
 
266
          while (*(++cp))
 
267
                 switch(*cp)
 
268
              {
 
269
              case 'U': pmp->encoding |= PE_ULAW; break; /* uLaw */
 
270
                        case 'l': pmp->encoding &= ~PE_ULAW; break; /* linear */
 
271
 
 
272
                        case '1': pmp->encoding |= PE_16BIT; break; /* 1 for 16-bit */
 
273
              case '8': pmp->encoding &= ~PE_16BIT; break;
 
274
 
 
275
              case 'M': pmp->encoding |= PE_MONO; break;
 
276
              case 'S': pmp->encoding &= ~PE_MONO; break; /* stereo */
 
277
 
 
278
              case 's': pmp->encoding |= PE_SIGNED; break;
 
279
              case 'u': pmp->encoding &= ~PE_SIGNED; break;
 
280
 
 
281
              case 'x': pmp->encoding ^= PE_BYTESWAP; break; /* toggle */
 
282
 
 
283
                        default:
 
284
                fprintf(stderr, "Unknown format modifier `%c'\n", *cp);
 
285
                return 1;
 
286
              }
 
287
          return 0;
 
288
        }
 
289
         }
 
290
 
 
291
  fprintf(stderr, "Playmode `%c' is not compiled in.\n", *cp);
 
292
  return 1;
 
293
}
 
294
 
 
295
static int set_ctl(char *cp)
 
296
{
 
297
  ControlMode *cmp, **cmpp=ctl_list;
 
298
 
 
299
  while ((cmp=*cmpp++))
 
300
    {
 
301
      if (cmp->id_character == *cp)
 
302
        {
 
303
          ctl=cmp;
 
304
          while (*(++cp))
 
305
            switch(*cp)
 
306
                        {
 
307
                        case 'v': cmp->verbosity++; break;
 
308
                        case 'q': cmp->verbosity--; break;
 
309
                        case 't': /* toggle */
 
310
                cmp->trace_playing= (cmp->trace_playing) ? 0 : 1;
 
311
                break;
 
312
 
 
313
              default:
 
314
                fprintf(stderr, "Unknown interface option `%c'\n", *cp);
 
315
                return 1;
 
316
              }
 
317
          return 0;
 
318
        }
 
319
         }
 
320
 
 
321
  fprintf(stderr, "Interface `%c' is not compiled in.\n", *cp);
 
322
  return 1;
 
323
}
 
324
 
 
325
char *cfg_names[30];
 
326
 
 
327
 
 
328
void clear_config(void)
 
329
{
 
330
    ToneBank *bank=0;
 
331
    int i, j;
 
332
 
 
333
    for (i = 0; i < MAXBANK; i++)
 
334
    {
 
335
                if (tonebank[i])
 
336
                {
 
337
                        bank = tonebank[i];
 
338
                        if (bank->name) free((void *)bank->name);
 
339
                        bank->name = 0;
 
340
                        for (j = 0; j < MAXPROG; j++)
 
341
                          if (bank->tone[j].name)
 
342
                          {
 
343
                             free((void *)bank->tone[j].name);
 
344
                             bank->tone[j].name = 0;
 
345
                          }
 
346
                        if (i > 0)
 
347
                        {
 
348
                             free(tonebank[i]);
 
349
                             tonebank[i] = 0;
 
350
                        }
 
351
                }
 
352
                if (drumset[i])
 
353
                {
 
354
                        bank = drumset[i];
 
355
                        if (bank->name) free((void *)bank->name);
 
356
                        bank->name = 0;
 
357
                        for (j = 0; j < MAXPROG; j++)
 
358
                          if (bank->tone[j].name)
 
359
                          {
 
360
                             free((void *)bank->tone[j].name);
 
361
                             bank->tone[j].name = 0;
 
362
                          }
 
363
                        if (i > 0)
 
364
                        {
 
365
                             free(drumset[i]);
 
366
                             drumset[i] = 0;
 
367
                        }
 
368
                }
 
369
    }
 
370
 
 
371
    memset(drumset[0], 0, sizeof(ToneBank));
 
372
    memset(tonebank[0], 0, sizeof(ToneBank));
 
373
    clear_pathlist(0);
 
374
}
 
375
 
 
376
 
 
377
int reverb_options=7;
 
378
int global_reverb = 0;
 
379
int global_chorus = 0;
 
380
int global_echo = 0;
 
381
int global_detune = 0;
 
382
 
 
383
int got_a_configuration=0;
 
384
 
 
385
#ifdef KMIDI
 
386
extern int createKApplication(int *argc, char ***argv);
 
387
#endif
 
388
 
 
389
#ifdef __WIN32__
 
390
int __cdecl main(int argc, char **argv)
 
391
#else
 
392
int main(int argc, char **argv)
 
393
#endif
 
394
{
 
395
  int c,
 
396
      cmderr=0,
 
397
      i,
 
398
      try_config_again=0,
 
399
      need_stdin=0,
 
400
      need_stdout=0;
 
401
  int orig_optind;
 
402
 
 
403
  int32 tmpi32, output_rate=0;
 
404
 
 
405
  char *output_name=0;
 
406
 
 
407
#if defined(AU_LINUX) || defined(AU_WIN32)
 
408
  int buffer_fragments=-1;
 
409
#endif
 
410
#ifdef __WIN32__
 
411
        int evil=0;
 
412
#endif
 
413
 
 
414
#ifdef DANGEROUS_RENICE
 
415
#include <sys/resource.h>
 
416
  int u_uid=getuid();
 
417
#endif
 
418
 
 
419
#ifdef KMIDI
 
420
#define KMIDI_CONFIG_SUBDIR "/share/apps/kmidi/config"
 
421
  char *KDEdir;
 
422
  char *kmidi_config;
 
423
 
 
424
 
 
425
  for (i = 0; i < argc; i++) {
 
426
        if (strlen(argv[i]) < 2) continue;
 
427
        if (argv[i][0] != '-') continue;
 
428
        if (argv[i][1] == 'h') break;
 
429
        if (strlen(argv[i]) < 3) continue;
 
430
        if (argv[i][1] != 'i') continue;
 
431
        if (argv[i][2] == 'c') continue;
 
432
        if (argv[i][2] != 'q') break;
 
433
  }
 
434
 
 
435
  if (i == argc) {
 
436
        if (!createKApplication(&argc, &argv)) return 0;
 
437
  }
 
438
  if ( ! (KDEdir = getenv("KDEDIR"))) {
 
439
       kmidi_config = (char *)safe_malloc(strlen(DEFAULT_PATH)+1);
 
440
       strcpy(kmidi_config, DEFAULT_PATH);
 
441
  }
 
442
  else {
 
443
       kmidi_config = (char *)safe_malloc(strlen(KDEdir)+strlen(KMIDI_CONFIG_SUBDIR)+1);
 
444
       strcpy(kmidi_config, KDEdir);
 
445
       strcat(kmidi_config, KMIDI_CONFIG_SUBDIR);
 
446
       /* add_to_pathlist(kmidi_config, 0); */
 
447
  }
 
448
  add_to_pathlist(kmidi_config, 0);
 
449
#else
 
450
#ifdef DEFAULT_PATH
 
451
  add_to_pathlist(DEFAULT_PATH, 0);
 
452
#endif
 
453
#endif
 
454
 
 
455
 
 
456
#ifdef DANGEROUS_RENICE
 
457
  if (setpriority(PRIO_PROCESS, 0, DANGEROUS_RENICE) < 0)
 
458
         fprintf(stderr, "Couldn't set priority to %d.\n", DANGEROUS_RENICE);
 
459
  setreuid(u_uid, u_uid);
 
460
#endif
 
461
 
 
462
 
 
463
  if ((program_name=rindex(argv[0], '/'))) program_name++;
 
464
  else program_name=argv[0];
 
465
#ifndef KMIDI
 
466
  if (argc==1)
 
467
         {
 
468
                interesting_message();
 
469
                return 0;
 
470
         }
 
471
#endif
 
472
 
 
473
  command_cutoff_allowed = 0;
 
474
#ifdef CHANNEL_EFFECT
 
475
  init_effect() ;
 
476
#endif /*CHANNEL_EFFECT*/
 
477
 
 
478
#ifdef KMIDI
 
479
  if (argc >= 3 && !strcmp(argv[1],"-miniicon")) {
 
480
          for (i=1; i<argc-2; i++) argv[i] = argv[i+2];
 
481
          argc -= 2;
 
482
  }
 
483
  if (argc >= 3 && !strcmp(argv[1],"-icon")) {
 
484
          for (i=1; i<argc-2; i++) argv[i] = argv[i+2];
 
485
          argc -= 2;
 
486
  }
 
487
#endif
 
488
 
 
489
  while ((c=getopt(argc, argv, "UI:P:L:c:A:C:ap:fo:O:s:Q:R:FD:hi:#:qEmk:r:X:V:d-::"
 
490
#if defined(AU_LINUX) || defined(AU_WIN32)
 
491
                        "B:" /* buffer fragments */
 
492
#endif
 
493
#ifdef __WIN32__
 
494
                        "e"     /* evil (be careful) */
 
495
#endif
 
496
                        ))>0)
 
497
         switch(c)
 
498
                {
 
499
                case 'q': command_cutoff_allowed=1; break;
 
500
                case 'U': free_instruments_afterwards=1; break;
 
501
                case 'L': add_to_pathlist(optarg, 0); try_config_again=1; break;
 
502
                case 'c':
 
503
        if (read_config_file(optarg, 1)) cmderr++;
 
504
        break;
 
505
#ifdef CHANNEL_EFFECT
 
506
                case 'E':  effect_activate( 1 ); break ;
 
507
#endif /* CHANNEL_EFFECT */
 
508
 
 
509
                case 'Q':
 
510
        if (set_channel_flag(&quietchannels, atoi(optarg), "Quiet channel"))
 
511
          cmderr++;
 
512
        break;
 
513
 
 
514
                case 'k':
 
515
        current_interpolation = atoi(optarg);
 
516
        break;
 
517
 
 
518
                case 'd':
 
519
        opt_dry = 1;
 
520
        break;
 
521
 
 
522
                case 'X':
 
523
        opt_expression_curve = atoi(optarg);
 
524
        break;
 
525
 
 
526
                case 'V':
 
527
        opt_volume_curve = atoi(optarg);
 
528
        break;
 
529
 
 
530
                case 'r':
 
531
        max_patch_memory = atoi(optarg) * 1000000;
 
532
        break;
 
533
 
 
534
                case 'R': reverb_options = atoi(optarg); break;
 
535
 
 
536
                case 'D':
 
537
        if (set_channel_flag(&drumchannels, atoi(optarg), "Drum channel"))
 
538
          cmderr++;
 
539
        break;
 
540
 
 
541
                case 'O': /* output mode */
 
542
        if (set_play_mode(optarg))
 
543
          cmderr++;
 
544
        break;
 
545
 
 
546
                case 'o':       output_name=optarg; break;
 
547
 
 
548
                case 'a': antialiasing_allowed=1; break;
 
549
 
 
550
                case 'f': fast_decay=(fast_decay) ? 0 : 1; break;
 
551
 
 
552
                case 'F': adjust_panning_immediately=1; break;
 
553
 
 
554
                case 's': /* sampling rate */
 
555
        i=atoi(optarg);
 
556
        if (i < 100) i *= 1000;
 
557
        if (set_value(&output_rate, i, MIN_OUTPUT_RATE, MAX_OUTPUT_RATE,
 
558
                                "Resampling frequency")) cmderr++;
 
559
        break;
 
560
 
 
561
                case 'P': /* set overriding instrument */
 
562
        strncpy(def_instr_name, optarg, 255);
 
563
        def_instr_name[255]='\0';
 
564
        break;
 
565
 
 
566
                case 'I':
 
567
        if (set_value(&tmpi32, atoi(optarg), 0, 127,
 
568
                                "Default program")) cmderr++;
 
569
        else default_program=tmpi32;
 
570
        break;
 
571
                case 'A':
 
572
        if (set_value(&amplification, atoi(optarg), 1, MAX_AMPLIFICATION,
 
573
                                "Amplification")) cmderr++;
 
574
        break;
 
575
                case 'C':
 
576
        if (set_value(&control_ratio, atoi(optarg), 1, MAX_CONTROL_RATIO,
 
577
                                "Control ratio")) cmderr++;
 
578
        break;
 
579
                case 'p':
 
580
        if (set_value(&tmpi32, atoi(optarg), 1, MAX_VOICES,
 
581
                                "Polyphony")) cmderr++;
 
582
        else voices=tmpi32;
 
583
        break;
 
584
 
 
585
                case 'i':
 
586
        if (set_ctl(optarg))
 
587
          cmderr++;
 
588
#if defined(AU_LINUX) || defined(AU_WIN32)
 
589
        else if (buffer_fragments==-1 && ctl->trace_playing)
 
590
          /* user didn't specify anything, so use 2 for real-time response */
 
591
#if defined(AU_LINUX)
 
592
          buffer_fragments=2;
 
593
#else
 
594
          buffer_fragments=3;           /* On Win32 2 is chunky */
 
595
#endif
 
596
#endif
 
597
        break;
 
598
 
 
599
#if defined(AU_LINUX) || defined(AU_WIN32)
 
600
 
 
601
                case 'B':
 
602
        if (set_value(&tmpi32, atoi(optarg), 0, 1000,
 
603
                                "Buffer fragments")) cmderr++;
 
604
        else buffer_fragments=tmpi32;
 
605
        break;
 
606
#endif
 
607
#ifdef __WIN32__
 
608
                case 'e': /* evil */
 
609
                        evil = 1;
 
610
                        break;
 
611
 
 
612
#endif
 
613
                case '#':
 
614
        cfg_select=atoi(optarg);
 
615
        break;
 
616
                case 'h':
 
617
        help();
 
618
        return 0;
 
619
 
 
620
                case '-':
 
621
        break;
 
622
 
 
623
                default:
 
624
        cmderr++; break;
 
625
                }
 
626
 
 
627
  try_config_again = 1;
 
628
/* don't use unnecessary memory until we're a child process */
 
629
  if (!got_a_configuration)
 
630
         {
 
631
                if (!try_config_again || read_config_file(CONFIG_FILE, 1)) cmderr++;
 
632
         }
 
633
 
 
634
  /* If there were problems, give up now */
 
635
  if (cmderr )
 
636
         {
 
637
                fprintf(stderr, "Try %s -h for help\n", program_name);
 
638
                return 1; /* problems with command line */
 
639
         }
 
640
 
 
641
  /* Set play mode parameters */
 
642
  if (output_rate) play_mode->rate=output_rate;
 
643
  if (output_name)
 
644
         {
 
645
                play_mode->name=output_name;
 
646
                if (!strcmp(output_name, "-"))
 
647
        need_stdout=1;
 
648
         }
 
649
 
 
650
#if defined(AU_LINUX) || defined(AU_WIN32)
 
651
 
 
652
  if (buffer_fragments != -1)
 
653
         play_mode->extra_param[0]=buffer_fragments;
 
654
#endif
 
655
 
 
656
  init_tables();
 
657
 
 
658
  orig_optind=optind;
 
659
 
 
660
  while (optind<argc) if (!strcmp(argv[optind++], "-")) need_stdin=1;
 
661
  optind=orig_optind;
 
662
 
 
663
  if(argc-optind > 0 ) have_commandline_midis = argc - optind;
 
664
  else have_commandline_midis = 0;
 
665
 
 
666
  if (play_mode->open_output()<0){
 
667
 
 
668
      /* fprintf(stderr, "KMidi: Sorry, couldn't open %s.\n", play_mode->id_name); */
 
669
      /*          ctl->close();*/
 
670
 
 
671
      output_device_open = 0;
 
672
 
 
673
      /*return 2;*/
 
674
  }
 
675
  else output_device_open = 1;
 
676
 
 
677
  if (!output_device_open && play_mode != &arts_play_mode) {
 
678
        PlayMode *save_play_mode;
 
679
        save_play_mode = play_mode;
 
680
        play_mode = &arts_play_mode;
 
681
        if (output_rate) play_mode->rate=output_rate;
 
682
        if (output_name) play_mode->name=output_name;
 
683
        if (play_mode->open_output()<0) play_mode = save_play_mode;
 
684
        else {
 
685
          output_device_open = 1;
 
686
        }
 
687
  }
 
688
 
 
689
  if (ctl->open(need_stdin, need_stdout)) {
 
690
 
 
691
          fprintf(stderr, "Couldn't open %s\n", ctl->id_name);
 
692
          play_mode->close_output();
 
693
          return 3;
 
694
  }
 
695
 
 
696
#ifndef KMIDI
 
697
  if (!output_device_open) {
 
698
          fprintf(stderr, "Couldn't open %s\n", play_mode->id_name);
 
699
          ctl->close();
 
700
          return 2;
 
701
  }
 
702
#endif /* not KMIDI */
 
703
 
 
704
 
 
705
  if (!control_ratio) {
 
706
 
 
707
          control_ratio = play_mode->rate / CONTROLS_PER_SECOND;
 
708
          if(control_ratio<1)
 
709
                 control_ratio=1;
 
710
          else if (control_ratio > MAX_CONTROL_RATIO)
 
711
                 control_ratio=MAX_CONTROL_RATIO;
 
712
  }
 
713
 
 
714
  if (*def_instr_name) set_default_instrument(def_instr_name);
 
715
 
 
716
#ifdef __WIN32__
 
717
 
 
718
                SetConsoleCtrlHandler (handler, TRUE);
 
719
                InitializeCriticalSection (&critSect);
 
720
 
 
721
                if(evil)
 
722
                        if (!SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL))
 
723
                                fprintf(stderr, "Error raising process priority.\n");
 
724
#endif
 
725
 
 
726
  if (got_a_configuration < 2) read_config_file(current_config_file, 0);
 
727
 
 
728
  ctl->pass_playing_list(argc-optind, (const char **)&argv[orig_optind]);
 
729
 
 
730
  play_mode->close_output();
 
731
  ctl->close();
 
732
 
 
733
#ifdef __WIN32__
 
734
                        DeleteCriticalSection (&critSect);
 
735
#endif
 
736
  return 0;
 
737
}