timidity/timidity.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 15 Feb 2013 15:09:10 -0800
changeset 604 c932750a78ce
parent 603 72a476dc43ea
child 700 a9867904addc
permissions -rw-r--r--
Applied Debian patch 05_fallback_to_freepats.patch
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@0
    71
        return -2;
slouken@245
    72
       }
slouken@245
    73
      for (i=1; i<words; i++)
slouken@245
    74
        add_to_pathlist(w[i]);
slouken@245
    75
    }
slouken@245
    76
  else if (!strcmp(w[0], "source"))
slouken@0
    77
  {
slouken@0
    78
    if (words < 2)
slouken@0
    79
      {
slouken@0
    80
        ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
slouken@0
    81
          "%s: line %d: No file name given\n", name, line);
slouken@0
    82
        return -2;
slouken@0
    83
     }
slouken@0
    84
    for (i=1; i<words; i++)
slouken@0
    85
      {
slouken@0
    86
        rcf_count++;
slouken@0
    87
      read_config_file(w[i]);
slouken@0
    88
        rcf_count--;
slouken@0
    89
      }
slouken@0
    90
  }
slouken@0
    91
      else if (!strcmp(w[0], "default"))
slouken@0
    92
  {
slouken@0
    93
    if (words != 2)
slouken@0
    94
      {
slouken@0
    95
        ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
slouken@0
    96
        "%s: line %d: Must specify exactly one patch name\n",
slouken@0
    97
          name, line);
slouken@0
    98
        return -2;
slouken@0
    99
      }
slouken@0
   100
    strncpy(def_instr_name, w[1], 255);
slouken@0
   101
    def_instr_name[255]='\0';
slouken@0
   102
  }
slouken@0
   103
    else if (!strcmp(w[0], "drumset"))
slouken@0
   104
  {
slouken@0
   105
    if (words < 2)
slouken@0
   106
      {
slouken@0
   107
        ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
slouken@0
   108
          "%s: line %d: No drum set number given\n", 
slouken@0
   109
          name, line);
slouken@0
   110
      return -2;
slouken@0
   111
      }
slouken@0
   112
    i=atoi(w[1]);
slouken@0
   113
    if (i<0 || i>127)
slouken@0
   114
     {
slouken@0
   115
        ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
slouken@0
   116
          "%s: line %d: Drum set must be between 0 and 127\n",
slouken@0
   117
        name, line);
slouken@0
   118
        return -2;
slouken@0
   119
     }
slouken@0
   120
    if (!drumset[i])
slouken@0
   121
      {
slouken@0
   122
        drumset[i]=safe_malloc(sizeof(ToneBank));
slouken@0
   123
      memset(drumset[i], 0, sizeof(ToneBank));
slouken@0
   124
     }
slouken@0
   125
    bank=drumset[i];
slouken@0
   126
  }
slouken@0
   127
    else if (!strcmp(w[0], "bank"))
slouken@0
   128
  {
slouken@0
   129
    if (words < 2)
slouken@0
   130
     {
slouken@0
   131
        ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
slouken@0
   132
          "%s: line %d: No bank number given\n", 
slouken@0
   133
        name, line);
slouken@0
   134
        return -2;
slouken@0
   135
     }
slouken@0
   136
    i=atoi(w[1]);
slouken@0
   137
    if (i<0 || i>127)
slouken@0
   138
      {
slouken@0
   139
        ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
slouken@0
   140
          "%s: line %d: Tone bank must be between 0 and 127\n",
slouken@0
   141
        name, line);
slouken@0
   142
        return -2;
slouken@0
   143
      }
slouken@0
   144
    if (!tonebank[i])
slouken@0
   145
     {
slouken@0
   146
      tonebank[i]=safe_malloc(sizeof(ToneBank));
slouken@0
   147
        memset(tonebank[i], 0, sizeof(ToneBank));
slouken@0
   148
      }
slouken@0
   149
    bank=tonebank[i];
slouken@0
   150
  }
slouken@0
   151
      else {
slouken@0
   152
  if ((words < 2) || (*w[0] < '0' || *w[0] > '9'))
slouken@0
   153
    {
slouken@0
   154
     ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
slouken@0
   155
        "%s: line %d: syntax error\n", name, line);
slouken@603
   156
     continue;
slouken@0
   157
    }
slouken@0
   158
  i=atoi(w[0]);
slouken@0
   159
  if (i<0 || i>127)
slouken@0
   160
    {
slouken@0
   161
      ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
slouken@0
   162
        "%s: line %d: Program must be between 0 and 127\n",
slouken@0
   163
        name, line);
slouken@0
   164
      return -2;
slouken@0
   165
    }
slouken@0
   166
  if (!bank)
slouken@0
   167
    {
slouken@0
   168
      ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
slouken@0
   169
       "%s: line %d: Must specify tone bank or drum set "
slouken@0
   170
        "before assignment\n",
slouken@0
   171
        name, line);
slouken@0
   172
     return -2;
slouken@0
   173
    }
