timidity/timidity.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 02 May 2015 19:27:46 -0700
changeset 700 a9867904addc
parent 604 c932750a78ce
child 701 25605697981c
permissions -rw-r--r--
Fixed bug 2968 - FILE *fp getting leak in read_config_file function

Nitz

In function read_config_file(const char *name)

FILE *fp getting leak at many places when function returns -2.
slouken@0
     1
/*
slouken@0
     2
    TiMidity -- Experimental MIDI to WAVE converter
slouken@0
     3
    Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>
slouken@0
     4
slouken@518
     5
    This program is free software; you can redistribute it and/or modify
slouken@518
     6
    it under the terms of the Perl Artistic License, available in COPYING.
slouken@518
     7
 */
slouken@0
     8
slouken@0
     9
#include <stdio.h>
slouken@0
    10
#include <stdlib.h>
slouken@0
    11
#include <string.h>
slouken@0
    12
slouken@24
    13
#include "SDL.h"
slouken@0
    14
#include "config.h"
slouken@0
    15
#include "common.h"
slouken@0
    16
#include "instrum.h"
slouken@0
    17
#include "playmidi.h"
slouken@0
    18
#include "readmidi.h"
slouken@0
    19
#include "output.h"
slouken@319
    20
#include "ctrlmode.h"
slouken@0
    21
#include "timidity.h"
slouken@0
    22
slouken@0
    23
#include "tables.h"
slouken@0
    24
slouken@0
    25
void (*s32tobuf)(void *dp, int32 *lp, int32 c);
slouken@0
    26
int free_instruments_afterwards=0;
slouken@0
    27
static char def_instr_name[256]="";
slouken@0
    28
slouken@0
    29
int AUDIO_BUFFER_SIZE;
patmandin@264
    30
resample_t *resample_buffer=NULL;
patmandin@264
    31
int32 *common_buffer=NULL;
slouken@245
    32
int num_ochannels;
slouken@0
    33
slouken@0
    34
#define MAXWORDS 10
slouken@0
    35
slouken@421
    36
static int read_config_file(const char *name)
slouken@0
    37
{
slouken@0
    38
  FILE *fp;
icculus@449
    39
  char tmp[PATH_MAX], *w[MAXWORDS], *cp;
slouken@0
    40
  ToneBank *bank=0;
slouken@0
    41
  int i, j, k, line=0, words;
slouken@0
    42
  static int rcf_count=0;
slouken@0
    43
slouken@0
    44
  if (rcf_count>50)
slouken@0
    45
   {
slouken@0
    46
    ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
slouken@0
    47
      "Probable source loop in configuration files");
slouken@0
    48
    return (-1);
slouken@0
    49
   }
slouken@0
    50
slouken@0
    51
  if (!(fp=open_file(name, 1, OF_VERBOSE)))
slouken@0
    52
   return -1;
slouken@0
    53
slouken@0
    54
  while (fgets(tmp, sizeof(tmp), fp))
slouken@245
    55
  {
slouken@245
    56
    line++;
slouken@0
    57
    w[words=0]=strtok(tmp, " \t\r\n\240");
slouken@0
    58
    if (!w[0] || (*w[0]=='#')) continue;
slouken@245
    59
    while (w[words] && (words < MAXWORDS))
slouken@245
    60
      {
slouken@245
    61
        w[++words]=strtok(0," \t\r\n\240");
slouken@245
    62
        if (w[words] && w[words][0]=='#') break;
slouken@245
    63
      }
slouken@245
    64
    if (!strcmp(w[0], "map")) continue;
slouken@245
    65
    if (!strcmp(w[0], "dir"))
slouken@245
    66
    {
slouken@245
    67
      if (words < 2)
slouken@245
    68
       {
slouken@0
    69
        ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
slouken@0
    70
          "%s: line %d: No directory given\n", name, line);
slouken@700
    71
        close_file(fp);
slouken@0
    72
        return -2;
slouken@245
    73
       }
slouken@245
    74
      for (i=1; i<words; i++)
slouken@245
    75
        add_to_pathlist(w[i]);
slouken@245
    76
    }
slouken@245
    77
  else if (!strcmp(w[0], "source"))
slouken@0
    78
  {
slouken@0
    79
    if (words < 2)
slouken@0
    80
      {
slouken@0
    81
        ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
slouken@0
    82
          "%s: line %d: No file name given\n", name, line);
slouken@700
    83
        close_file(fp);
slouken@0
    84
        return -2;
slouken@0
    85
     }
slouken@0
    86
    for (i=1; i<words; i++)
slouken@0
    87
      {
slouken@0
    88
        rcf_count++;
slouken@0
    89
      read_config_file(w[i]);
slouken@0
    90
        rcf_count--;
slouken@0
    91
      }
slouken@0
    92
  }
slouken@0
    93
      else if (!strcmp(w[0], "default"))
