timidity/instrum.c
author Ozkan Sezer <sezeroz@gmail.com>
Sat, 13 Oct 2018 23:02:04 +0300
branchSDL-1.2
changeset 908 6b860486ce24
parent 518 8bc9b5fd2aae
child 642 dae7bb0a66b1
permissions -rw-r--r--
Mix_InitMP3: unload dll if mpg123_init() fails.
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@0
     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 <string.h>
slouken@0
    11
#include <stdlib.h>
slouken@0
    12
slouken@0
    13
#include "config.h"
slouken@0
    14
#include "common.h"
slouken@0
    15
#include "instrum.h"
slouken@0
    16
#include "playmidi.h"
slouken@0
    17
#include "output.h"
slouken@319
    18
#include "ctrlmode.h"
slouken@0
    19
#include "resample.h"
slouken@0
    20
#include "tables.h"
slouken@0
    21
#include "filter.h"
slouken@0
    22
slouken@0
    23
/* Some functions get aggravated if not even the standard banks are 
slouken@0
    24
   available. */
slouken@0
    25
static ToneBank standard_tonebank, standard_drumset;
slouken@0
    26
ToneBank 
slouken@245
    27
  *tonebank[MAXBANK]={&standard_tonebank},
slouken@245
    28
  *drumset[MAXBANK]={&standard_drumset};
slouken@0
    29
slouken@0
    30
/* This is a special instrument, used for all melodic programs */
slouken@245
    31
InstrumentLayer *default_instrument=0;
slouken@0
    32
slouken@0
    33
/* This is only used for tracks that don't specify a program */
slouken@0
    34
int default_program=DEFAULT_PROGRAM;
slouken@0
    35
slouken@0
    36
int antialiasing_allowed=0;
slouken@0
    37
#ifdef FAST_DECAY
slouken@0
    38
int fast_decay=1;
slouken@0
    39
#else
slouken@0
    40
int fast_decay=0;
slouken@0
    41
#endif
slouken@0
    42
slouken@245
    43
slouken@245
    44
int current_tune_number = 0;
slouken@245
    45
int last_tune_purged = 0;
slouken@245
    46
int current_patch_memory = 0;
slouken@245
    47
int max_patch_memory = 60000000;
slouken@245
    48
slouken@245
    49
static void purge_as_required(void);
slouken@245
    50
slouken@0
    51
static void free_instrument(Instrument *ip)
slouken@0
    52
{
slouken@0
    53
  Sample *sp;
slouken@0
    54
  int i;
slouken@0
    55
  if (!ip) return;
slouken@245
    56
slouken@245
    57
  if (!ip->contents)
slouken@0
    58
  for (i=0; i<ip->samples; i++)
slouken@0
    59
    {
slouken@0
    60
      sp=&(ip->sample[i]);
slouken@245
    61
      if (sp->data) free(sp->data);
slouken@0
    62
    }
slouken@0
    63
  free(ip->sample);
slouken@245
    64
slouken@245
    65
  if (!ip->contents)
slouken@245
    66
  for (i=0; i<ip->right_samples; i++)
slouken@245
    67
    {
slouken@245
    68
      sp=&(ip->right_sample[i]);
slouken@245
    69
      if (sp->data) free(sp->data);
slouken@245
    70
    }
slouken@245
    71
  if (ip->right_sample)
slouken@245
    72
    free(ip->right_sample);
slouken@0
    73
  free(ip);
slouken@0
    74
}
slouken@0
    75
slouken@245
    76
slouken@245
    77
static void free_layer(InstrumentLayer *lp)
slouken@245
    78
{
slouken@245
    79
  InstrumentLayer *next;
slouken@245
    80
slouken@245
    81
  current_patch_memory -= lp->size;
slouken@245
    82
slouken@245
    83
  for (; lp; lp = next)
slouken@245
    84
   {
slouken@245
    85
     next = lp->next;
slouken@245
    86
     free_instrument(lp->instrument);
slouken@245
    87
     free(lp);
slouken@245
    88
   }
slouken@245
    89
}
slouken@245
    90
slouken@0
    91
static void free_bank(int dr, int b)
slouken@0
    92
{
slouken@0
    93
  int i;
slouken@0
    94
  ToneBank *bank=((dr) ? drumset[b] : tonebank[b]);
slouken@245
    95
  for (i=0; i<MAXPROG; i++)
patmandin@264
    96
  {
slouken@245
    97
    if (bank->tone[i].layer)
patmandin@264
    98
    {
patmandin@264
    99
	  /* Not that this could ever happen, of course */
patmandin@264
   100
	  if (bank->tone[i].layer != MAGIC_LOAD_INSTRUMENT)
slouken@245
   101
	  {
slouken@245
   102
	    free_layer(bank->tone[i].layer);
patmandin@264
   103
	    bank->tone[i].layer=NULL;
slouken@245
   104
	    bank->tone[i].last_used=-1;
slouken@245
   105
	  }
patmandin@264
   106
    }
patmandin@264
   107
    if (bank->tone[i].name)
patmandin@264
   108
    {
patmandin@264
   109
      free(bank->tone[i].name);
patmandin@264
   110
      bank->tone[i].name = NULL;
patmandin@264
   111
    }
patmandin@264
   112
  }
slouken@0
   113
}
slouken@0
   114
slouken@245
   115
slouken@245
   116
static void free_old_bank(int dr, int b, int how_old)
slouken@245
   117
{
slouken@245
   118
  int i;
slouken@245
   119
  ToneBank *bank=((dr) ? drumset[b] : tonebank[b]);
slouken@245
   120
  for (i=0; i<MAXPROG; i++)
slouken@245
   121
    if (bank->tone[i].layer && bank->tone[i].last_used < how_old)
slouken@245
   122
      {
slouken@245
   123
	if (bank->tone[i].layer != MAGIC_LOAD_INSTRUMENT)
slouken@245
   124
	  {
slouken@245
   125
	    ctl->cmsg(CMSG_INFO, VERB_DEBUG,
slouken@245
   126
		"Unloading %s %s[%d,%d] - last used %d.",
slouken@245
   127
		(dr)? "drum" : "inst", bank->tone[i].name,
slouken@245
   128
		i, b, bank->tone[i].last_used);
slouken@245
   129
	    free_layer(bank->tone[i].layer);
patmandin@264
   130
	    bank->tone[i].layer=NULL;
slouken@245
   131
	    bank->tone[i].last_used=-1;
slouken@245
   132
	  }
slouken@245
   133
      }
slouken@245
   134
}
slouken@245
   135
slouken@245
   136
slouken@245
   137
int32 convert_envelope_rate_attack(uint8 rate, uint8 fastness)
slouken@245
   138
{
slouken@245
   139
  int32 r;
slouken@245
   140
slouken@245
   141
  r=3-((rate>>6) & 0x3);
slouken@245
   142
  r*=3;
slouken@245
   143
  r = (int32)(rate & 0x3f) << r; /* 6.9 fixed point */
slouken@245
   144
slouken@245
   145
  /* 15.15 fixed point. */
slouken@245
   146
  return (((r * 44100) / play_mode->rate) * control_ratio) 
slouken@245
   147
    << 10;
slouken@245
   148
}
slouken@245
   149
