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