timidity/timidity.c
author Sam Lantinga <slouken@lokigames.com>
Tue, 21 Dec 1999 17:13:05 +0000
changeset 24 a25bb2d59ce8
parent 0 4ce2db4db959
child 245 63b3650714de
permissions -rw-r--r--
Updated for SDL 1.0.1
slouken@0
     1
/*
slouken@0
     2
slouken@0
     3
    TiMidity -- Experimental MIDI to WAVE converter
slouken@0
     4
    Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>
slouken@0
     5
slouken@0
     6
	 This program is free software; you can redistribute it and/or modify
slouken@0
     7
	 it under the terms of the GNU General Public License as published by
slouken@0
     8
    the Free Software Foundation; either version 2 of the License, or
slouken@0
     9
	 (at your option) any later version.
slouken@0
    10
slouken@0
    11
    This program is distributed in the hope that it will be useful,
slouken@0
    12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
slouken@0
    14
	 GNU General Public License for more details.
slouken@0
    15
slouken@0
    16
    You should have received a copy of the GNU General Public License
slouken@0
    17
    along with this program; if not, write to the Free Software
slouken@0
    18
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
slouken@0
    19
slouken@0
    20
*/
slouken@0
    21
#include <stdio.h>
slouken@0
    22
#include <stdlib.h>
slouken@0
    23
#include <string.h>
slouken@0
    24
slouken@24
    25
#include "SDL.h"
slouken@0
    26
#include "config.h"
slouken@0
    27
#include "common.h"
slouken@0
    28
#include "instrum.h"
slouken@0
    29
#include "playmidi.h"
slouken@0
    30
#include "readmidi.h"
slouken@0
    31
#include "output.h"
slouken@0
    32
#include "controls.h"
slouken@0
    33
#include "timidity.h"
slouken@0
    34
slouken@0
    35
#include "tables.h"
slouken@0
    36
slouken@0
    37
void (*s32tobuf)(void *dp, int32 *lp, int32 c);
slouken@0
    38
int free_instruments_afterwards=0;
slouken@0
    39
static char def_instr_name[256]="";
slouken@0
    40
slouken@0
    41
int AUDIO_BUFFER_SIZE;
slouken@0
    42
sample_t *resample_buffer;
slouken@0
    43
int32 *common_buffer;
slouken@0
    44
slouken@0
    45
#define MAXWORDS 10
slouken@0
    46
slouken@0
    47
static int read_config_file(char *name)
slouken@0
    48
{
slouken@0
    49
  FILE *fp;
slouken@0
    50
  char tmp[1024], *w[MAXWORDS], *cp;
slouken@0
    51
  ToneBank *bank=0;
slouken@0
    52
  int i, j, k, line=0, words;
slouken@0
    53
  static int rcf_count=0;
slouken@0
    54
slouken@0
    55
  if (rcf_count>50)
slouken@0
    56
   {
slouken@0
    57
    ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
slouken@0
    58
      "Probable source loop in configuration files");
slouken@0
    59
    return (-1);
slouken@0
    60
   }
slouken@0
    61
slouken@0
    62
  if (!(fp=open_file(name, 1, OF_VERBOSE)))
slouken@0
    63
   return -1;
slouken@0
    64
slouken@0
    65
  while (fgets(tmp, sizeof(tmp), fp))
slouken@0
    66
   {
slouken@0
    67
      line++;
slouken@0
    68
    w[words=0]=strtok(tmp, " \t\r\n\240");
slouken@0
    69
    if (!w[0] || (*w[0]=='#')) continue;
slouken@0
    70
      while (w[words] && (words < MAXWORDS))
slouken@0
    71
  w[++words]=strtok(0," \t\r\n\240");
slouken@0
    72
      if (!strcmp(w[0], "dir"))
slouken@0
    73
  {
slouken@0
    74
    if (words < 2)
slouken@0
    75
     {
slouken@0
    76
        ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
slouken@0
    77
          "%s: line %d: No directory given\n", name, line);
slouken@0
    78
        return -2;
slouken@0
    79
     }
slouken@0
    80
    for (i=1; i<words; i++)
slouken@0
    81
      add_to_pathlist(w[i]);
slouken@0
    82
  }