slouken@245
   150
int32 convert_envelope_rate(uint8 rate)
slouken@0
   151
{
slouken@0
   152
  int32 r;
slouken@0
   153
slouken@0
   154
  r=3-((rate>>6) & 0x3);
slouken@0
   155
  r*=3;
slouken@0
   156
  r = (int32)(rate & 0x3f) << r; /* 6.9 fixed point */
slouken@0
   157
slouken@0
   158
  /* 15.15 fixed point. */
slouken@0
   159
  return (((r * 44100) / play_mode->rate) * control_ratio) 
slouken@0
   160
    << ((fast_decay) ? 10 : 9);
slouken@0
   161
}
slouken@0
   162
slouken@245
   163
int32 convert_envelope_offset(uint8 offset)
slouken@0
   164
{
slouken@0
   165
  /* This is not too good... Can anyone tell me what these values mean?
slouken@0
   166
     Are they GUS-style "exponential" volumes? And what does that mean? */
slouken@0
   167
slouken@0
   168
  /* 15.15 fixed point */
slouken@0
   169
  return offset << (7+15);
slouken@0
   170
}
slouken@0
   171
slouken@245
   172
int32 convert_tremolo_sweep(uint8 sweep)
slouken@0
   173
{
slouken@0
   174
  if (!sweep)
slouken@0
   175
    return 0;
slouken@0
   176
slouken@0
   177
  return
slouken@0
   178
    ((control_ratio * SWEEP_TUNING) << SWEEP_SHIFT) /
slouken@0
   179
      (play_mode->rate * sweep);
slouken@0
   180
}
slouken@0
   181
slouken@245
   182
int32 convert_vibrato_sweep(uint8 sweep, int32 vib_control_ratio)
slouken@0
   183
{
slouken@0
   184
  if (!sweep)
slouken@0
   185
    return 0;
slouken@0
   186
slouken@0
   187
  return
slouken@0
   188
    (int32) (FSCALE((double) (vib_control_ratio) * SWEEP_TUNING, SWEEP_SHIFT)
slouken@0
   189
	     / (double)(play_mode->rate * sweep));
slouken@0
   190
slouken@0
   191
  /* this was overflowing with seashore.pat
slouken@0
   192
slouken@0
   193
      ((vib_control_ratio * SWEEP_TUNING) << SWEEP_SHIFT) /
slouken@0
   194
      (play_mode->rate * sweep); */
slouken@0
   195
}
slouken@0
   196
slouken@245
   197
int32 convert_tremolo_rate(uint8 rate)
slouken@0
   198
{
slouken@0
   199
  return
slouken@0
   200
    ((SINE_CYCLE_LENGTH * control_ratio * rate) << RATE_SHIFT) /
slouken@0
   201
      (TREMOLO_RATE_TUNING * play_mode->rate);
slouken@0
   202
}
slouken@0
   203
slouken@245
   204
int32 convert_vibrato_rate(uint8 rate)
slouken@0
   205
{
slouken@0
   206
  /* Return a suitable vibrato_control_ratio value */
slouken@0
   207
  return
slouken@0
   208
    (VIBRATO_RATE_TUNING * play_mode->rate) / 
slouken@0
   209
      (rate * 2 * VIBRATO_SAMPLE_INCREMENTS);
slouken@0
   210
}
slouken@0
   211
slouken@0
   212
static void reverse_data(int16 *sp, int32 ls, int32 le)
slouken@0
   213
{
slouken@0
   214
  int16 s, *ep=sp+le;
slouken@0
   215
  sp+=ls;
slouken@0
   216
  le-=ls;
slouken@0
   217
  le/=2;
slouken@0
   218
  while (le--)
slouken@0
   219
    {
slouken@0
   220
      s=*sp;
slouken@0
   221
      *sp++=*ep;
slouken@0
   222
      *ep--=s;
slouken@0
   223
    }
slouken@0
   224
}
slouken@0
   225
slouken@0
   226
/* 
slouken@0
   227
   If panning or note_to_use != -1, it will be used for all samples,
slouken@0
   228
   instead of the sample-specific values in the instrument file. 
slouken@0
   229
slouken@0
   230
   For note_to_use, any value <0 or >127 will be forced to 0.
slouken@0
   231
 
slouken@0
   232
   For other parameters, 1 means yes, 0 means no, other values are
slouken@0
   233
   undefined.
slouken@0
   234
slouken@0
   235
   TODO: do reverse loops right */
slouken@433
   236
static InstrumentLayer *load_instrument(const char *name, int font_type, int percussion,
slouken@245
   237
				   int panning, int amp, int cfg_tuning, int note_to_use,
slouken@0
   238
				   int strip_loop, int strip_envelope,
slouken@245
   239
				   int strip_tail, int bank, int gm_num, int sf_ix)
slouken@0
   240
{
slouken@408
   241
  InstrumentLayer *lp, *lastlp, *headlp = 0;
slouken@0
   242
  Instrument *ip;
slouken@0
   243
  FILE *fp;
slouken@0
   244
  uint8 tmp[1024];
slouken@0
   245
  int i,j,noluck=0;
slouken@0
   246
#ifdef PATCH_EXT_LIST
slouken@0
   247
  static char *patch_ext[] = PATCH_EXT_LIST;
slouken@0
   248
#endif
slouken@245
   249
  int sf2flag = 0;
slouken@245
   250
  int right_samples = 0;
slouken@245
   251
  int stereo_channels = 1, stereo_layer;
slouken@408
   252
  int vlayer_list[19][4], vlayer, vlayer_count = 0;
slouken@0
   253
slouken@0
   254
  if (!name) return 0;
slouken@0
   255
  
slouken@0
   256
  /* Open patch file */
slouken@144
   257
  if ((fp=open_file(name, 1, OF_NORMAL)) == NULL)
slouken@0
   258
    {
slouken@0
   259
      noluck=1;
slouken@0
   260
#ifdef PATCH_EXT_LIST
slouken@0
   261
      /* Try with various extensions */
slouken@0
   262
      for (i=0; patch_ext[i]; i++)
slouken@0
   263
	{
icculus@449
   264
	  if (strlen(name)+strlen(patch_ext[i])<PATH_MAX)
slouken@0
   265
	    {
icculus@449
   266
              char path[PATH_MAX];
slouken@144
   267
	      strcpy(path, name);
slouken@144
   268
	      strcat(path, patch_ext[i]);
slouken@144
   269
	      if ((fp=open_file(path, 1, OF_NORMAL)) != NULL)
slouken@0
   270
		{
slouken@0
   271
		  noluck=0;
slouken@0
   272
		  break;
slouken@0
   273
		}
slouken@0
   274
	    }
slouken@0
   275
	}
slouken@0
   276
#endif
slouken@0
   277
    }
slouken@0
   278
  
slouken@0
   279
  if (noluck)
slouken@0
   280
    {
slouken@0
   281
      ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
slouken@0
   282
		"Instrument `%s' can't be found.", name);
slouken@0
   283
      return 0;
slouken@0
   284
    }