slouken@0
    94
  {
slouken@0
    95
    if (words != 2)
slouken@0
    96
      {
slouken@0
    97
        ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
slouken@0
    98
        "%s: line %d: Must specify exactly one patch name\n",
slouken@0
    99
          name, line);
slouken@700
   100
        close_file(fp);
slouken@0
   101
        return -2;
slouken@0
   102
      }
slouken@0
   103
    strncpy(def_instr_name, w[1], 255);
slouken@0
   104
    def_instr_name[255]='\0';
slouken@0
   105
  }
slouken@0
   106
    else if (!strcmp(w[0], "drumset"))
slouken@0
   107
  {
slouken@0
   108
    if (words < 2)
slouken@0
   109
      {
slouken@0
   110
        ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
slouken@0
   111
          "%s: line %d: No drum set number given\n", 
slouken@0
   112
          name, line);
slouken@700
   113
      close_file(fp);
slouken@0
   114
      return -2;
slouken@0
   115
      }
slouken@0
   116
    i=atoi(w[1]);
slouken@0
   117
    if (i<0 || i>127)
slouken@0
   118
     {
slouken@0
   119
        ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
slouken@0
   120
          "%s: line %d: Drum set must be between 0 and 127\n",
slouken@0
   121
        name, line);
slouken@700
   122
        close_file(fp);
slouken@0
   123
        return -2;
slouken@0
   124
     }
slouken@0
   125
    if (!drumset[i])
slouken@0
   126
      {
slouken@0
   127
        drumset[i]=safe_malloc(sizeof(ToneBank));
slouken@0
   128
      memset(drumset[i], 0, sizeof(ToneBank));
slouken@0
   129
     }
slouken@0
   130
    bank=drumset[i];
slouken@0
   131
  }
slouken@0
   132
    else if (!strcmp(w[0], "bank"))
slouken@0
   133
  {
slouken@0
   134
    if (words < 2)
slouken@0
   135
     {
slouken@0
   136
        ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
slouken@0
   137
          "%s: line %d: No bank number given\n", 
slouken@0
   138
        name, line);
slouken@700
   139
        close_file(fp);
slouken@0
   140
        return -2;
slouken@0
   141
     }
slouken@0
   142
    i=atoi(w[1]);
slouken@0
   143
    if (i<0 || i>127)
slouken@0
   144
      {
slouken@0
   145
        ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
slouken@0
   146
          "%s: line %d: Tone bank must be between 0 and 127\n",
slouken@0
   147
        name, line);
slouken@700
   148
        close_file(fp);
slouken@0
   149
        return -2;
slouken@0
   150
      }
slouken@0
   151
    if (!tonebank[i])
slouken@0
   152
     {
slouken@0
   153
      tonebank[i]=safe_malloc(sizeof(ToneBank));
slouken@0
   154
        memset(tonebank[i], 0, sizeof(ToneBank));
slouken@0
   155
      }
slouken@0
   156
    bank=tonebank[i];
slouken@0
   157
  }
slouken@0
   158
      else {
slouken@0
   159
  if ((words < 2) || (*w[0] < '0' || *w[0] > '9'))
slouken@0
   160
    {
slouken@0
   161
     ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
slouken@0
   162
        "%s: line %d: syntax error\n", name, line);
slouken@603
   163
     continue;
slouken@0
   164
    }
slouken@0
   165
  i=atoi(w[0]);
slouken@0
   166
  if (i<0 || i>127)
slouken@0
   167
    {
slouken@0
   168
      ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
slouken@0
   169
        "%s: line %d: Program must be between 0 and 127\n",
slouken@0
   170
        name, line);
slouken@700
   171
      close_file(fp);
slouken@0
   172
      return -2;
slouken@0
   173
    }
slouken@0
   174
  if (!bank)
slouken@0
   175
    {
slouken@0
   176
      ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
slouken@0
   177
       "%s: line %d: Must specify tone bank or drum set "
slouken@0
   178
        "before assignment\n",
slouken@0
   179
        name, line);
slouken@700
   180
     close_file(fp);
slouken@0
   181
     return -2;
slouken@0
   182
    }
slouken@0
   183
  if (bank->tone[i].name)
slouken@0
   184
    free(bank->tone[i].name);
slouken@0
   185
  strcpy((bank->tone[i].name=safe_malloc(strlen(w[1])+1)),w[1]);
slouken@0
   186
  bank->tone[i].note=bank->tone[i].amp=bank->tone[i].pan=
slouken@0
   187
    bank->tone[i].strip_loop=bank->tone[i].strip_envelope=
slouken@0
   188
      bank->tone[i].strip_tail=-1;
slouken@0
   189
slouken@0
   190
  for (j=2; j<words; j++)