slouken@0
    83
    else if (!strcmp(w[0], "source"))
slouken@0
    84
  {
slouken@0
    85
    if (words < 2)
slouken@0
    86
      {
slouken@0
    87
        ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
slouken@0
    88
          "%s: line %d: No file name given\n", name, line);
slouken@0
    89
        return -2;
slouken@0
    90
     }
slouken@0
    91
    for (i=1; i<words; i++)
slouken@0
    92
      {
slouken@0
    93
        rcf_count++;
slouken@0
    94
      read_config_file(w[i]);
slouken@0
    95
        rcf_count--;
slouken@0
    96
      }
slouken@0
    97
  }
slouken@0
    98
      else if (!strcmp(w[0], "default"))
slouken@0
    99
  {
slouken@0
   100
    if (words != 2)
slouken@0
   101
      {
slouken@0
   102
        ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
slouken@0
   103
        "%s: line %d: Must specify exactly one patch name\n",
slouken@0
   104
          name, line);
slouken@0
   105
        return -2;
slouken@0
   106
      }
slouken@0
   107
    strncpy(def_instr_name, w[1], 255);
slouken@0
   108
    def_instr_name[255]='\0';
slouken@0
   109
  }
slouken@0
   110
    else if (!strcmp(w[0], "drumset"))
slouken@0
   111
  {
slouken@0
   112
    if (words < 2)
slouken@0
   113
      {
slouken@0
   114
        ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
slouken@0
   115
          "%s: line %d: No drum set number given\n", 
slouken@0
   116
          name, line);
slouken@0
   117
      return -2;
slouken@0
   118
      }
slouken@0
   119
    i=atoi(w[1]);
slouken@0
   120
    if (i<0 || i>127)
slouken@0
   121
     {
slouken@0
   122
        ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
slouken@0
   123
          "%s: line %d: Drum set must be between 0 and 127\n",
slouken@0
   124
        name, line);
slouken@0
   125
        return -2;
slouken@0
   126
     }
slouken@0
   127
    if (!drumset[i])
slouken@0
   128
      {
slouken@0
   129
        drumset[i]=safe_malloc(sizeof(ToneBank));
slouken@0
   130
      memset(drumset[i], 0, sizeof(ToneBank));
slouken@0
   131
     }
slouken@0
   132
    bank=drumset[i];
slouken@0
   133
  }
slouken@0
   134
    else if (!strcmp(w[0], "bank"))
slouken@0
   135
  {
slouken@0
   136
    if (words < 2)
slouken@0
   137
     {
slouken@0
   138
        ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
slouken@0
   139
          "%s: line %d: No bank number given\n", 
slouken@0
   140
        name, line);
slouken@0
   141
        return -2;
slouken@0
   142
     }
slouken@0
   143
    i=atoi(w[1]);
slouken@0
   144
    if (i<0 || i>127)
slouken@0
   145
      {
slouken@0
   146
        ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
slouken@0
   147
          "%s: line %d: Tone bank must be between 0 and 127\n",
slouken@0
   148
        name, line);
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@0
   163
     return -2;
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@0
   171
      return -2;
slouken@0
   172
    }
slouken@0
   173
  if (!bank)
slouken@0
   174
    {
slouken@0
   175
      ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
slouken@0
   176
       "%s: line %d: Must specify tone bank or drum set "
slouken@0
   177
        "before assignment\n",
slouken@0
   178
        name, line);
slouken@0
   179
     return -2;
slouken@0
   180
    }
slouken@0
   181
  if (bank->tone[i].name)
slouken@0
   182
    free(bank->tone[i].name);
slouken@0
   183
  strcpy((bank->tone[i].name=safe_malloc(strlen(w[1])+1)),w[1]);
slouken@0
   184
  bank->tone[i].note=bank->tone[i].amp=bank->tone[i].pan=
slouken@0
   185
    bank->tone[i].strip_loop=bank->tone[i].strip_envelope=
slouken@0
   186
      bank->tone[i].strip_tail=-1;
slouken@0
   187
slouken@0
   188
  for (j=2; j<words; j++)
slouken@0
   189
    {
slouken@0
   190
      if (!(cp=strchr(w[j], '=')))
slouken@0
   191
        {
slouken@0
   192
    ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: line %d: bad patch option %s\n",
slouken@0
   193
      name, line, w[j]);
slouken@0
   194
    return -2;
slouken@0
   195
        }