slouken@0
   285
      
slouken@245
   286
  /*ctl->cmsg(CMSG_INFO, VERB_NOISY, "Loading instrument %s", current_filename);*/
slouken@0
   287
  
slouken@0
   288
  /* Read some headers and do cursory sanity checks. There are loads
slouken@0
   289
     of magic offsets. This could be rewritten... */
slouken@0
   290
slouken@0
   291
  if ((239 != fread(tmp, 1, 239, fp)) ||
slouken@0
   292
      (memcmp(tmp, "GF1PATCH110\0ID#000002", 22) &&
slouken@0
   293
       memcmp(tmp, "GF1PATCH100\0ID#000002", 22))) /* don't know what the
slouken@0
   294
						      differences are */
slouken@0
   295
    {
slouken@0
   296
      ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: not an instrument", name);
slouken@0
   297
      return 0;
slouken@0
   298
    }
slouken@245
   299
slouken@245
   300
/* patch layout:
slouken@245
   301
 * bytes:  info:		starts at offset:
slouken@245
   302
 * 22	id (see above)		0
slouken@245
   303
 * 60	copyright		22
slouken@245
   304
 *  1	instruments		82
slouken@245
   305
 *  1	voices			83
slouken@245
   306
 *  1	channels		84
slouken@245
   307
 *  2	number of waveforms	85
slouken@245
   308
 *  2	master volume		87
slouken@245
   309
 *  4	datasize		89
slouken@245
   310
 * 36   reserved, but now:	93
slouken@245
   311
 * 	7 "SF2EXT\0" id			93
slouken@245
   312
 * 	1 right samples		       100
slouken@245
   313
 *     28 reserved		       101
slouken@245
   314
 *  2	instrument number	129
slouken@245
   315
 * 16	instrument name		131
slouken@245
   316
 *  4	instrument size		147
slouken@245
   317
 *  1	number of layers	151
slouken@245
   318
 * 40	reserved		152
slouken@245
   319
 *  1	layer duplicate		192
slouken@245
   320
 *  1	layer number		193
slouken@245
   321
 *  4	layer size		194
slouken@245
   322
 *  1	number of samples	198
slouken@245
   323
 * 40	reserved		199
slouken@245
   324
 * 				239
slouken@245
   325
 * THEN, for each sample, see below
slouken@245
   326
 */
slouken@245
   327
slouken@245
   328
  if (!memcmp(tmp + 93, "SF2EXT", 6))
slouken@245
   329
    {
slouken@245
   330
	    sf2flag = 1;
slouken@245
   331
	    vlayer_count = tmp[152];
slouken@245
   332
    }
slouken@245
   333
slouken@0
   334
  if (tmp[82] != 1 && tmp[82] != 0) /* instruments. To some patch makers, 
slouken@0
   335
				       0 means 1 */
slouken@0
   336
    {
slouken@0
   337
      ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
slouken@0
   338
	   "Can't handle patches with %d instruments", tmp[82]);
slouken@0
   339
      return 0;
slouken@0
   340
    }
slouken@0
   341
slouken@0
   342
  if (tmp[151] != 1 && tmp[151] != 0) /* layers. What's a layer? */
slouken@0
   343
    {
slouken@0
   344
      ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
slouken@0
   345
	   "Can't handle instruments with %d layers", tmp[151]);
slouken@0
   346
      return 0;
slouken@0
   347
    }
slouken@0
   348
  
slouken@245
   349
slouken@245
   350
  if (sf2flag && vlayer_count > 0) {
slouken@245
   351
	for (i = 0; i < 9; i++)
slouken@245
   352
	  for (j = 0; j < 4; j++)
slouken@245
   353
	    vlayer_list[i][j] = tmp[153+i*4+j];
slouken@245
   354
	for (i = 9; i < 19; i++)
slouken@245
   355
	  for (j = 0; j < 4; j++)
slouken@245
   356
	    vlayer_list[i][j] = tmp[199+(i-9)*4+j];
slouken@245
   357
  }
slouken@245
   358
  else {
slouken@245
   359
	for (i = 0; i < 19; i++)
slouken@245
   360
	  for (j = 0; j < 4; j++)
slouken@245
   361
	    vlayer_list[i][j] = 0;
slouken@245
   362
	vlayer_list[0][0] = 0;
slouken@245
   363
	vlayer_list[0][1] = 127;
slouken@245
   364
	vlayer_list[0][2] = tmp[198];
slouken@245
   365
	vlayer_list[0][3] = 0;
slouken@245
   366
	vlayer_count = 1;
slouken@245
   367
  }
slouken@245
   368
slouken@245
   369
  lastlp = 0;
slouken@245
   370