slouken@0
   191
    {
slouken@0
   192
      if (!(cp=strchr(w[j], '=')))
slouken@0
   193
        {
slouken@0
   194
    ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: line %d: bad patch option %s\n",
slouken@0
   195
      name, line, w[j]);
slouken@700
   196
    close_file(fp);
slouken@0
   197
    return -2;
slouken@0
   198
        }
slouken@0
   199
      *cp++=0;
slouken@0
   200
      if (!strcmp(w[j], "amp"))
slouken@0
   201
      {
slouken@0
   202
    k=atoi(cp);
slouken@0
   203
    if ((k<0 || k>MAX_AMPLIFICATION) || (*cp < '0' || *cp > '9'))
slouken@0
   204
      {
slouken@0
   205
       ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
slouken@0
   206
          "%s: line %d: amplification must be between "
slouken@0
   207
         "0 and %d\n", name, line, MAX_AMPLIFICATION);
slouken@700
   208
       close_file(fp);
slouken@0
   209
       return -2;
slouken@0
   210
      }
slouken@0
   211
    bank->tone[i].amp=k;
slouken@0
   212
        }
slouken@0
   213
      else if (!strcmp(w[j], "note"))
slouken@0
   214
        {
slouken@0
   215
    k=atoi(cp);
slouken@0
   216
    if ((k<0 || k>127) || (*cp < '0' || *cp > '9'))
slouken@0
   217
      {
slouken@0
   218
       ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
slouken@0
   219
         "%s: line %d: note must be between 0 and 127\n",
slouken@0
   220
          name, line);
slouken@700
   221
        close_file(fp);
slouken@0
   222
        return -2;
slouken@0
   223
      }
slouken@0
   224
    bank->tone[i].note=k;
slouken@0
   225
      }
slouken@0
   226
     else if (!strcmp(w[j], "pan"))
slouken@0
   227
      {
slouken@0
   228
    if (!strcmp(cp, "center"))
slouken@0
   229
      k=64;
slouken@0
   230
    else if (!strcmp(cp, "left"))
slouken@0
   231
      k=0;
slouken@0
   232
    else if (!strcmp(cp, "right"))
slouken@0
   233
      k=127;
slouken@0
   234
    else
slouken@0
   235
      k=((atoi(cp)+100) * 100) / 157;
slouken@0
   236
    if ((k<0 || k>127) ||
slouken@0
   237
       (k==0 && *cp!='-' && (*cp < '0' || *cp > '9')))
slouken@0
   238
      {
slouken@0
   239
       ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
slouken@0
   240
         "%s: line %d: panning must be left, right, "
slouken@0
   241
         "center, or between -100 and 100\n",
slouken@0
   242
         name, line);
slouken@700
   243
       close_file(fp);
slouken@0
   244
       return -2;
slouken@0
   245
      }
slouken@0
   246
    bank->tone[i].pan=k;
slouken@0
   247
      }
slouken@0
   248
     else if (!strcmp(w[j], "keep"))
slouken@0
   249
      {
slouken@0
   250
    if (!strcmp(cp, "env"))
slouken@0
   251
      bank->tone[i].strip_envelope=0;
slouken@0
   252
    else if (!strcmp(cp, "loop"))
slouken@0
   253
      bank->tone[i].strip_loop=0;
slouken@0
   254
    else
slouken@0
   255
      {
slouken@0
   256
        ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
slouken@0
   257
          "%s: line %d: keep must be env or loop\n", name, line);
slouken@700
   258
       close_file(fp);
slouken@0
   259
       return -2;
slouken@0
   260
      }
slouken@0
   261
      }
slouken@0
   262
     else if (!strcmp(w[j], "strip"))
slouken@0
   263
      {
slouken@0
   264
    if (!strcmp(cp, "env"))
slouken@0
   265
      bank->tone[i].strip_envelope=1;
slouken@0
   266
    else if (!strcmp(cp, "loop"))
slouken@0
   267
      bank->tone[i].strip_loop=1;
slouken@0
   268
    else if (!strcmp(cp, "tail"))
slouken@0
   269
      bank->tone[i].strip_tail=1;
slouken@0
   270
    else
slouken@0
   271
      {
slouken@0
   272
       ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
slouken@0
   273
         "%s: line %d: strip must be env, loop, or tail\n",
slouken@0
   274
         name, line);
slouken@700
   275
       close_file(fp);
slouken@0
   276
       return -2;
slouken@0
   277
      }
slouken@0
   278
      }
slouken@0
   279
     else
slouken@0
   280
      {
slouken@0
   281
    ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: line %d: bad patch option %s\n",
slouken@0
   282
      name, line, w[j]);
slouken@700
   283
    close_file(fp);
slouken@0
   284
    return -2;
slouken@0
   285
      }
slouken@0
   286
    }
slouken@0
   287
    }
slouken@0
   288
   }
slouken@0
   289
  if (ferror(fp))