slouken@0
   174
  if (bank->tone[i].name)
slouken@0
   175
    free(bank->tone[i].name);
slouken@0
   176
  strcpy((bank->tone[i].name=safe_malloc(strlen(w[1])+1)),w[1]);
slouken@0
   177
  bank->tone[i].note=bank->tone[i].amp=bank->tone[i].pan=
slouken@0
   178
    bank->tone[i].strip_loop=bank->tone[i].strip_envelope=
slouken@0
   179
      bank->tone[i].strip_tail=-1;
slouken@0
   180
slouken@0
   181
  for (j=2; j<words; j++)
slouken@0
   182
    {
slouken@0
   183
      if (!(cp=strchr(w[j], '=')))
slouken@0
   184
        {
slouken@0
   185
    ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: line %d: bad patch option %s\n",
slouken@0
   186
      name, line, w[j]);
slouken@0
   187
    return -2;
slouken@0
   188
        }
slouken@0
   189
      *cp++=0;
slouken@0
   190
      if (!strcmp(w[j], "amp"))
slouken@0
   191
      {
slouken@0
   192
    k=atoi(cp);
slouken@0
   193
    if ((k<0 || k>MAX_AMPLIFICATION) || (*cp < '0' || *cp > '9'))
slouken@0
   194
      {
slouken@0
   195
       ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
slouken@0
   196
          "%s: line %d: amplification must be between "
slouken@0
   197
         "0 and %d\n", name, line, MAX_AMPLIFICATION);
slouken@0
   198
       return -2;
slouken@0
   199
      }
slouken@0
   200
    bank->tone[i].amp=k;
slouken@0
   201
        }
slouken@0
   202
      else if (!strcmp(w[j], "note"))
slouken@0
   203
        {
slouken@0
   204
    k=atoi(cp);
slouken@0
   205
    if ((k<0 || k>127) || (*cp < '0' || *cp > '9'))
slouken@0
   206
      {
slouken@0
   207
       ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
slouken@0
   208
         "%s: line %d: note must be between 0 and 127\n",
slouken@0
   209
          name, line);
slouken@0
   210
        return -2;
slouken@0
   211
      }
slouken@0
   212
    bank->tone[i].note=k;
slouken@0
   213
      }
slouken@0
   214
     else if (!strcmp(w[j], "pan"))
slouken@0
   215
      {
slouken@0
   216
    if (!strcmp(cp, "center"))
slouken@0
   217
      k=64;
slouken@0
   218
    else if (!strcmp(cp, "left"))
slouken@0
   219
      k=0;
slouken@0
   220
    else if (!strcmp(cp, "right"))
slouken@0
   221
      k=127;
slouken@0
   222
    else
slouken@0
   223
      k=((atoi(cp)+100) * 100) / 157;
slouken@0
   224
    if ((k<0 || k>127) ||
slouken@0
   225
       (k==0 && *cp!='-' && (*cp < '0' || *cp > '9')))
slouken@0
   226
      {
slouken@0
   227
       ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
slouken@0
   228
         "%s: line %d: panning must be left, right, "
slouken@0
   229
         "center, or between -100 and 100\n",
slouken@0
   230
         name, line);
slouken@0
   231
       return -2;
slouken@0
   232
      }
slouken@0
   233
    bank->tone[i].pan=k;
slouken@0
   234
      }
slouken@0
   235
     else if (!strcmp(w[j], "keep"))
slouken@0
   236
      {
slouken@0
   237
    if (!strcmp(cp, "env"))
slouken@0
   238
      bank->tone[i].strip_envelope=0;
slouken@0
   239
    else if (!strcmp(cp, "loop"))
slouken@0
   240
      bank->tone[i].strip_loop=0;
slouken@0
   241
    else
slouken@0
   242
      {
slouken@0
   243
        ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
slouken@0
   244
          "%s: line %d: keep must be env or loop\n", name, line);
slouken@0
   245
       return -2;
slouken@0
   246
      }
slouken@0
   247
      }
slouken@0
   248
     else if (!strcmp(w[j], "strip"))
slouken@0
   249
      {
slouken@0
   250
    if (!strcmp(cp, "env"))
slouken@0
   251
      bank->tone[i].strip_envelope=1;
slouken@0
   252
    else if (!strcmp(cp, "loop"))
slouken@0
   253
      bank->tone[i].strip_loop=1;
slouken@0
   254
    else if (!strcmp(cp, "tail"))
slouken@0
   255
      bank->tone[i].strip_tail=1;
slouken@0
   256
    else
slouken@0
   257
      {
slouken@0
   258
       ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
slouken@0
   259
         "%s: line %d: strip must be env, loop, or tail\n",
slouken@0
   260
         name, line);
slouken@0
   261
       return -2;
slouken@0
   262
      }
slouken@0
   263
      }
slouken@0
   264
     else
slouken@0
   265
      {
slouken@0
   266
    ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: line %d: bad patch option %s\n",
slouken@0
   267
      name, line, w[j]);
slouken@0
   268
    return -2;