slouken@245
   371
  for (vlayer = 0; vlayer < vlayer_count; vlayer++) {
slouken@245
   372
slouken@245
   373
  lp=(InstrumentLayer *)safe_malloc(sizeof(InstrumentLayer));
slouken@245
   374
  lp->size = sizeof(InstrumentLayer);
slouken@245
   375
  lp->lo = vlayer_list[vlayer][0];
slouken@245
   376
  lp->hi = vlayer_list[vlayer][1];
slouken@245
   377
  ip=(Instrument *)safe_malloc(sizeof(Instrument));
slouken@245
   378
  lp->size += sizeof(Instrument);
slouken@245
   379
  lp->instrument = ip;
slouken@245
   380
  lp->next = 0;
slouken@245
   381
slouken@245
   382
  if (lastlp) lastlp->next = lp;
slouken@245
   383
  else headlp = lp;
slouken@245
   384
slouken@245
   385
  lastlp = lp;
slouken@245
   386
slouken@245
   387
  if (sf2flag) ip->type = INST_SF2;
slouken@245
   388
  else ip->type = INST_GUS;
slouken@245
   389
  ip->samples = vlayer_list[vlayer][2];
slouken@245
   390
  ip->sample = (Sample *)safe_malloc(sizeof(Sample) * ip->samples);
slouken@245
   391
  lp->size += sizeof(Sample) * ip->samples;
slouken@245
   392
  ip->left_samples = ip->samples;
slouken@245
   393
  ip->left_sample = ip->sample;
slouken@245
   394
  right_samples = vlayer_list[vlayer][3];
slouken@245
   395
  ip->right_samples = right_samples;
slouken@245
   396
  if (right_samples)
slouken@0
   397
    {
slouken@245
   398
      ip->right_sample = (Sample *)safe_malloc(sizeof(Sample) * right_samples);
slouken@245
   399
      lp->size += sizeof(Sample) * right_samples;
slouken@245
   400
      stereo_channels = 2;
slouken@245
   401
    }
slouken@245
   402
  else ip->right_sample = 0;
slouken@245
   403
  ip->contents = 0;
slouken@0
   404
slouken@245
   405
  ctl->cmsg(CMSG_INFO, VERB_NOISY, "%s%s[%d,%d] %s(%d-%d layer %d of %d)",
slouken@245
   406
	(percussion)? "   ":"", name,
slouken@245
   407
	(percussion)? note_to_use : gm_num, bank,
slouken@245
   408
	(right_samples)? "(2) " : "",
slouken@245
   409
	lp->lo, lp->hi, vlayer+1, vlayer_count);
slouken@245
   410
slouken@245
   411
 for (stereo_layer = 0; stereo_layer < stereo_channels; stereo_layer++)
slouken@245
   412
 {
slouken@408
   413
  int sample_count = 0;
slouken@245
   414
slouken@245
   415
  if (stereo_layer == 0) sample_count = ip->left_samples;
slouken@245
   416
  else if (stereo_layer == 1) sample_count = ip->right_samples;
slouken@245
   417
slouken@245
   418
  for (i=0; i < sample_count; i++)
slouken@245
   419
    {
slouken@0
   420
      uint8 fractions;
slouken@0
   421
      int32 tmplong;
slouken@0
   422
      uint16 tmpshort;
slouken@408
   423
      uint16 sample_volume = 0;
slouken@0
   424
      uint8 tmpchar;
slouken@408
   425
      Sample *sp = 0;
slouken@408
   426
      uint8 sf2delay = 0;
slouken@0
   427
slouken@0
   428
#define READ_CHAR(thing) \
slouken@0
   429
      if (1 != fread(&tmpchar, 1, 1, fp)) goto fail; \
slouken@0
   430
      thing = tmpchar;
slouken@0
   431
#define READ_SHORT(thing) \
slouken@0
   432
      if (1 != fread(&tmpshort, 2, 1, fp)) goto fail; \
slouken@0
   433
      thing = LE_SHORT(tmpshort);
slouken@0
   434
#define READ_LONG(thing) \
slouken@0
   435
      if (1 != fread(&tmplong, 4, 1, fp)) goto fail; \
slouken@0
   436
      thing = LE_LONG(tmplong);
slouken@0
   437
slouken@245
   438
/*
slouken@245
   439
 *  7	sample name
slouken@245
   440
 *  1	fractions
slouken@245
   441
 *  4	length
slouken@245
   442
 *  4	loop start
slouken@245
   443
 *  4	loop end
slouken@245
   444
 *  2	sample rate
slouken@245
   445
 *  4	low frequency
slouken@245
   446
 *  4	high frequency
slouken@245
   447
 *  2	finetune
slouken@245
   448
 *  1	panning
slouken@245
   449
 *  6	envelope rates			|
slouken@245
   450
 *  6	envelope offsets		|  18 bytes
slouken@245
   451
 *  3	tremolo sweep, rate, depth	|
slouken@245
   452
 *  3	vibrato sweep, rate, depth	|
slouken@245
   453
 *  1	sample mode
slouken@245
   454
 *  2	scale frequency
slouken@245
   455
 *  2	scale factor
slouken@245
   456
 *  2	sample volume (??)
slouken@245
   457
 * 34	reserved
slouken@245
   458
 * Now: 1	delay
slouken@245
   459
 * 	33	reserved
slouken@245
   460
 */
slouken@0
   461
      skip(fp, 7); /* Skip the wave name */
slouken@0
   462
slouken@0
   463
      if (1 != fread(&fractions, 1, 1, fp))
slouken@0
   464
	{
slouken@0
   465
	fail:
slouken@0
   466
	  ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Error reading sample %d", i);
slouken@245
   467
	  if (stereo_layer == 1)
slouken@245
   468
	     {
slouken@245
   469
	       for (j=0; j<i; j++)
slouken@245
   470
	         free(ip->right_sample[j].data);
slouken@245
   471
	       free(ip->right_sample);
slouken@245
   472
	       i = ip->left_samples;
slouken@245
   473
	     }
slouken@0
   474
	  for (j=0; j<i; j++)
slouken@245
   475
	    free(ip->left_sample[j].data);
slouken@245
   476
	  free(ip->left_sample);
slouken@0
   477
	  free(ip);
slouken@245
   478
	  free(lp);
slouken@0
   479
	  return 0;
slouken@0
   480
	}
slouken@0
   481
slouken@245
   482
      if (stereo_layer == 0) sp=&(ip->left_sample[i]);
slouken@245
   483
      else if (stereo_layer == 1) sp=&(ip->right_sample[i]);
slouken@245
   484
slouken@0
   485
      READ_LONG(sp->data_length);
slouken@0
   486
      READ_LONG(sp->loop_start);
slouken@0
   487
      READ_LONG(sp->loop_end);
slouken@0
   488
      READ_SHORT(sp->sample_rate);
slouken@0
   489
      READ_LONG(sp->low_freq);
slouken@0
   490
      READ_LONG(sp->high_freq);
slouken@0
   491
      READ_LONG(sp->root_freq);
slouken@0
   492
      skip(fp, 2); /* Why have a "root frequency" and then "tuning"?? */
slouken@0
   493
      
slouken@0
   494
      READ_CHAR(tmp[0]);
slouken@0
   495
slouken@0
   496
      if (panning==-1)
slouken@0
   497
	sp->panning = (tmp[0] * 8 + 4) & 0x7f;
slouken@0
   498
      else
slouken@0
   499
	sp->panning=(uint8)(panning & 0x7F);
slouken@0
   500
slouken@245
   501
      sp->resonance=0;
slouken@245
   502
      sp->cutoff_freq=0;
slouken@245
   503
      sp->reverberation=0;
slouken@245
   504
      sp->chorusdepth=0;
slouken@245
   505
      sp->exclusiveClass=0;
slouken@245
   506
      sp->keyToModEnvHold=0;
slouken@245
   507
      sp->keyToModEnvDecay=0;
slouken@245
   508
      sp->keyToVolEnvHold=0;
slouken@245
   509
      sp->keyToVolEnvDecay=0;
slouken@245
   510
slouken@245
   511
      if (cfg_tuning)
slouken@245
   512
	{
slouken@245
   513
	  double tune_factor = (double)(cfg_tuning)/1200.0;
slouken@245
   514
	  tune_factor = pow(2.0, tune_factor);
slouken@245
   515
	  sp->root_freq = (uint32)( tune_factor * (double)sp->root_freq );
slouken@245
   516
	}
slouken@245
   517
slouken@0
   518
      /* envelope, tremolo, and vibrato */
slouken@0
   519
      if (18 != fread(tmp, 1, 18, fp)) goto fail; 
slouken@0
   520
slouken@0
   521
      if (!tmp[13] || !tmp[14])
slouken@0
   522
	{
slouken@0
   523
	  sp->tremolo_sweep_increment=
slouken@0
   524
	    sp->tremolo_phase_increment=sp->tremolo_depth=0;
slouken@0
   525
	  ctl->cmsg(CMSG_INFO, VERB_DEBUG, " * no tremolo");
slouken@0
   526
	}
slouken@0
   527
      else
slouken@0
   528
	{
slouken@0
   529
	  sp->tremolo_sweep_increment=convert_tremolo_sweep(tmp[12]);
slouken@0
   530
	  sp->tremolo_phase_increment=convert_tremolo_rate(tmp[13]);
slouken@0
   531
	  sp->tremolo_depth=tmp[14];
slouken@0
   532
	  ctl->cmsg(CMSG_INFO, VERB_DEBUG,
slouken@0
   533
	       " * tremolo: sweep %d, phase %d, depth %d",
slouken@0
   534
	       sp->tremolo_sweep_increment, sp->tremolo_phase_increment,
slouken@0
   535
	       sp->tremolo_depth);
slouken@0
   536
	}
slouken@0
   537
slouken@0
   538
      if (!tmp[16] || !tmp[17])
slouken@0
   539
	{
slouken@0
   540
	  sp->vibrato_sweep_increment=
slouken@0
   541
	    sp->vibrato_control_ratio=sp->vibrato_depth=0;
slouken@0
   542
	  ctl->cmsg(CMSG_INFO, VERB_DEBUG, " * no vibrato");
slouken@0
   543
	}
slouken@0
   544
      else
slouken@0
   545
	{
slouken@0
   546
	  sp->vibrato_control_ratio=convert_vibrato_rate(tmp[16]);
slouken@0
   547
	  sp->vibrato_sweep_increment=
slouken@0
   548
	    convert_vibrato_sweep(tmp[15], sp->vibrato_control_ratio);
slouken@0
   549
	  sp->vibrato_depth=tmp[17];
slouken@0
   550
	  ctl->cmsg(CMSG_INFO, VERB_DEBUG,
slouken@0
   551
	       " * vibrato: sweep %d, ctl %d, depth %d",
slouken@0
   552
	       sp->vibrato_sweep_increment, sp->vibrato_control_ratio,
slouken@0
   553
	       sp->vibrato_depth);
slouken@0
   554
slouken@0
   555
	}
slouken@0
   556
slouken@0
   557
      READ_CHAR(sp->modes);
slouken@245
   558
      READ_SHORT(sp->freq_center);
slouken@245
   559
      READ_SHORT(sp->freq_scale);
slouken@0
   560
slouken@245
   561
      if (sf2flag)
slouken@245
   562
        {
slouken@245
   563
          READ_SHORT(sample_volume);
slouken@245
   564
	  READ_CHAR(sf2delay);
slouken@245
   565
          READ_CHAR(sp->exclusiveClass);
slouken@245
   566
          skip(fp, 32);
slouken@245
   567
	}
slouken@245
   568
      else
slouken@245
   569
        {
slouken@245
   570
          skip(fp, 36);
slouken@245
   571
        }
slouken@0
   572
slouken@0
   573
      /* Mark this as a fixed-pitch instrument if such a deed is desired. */
slouken@0
   574
      if (note_to_use!=-1)
slouken@0
   575
	sp->note_to_use=(uint8)(note_to_use);
slouken@0
   576
      else
slouken@0
   577
	sp->note_to_use=0;
slouken@0
   578
      
slouken@0
   579
      /* seashore.pat in the Midia patch set has no Sustain. I don't
slouken@0
   580
         understand why, and fixing it by adding the Sustain flag to
slouken@0
   581
         all looped patches probably breaks something else. We do it
slouken@0
   582
         anyway. */
slouken@0
   583
	 
slouken@0
   584
      if (sp->modes & MODES_LOOPING) 
slouken@0
   585
	sp->modes |= MODES_SUSTAIN;
slouken@0
   586
slouken@0
   587
      /* Strip any loops and envelopes we're permitted to */
slouken@0
   588
      if ((strip_loop==1) && 
slouken@0
   589
	  (sp->modes & (MODES_SUSTAIN | MODES_LOOPING | 
slouken@0
   590
			MODES_PINGPONG | MODES_REVERSE)))
slouken@0
   591
	{
slouken@0
   592
	  ctl->cmsg(CMSG_INFO, VERB_DEBUG, " - Removing loop and/or sustain");
slouken@0
   593
	  sp->modes &=~(MODES_SUSTAIN | MODES_LOOPING | 
slouken@0
   594
			MODES_PINGPONG | MODES_REVERSE);
slouken@0
   595
	}
slouken@0
   596
slouken@0
   597
      if (strip_envelope==1)
slouken@0
   598
	{
slouken@0
   599
	  if (sp->modes & MODES_ENVELOPE)
slouken@0
   600
	    ctl->cmsg(CMSG_INFO, VERB_DEBUG, " - Removing envelope");
slouken@0
   601
	  sp->modes &= ~MODES_ENVELOPE;
slouken@0
   602
	}
slouken@0
   603
      else if (strip_envelope != 0)
slouken@0
   604
	{
slouken@0
   605
	  /* Have to make a guess. */
slouken@0
   606
	  if (!(sp->modes & (MODES_LOOPING | MODES_PINGPONG | MODES_REVERSE)))
slouken@0
   607
	    {
slouken@0
   608
	      /* No loop? Then what's there to sustain? No envelope needed
slouken@0
   609
		 either... */
slouken@0
   610
	      sp->modes &= ~(MODES_SUSTAIN|MODES_ENVELOPE);
slouken@0
   611
	      ctl->cmsg(CMSG_INFO, VERB_DEBUG, 
slouken@0
   612
			" - No loop, removing sustain and envelope");
slouken@0
   613
	    }
slouken@0
   614
	  else if (!memcmp(tmp, "??????", 6) || tmp[11] >= 100) 
slouken@0
   615
	    {
slouken@0
   616
	      /* Envelope rates all maxed out? Envelope end at a high "offset"?
slouken@0
   617
		 That's a weird envelope. Take it out. */
slouken@0
   618
	      sp->modes &= ~MODES_ENVELOPE;
slouken@0
   619
	      ctl->cmsg(CMSG_INFO, VERB_DEBUG, 
slouken@0
   620
			" - Weirdness, removing envelope");
slouken@0
   621
	    }
slouken@0
   622
	  else if (!(sp->modes & MODES_SUSTAIN))
slouken@0
   623
	    {
slouken@0
   624
	      /* No sustain? Then no envelope.  I don't know if this is
slouken@0
   625
		 justified, but patches without sustain usually don't need the
slouken@0
   626
		 envelope either... at least the Gravis ones. They're mostly
slouken@0
   627
		 drums.  I think. */
slouken@0
   628
	      sp->modes &= ~MODES_ENVELOPE;
slouken@0
   629
	      ctl->cmsg(CMSG_INFO, VERB_DEBUG, 
slouken@0
   630
			" - No sustain, removing envelope");
slouken@0
   631
	    }
slouken@0
   632
	}
slouken@0
   633
slouken@245
   634
      sp->attenuation = 0;
slouken@245
   635
slouken@245
   636
      for (j=ATTACK; j<DELAY; j++)
slouken@0
   637
	{
slouken@0
   638
	  sp->envelope_rate[j]=
slouken@245
   639
	    (j<3)? convert_envelope_rate_attack(tmp[j], 11) : convert_envelope_rate(tmp[j]);
slouken@0
   640
	  sp->envelope_offset[j]= 
slouken@0
   641
	    convert_envelope_offset(tmp[6+j]);
slouken@0
   642
	}
slouken@245
   643
      if (sf2flag)
slouken@245
   644
	{
slouken@245
   645
	  if (sf2delay > 5) sf2delay = 5;
slouken@245
   646
	  sp->envelope_rate[DELAY] = (int32)( (sf2delay*play_mode->rate) / 1000 );
slouken@245
   647
	}
slouken@245
   648
      else
slouken@245
   649
	{
slouken@245
   650
          sp->envelope_rate[DELAY]=0;
slouken@245
   651
	}
slouken@245
   652
      sp->envelope_offset[DELAY]=0;
slouken@245
   653
slouken@245
   654
      for (j=ATTACK; j<DELAY; j++)
slouken@245
   655
	{
slouken@245
   656
	  sp->modulation_rate[j]=sp->envelope_rate[j];
slouken@245
   657
	  sp->modulation_offset[j]=sp->envelope_offset[j];
slouken@245
   658
	}
slouken@245
   659
      sp->modulation_rate[DELAY] = sp->modulation_offset[DELAY] = 0;
slouken@245
   660
      sp->modEnvToFilterFc=0;
slouken@245
   661
      sp->modEnvToPitch=0;
slouken@245
   662
      sp->lfo_sweep_increment = 0;
slouken@245
   663
      sp->lfo_phase_increment = 0;
slouken@245
   664
      sp->modLfoToFilterFc = 0;
slouken@245
   665
      sp->vibrato_delay = 0;
slouken@0
   666
slouken@0
   667
      /* Then read the sample data */
slouken@245
   668
      if (sp->data_length/2 > MAX_SAMPLE_SIZE)
slouken@245
   669
        {
slouken@245
   670
	  goto fail;
slouken@245
   671
	}
slouken@245
   672
      sp->data = safe_malloc(sp->data_length + 1);
slouken@245
   673
      lp->size += sp->data_length + 1;
slouken@245
   674
slouken@0
   675
      if (1 != fread(sp->data, sp->data_length, 1, fp))
slouken@0
   676
	goto fail;
slouken@0
   677
      
slouken@0
   678
      if (!(sp->modes & MODES_16BIT)) /* convert to 16-bit data */
slouken@0
   679
	{
slouken@0
   680
	  int32 i=sp->data_length;
slouken@0
   681
	  uint8 *cp=(uint8 *)(sp->data);
slouken@245
   682
	  uint16 *tmp,*newdta;
slouken@245
   683
	  tmp=newdta=safe_malloc(sp->data_length*2 + 2);
slouken@0
   684
	  while (i--)
slouken@0
   685
	    *tmp++ = (uint16)(*cp++) << 8;
slouken@0
   686
	  cp=(uint8 *)(sp->data);
slouken@245
   687
	  sp->data = (sample_t *)newdta;
slouken@0
   688
	  free(cp);
slouken@0
   689
	  sp->data_length *= 2;
slouken@0
   690
	  sp->loop_start *= 2;
slouken@0
   691
	  sp->loop_end *= 2;
slouken@0
   692
	}
slouken@294
   693
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
slouken@0
   694
      else
slouken@0
   695
	/* convert to machine byte order */
slouken@0
   696
	{
slouken@0
   697
	  int32 i=sp->data_length/2;
slouken@0
   698
	  int16 *tmp=(int16 *)sp->data,s;
slouken@0
   699
	  while (i--)
slouken@0
   700
	    { 
slouken@0
   701
	      s=LE_SHORT(*tmp);
slouken@0
   702
	      *tmp++=s;
slouken@0
   703
	    }
slouken@0
   704
	}
slouken@0
   705
#endif
slouken@0
   706
      
slouken@0
   707
      if (sp->modes & MODES_UNSIGNED) /* convert to signed data */
slouken@0
   708
	{
slouken@0
   709
	  int32 i=sp->data_length/2;
slouken@0
   710
	  int16 *tmp=(int16 *)sp->data;
slouken@0
   711
	  while (i--)
slouken@0
   712
	    *tmp++ ^= 0x8000;
slouken@0
   713
	}
slouken@0
   714
slouken@0
   715
      /* Reverse reverse loops and pass them off as normal loops */
slouken@0
   716
      if (sp->modes & MODES_REVERSE)
slouken@0
   717
	{
slouken@0
   718
	  int32 t;
slouken@0
   719
	  /* The GUS apparently plays reverse loops by reversing the
slouken@0
   720
	     whole sample. We do the same because the GUS does not SUCK. */
slouken@0
   721
slouken@0
   722
	  ctl->cmsg(CMSG_WARNING, VERB_NORMAL, "Reverse loop in %s", name);
slouken@0
   723
	  reverse_data((int16 *)sp->data, 0, sp->data_length/2);
slouken@0
   724
slouken@0
   725
	  t=sp->loop_start;
slouken@0
   726
	  sp->loop_start=sp->data_length - sp->loop_end;
slouken@0
   727
	  sp->loop_end=sp->data_length - t;
slouken@0
   728
slouken@0
   729
	  sp->modes &= ~MODES_REVERSE;
slouken@0
   730
	  sp->modes |= MODES_LOOPING; /* just in case */
slouken@0
   731
	}
slouken@0
   732
slouken@0
   733
      /* If necessary do some anti-aliasing filtering  */
slouken@0
   734
slouken@0
   735
      if (antialiasing_allowed)
slouken@0
   736
	  antialiasing(sp,play_mode->rate);
slouken@0
   737
slouken@0
   738
#ifdef ADJUST_SAMPLE_VOLUMES
slouken@0
   739
      if (amp!=-1)
slouken@245
   740
	sp->volume=(FLOAT_T)((amp) / 100.0);
slouken@245
   741
      else if (sf2flag)
slouken@245
   742
	sp->volume=(FLOAT_T)((sample_volume) / 255.0);
slouken@0
   743
      else
slouken@0
   744
	{
slouken@0
   745
	  /* Try to determine a volume scaling factor for the sample.
slouken@0
   746
	     This is a very crude adjustment, but things sound more
slouken@0
   747
	     balanced with it. Still, this should be a runtime option. */
slouken@245
   748
	  uint32 i, numsamps=sp->data_length/2;
slouken@245
   749
	  uint32 higher=0, highcount=0;
slouken@0
   750
	  int16 maxamp=0,a;
slouken@0
   751
	  int16 *tmp=(int16 *)sp->data;
slouken@245
   752
	  i = numsamps;
slouken@0
   753
	  while (i--)
slouken@0
   754
	    {
slouken@0
   755
	      a=*tmp++;
slouken@0
   756
	      if (a<0) a=-a;
slouken@0
   757
	      if (a>maxamp)
slouken@0
   758
		maxamp=a;
slouken@0
   759
	    }
slouken@245
   760
	  tmp=(int16 *)sp->data;
slouken@245
   761
	  i = numsamps;
slouken@245
   762
	  while (i--)
slouken@245
   763
	    {
slouken@245
   764
	      a=*tmp++;
slouken@245
   765
	      if (a<0) a=-a;
slouken@245
   766
	      if (a > 3*maxamp/4)
slouken@245
   767
		{
slouken@245
   768
		   higher += a;
slouken@245
   769
		   highcount++;
slouken@245
   770
		}
slouken@245
   771
	    }
slouken@245
   772
	  if (highcount) higher /= highcount;
slouken@245
   773
	  else higher = 10000;
slouken@245
   774
	  sp->volume = (32768.0 * 0.875) /  (double)higher ;
slouken@0
   775
	  ctl->cmsg(CMSG_INFO, VERB_DEBUG, " * volume comp: %f", sp->volume);
slouken@0
   776
	}
slouken@0
   777
#else
slouken@0
   778
      if (amp!=-1)
slouken@0
   779
	sp->volume=(double)(amp) / 100.0;
slouken@0
   780
      else
slouken@0
   781
	sp->volume=1.0;
slouken@0
   782
#endif
slouken@0
   783
slouken@0
   784
      sp->data_length /= 2; /* These are in bytes. Convert into samples. */
slouken@245
   785
slouken@0
   786
      sp->loop_start /= 2;
slouken@0
   787
      sp->loop_end /= 2;
slouken@245
   788
      sp->data[sp->data_length] = sp->data[sp->data_length-1];
slouken@0
   789
slouken@0
   790
      /* Then fractional samples */
slouken@0
   791
      sp->data_length <<= FRACTION_BITS;
slouken@0
   792
      sp->loop_start <<= FRACTION_BITS;
slouken@0
   793
      sp->loop_end <<= FRACTION_BITS;
slouken@0
   794
slouken@245
   795
    /* trim off zero data at end */
slouken@245
   796
    {
slouken@245
   797
	int ls = sp->loop_start>>FRACTION_BITS;
slouken@245
   798
	int le = sp->loop_end>>FRACTION_BITS;
slouken@245
   799
	int se = sp->data_length>>FRACTION_BITS;
slouken@245
   800
	while (se > 1 && !sp->data[se-1]) se--;
slouken@245
   801
	if (le > se) le = se;
slouken@245
   802
	if (ls >= le) sp->modes &= ~MODES_LOOPING;
slouken@245
   803
	sp->loop_end = le<<FRACTION_BITS;
slouken@245
   804
	sp->data_length = se<<FRACTION_BITS;
slouken@245
   805
    }
slouken@245
   806
slouken@0
   807
      /* Adjust for fractional loop points. This is a guess. Does anyone
slouken@0
   808
	 know what "fractions" really stands for? */
slouken@0
   809
      sp->loop_start |=
slouken@0
   810
	(fractions & 0x0F) << (FRACTION_BITS-4);
slouken@0
   811
      sp->loop_end |=
slouken@0
   812
	((fractions>>4) & 0x0F) << (FRACTION_BITS-4);
slouken@0
   813
slouken@0
   814
      /* If this instrument will always be played on the same note,
slouken@0
   815
	 and it's not looped, we can resample it now. */
slouken@0
   816
      if (sp->note_to_use && !(sp->modes & MODES_LOOPING))
slouken@0
   817
	pre_resample(sp);
slouken@0
   818
slouken@0
   819
#ifdef LOOKUP_HACK
slouken@0
   820
      /* Squash the 16-bit data into 8 bits. */
slouken@0
   821
      {
slouken@0
   822
	uint8 *gulp,*ulp;
slouken@0
   823
	int16 *swp;
slouken@0
   824
	int l=sp->data_length >> FRACTION_BITS;
slouken@0
   825
	gulp=ulp=safe_malloc(l+1);
slouken@0
   826
	swp=(int16 *)sp->data;
slouken@0
   827
	while(l--)
slouken@0
   828
	  *ulp++ = (*swp++ >> 8) & 0xFF;
slouken@0
   829
	free(sp->data);
slouken@0
   830
	sp->data=(sample_t *)gulp;
slouken@0
   831
      }
slouken@0
   832
#endif
slouken@0
   833
      
slouken@0
   834
      if (strip_tail==1)
slouken@0
   835
	{
slouken@0
   836
	  /* Let's not really, just say we did. */
slouken@0
   837
	  ctl->cmsg(CMSG_INFO, VERB_DEBUG, " - Stripping tail");
slouken@0
   838
	  sp->data_length = sp->loop_end;
slouken@0
   839
	}
slouken@245
   840
    } /* end of sample loop */
slouken@245
   841
 } /* end of stereo layer loop */
slouken@245
   842
 } /* end of vlayer loop */
