DOOM-3-BFG

DOOM 3 BFG Edition
Log | Files | Refs

instrum.cpp (16965B)


      1 /*
      2 
      3 TiMidity -- Experimental MIDI to WAVE converter
      4 Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>
      5 
      6 This program is free software; you can redistribute it and/or modify
      7 it under the terms of the GNU General Public License as published by
      8 the Free Software Foundation; either version 2 of the License, or
      9 (at your option) any later version.
     10 
     11 This program is distributed in the hope that it will be useful,
     12 but WITHOUT ANY WARRANTY; without even the implied warranty of
     13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14 GNU General Public License for more details.
     15 
     16 You should have received a copy of the GNU General Public License
     17 along with this program; if not, write to the Free Software
     18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
     19 
     20 instrum.c 
     21 
     22 Code to load and unload GUS-compatible instrument patches.
     23 
     24 */
     25 
     26 #include "../../neo/idlib/precompiled.h"
     27 
     28 #include <stdio.h>
     29 #include <string.h>
     30 #include <stdlib.h>
     31 #include <ctype.h>
     32 
     33 #include "config.h"
     34 #include "common.h"
     35 #include "instrum.h"
     36 #include "playmidi.h"
     37 #include "output.h"
     38 #include "controls.h"
     39 #include "resample.h"
     40 #include "tables.h"
     41 #include "filter.h"
     42 
     43 
     44 
     45 //void Real_Tim_Free( void *pt );
     46 
     47 /* Some functions get aggravated if not even the standard banks are 
     48 available. */
     49 static ToneBank standard_tonebank, standard_drumset;
     50 ToneBank 
     51 *tonebank[128]={&standard_tonebank},
     52 *drumset[128]={&standard_drumset};
     53 
     54 /* This is a special instrument, used for all melodic programs */
     55 Instrument *default_instrument=0;
     56 
     57 /* This is only used for tracks that don't specify a program */
     58 int default_program=DEFAULT_PROGRAM;
     59 
     60 int antialiasing_allowed=0;
     61 #ifdef FAST_DECAY
     62 int fast_decay=1;
     63 #else
     64 int fast_decay=0;
     65 #endif
     66 
     67 static void free_instrument(Instrument *ip)
     68 {
     69 	Sample *sp;
     70 	int i;
     71 	if (!ip) return;
     72 	for (i=0; i<ip->samples; i++)
     73 	{
     74 		sp=&(ip->sample[i]);
     75 		Real_Tim_Free(sp->data);
     76 	}
     77 	Real_Tim_Free(ip->sample);
     78 	Real_Tim_Free(ip);
     79 }
     80 
     81 static void free_bank(int dr, int b)
     82 {
     83 	int i;
     84 	ToneBank *bank=((dr) ? drumset[b] : tonebank[b]);
     85 	for (i=0; i<128; i++) {
     86 		if (bank->tone[i].instrument)
     87 		{
     88 			/* Not that this could ever happen, of course */
     89 			if (bank->tone[i].instrument != MAGIC_LOAD_INSTRUMENT)
     90 				free_instrument(bank->tone[i].instrument);
     91 			bank->tone[i].instrument=0;
     92 		}
     93 		if (bank->tone[i].name)
     94 		{
     95 			Real_Tim_Free( bank->tone[i].name );
     96 			bank->tone[i].name = NULL;
     97 		}
     98 	}
     99 }
    100 
    101 static  int32_t convert_envelope_rate(uint8_t rate)
    102 {
    103 	 int32_t r;
    104 
    105 	r=3-((rate>>6) & 0x3);
    106 	r*=3;
    107 	r = (int32_t)(rate & 0x3f) << r; /* 6.9 fixed point */
    108 
    109 	/* 15.15 fixed point. */
    110 	return (((r * 44100) / play_mode->rate) * control_ratio) 
    111 		<< ((fast_decay) ? 10 : 9);
    112 }
    113 
    114 static  int32_t  convert_envelope_offset(uint8_t offset)
    115 {
    116 	/* This is not too good... Can anyone tell me what these values mean?
    117 	Are they GUS-style "exponential" volumes? And what does that mean? */
    118 
    119 	/* 15.15 fixed point */
    120 	return offset << (7+15);
    121 }
    122 
    123 static  int32_t convert_tremolo_sweep(uint8_t sweep)
    124 {
    125 	if (!sweep)
    126 		return 0;
    127 
    128 	return
    129 		((control_ratio * SWEEP_TUNING) << SWEEP_SHIFT) /
    130 		(play_mode->rate * sweep);
    131 }
    132 
    133 static  int32_t convert_vibrato_sweep(uint8_t sweep,  int32_t vib_control_ratio)
    134 {
    135 	if (!sweep)
    136 		return 0;
    137 
    138 	return
    139 		(int32_t) (FSCALE((double) (vib_control_ratio) * SWEEP_TUNING, SWEEP_SHIFT)
    140 		/ (double)(play_mode->rate * sweep));
    141 
    142 	/* this was overflowing with seashore.pat
    143 
    144 	((vib_control_ratio * SWEEP_TUNING) << SWEEP_SHIFT) /
    145 	(play_mode->rate * sweep); */
    146 }
    147 
    148 static  int32_t convert_tremolo_rate(uint8_t rate)
    149 {
    150 	return
    151 		((SINE_CYCLE_LENGTH * control_ratio * rate) << RATE_SHIFT) /
    152 		(TREMOLO_RATE_TUNING * play_mode->rate);
    153 }
    154 
    155 static  int32_t convert_vibrato_rate(uint8_t rate)
    156 {
    157 	/* Return a suitable vibrato_control_ratio value */
    158 	return
    159 		(VIBRATO_RATE_TUNING * play_mode->rate) / 
    160 		(rate * 2 * VIBRATO_SAMPLE_INCREMENTS);
    161 }
    162 
    163 static void reverse_data(int16_t *sp,  int32_t ls,  int32_t le)
    164 {
    165 	int16_t s, *ep=sp+le;
    166 	sp+=ls;
    167 	le-=ls;
    168 	le/=2;
    169 	while (le--)
    170 	{
    171 		s=*sp;
    172 		*sp++=*ep;
    173 		*ep--=s;
    174 	}
    175 }
    176 
    177 /* 
    178 If panning or note_to_use != -1, it will be used for all samples,
    179 instead of the sample-specific values in the instrument file. 
    180 
    181 For note_to_use, any value <0 or >127 will be forced to 0.
    182 
    183 For other parameters, 1 means yes, 0 means no, other values are
    184 undefined.
    185 
    186 TODO: do reverse loops right */
    187 static Instrument *load_instrument(char *name, int percussion,
    188 								   int panning, int amp, int note_to_use,
    189 								   int strip_loop, int strip_envelope,
    190 								   int strip_tail)
    191 {
    192 	Instrument *ip;
    193 	Sample *sp;
    194 	idFile * fp;
    195 	uint8_t tmp[1024];
    196 	int i,j,noluck=0;
    197 	char *path;
    198 	char filename[1024];
    199 #ifdef PATCH_EXT_LIST
    200 	static char *patch_ext[] = PATCH_EXT_LIST;
    201 #endif
    202 
    203 	if (!name) return 0;
    204 
    205 	path = "classicmusic/instruments/";
    206 
    207 	idStr instName = name;
    208 	instName.ToUpper();
    209 
    210 	strcpy( filename, path );
    211 	strcat( filename, instName.c_str() );
    212 	strcat( filename, ".PAT" );
    213 
    214 	/* Open patch file */
    215 	if ((fp=open_file(filename, 1, OF_VERBOSE)) == NULL)
    216 	{
    217 		noluck=1;
    218 #ifdef PATCH_EXT_LIST
    219 		/* Try with various extensions */
    220 		for (i=0; patch_ext[i]; i++)
    221 		{
    222 			if (strlen(name)+strlen(patch_ext[i])<1024)
    223 			{
    224 				strcpy(filename, path);
    225 				strcat(filename, name);
    226 				strcat(filename, patch_ext[i]);
    227 				if ((fp=open_file(filename, 1, OF_VERBOSE)) != NULL)
    228 				{
    229 					noluck=0;
    230 					break;
    231 				}
    232 			}
    233 		}
    234 #endif
    235 	}
    236 
    237 	if (noluck)
    238 	{
    239 		ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
    240 			"Instrument `%s' can't be found.", name);
    241 		return 0;
    242 	}
    243 
    244 	ctl->cmsg(CMSG_INFO, VERB_NOISY, "Loading instrument %s", current_filename);
    245 
    246 	/* Read some headers and do cursory sanity checks. There are loads
    247 	of magic offsets. This could be rewritten... */
    248 
    249 	if ((239 != fp->Read(tmp, 239)) ||
    250 		(memcmp(tmp, "GF1PATCH110\0ID#000002", 22) &&
    251 		memcmp(tmp, "GF1PATCH100\0ID#000002", 22))) /* don't know what the
    252 													differences are */
    253 	{
    254 		ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: not an instrument", name);
    255 		return 0;
    256 	}
    257 
    258 	if (tmp[82] != 1 && tmp[82] != 0) /* instruments. To some patch makers, 
    259 									  0 means 1 */
    260 	{
    261 		ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
    262 			"Can't handle patches with %d instruments", tmp[82]);
    263 		return 0;
    264 	}
    265 
    266 	if (tmp[151] != 1 && tmp[151] != 0) /* layers. What's a layer? */
    267 	{
    268 		ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
    269 			"Can't handle instruments with %d layers", tmp[151]);
    270 		return 0;
    271 	}
    272 
    273 	ip=(Instrument *)safe_malloc(sizeof(Instrument));
    274 	ip->samples = tmp[198];
    275 	ip->sample = (Sample *)safe_malloc(sizeof(Sample) * ip->samples);
    276 	for (i=0; i<ip->samples; i++)
    277 	{
    278 
    279 		uint8_t fractions;
    280 		 int32_t tmplong;
    281 		uint16_t tmpshort;
    282 		uint8_t tmpchar;
    283 
    284 #define READ_CHAR(thing) \
    285 	if (1 != fp->Read(&tmpchar, 1)) goto fail; \
    286 	thing = tmpchar;
    287 #define READ_SHORT(thing) \
    288 	if (2 != fp->Read(&tmpshort, 2 )) goto fail; \
    289 	thing = LE_SHORT(tmpshort);
    290 #define READ_LONG(thing) \
    291 	if (4 != fp->Read(&tmplong, 4 )) goto fail; \
    292 	thing = LE_LONG(tmplong);
    293 
    294 		skip(fp, 7); /* Skip the wave name */
    295 
    296 		if (1 != fp->Read(&fractions, 1 ))
    297 		{
    298 fail:
    299 			ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Error reading sample %d", i);
    300 			for (j=0; j<i; j++)
    301 				Real_Tim_Free(ip->sample[j].data);
    302 			Real_Tim_Free(ip->sample);
    303 			Real_Tim_Free(ip);
    304 			return 0;
    305 		}
    306 
    307 		sp=&(ip->sample[i]);
    308 
    309 		READ_LONG(sp->data_length);
    310 		READ_LONG(sp->loop_start);
    311 		READ_LONG(sp->loop_end);
    312 		READ_SHORT(sp->sample_rate);
    313 		READ_LONG(sp->low_freq);
    314 		READ_LONG(sp->high_freq);
    315 		READ_LONG(sp->root_freq);
    316 		skip(fp, 2); /* Why have a "root frequency" and then "tuning"?? */
    317 
    318 		READ_CHAR(tmp[0]);
    319 
    320 		if (panning==-1)
    321 			sp->panning = (tmp[0] * 8 + 4) & 0x7f;
    322 		else
    323 			sp->panning=(uint8)(panning & 0x7F);
    324 
    325 		/* envelope, tremolo, and vibrato */
    326 		if (18 != fp->Read(tmp, 18)) goto fail; 
    327 
    328 		if (!tmp[13] || !tmp[14])
    329 		{
    330 			sp->tremolo_sweep_increment=
    331 				sp->tremolo_phase_increment=sp->tremolo_depth=0;
    332 			ctl->cmsg(CMSG_INFO, VERB_DEBUG, " * no tremolo");
    333 		}
    334 		else
    335 		{
    336 			sp->tremolo_sweep_increment=convert_tremolo_sweep(tmp[12]);
    337 			sp->tremolo_phase_increment=convert_tremolo_rate(tmp[13]);
    338 			sp->tremolo_depth=tmp[14];
    339 			ctl->cmsg(CMSG_INFO, VERB_DEBUG,
    340 				" * tremolo: sweep %d, phase %d, depth %d",
    341 				sp->tremolo_sweep_increment, sp->tremolo_phase_increment,
    342 				sp->tremolo_depth);
    343 		}
    344 
    345 		if (!tmp[16] || !tmp[17])
    346 		{
    347 			sp->vibrato_sweep_increment=
    348 				sp->vibrato_control_ratio=sp->vibrato_depth=0;
    349 			ctl->cmsg(CMSG_INFO, VERB_DEBUG, " * no vibrato");
    350 		}
    351 		else
    352 		{
    353 			sp->vibrato_control_ratio=convert_vibrato_rate(tmp[16]);
    354 			sp->vibrato_sweep_increment=
    355 				convert_vibrato_sweep(tmp[15], sp->vibrato_control_ratio);
    356 			sp->vibrato_depth=tmp[17];
    357 			ctl->cmsg(CMSG_INFO, VERB_DEBUG,
    358 				" * vibrato: sweep %d, ctl %d, depth %d",
    359 				sp->vibrato_sweep_increment, sp->vibrato_control_ratio,
    360 				sp->vibrato_depth);
    361 
    362 		}
    363 
    364 		READ_CHAR(sp->modes);
    365 
    366 		skip(fp, 40); /* skip the useless scale frequency, scale factor
    367 					  (what's it mean?), and reserved space */
    368 
    369 		/* Mark this as a fixed-pitch instrument if such a deed is desired. */
    370 		if (note_to_use!=-1)
    371 			sp->note_to_use=(uint8)(note_to_use);
    372 		else
    373 			sp->note_to_use=0;
    374 
    375 		/* seashore.pat in the Midia patch set has no Sustain. I don't
    376 		understand why, and fixing it by adding the Sustain flag to
    377 		all looped patches probably breaks something else. We do it
    378 		anyway. */
    379 
    380 		if (sp->modes & MODES_LOOPING) 
    381 			sp->modes |= MODES_SUSTAIN;
    382 
    383 		/* Strip any loops and envelopes we're permitted to */
    384 		if ((strip_loop==1) && 
    385 			(sp->modes & (MODES_SUSTAIN | MODES_LOOPING | 
    386 			MODES_PINGPONG | MODES_REVERSE)))
    387 		{
    388 			ctl->cmsg(CMSG_INFO, VERB_DEBUG, " - Removing loop and/or sustain");
    389 			sp->modes &=~(MODES_SUSTAIN | MODES_LOOPING | 
    390 				MODES_PINGPONG | MODES_REVERSE);
    391 		}
    392 
    393 		if (strip_envelope==1)
    394 		{
    395 			if (sp->modes & MODES_ENVELOPE)
    396 				ctl->cmsg(CMSG_INFO, VERB_DEBUG, " - Removing envelope");
    397 			sp->modes &= ~MODES_ENVELOPE;
    398 		}
    399 		else if (strip_envelope != 0)
    400 		{
    401 			/* Have to make a guess. */
    402 			if (!(sp->modes & (MODES_LOOPING | MODES_PINGPONG | MODES_REVERSE)))
    403 			{
    404 				/* No loop? Then what's there to sustain? No envelope needed
    405 				either... */
    406 				sp->modes &= ~(MODES_SUSTAIN|MODES_ENVELOPE);
    407 				ctl->cmsg(CMSG_INFO, VERB_DEBUG, 
    408 					" - No loop, removing sustain and envelope");
    409 			}
    410 			else if (!memcmp(tmp, "??????", 6) || tmp[11] >= 100) 
    411 			{
    412 				/* Envelope rates all maxed out? Envelope end at a high "offset"?
    413 				That's a weird envelope. Take it out. */
    414 				sp->modes &= ~MODES_ENVELOPE;
    415 				ctl->cmsg(CMSG_INFO, VERB_DEBUG, 
    416 					" - Weirdness, removing envelope");
    417 			}
    418 			else if (!(sp->modes & MODES_SUSTAIN))
    419 			{
    420 				/* No sustain? Then no envelope.  I don't know if this is
    421 				justified, but patches without sustain usually don't need the
    422 				envelope either... at least the Gravis ones. They're mostly
    423 				drums.  I think. */
    424 				sp->modes &= ~MODES_ENVELOPE;
    425 				ctl->cmsg(CMSG_INFO, VERB_DEBUG, 
    426 					" - No sustain, removing envelope");
    427 			}
    428 		}
    429 
    430 		for (j=0; j<6; j++)
    431 		{
    432 			sp->envelope_rate[j]=
    433 				convert_envelope_rate(tmp[j]);
    434 			sp->envelope_offset[j]= 
    435 				convert_envelope_offset(tmp[6+j]);
    436 		}
    437 
    438 		/* Then read the sample data */
    439 		sp->data = (sample_t*)safe_malloc(sp->data_length);
    440 		if ( static_cast< size_t >( sp->data_length ) != fp->Read(sp->data, sp->data_length ))
    441 			goto fail;
    442 
    443 		if (!(sp->modes & MODES_16BIT)) /* convert to 16-bit data */
    444 		{
    445 			 int32_t i=sp->data_length;
    446 			uint8_t *cp=(uint8_t *)(sp->data);
    447 			uint16_t *tmp,*anew;
    448 			tmp=anew=(uint16*)safe_malloc(sp->data_length*2);
    449 			while (i--)
    450 				*tmp++ = (uint16)(*cp++) << 8;
    451 			cp=(uint8_t *)(sp->data);
    452 			sp->data = (sample_t *)anew;
    453 			Real_Tim_Free(cp);
    454 			sp->data_length *= 2;
    455 			sp->loop_start *= 2;
    456 			sp->loop_end *= 2;
    457 		}
    458 #ifndef LITTLE_ENDIAN
    459 		else
    460 			/* convert to machine byte order */
    461 		{
    462 			 int32_t i=sp->data_length/2;
    463 			int16_t *tmp=(int16_t *)sp->data,s;
    464 			while (i--)
    465 			{ 
    466 				s=LE_SHORT(*tmp);
    467 				*tmp++=s;
    468 			}
    469 		}
    470 #endif
    471 
    472 		if (sp->modes & MODES_UNSIGNED) /* convert to signed data */
    473 		{
    474 			 int32_t i=sp->data_length/2;
    475 			int16_t *tmp=(int16_t *)sp->data;
    476 			while (i--)
    477 				*tmp++ ^= 0x8000;
    478 		}
    479 
    480 		/* Reverse reverse loops and pass them off as normal loops */
    481 		if (sp->modes & MODES_REVERSE)
    482 		{
    483 			 int32_t t;
    484 			/* The GUS apparently plays reverse loops by reversing the
    485 			whole sample. We do the same because the GUS does not SUCK. */
    486 
    487 			ctl->cmsg(CMSG_WARNING, VERB_NORMAL, "Reverse loop in %s", name);
    488 			reverse_data((int16_t *)sp->data, 0, sp->data_length/2);
    489 
    490 			t=sp->loop_start;
    491 			sp->loop_start=sp->data_length - sp->loop_end;
    492 			sp->loop_end=sp->data_length - t;
    493 
    494 			sp->modes &= ~MODES_REVERSE;
    495 			sp->modes |= MODES_LOOPING; /* just in case */
    496 		}
    497 
    498 		/* If necessary do some anti-aliasing filtering  */
    499 
    500 		if (antialiasing_allowed)
    501 			antialiasing(sp,play_mode->rate);
    502 
    503 #ifdef ADJUST_SAMPLE_VOLUMES
    504 		if (amp!=-1)
    505 			sp->volume=(float)((amp) / 100.0);
    506 		else
    507 		{
    508 			/* Try to determine a volume scaling factor for the sample.
    509 			This is a very crude adjustment, but things sound more
    510 			balanced with it. Still, this should be a runtime option. */
    511 			 int32_t i=sp->data_length/2;
    512 			int16_t maxamp=0,a;
    513 			int16_t *tmp=(int16_t *)sp->data;
    514 			while (i--)
    515 			{
    516 				a=*tmp++;
    517 				if (a<0) a=-a;
    518 				if (a>maxamp)
    519 					maxamp=a;
    520 			}
    521 			sp->volume=(float)(32768.0 / maxamp);
    522 			ctl->cmsg(CMSG_INFO, VERB_DEBUG, " * volume comp: %f", sp->volume);
    523 		}
    524 #else
    525 		if (amp!=-1)
    526 			sp->volume=(double)(amp) / 100.0;
    527 		else
    528 			sp->volume=1.0;
    529 #endif
    530 
    531 		sp->data_length /= 2; /* These are in bytes. Convert into samples. */
    532 		sp->loop_start /= 2;
    533 		sp->loop_end /= 2;
    534 
    535 		/* Then fractional samples */
    536 		sp->data_length <<= FRACTION_BITS;
    537 		sp->loop_start <<= FRACTION_BITS;
    538 		sp->loop_end <<= FRACTION_BITS;
    539 
    540 		/* Adjust for fractional loop points. This is a guess. Does anyone
    541 		know what "fractions" really stands for? */
    542 		sp->loop_start |=
    543 			(fractions & 0x0F) << (FRACTION_BITS-4);
    544 		sp->loop_end |=
    545 			((fractions>>4) & 0x0F) << (FRACTION_BITS-4);
    546 
    547 		/* If this instrument will always be played on the same note,
    548 		and it's not looped, we can resample it now. */
    549 		if (sp->note_to_use && !(sp->modes & MODES_LOOPING))
    550 			pre_resample(sp);
    551 
    552 #ifdef LOOKUP_HACK
    553 		/* Squash the 16-bit data into 8 bits. */
    554 		{
    555 			uint8_t *gulp,*ulp;
    556 			int16_t *swp;
    557 			int l=sp->data_length >> FRACTION_BITS;
    558 			gulp=ulp=safe_malloc(l+1);
    559 			swp=(int16_t *)sp->data;
    560 			while(l--)
    561 				*ulp++ = (*swp++ >> 8) & 0xFF;
    562 			Real_Tim_Free(sp->data);
    563 			sp->data=(sample_t *)gulp;
    564 		}
    565 #endif
    566 
    567 		if (strip_tail==1)
    568 		{
    569 			/* Let's not really, just say we did. */
    570 			ctl->cmsg(CMSG_INFO, VERB_DEBUG, " - Stripping tail");
    571 			sp->data_length = sp->loop_end;
    572 		}
    573 	}
    574 
    575 	delete fp;
    576 
    577 	return ip;
    578 }
    579 
    580 static int fill_bank(int dr, int b)
    581 {
    582 	int i, errors=0;
    583 	ToneBank *bank=((dr) ? drumset[b] : tonebank[b]);
    584 	if (!bank)
    585 	{
    586 		ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
    587 			"Huh. Tried to load instruments in non-existent %s %d",
    588 			(dr) ? "drumset" : "tone bank", b);
    589 		return 0;
    590 	}
    591 	for (i=0; i<128; i++)
    592 	{
    593 		if (bank->tone[i].instrument==MAGIC_LOAD_INSTRUMENT)
    594 		{
    595 			if (!(bank->tone[i].name))
    596 			{
    597 				ctl->cmsg(CMSG_WARNING, (b!=0) ? VERB_VERBOSE : VERB_NORMAL,
    598 					"No instrument mapped to %s %d, program %d%s",
    599 					(dr)? "drum set" : "tone bank", b, i, 
    600 					(b!=0) ? "" : " - this instrument will not be heard");
    601 				if (b!=0)
    602 				{
    603 					/* Mark the corresponding instrument in the default
    604 					bank / drumset for loading (if it isn't already) */
    605 					if (!dr)
    606 					{
    607 						if (!(standard_tonebank.tone[i].instrument))
    608 							standard_tonebank.tone[i].instrument=
    609 							MAGIC_LOAD_INSTRUMENT;
    610 					}
    611 					else
    612 					{
    613 						if (!(standard_drumset.tone[i].instrument))
    614 							standard_drumset.tone[i].instrument=
    615 							MAGIC_LOAD_INSTRUMENT;
    616 					}
    617 				}
    618 				bank->tone[i].instrument=0;
    619 				errors++;
    620 			}
    621 			else if (!(bank->tone[i].instrument=
    622 				load_instrument(bank->tone[i].name, 
    623 				(dr) ? 1 : 0,
    624 				bank->tone[i].pan,
    625 				bank->tone[i].amp,
    626 				(bank->tone[i].note!=-1) ? 
    627 				bank->tone[i].note :
    628 			((dr) ? i : -1),
    629 				(bank->tone[i].strip_loop!=-1) ?
    630 				bank->tone[i].strip_loop :
    631 			((dr) ? 1 : -1),
    632 				(bank->tone[i].strip_envelope != -1) ? 
    633 				bank->tone[i].strip_envelope :
    634 			((dr) ? 1 : -1),
    635 				bank->tone[i].strip_tail )))
    636 			{
    637 				ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
    638 					"Couldn't load instrument %s (%s %d, program %d)",
    639 					bank->tone[i].name,
    640 					(dr)? "drum set" : "tone bank", b, i);
    641 				errors++;
    642 			}
    643 		}
    644 	}
    645 	return errors;
    646 }
    647 
    648 int load_missing_instruments(void)
    649 {
    650 	int i=128,errors=0;
    651 	while (i--)
    652 	{
    653 		if (tonebank[i])
    654 			errors+=fill_bank(0,i);
    655 		if (drumset[i])
    656 			errors+=fill_bank(1,i);
    657 	}
    658 	return errors;
    659 }
    660 
    661 void free_instruments(void)
    662 {
    663 	int i=128;
    664 	while(i--)
    665 	{
    666 		if (tonebank[i])
    667 			free_bank(0,i);
    668 		if (drumset[i])
    669 			free_bank(1,i);
    670 	}
    671 }
    672 
    673 int set_default_instrument(char *name)
    674 {
    675 	Instrument *ip;
    676 	if (!(ip=load_instrument(name, 0, -1, -1, -1, 0, 0, 0)))
    677 		return -1;
    678 	if (default_instrument)
    679 		free_instrument(default_instrument);
    680 	default_instrument=ip;
    681 	default_program=SPECIAL_PROGRAM;
    682 	return 0;
    683 }