instrum.cpp 17 KB

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