slouken@0
   196
      *cp++=0;
slouken@0
   197
      if (!strcmp(w[j], "amp"))
slouken@0
   198
      {
slouken@0
   199
    k=atoi(cp);
slouken@0
   200
    if ((k<0 || k>MAX_AMPLIFICATION) || (*cp < '0' || *cp > '9'))
slouken@0
   201
      {
slouken@0
   202
       ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
slouken@0
   203
          "%s: line %d: amplification must be between "
slouken@0
   204
         "0 and %d\n", name, line, MAX_AMPLIFICATION);
slouken@0
   205
       return -2;
slouken@0
   206
      }
slouken@0
   207
    bank->tone[i].amp=k;
slouken@0
   208
        }
slouken@0
   209
      else if (!strcmp(w[j], "note"))
slouken@0
   210
        {
slouken@0
   211
    k=atoi(cp);
slouken@0
   212
    if ((k<0 || k>127) || (*cp < '0' || *cp > '9'))
slouken@0
   213
      {
slouken@0
   214
       ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
slouken@0
   215
         "%s: line %d: note must be between 0 and 127\n",
slouken@0
   216
          name, line);
slouken@0
   217
        return -2;
slouken@0
   218
      }
slouken@0
   219
    bank->tone[i].note=k;
slouken@0
   220
      }
slouken@0
   221
     else if (!strcmp(w[j], "pan"))
slouken@0
   222
      {
slouken@0
   223
    if (!strcmp(cp, "center"))
slouken@0
   224
      k=64;
slouken@0
   225
    else if (!strcmp(cp, "left"))
slouken@0
   226
      k=0;
slouken@0
   227
    else if (!strcmp(cp, "right"))
slouken@0
   228
      k=127;
slouken@0
   229
    else
slouken@0
   230
      k=((atoi(cp)+100) * 100) / 157;
slouken@0
   231
    if ((k<0 || k>127) ||
slouken@0
   232
       (k==0 && *cp!='-' && (*cp < '0' || *cp > '9')))
slouken@0
   233
      {
slouken@0
   234
       ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
slouken@0
   235
         "%s: line %d: panning must be left, right, "
slouken@0
   236
         "center, or between -100 and 100\n",
slouken@0
   237
         name, line);
slouken@0
   238
       return -2;
slouken@0
   239
      }
slouken@0
   240
    bank->tone[i].pan=k;
slouken@0
   241
      }
slouken@0
   242
     else if (!strcmp(w[j], "keep"))
slouken@0
   243
      {
slouken@0
   244
    if (!strcmp(cp, "env"))
slouken@0
   245
      bank->tone[i].strip_envelope=0;
slouken@0
   246
    else if (!strcmp(cp, "loop"))
slouken@0
   247
      bank->tone[i].strip_loop=0;
slouken@0
   248
    else
slouken@0
   249
      {
slouken@0
   250
        ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
slouken@0
   251
          "%s: line %d: keep must be env or loop\n", name, line);
slouken@0
   252
       return -2;
slouken@0
   253
      }
slouken@0
   254
      }
slouken@0
   255
     else if (!strcmp(w[j], "strip"))
slouken@0
   256
      {
slouken@0
   257
    if (!strcmp(cp, "env"))
slouken@0
   258
      bank->tone[i].strip_envelope=1;
slouken@0
   259
    else if (!strcmp(cp, "loop"))
slouken@0
   260
      bank->tone[i].strip_loop=1;
slouken@0
   261
    else if (!strcmp(cp, "tail"))
slouken@0
   262
      bank->tone[i].strip_tail=1;
slouken@0
   263
    else
slouken@0
   264
      {
slouken@0
   265
       ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
slouken@0
   266
         "%s: line %d: strip must be env, loop, or tail\n",
slouken@0
   267
         name, line);
slouken@0
   268
       return -2;
slouken@0
   269
      }
slouken@0
   270
      }
slouken@0
   271
     else
slouken@0
   272
      {
slouken@0
   273
    ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: line %d: bad patch option %s\n",
slouken@0
   274
      name, line, w[j]);
slouken@0
   275
    return -2;
slouken@0
   276
      }