slouken@0
   290
   {
slouken@0
   291
    ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Can't read from %s\n", name);
slouken@0
   292
    close_file(fp);
slouken@0
   293
    return -2;
slouken@0
   294
   }
slouken@0
   295
  close_file(fp);
slouken@0
   296
  return 0;
slouken@0
   297
}
slouken@0
   298
slouken@0
   299
int Timidity_Init(int rate, int format, int channels, int samples)
slouken@0
   300
{
slouken@416
   301
  const char *env = getenv("TIMIDITY_CFG");
slouken@416
   302
  if (!env || read_config_file(env)<0) {
slouken@416
   303
    if (read_config_file(CONFIG_FILE)<0) {
slouken@416
   304
      if (read_config_file(CONFIG_FILE_ETC)<0) {
slouken@604
   305
        if (read_config_file(CONFIG_FILE_ETC_TIMIDITY_FREEPATS)<0) {
slouken@604
   306
          return(-1);
slouken@604
   307
        }
patmandin@271
   308
      }
patmandin@263
   309
    }
slouken@0
   310
  }
slouken@0
   311
slouken@245
   312
  if (channels < 1 || channels == 3 || channels == 5 || channels > 6) return(-1);
slouken@245
   313
slouken@245
   314
  num_ochannels = channels;
slouken@245
   315
slouken@0
   316
  /* Set play mode parameters */
slouken@0
   317
  play_mode->rate = rate;
slouken@0
   318
  play_mode->encoding = 0;
slouken@0
   319
  if ( (format&0xFF) == 16 ) {
slouken@0
   320
    play_mode->encoding |= PE_16BIT;
slouken@0
   321
  }
slouken@0
   322
  if ( (format&0x8000) ) {
slouken@0
   323
    play_mode->encoding |= PE_SIGNED;
slouken@0
   324
  }
slouken@0
   325
  if ( channels == 1 ) {
slouken@0
   326
    play_mode->encoding |= PE_MONO;
slouken@0
   327
  } 
slouken@0
   328
  switch (format) {
slouken@0
   329
    case AUDIO_S8:
slouken@0
   330
      s32tobuf = s32tos8;
slouken@0
   331
      break;
slouken@0
   332
    case AUDIO_U8:
slouken@0
   333
      s32tobuf = s32tou8;
slouken@0
   334
      break;
slouken@0
   335
    case AUDIO_S16LSB:
slouken@0
   336
      s32tobuf = s32tos16l;
slouken@0
   337
      break;
slouken@0
   338
    case AUDIO_S16MSB:
slouken@0
   339
      s32tobuf = s32tos16b;
slouken@0
   340
      break;
slouken@0
   341
    case AUDIO_U16LSB:
slouken@0
   342
      s32tobuf = s32tou16l;
slouken@0
   343
      break;
slouken@0
   344
    case AUDIO_U16MSB:
slouken@0
   345
      s32tobuf = s32tou16b;
slouken@0
   346
      break;
slouken@0
   347
    default:
slouken@0
   348
      ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Unsupported audio format");
slouken@0
   349
      return(-1);
slouken@0
   350
  }
slouken@0
   351
  AUDIO_BUFFER_SIZE = samples;
slouken@0
   352
slouken@0
   353
  /* Allocate memory for mixing (WARNING:  Memory leak!) */
slouken@245
   354
  resample_buffer = safe_malloc(AUDIO_BUFFER_SIZE*sizeof(resample_t)+100);
slouken@245
   355
  common_buffer = safe_malloc(AUDIO_BUFFER_SIZE*num_ochannels*sizeof(int32));
slouken@0
   356
slouken@0
   357
  init_tables();
slouken@0
   358
slouken@0
   359
  if (ctl->open(0, 0)) {
slouken@0
   360
    ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Couldn't open %s\n", ctl->id_name);
slouken@0
   361
    return(-1);
slouken@0
   362
  }
slouken@0
   363
slouken@0
   364
  if (!control_ratio) {
slouken@0
   365
    control_ratio = play_mode->rate / CONTROLS_PER_SECOND;
slouken@0
   366
    if(control_ratio<1)
slouken@0
   367
      control_ratio=1;
slouken@0
   368
    else if (control_ratio > MAX_CONTROL_RATIO)
slouken@0
   369
      control_ratio=MAX_CONTROL_RATIO;
slouken@0
   370
  }
slouken@0
   371
  if (*def_instr_name)
slouken@0
   372
    set_default_instrument(def_instr_name);
slouken@0
   373
  return(0);
slouken@0
   374
}
slouken@0
   375
icculus@450
   376
char timidity_error[TIMIDITY_ERROR_SIZE] = "";
slouken@421
   377
const char *Timidity_Error(void)
slouken@0
   378
{
slouken@0
   379
  return(timidity_error);
slouken@0
   380
}
icculus@450
   381