slouken@0
   269
      }
slouken@0
   270
    }
slouken@0
   271
    }
slouken@0
   272
   }
slouken@0
   273
  if (ferror(fp))
slouken@0
   274
   {
slouken@0
   275
    ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Can't read from %s\n", name);
slouken@0
   276
    close_file(fp);
slouken@0
   277
    return -2;
slouken@0
   278
   }
slouken@0
   279
  close_file(fp);
slouken@0
   280
  return 0;
slouken@0
   281
}
slouken@0
   282
slouken@0
   283
int Timidity_Init(int rate, int format, int channels, int samples)
slouken@0
   284
{
slouken@416
   285
  const char *env = getenv("TIMIDITY_CFG");
slouken@416
   286
  if (!env || read_config_file(env)<0) {
slouken@416
   287
    if (read_config_file(CONFIG_FILE)<0) {
slouken@416
   288
      if (read_config_file(CONFIG_FILE_ETC)<0) {
slouken@604
   289
        if (read_config_file(CONFIG_FILE_ETC_TIMIDITY_FREEPATS)<0) {
slouken@604
   290
          return(-1);
slouken@604
   291
        }
patmandin@271
   292
      }
patmandin@263
   293
    }
slouken@0
   294
  }
slouken@0
   295
slouken@245
   296
  if (channels < 1 || channels == 3 || channels == 5 || channels > 6) return(-1);
slouken@245
   297
slouken@245
   298
  num_ochannels = channels;
slouken@245
   299
slouken@0
   300
  /* Set play mode parameters */
slouken@0
   301
  play_mode->rate = rate;
slouken@0
   302
  play_mode->encoding = 0;
slouken@0
   303
  if ( (format&0xFF) == 16 ) {
slouken@0
   304
    play_mode->encoding |= PE_16BIT;
slouken@0
   305
  }
slouken@0
   306
  if ( (format&0x8000) ) {
slouken@0
   307
    play_mode->encoding |= PE_SIGNED;
slouken@0
   308
  }
slouken@0
   309
  if ( channels == 1 ) {
slouken@0
   310
    play_mode->encoding |= PE_MONO;
slouken@0
   311
  } 
slouken@0
   312
  switch (format) {
slouken@0
   313
    case AUDIO_S8:
slouken@0
   314
      s32tobuf = s32tos8;
slouken@0
   315
      break;
slouken@0
   316
    case AUDIO_U8:
slouken@0
   317
      s32tobuf = s32tou8;
slouken@0
   318
      break;
slouken@0
   319
    case AUDIO_S16LSB:
slouken@0
   320
      s32tobuf = s32tos16l;
slouken@0
   321
      break;
slouken@0
   322
    case AUDIO_S16MSB:
slouken@0
   323
      s32tobuf = s32tos16b;
slouken@0
   324
      break;
slouken@0
   325
    case AUDIO_U16LSB:
slouken@0
   326
      s32tobuf = s32tou16l;
slouken@0
   327
      break;
slouken@0
   328
    case AUDIO_U16MSB:
slouken@0
   329
      s32tobuf = s32tou16b;
slouken@0
   330
      break;
slouken@0
   331
    default:
slouken@0
   332
      ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Unsupported audio format");
slouken@0
   333
      return(-1);
slouken@0
   334
  }
slouken@0
   335
  AUDIO_BUFFER_SIZE = samples;
slouken@0
   336
slouken@0
   337
  /* Allocate memory for mixing (WARNING:  Memory leak!) */
slouken@245
   338
  resample_buffer = safe_malloc(AUDIO_BUFFER_SIZE*sizeof(resample_t)+100);
slouken@245
   339
  common_buffer = safe_malloc(AUDIO_BUFFER_SIZE*num_ochannels*sizeof(int32));
slouken@0
   340
slouken@0
   341
  init_tables();
slouken@0
   342
slouken@0
   343
  if (ctl->open(0, 0)) {
slouken@0
   344
    ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Couldn't open %s\n", ctl->id_name);
slouken@0
   345
    return(-1);
slouken@0
   346
  }
slouken@0
   347
slouken@0
   348
  if (!control_ratio) {
slouken@0
   349
    control_ratio = play_mode->rate / CONTROLS_PER_SECOND;
slouken@0
   350
    if(control_ratio<1)
slouken@0
   351
      control_ratio=1;
slouken@0
   352
    else if (control_ratio > MAX_CONTROL_RATIO)
slouken@0
   353
      control_ratio=MAX_CONTROL_RATIO;
slouken@0
   354
  }
slouken@0
   355
  if (*def_instr_name)
slouken@0
   356
    set_default_instrument(def_instr_name);
slouken@0
   357
  return(0);
slouken@0
   358
}
slouken@0
   359
icculus@450
   360
char timidity_error[TIMIDITY_ERROR_SIZE] = "";
slouken@421
   361
const char *Timidity_Error(void)
slouken@0
   362
{
slouken@0
   363
  return(timidity_error);
slouken@0
   364
}
icculus@450
   365