slouken@0
   277
    }
slouken@0
   278
    }
slouken@0
   279
   }
slouken@0
   280
  if (ferror(fp))
slouken@0
   281
   {
slouken@0
   282
    ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Can't read from %s\n", name);
slouken@0
   283
    close_file(fp);
slouken@0
   284
    return -2;
slouken@0
   285
   }
slouken@0
   286
  close_file(fp);
slouken@0
   287
  return 0;
slouken@0
   288
}
slouken@0
   289
slouken@0
   290
int Timidity_Init(int rate, int format, int channels, int samples)
slouken@0
   291
{
slouken@0
   292
  if (read_config_file(CONFIG_FILE)<0) {
slouken@0
   293
    return(-1);
slouken@0
   294
  }
slouken@0
   295
slouken@0
   296
  /* Set play mode parameters */
slouken@0
   297
  play_mode->rate = rate;
slouken@0
   298
  play_mode->encoding = 0;
slouken@0
   299
  if ( (format&0xFF) == 16 ) {
slouken@0
   300
    play_mode->encoding |= PE_16BIT;
slouken@0
   301
  }
slouken@0
   302
  if ( (format&0x8000) ) {
slouken@0
   303
    play_mode->encoding |= PE_SIGNED;
slouken@0
   304
  }
slouken@0
   305
  if ( channels == 1 ) {
slouken@0
   306
    play_mode->encoding |= PE_MONO;
slouken@0
   307
  } 
slouken@0
   308
  switch (format) {
slouken@0
   309
    case AUDIO_S8:
slouken@0
   310
      s32tobuf = s32tos8;
slouken@0
   311
      break;
slouken@0
   312
    case AUDIO_U8:
slouken@0
   313
      s32tobuf = s32tou8;
slouken@0
   314
      break;
slouken@0
   315
    case AUDIO_S16LSB:
slouken@0
   316
      s32tobuf = s32tos16l;
slouken@0
   317
      break;
slouken@0
   318
    case AUDIO_S16MSB:
slouken@0
   319
      s32tobuf = s32tos16b;
slouken@0
   320
      break;
slouken@0
   321
    case AUDIO_U16LSB:
slouken@0
   322
      s32tobuf = s32tou16l;
slouken@0
   323
      break;
slouken@0
   324
    case AUDIO_U16MSB:
slouken@0
   325
      s32tobuf = s32tou16b;
slouken@0
   326
      break;
slouken@0
   327
    default:
slouken@0
   328
      ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Unsupported audio format");
slouken@0
   329
      return(-1);
slouken@0
   330
  }
slouken@0
   331
  AUDIO_BUFFER_SIZE = samples;
slouken@0
   332
slouken@0
   333
  /* Allocate memory for mixing (WARNING:  Memory leak!) */
slouken@0
   334
  resample_buffer = safe_malloc(AUDIO_BUFFER_SIZE*sizeof(sample_t));
slouken@0
   335
  common_buffer = safe_malloc(AUDIO_BUFFER_SIZE*2*sizeof(int32));
slouken@0
   336
slouken@0
   337
  init_tables();
slouken@0
   338
slouken@0
   339
  if (ctl->open(0, 0)) {
slouken@0
   340
    ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Couldn't open %s\n", ctl->id_name);
slouken@0
   341
    return(-1);
slouken@0
   342
  }
slouken@0
   343
slouken@0
   344
  if (!control_ratio) {
slouken@0
   345
    control_ratio = play_mode->rate / CONTROLS_PER_SECOND;
slouken@0
   346
    if(control_ratio<1)
slouken@0
   347
      control_ratio=1;
slouken@0
   348
    else if (control_ratio > MAX_CONTROL_RATIO)
slouken@0
   349
      control_ratio=MAX_CONTROL_RATIO;
slouken@0
   350
  }
slouken@0
   351
  if (*def_instr_name)
slouken@0
   352
    set_default_instrument(def_instr_name);
slouken@0
   353
  return(0);
slouken@0
   354
}
slouken@0
   355
slouken@0
   356
char timidity_error[1024] = "";
slouken@0
   357
char *Timidity_Error(void)
slouken@0
   358
{
slouken@0
   359
  return(timidity_error);
slouken@0
   360
}