slouken@245
   843
slouken@0
   844
slouken@0
   845
  close_file(fp);
slouken@245
   846
  return headlp;
slouken@0
   847
}
slouken@0
   848
slouken@0
   849
static int fill_bank(int dr, int b)
slouken@0
   850
{
slouken@0
   851
  int i, errors=0;
slouken@0
   852
  ToneBank *bank=((dr) ? drumset[b] : tonebank[b]);
slouken@0
   853
  if (!bank)
slouken@0
   854
    {
slouken@0
   855
      ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
slouken@0
   856
	   "Huh. Tried to load instruments in non-existent %s %d",
slouken@0
   857
	   (dr) ? "drumset" : "tone bank", b);
slouken@0
   858
      return 0;
slouken@0
   859
    }
slouken@245
   860
  for (i=0; i<MAXPROG; i++)
slouken@0
   861
    {
slouken@245
   862
      if (bank->tone[i].layer==MAGIC_LOAD_INSTRUMENT)
slouken@0
   863
	{
slouken@0
   864
	  if (!(bank->tone[i].name))
slouken@0
   865
	    {
slouken@0
   866
	      ctl->cmsg(CMSG_WARNING, (b!=0) ? VERB_VERBOSE : VERB_NORMAL,
slouken@0
   867
		   "No instrument mapped to %s %d, program %d%s",
slouken@0
   868
		   (dr)? "drum set" : "tone bank", b, i, 
slouken@0
   869
		   (b!=0) ? "" : " - this instrument will not be heard");
slouken@0
   870
	      if (b!=0)
slouken@0
   871
		{
slouken@0
   872
		  /* Mark the corresponding instrument in the default
slouken@0
   873
		     bank / drumset for loading (if it isn't already) */
slouken@0
   874
		  if (!dr)
slouken@0
   875
		    {
slouken@245
   876
		      if (!(standard_tonebank.tone[i].layer))
slouken@245
   877
			standard_tonebank.tone[i].layer=
slouken@0
   878
			  MAGIC_LOAD_INSTRUMENT;
slouken@0
   879
		    }
slouken@0
   880
		  else
slouken@0
   881
		    {
slouken@245
   882
		      if (!(standard_drumset.tone[i].layer))
slouken@245
   883
			standard_drumset.tone[i].layer=
slouken@0
   884
			  MAGIC_LOAD_INSTRUMENT;
slouken@0
   885
		    }
slouken@0
   886
		}
slouken@245
   887
	      bank->tone[i].layer=0;
slouken@0
   888
	      errors++;
slouken@0
   889
	    }
slouken@245
   890
	  else if (!(bank->tone[i].layer=
slouken@0
   891
		     load_instrument(bank->tone[i].name, 
slouken@245
   892
			     	     bank->tone[i].font_type,
slouken@0
   893
				     (dr) ? 1 : 0,
slouken@0
   894
				     bank->tone[i].pan,
slouken@0
   895
				     bank->tone[i].amp,
slouken@245
   896
				     bank->tone[i].tuning,
slouken@0
   897
				     (bank->tone[i].note!=-1) ? 
slouken@245
   898
				       bank->tone[i].note :
slouken@245
   899
				       ((dr) ? i : -1),
slouken@0
   900
				     (bank->tone[i].strip_loop!=-1) ?
slouken@0
   901
				     bank->tone[i].strip_loop :
slouken@0
   902
				     ((dr) ? 1 : -1),
slouken@0
   903
				     (bank->tone[i].strip_envelope != -1) ? 
slouken@0
   904
				     bank->tone[i].strip_envelope :
slouken@0
   905
				     ((dr) ? 1 : -1),
slouken@245
   906
				     bank->tone[i].strip_tail,
slouken@245
   907
				     b,
slouken@245
   908
				     ((dr) ? i + 128 : i),
slouken@245
   909
				     bank->tone[i].sf_ix
slouken@245
   910
			    			 )))
slouken@0
   911
	    {
slouken@0
   912
	      ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
slouken@0
   913
		   "Couldn't load instrument %s (%s %d, program %d)",
slouken@0
   914
		   bank->tone[i].name,
slouken@0
   915
		   (dr)? "drum set" : "tone bank", b, i);
slouken@0
   916
	      errors++;
slouken@0
   917
	    }
slouken@245
   918
	  else
slouken@245
   919
	    { /* it's loaded now */
slouken@245
   920
		bank->tone[i].last_used = current_tune_number;
slouken@245
   921
		current_patch_memory += bank->tone[i].layer->size;
slouken@245
   922
		purge_as_required();
slouken@245
   923
		if (current_patch_memory > max_patch_memory) {
slouken@245
   924
	      		ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
slouken@245
   925
		   		"Not enough memory to load instrument %s (%s %d, program %d)",
slouken@245
   926
		   		bank->tone[i].name,
slouken@245
   927
		   		(dr)? "drum set" : "tone bank", b, i);
slouken@245
   928
	      		errors++;
slouken@245
   929
	    		free_layer(bank->tone[i].layer);
slouken@245
   930
	    		bank->tone[i].layer=0;
slouken@245
   931
	    		bank->tone[i].last_used=-1;
slouken@245
   932
		}
slouken@245
   933
#if 0
slouken@245
   934
  	        if (check_for_rc()) {
slouken@245
   935
	    		free_layer(bank->tone[i].layer);
slouken@245
   936
	    		bank->tone[i].layer=0;
slouken@245
   937
	    		bank->tone[i].last_used=-1;
slouken@245
   938
			return 0;
slouken@245
   939
		}
slouken@245
   940
#endif
slouken@245
   941
	    }
slouken@0
   942
	}
slouken@0
   943
    }
slouken@0
   944
  return errors;
slouken@0
   945
}
slouken@0
   946
slouken@245
   947
static void free_old_instruments(int how_old)
slouken@245
   948
{
slouken@245
   949
  int i=MAXBANK;
slouken@245
   950
  while(i--)
slouken@245
   951
    {
slouken@245
   952
      if (tonebank[i])
slouken@245
   953
	free_old_bank(0, i, how_old);
slouken@245
   954
      if (drumset[i])
slouken@245
   955
	free_old_bank(1, i, how_old);
slouken@245
   956
    }
slouken@245
   957
}
slouken@245
   958
slouken@245
   959
static void purge_as_required(void)
slouken@245
   960
{
slouken@245
   961
  if (!max_patch_memory) return;
slouken@245
   962
slouken@245
   963
  while (last_tune_purged < current_tune_number
slouken@245
   964
	&& current_patch_memory > max_patch_memory)
slouken@245
   965
    {
slouken@245
   966
	last_tune_purged++;
slouken@245
   967
	free_old_instruments(last_tune_purged);
slouken@245
   968
    }
slouken@245
   969
}
slouken@245
   970
slouken@245
   971
slouken@0
   972
int load_missing_instruments(void)
slouken@0
   973
{
slouken@245
   974
  int i=MAXBANK,errors=0;
slouken@0
   975
  while (i--)
slouken@0
   976
    {
slouken@0
   977
      if (tonebank[i])
slouken@0
   978
	errors+=fill_bank(0,i);
slouken@0
   979
      if (drumset[i])
slouken@0
   980
	errors+=fill_bank(1,i);
slouken@0
   981
    }
slouken@245
   982
  current_tune_number++;
slouken@0
   983
  return errors;
slouken@0
   984
}
slouken@0
   985
slouken@0
   986
void free_instruments(void)
slouken@0
   987
{
slouken@0
   988
  int i=128;
slouken@0
   989
  while(i--)
slouken@0
   990
    {
slouken@0
   991
      if (tonebank[i])
slouken@0
   992
	free_bank(0,i);
slouken@0
   993
      if (drumset[i])
slouken@0
   994
	free_bank(1,i);
slouken@0
   995
    }
slouken@0
   996
}
slouken@0
   997
slouken@433
   998
int set_default_instrument(const char *name)
slouken@0
   999
{
slouken@245
  1000
  InstrumentLayer *lp;
slouken@245
  1001
/*  if (!(lp=load_instrument(name, 0, -1, -1, -1, 0, 0, 0))) */
slouken@245
  1002
  if (!(lp=load_instrument(name, FONT_NORMAL, 0, -1, -1, 0, -1, -1, -1, -1, 0, -1, -1)))
slouken@0
  1003
    return -1;
slouken@0
  1004
  if (default_instrument)
slouken@245
  1005
    free_layer(default_instrument);
slouken@245
  1006
  default_instrument=lp;
slouken@0
  1007
  default_program=SPECIAL_PROGRAM;
slouken@0
  1008
  return 0;
slouken@0
  1009
}