1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704
|
/* FluidSynth - A Software Synthesizer
*
* Copyright (C) 2003 Peter Hanappe and others.
*
* SoundFont file loading code borrowed from Smurf SoundFont Editor
* Copyright (C) 1999-2001 Josh Green
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public License
* as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307, USA
*/
#include "sfont.h"
#include "fluid.h"
#include "voice.h"
// #define DEBUG_SFONT
#include "libmscore/xml.h"
static bool debugMode = false;
namespace FluidS {
//---------------------------------------------------------
// SFVersion
//---------------------------------------------------------
SFVersion::SFVersion()
{
major = 0;
minor = 0;
}
//---------------------------------------------------------
// SFont
//---------------------------------------------------------
SFont::SFont(Fluid* f)
{
synth = f;
samplepos = 0;
samplesize = 0;
_bankOffset = 0;
}
SFont::~SFont()
{
foreach(Sample* s, sample)
delete s;
foreach(Preset* p, presets)
delete p;
foreach(unsigned char* p, infos)
delete[] p;
foreach(Instrument* i, instruments) {
// foreach(Zone* z, i->zones)
// delete z;
delete i;
}
}
//---------------------------------------------------------
// read
//---------------------------------------------------------
bool SFont::read(const QString& s)
{
f.setFileName(s);
if (!load())
return false;
foreach(Instrument* i, instruments) {
if (!i->import_sfont())
return false;
}
foreach(Preset* p, presets) {
if (!p->importSfont())
return false;
}
return true;
}
//---------------------------------------------------------
// get_preset
//---------------------------------------------------------
Preset* SFont::get_preset(int bank, int num)
{
bank -= _bankOffset;
for (Preset* p : presets) {
if ((p->get_banknum() == bank) && (p->get_num() == num))
return p;
}
return 0;
}
//---------------------------------------------------------
// Preset
//---------------------------------------------------------
Preset::Preset(SFont* s)
{
sfont = s;
bank = 0;
num = 0;
_global_zone = 0;
}
//---------------------------------------------------------
// Preset
//---------------------------------------------------------
Preset::~Preset()
{
delete _global_zone;
foreach(Zone* z, zones)
delete z;
}
//---------------------------------------------------------
// loadSamples
// this is called if the preset is associated with a
// channel
//---------------------------------------------------------
void Preset::loadSamples()
{
// sfont->synth->mutex.lock();
if (_global_zone && _global_zone->instrument) {
Instrument* i = _global_zone->instrument;
if (i->global_zone && i->global_zone->sample)
i->global_zone->sample->load();
foreach(Zone* iz, i->zones)
iz->sample->load();
}
foreach(Zone* z, zones) {
Instrument* i = z->instrument;
if (i->global_zone && i->global_zone->sample)
i->global_zone->sample->load();
foreach(Zone* iz, i->zones)
iz->sample->load();
}
// sfont->synth->mutex.unlock();
}
//---------------------------------------------------------
// noteon
//---------------------------------------------------------
bool Preset::noteon(Fluid* synth, unsigned id, int chan, int key, int vel, double nt)
{
Mod* mod;
Mod* mod_list[FLUID_NUM_MOD]; /* list for 'sorting' preset modulators */
Zone* global_preset_zone = global_zone();
/* run thru all the zones of this preset */
foreach (Zone* preset_zone, zones) {
/* check if the note falls into the key and velocity range of this
preset */
if (preset_zone->inside_range(key, vel)) {
Instrument* inst = preset_zone->get_inst();
Zone* global_inst_zone = inst->get_global_zone();
/* run thru all the zones of this instrument */
foreach(Zone* inst_zone, inst->get_zone()) {
/* make sure this instrument zone has a valid sample */
Sample* sample = inst_zone->get_sample();
if (sample == 0 || sample->inRom())
continue;
/* check if the note falls into the key and velocity range of this
instrument */
if (inst_zone->inside_range(key, vel) && (sample != 0)) {
/* this is a good zone. allocate a new synthesis process and
initialize it */
Voice* voice = synth->alloc_voice(id, sample, chan, key, vel, nt);
if (voice == 0)
return false;
/* Instrumentrument level, generators */
for (int i = 0; i < GEN_LAST; i++) {
/* SF 2.01 section 9.4 'bullet' 4:
*
* A generator in a local instrument zone supersedes a
* global instrument zone generator. Both cases supersede
* the default generator -> voice_gen_set */
if (inst_zone->genlist[i].flags)
voice->gen_set(i, inst_zone->genlist[i].val);
else if ((global_inst_zone != 0) && (global_inst_zone->genlist[i].flags))
voice->gen_set(i, global_inst_zone->genlist[i].val);
else {
/* The generator has not been defined in this instrument.
* Do nothing, leave it at the default.
*/
}
} /* for all generators */
/* global instrument zone, modulators: Put them all into a
* list. */
int mod_list_count = 0;
if (global_inst_zone){
foreach(Mod* mod, global_inst_zone->modlist)
mod_list[mod_list_count++] = mod;
}
/* local instrument zone, modulators.
* Replace modulators with the same definition in the list:
* SF 2.01 page 69, 'bullet' 8
*/
foreach(Mod* mod, inst_zone->modlist) {
/* 'Identical' modulators will be deleted by setting their
* list entry to 0. The list length is known, 0
* entries will be ignored later. SF2.01 section 9.5.1
* page 69, 'bullet' 3 defines 'identical'. */
for (int i = 0; i < mod_list_count; i++){
if (mod_list[i] && test_identity(mod, mod_list[i])){
mod_list[i] = 0;
}
}
/* Finally add the new modulator to to the list. */
mod_list[mod_list_count++] = mod;
}
/* Add instrument modulators (global / local) to the voice. */
for (int i = 0; i < mod_list_count; i++){
mod = mod_list[i];
if (mod) { // disabled modulators CANNOT be skipped.
/* Instrumentrument modulators -supersede- existing (default)
* modulators. SF 2.01 page 69, 'bullet' 6 */
voice->add_mod(mod, FLUID_VOICE_OVERWRITE);
}
}
/* Preset level, generators */
for (int i = 0; i < GEN_LAST; i++) {
/* SF 2.01 section 8.5 page 58: If some generators are
* encountered at preset level, they should be ignored */
if ((i != GEN_STARTADDROFS)
&& (i != GEN_ENDADDROFS)
&& (i != GEN_STARTLOOPADDROFS)
&& (i != GEN_ENDLOOPADDROFS)
&& (i != GEN_STARTADDRCOARSEOFS)
&& (i != GEN_ENDADDRCOARSEOFS)
&& (i != GEN_STARTLOOPADDRCOARSEOFS)
&& (i != GEN_KEYNUM)
&& (i != GEN_VELOCITY)
&& (i != GEN_ENDLOOPADDRCOARSEOFS)
&& (i != GEN_SAMPLEMODE)
&& (i != GEN_EXCLUSIVECLASS)
&& (i != GEN_OVERRIDEROOTKEY)) {
/* SF 2.01 section 9.4 'bullet' 9: A generator in a
* local preset zone supersedes a global preset zone
* generator. The effect is -added- to the destination
* summing node -> voice_gen_incr */
if (preset_zone->genlist[i].flags) {
voice->gen_incr(i, preset_zone->genlist[i].val);
}
else if ((global_preset_zone != 0) && global_preset_zone->genlist[i].flags) {
voice->gen_incr(i, global_preset_zone->genlist[i].val);
}
else {
/* The generator has not been defined in this preset
* Do nothing, leave it unchanged.
*/
}
} /* if available at preset level */
} /* for all generators */
/* Global preset zone, modulators: put them all into a
* list. */
mod_list_count = 0;
if (global_preset_zone){
foreach(Mod* mod, global_preset_zone->modlist)
mod_list[mod_list_count++] = mod;
}
/* Process the modulators of the local preset zone. Kick
* out all identical modulators from the global preset zone
* (SF 2.01 page 69, second-last bullet) */
foreach(Mod* mod, preset_zone->modlist) {
for (int i = 0; i < mod_list_count; i++) {
if (mod_list[i] && test_identity(mod,mod_list[i]))
mod_list[i] = 0;
}
/* Finally add the new modulator to the list. */
mod_list[mod_list_count++] = mod;
}
/* Add preset modulators (global / local) to the voice. */
for (int i = 0; i < mod_list_count; i++){
mod = mod_list[i];
if ((mod != 0) && (mod->amount != 0)) { /* disabled modulators can be skipped. */
/* Preset modulators -add- to existing instrument /
* default modulators. SF2.01 page 70 first bullet on
* page */
voice->add_mod(mod, FLUID_VOICE_ADD);
}
}
/* add the synthesis process to the synthesis loop. */
synth->start_voice(voice);
/* Store the ID of the first voice that was created by this noteon event.
* Exclusive class may only terminate older voices.
* That avoids killing voices, which have just been created.
* (a noteon event can create several voice processes with the same exclusive
* class - for example when using stereo samples)
*/
}
}
}
}
return true;
}
//---------------------------------------------------------
// importSfont
//---------------------------------------------------------
bool Preset::importSfont()
{
if (name.isEmpty())
name = QString("Bank%1,Preset%2").arg(bank).arg(num);
int idx = 0;
foreach(Zone* zone, zones) {
// zone->name = QString("%1/%2").arg(name).arg(idx);
if (!zone->importZone())
return false;
if ((idx == 0) && (zone->get_inst() == 0))
setGlobalZone(zones.takeAt(0));
++idx;
}
return true;
}
//---------------------------------------------------------
// import_sfont
//---------------------------------------------------------
bool Instrument::import_sfont()
{
int idx = 0;
foreach(Zone* zone, zones) {
if (!zone->importZone())
return false;
if ((idx == 0) && (zone->get_sample() == 0))
global_zone = zones.takeAt(0);
idx++;
}
return true;
}
//---------------------------------------------------------
// Zone
//---------------------------------------------------------
Zone::Zone()
{
instrument = 0;
sample = 0;
sampIdx = 0;
instIdx = 0;
keylo = 0;
keyhi = 128;
vello = 0;
velhi = 128;
/* Flag all generators as unused (default, they will be set when they are found
* in the sound font).
* This also sets the generator values to default, but that is of no concern here.*/
fluid_gen_set_default_values(&genlist[0]);
}
Zone::~Zone()
{
foreach(Mod* m, modlist)
delete m;
foreach(SFGen* p, gen)
delete p;
foreach(SFMod* p, mod)
delete p;
}
//---------------------------------------------------------
// inside_range
//---------------------------------------------------------
bool Zone::inside_range(int key, int vel) const
{
return ((keylo <= key) && (keyhi >= key) && (vello <= vel) && (velhi >= vel));
}
//---------------------------------------------------------
// Instrument
//---------------------------------------------------------
Instrument::Instrument()
{
global_zone = 0;
}
Instrument::~Instrument()
{
delete global_zone;
foreach(Zone* z, zones)
delete z;
}
//---------------------------------------------------------
// importZone
//---------------------------------------------------------
bool Zone::importZone()
{
foreach (SFGen* sfgen, gen) {
switch (sfgen->id) {
case GEN_KEYRANGE:
keylo = sfgen->amount.range.lo;
keyhi = sfgen->amount.range.hi;
break;
case GEN_VELRANGE:
vello = sfgen->amount.range.lo;
velhi = sfgen->amount.range.hi;
break;
default:
/* FIXME: some generators have an unsigned word amount value but i don't know which ones
*/
genlist[sfgen->id].val = (float) sfgen->amount.sword;
genlist[sfgen->id].flags = GEN_SET;
break;
}
}
// Import the modulators (only SF2.1 and higher)
foreach(SFMod* mod_src, mod) {
Mod* mod_dest = new Mod;
int type;
// mod_dest->next = 0; /* pointer to next modulator, this is the end of the list now.*/
/* *** Amount *** */
mod_dest->amount = mod_src->amount;
/* *** Source *** */
mod_dest->src1 = mod_src->src & 127; /* index of source 1, seven-bit value, SF2.01 section 8.2, page 50 */
mod_dest->flags1 = 0;
/* Bit 7: CC flag SF 2.01 section 8.2.1 page 50*/
if (mod_src->src & (1<<7))
mod_dest->flags1 |= FLUID_MOD_CC;
else
mod_dest->flags1 |= FLUID_MOD_GC;
/* Bit 8: D flag SF 2.01 section 8.2.2 page 51*/
if (mod_src->src & (1<<8))
mod_dest->flags1 |= FLUID_MOD_NEGATIVE;
else
mod_dest->flags1 |= FLUID_MOD_POSITIVE;
/* Bit 9: P flag SF 2.01 section 8.2.3 page 51*/
if (mod_src->src & (1<<9))
mod_dest->flags1 |= FLUID_MOD_BIPOLAR;
else
mod_dest->flags1 |= FLUID_MOD_UNIPOLAR;
/* modulator source types: SF2.01 section 8.2.1 page 52 */
type = (mod_src->src) >> 10;
type &= 63; /* type is a 6-bit value */
if (type == 0)
mod_dest->flags1 |= FLUID_MOD_LINEAR;
else if (type == 1)
mod_dest->flags1 |= FLUID_MOD_CONCAVE;
else if (type == 2)
mod_dest->flags1 |= FLUID_MOD_CONVEX;
else if (type == 3)
mod_dest->flags1 |= FLUID_MOD_SWITCH;
else {
/* This shouldn't happen - unknown type!
* Deactivate the modulator by setting the amount to 0.
*/
mod_dest->amount=0;
}
/* *** Dest *** */
mod_dest->dest = mod_src->dest; /* index of controlled generator */
/* *** Amount source *** */
mod_dest->src2 = mod_src->amtsrc & 127; /* index of source 2, seven-bit value, SF2.01 section 8.2, p.50 */
mod_dest->flags2 = 0;
/* Bit 7: CC flag SF 2.01 section 8.2.1 page 50*/
if (mod_src->amtsrc & (1<<7))
mod_dest->flags2 |= FLUID_MOD_CC;
else
mod_dest->flags2 |= FLUID_MOD_GC;
/* Bit 8: D flag SF 2.01 section 8.2.2 page 51*/
if (mod_src->amtsrc & (1<<8))
mod_dest->flags2 |= FLUID_MOD_NEGATIVE;
else
mod_dest->flags2 |= FLUID_MOD_POSITIVE;
/* Bit 9: P flag SF 2.01 section 8.2.3 page 51*/
if (mod_src->amtsrc & (1<<9))
mod_dest->flags2 |= FLUID_MOD_BIPOLAR;
else
mod_dest->flags2 |= FLUID_MOD_UNIPOLAR;
/* modulator source types: SF2.01 section 8.2.1 page 52 */
type = (mod_src->amtsrc) >> 10;
type &= 63; /* type is a 6-bit value */
if (type == 0)
mod_dest->flags2 |= FLUID_MOD_LINEAR;
else if (type == 1)
mod_dest->flags2 |= FLUID_MOD_CONCAVE;
else if (type == 2)
mod_dest->flags2 |= FLUID_MOD_CONVEX;
else if (type == 3)
mod_dest->flags2 |= FLUID_MOD_SWITCH;
else {
/* This shouldn't happen - unknown type!
* Deactivate the modulator by setting the amount to 0. */
mod_dest->amount=0;
}
/* *** Transform *** */
/* SF2.01 only uses the 'linear' transform (0).
* Deactivate the modulator by setting the amount to 0 in any other case.
*/
if (mod_src->trans !=0)
mod_dest->amount = 0;
/* Store the new modulator in the zone The order of modulators
* will make a difference, at least in an instrument context: The
* second modulator overwrites the first one, if they only differ
* in amount.
*/
modlist.append(mod_dest);
}
return true;
}
//---------------------------------------------------------
// Sample
//---------------------------------------------------------
Sample::Sample(SFont* s)
{
sf = s;
_valid = false;
start = 0;
end = 0;
loopstart = 0;
loopend = 0;
samplerate = 0;
origpitch = 0;
pitchadj = 0;
sampletype = 0;
data = 0;
amplitude_that_reaches_noise_floor_is_valid = false;
amplitude_that_reaches_noise_floor = 0.0;
}
//---------------------------------------------------------
// Sample
//---------------------------------------------------------
Sample::~Sample()
{
delete[] data;
}
//---------------------------------------------------------
// load
//---------------------------------------------------------
void Sample::load()
{
if (!_valid || data)
return;
QFile fd(sf->get_name());
if (!fd.open(QIODevice::ReadOnly))
return;
if (sampletype & FLUID_SAMPLETYPE_OGG_VORBIS) {
if (!fd.seek(sf->samplePos() + start))
return;
}
else {
if (!fd.seek(sf->samplePos() + start * sizeof(short)))
return;
}
unsigned int size = end - start;
if (sampletype & FLUID_SAMPLETYPE_OGG_VORBIS) {
#ifdef SOUNDFONT3
char* p = new char[size];
if (fd.read(p, size) != size) {
printf(" read %d failed\n", size);
delete[] p;
return;
}
decompressOggVorbis(p, size);
delete[] p;
#endif
}
else {
data = new short[size];
size *= sizeof(short);
if (fd.read((char*)data, size) != size)
return;
if (QSysInfo::ByteOrder == QSysInfo::BigEndian) {
unsigned char hi, lo;
unsigned int i, j;
short s;
uchar* cbuf = (uchar*) data;
for (i = 0, j = 0; j < size; i++) {
lo = cbuf[j++];
hi = cbuf[j++];
s = (hi << 8) | lo;
data[i] = s;
}
}
end -= (start + 1); // marks last sample, contrary to SF spec.
loopstart -= start;
loopend -= start;
start = 0;
}
optimize();
}
//---------------------------------------------------------
// inRom
//---------------------------------------------------------
bool Sample::inRom() const
{
return sampletype & FLUID_SAMPLETYPE_ROM;
}
static const char idlist[] = {
"RIFFLISTsfbkINFOsdtapdtaifilisngINAMiromiverICRDIENGIPRD"
"ICOPICMTISFTsnamsmplphdrpbagpmodpgeninstibagimodigenshdr"
};
//---------------------------------------------------------
// chunkid
//---------------------------------------------------------
static int chunkid (unsigned int id)
{
unsigned int* p = (unsigned int *) & idlist;
for (unsigned i = 0; i < sizeof (idlist) / sizeof (int); i++, p++) {
if (*p == id)
return (i + 1);
}
return UNKN_ID;
}
//---------------------------------------------------------
// preset_compare
//---------------------------------------------------------
static bool preset_compare (Preset* a, Preset* b)
{
int aval = (a->bank) << 16 | a->num;
int bval = (b->bank) << 16 | b->num;
return aval < bval;
}
//---------------------------------------------------------
// readchunk
//---------------------------------------------------------
void SFont::readchunk(SFChunk* var)
{
safe_fread(var, 8);
if (QSysInfo::ByteOrder == QSysInfo::BigEndian)
var->size = GUINT32_FROM_BE(var->size);
else
var->size = GUINT32_FROM_LE(var->size);
}
//---------------------------------------------------------
// load
// return true on success
//---------------------------------------------------------
bool SFont::load()
{
if (!f.open(QIODevice::ReadOnly)) {
qCritical("Unable to open file \"%s\"", qPrintable(f.fileName()));
return false;
}
SFChunk chunk;
try {
readchunk(&chunk);
if (chunkid(chunk.id) != RIFF_ID)
throw(QString("Not a RIFF file"));
safe_fread(&chunk.id, 4);
if (chunkid (chunk.id) != SFBK_ID) /* error if not SFBK_ID */
throw(QString("Not a sound font file"));
if (chunk.size != f.size() - 8)
throw(QString("Sound font file size mismatch %1 %2").arg(chunk.size).arg(f.size() - 8));
/* Process INFO block */
read_listchunk(&chunk);
if (chunkid(chunk.id) != INFO_ID)
throw(QString("Invalid ID found when expecting INFO chunk"));
process_info(chunk.size);
/* Process sample chunk */
read_listchunk(&chunk);
if (chunkid (chunk.id) != SDTA_ID)
throw(QString("Invalid ID found when expecting SAMPLE chunk"));
process_sdta(chunk.size);
/* process HYDRA chunk */
read_listchunk(&chunk);
if (chunkid (chunk.id) != PDTA_ID)
throw(QString("Invalid ID found when expecting HYDRA chunk"));
process_pdta (chunk.size);
fixup_pgen();
fixup_igen();
}
catch (QString s) {
qDebug("fluid: error loading sound font: %s", qPrintable(s));
f.close();
return false;
}
f.close();
/* sort preset list by bank, preset # */
qSort(presets.begin(), presets.end(), preset_compare);
return true;
}
//---------------------------------------------------------
// READW
//---------------------------------------------------------
unsigned short SFont::READW()
{
unsigned short _temp;
safe_fread(&_temp, 2);
if (QSysInfo::ByteOrder == QSysInfo::BigEndian)
return GINT16_FROM_BE(_temp);
else
return GINT16_FROM_LE(_temp);
}
//---------------------------------------------------------
// READD
//---------------------------------------------------------
void SFont::READD(unsigned int& var)
{
unsigned int _temp;
safe_fread(&_temp, 4);
if (QSysInfo::ByteOrder == QSysInfo::BigEndian)
var = GINT32_FROM_BE(_temp);
else
var = GINT32_FROM_LE(_temp);
}
//---------------------------------------------------------
// READB
//---------------------------------------------------------
unsigned char SFont::READB()
{
unsigned char var;
safe_fread(&var, 1);
return var;
}
//---------------------------------------------------------
// READC
//---------------------------------------------------------
char SFont::READC()
{
char var;
safe_fread(&var, 1);
return var;
}
//---------------------------------------------------------
// FSKIPW
//---------------------------------------------------------
void SFont::FSKIPW()
{
safe_fseek(2);
}
void SFont::READSTR(char* name)
{
safe_fread(name, 20);
name[20] = '\0';
}
//---------------------------------------------------------
// read_listchunk
//---------------------------------------------------------
void SFont::read_listchunk (SFChunk* chunk)
{
readchunk (chunk);
if (chunkid (chunk->id) != LIST_ID)
throw(QString("Invalid chunk id in level 0 parse"));
safe_fread(&chunk->id, 4);
chunk->size -= 4;
}
//---------------------------------------------------------
// process_info
//---------------------------------------------------------
void SFont::process_info(int size)
{
while (size > 0) {
SFChunk chunk;
readchunk (&chunk);
size -= 8;
unsigned char id = chunkid (chunk.id);
if (id == IFIL_ID) { // sound font version chunk?
if (chunk.size != 4)
throw(QString("Sound font version info chunk has invalid size"));
_version.major = READW();
_version.minor = READW();
if (_version.major < 2 || _version.major > 3)
throw(QString("Bad Sound font version %1.%2").arg(_version.major).arg(_version.minor));
}
else if (id == IVER_ID) { /* ROM version chunk? */
if (chunk.size != 4)
throw(QString("ROM version info chunk has invalid size"));
romver.major = READW();
romver.minor = READW();
}
else if (id != UNKN_ID) {
if ((id != ICMT_ID && chunk.size > 256) || (chunk.size > 65536) || (chunk.size % 2))
throw(QString("INFO sub chunk has invalid chunk size"));
/* alloc for chunk id and da chunk */
unsigned char* item = new unsigned char[chunk.size + 1];
*(unsigned char *) item = id;
safe_fread (&item[1], chunk.size);
/* force terminate info item (don't forget uint8 info ID) */
*(item + chunk.size) = '\0';
infos.append(item);
}
else
throw(QString("Invalid chunk id in INFO chunk"));
size -= chunk.size;
}
if (size < 0)
throw(QString("INFO chunk size mismatch"));
}
//---------------------------------------------------------
// process_sdta
// return true on success
//---------------------------------------------------------
void SFont::process_sdta (unsigned int size)
{
if (size == 0)
return; // no sample data?
/* read sub chunk */
SFChunk chunk;
readchunk (&chunk);
size -= 8;
if (chunkid (chunk.id) != SMPL_ID)
throw(QString("Expected SMPL chunk found invalid id instead"));
/* SDTA chunk may also contain sm24 chunk for 24 bit samples
* (not yet supported), only an error if SMPL chunk size is
* greater than SDTA. */
if (chunk.size > size)
throw(QString("SDTA chunk size mismatch %1 != %2").arg(size).arg(chunk.size));
/* sample data follows */
setSamplepos(f.pos());
setSamplesize(chunk.size);
FSKIP(size);
}
//---------------------------------------------------------
// pdtahelper
//---------------------------------------------------------
void SFont::pdtahelper (unsigned expid, unsigned reclen, SFChunk* chunk, int* size)
{
const char* expstr = CHNKIDSTR (expid);
readchunk (chunk);
*size -= 8;
unsigned id = chunkid(chunk->id);
if (id != expid)
throw(QString("Expected PDTA sub-chunk %1 found invalid id instead").arg(expstr));
if (chunk->size % reclen) /* valid chunk size? */
throw(QString("chunk size is not a multiple of %1 bytes").arg(reclen));
if ((*size -= chunk->size) < 0)
throw(QString("%1 chunk size exceeds remaining PDTA chunk size").arg(expstr));
}
//---------------------------------------------------------
// process_pdta
//---------------------------------------------------------
void SFont::process_pdta (int size)
{
static const unsigned id[] = {
PHDR_ID, PBAG_ID, PMOD_ID, PGEN_ID, IHDR_ID, IBAG_ID, IMOD_ID, IGEN_ID, SHDR_ID
};
static const unsigned len[] = {
SFPHDRSIZE, SFBAGSIZE, SFMODSIZE, SFGENSIZE, SFIHDRSIZE,
SFBAGSIZE, SFMODSIZE, SFGENSIZE, SFSHDRSIZE
};
typedef void (SFont::*LoadFunc)(int);
static const LoadFunc funcArray[] = {
&SFont::load_phdr, &SFont::load_pbag, &SFont::load_pmod, &SFont::load_pgen,
&SFont::load_ihdr, &SFont::load_ibag, &SFont::load_imod, &SFont::load_igen,
&SFont::load_shdr
};
SFChunk chunk;
for (int i = 0; i < 9; ++i) {
pdtahelper(id[i], len[i], &chunk, &size);
(this->*funcArray[i])(chunk.size);
}
}
/* preset header loader */
void SFont::load_phdr (int size)
{
Preset* pr = 0; /* ptr to current & previous preset */
unsigned short zndx, pzndx = 0;
if (size % SFPHDRSIZE || size == 0)
throw(QString("Preset header chunk size is invalid"));
int i = size / SFPHDRSIZE - 1;
if (i == 0) { /* at least one preset + term record */
qWarning("File contains no presets");
FSKIP (SFPHDRSIZE);
return;
}
for (; i > 0; i--) { /* load all preset headers */
Preset* p = new Preset(this);
presets.append(p);
char str[21];
READSTR (str);
p->name = QString::fromLatin1(str);
p->num = READW();
p->bank = READW();
zndx = READW();
unsigned int tmp;
READD (tmp);
READD (tmp);
READD (tmp);
if (pr) { /* not first preset? */
if (zndx < pzndx)
throw(QString("Preset header indices not monotonic"));
int i2 = zndx - pzndx;
while (i2--)
pr->zones.prepend(0);
}
else if (zndx > 0) /* 1st preset, warn if ofs >0 */
qWarning("%d preset zones not referenced, discarding", zndx);
pr = p; /* update preset ptr */
pzndx = zndx;
}
FSKIP (24);
zndx = READW(); /* Read terminal generator index */
FSKIP (12);
if (zndx < pzndx)
throw(QString("Preset header indices not monotonic"));
int i2 = zndx - pzndx;
while (i2--)
pr->zones.prepend(0);
}
/* preset bag loader */
void SFont::load_pbag (int size)
{
Zone *z, *pz = 0;
unsigned short genndx, modndx;
unsigned short pgenndx = 0, pmodndx = 0;
if (size % SFBAGSIZE || size == 0) /* size is multiple of SFBAGSIZE? */
throw(QString("Preset bag chunk size is invalid"));
foreach(Preset* p, presets) {
for (int i = 0; i < p->zones.size(); ++i) {
if ((size -= SFBAGSIZE) < 0)
throw(QString("1:Preset bag chunk size mismatch"));
z = new Zone;
genndx = READW(); /* possible read failure ^ */
modndx = READW();
z->sample = 0;
if (pz) { /* if not first zone */
if (genndx < pgenndx)
throw(QString("Preset bag generator indices not monotonic"));
if (modndx < pmodndx)
throw(QString("Preset bag modulator indices not monotonic"));
int ii = genndx - pgenndx;
while (ii--)
pz->gen.prepend(0);
ii = modndx - pmodndx;
while (ii--)
pz->mod.prepend(0);
}
pz = z; /* update previous zone ptr */
pgenndx = genndx; /* update previous zone gen index */
pmodndx = modndx; /* update previous zone mod index */
p->zones[i] = z;
}
}
size -= SFBAGSIZE;
if (size != 0)
throw(QString("2:Preset bag chunk size mismatch"));
genndx = READW();
modndx = READW();
if (!pz) {
if (genndx > 0)
qWarning("No preset generators and terminal index not 0");
if (modndx > 0)
qWarning("No preset modulators and terminal index not 0");
return;
}
if (genndx < pgenndx)
throw(QString("Preset bag generator indices not monotonic"));
if (modndx < pmodndx)
throw(QString("Preset bag modulator indices not monotonic"));
int i = genndx - pgenndx;
while (i--)
pz->gen.prepend(0);
i = modndx - pmodndx;
while (i--)
pz->mod.prepend(0);
}
//---------------------------------------------------------
// load_pmod
// preset modulator loader
//---------------------------------------------------------
void SFont::load_pmod (int size)
{
foreach (Preset* p, presets) {
foreach(Zone* p2, p->zones) {
for (int i = 0; i < p2->mod.size(); ++i) {
if ((size -= SFMODSIZE) < 0)
throw(QString("Preset modulator chunk size mismatch"));
SFMod* m = new SFMod;
m->src = READW();
m->dest = READW();
m->amount = READW();
m->amtsrc = READW();
m->trans = READW();
p2->mod[i] = m;
}
}
}
/*
If there isn't even a terminal record
Hmmm, the specs say there should be one, but..
*/
if (size == 0)
return;
size -= SFMODSIZE;
if (size != 0)
throw(QString("Preset modulator chunk size mismatch"));
FSKIP (SFMODSIZE); /* terminal mod */
}
static const unsigned short badgen[] = {
Gen_Unused1, Gen_Unused2, Gen_Unused3, Gen_Unused4,
Gen_Reserved1, Gen_Reserved2, Gen_Reserved3, 0
};
static const unsigned short badpgen[] = {
Gen_StartAddrOfs, Gen_EndAddrOfs, Gen_StartLoopAddrOfs,
Gen_EndLoopAddrOfs, Gen_StartAddrCoarseOfs, Gen_EndAddrCoarseOfs,
Gen_StartLoopAddrCoarseOfs, Gen_Keynum, Gen_Velocity,
Gen_EndLoopAddrCoarseOfs, Gen_SampleModes, Gen_ExclusiveClass,
Gen_OverrideRootKey, 0
};
/* check validity of instrument generator */
static int gen_valid (int gen)
{ /* is generator id valid? */
int i = 0;
if (gen > Gen_MaxValid)
return false;
while (badgen[i] && badgen[i] != gen)
i++;
return (badgen[i] == 0);
}
/* check validity of preset generator */
static int gen_validp (int gen)
{ /* is preset generator valid? */
int i = 0;
if (!gen_valid (gen))
return false;
while (badpgen[i] && badpgen[i] != (unsigned short) gen)
i++;
return (badpgen[i] == 0);
}
//---------------------------------------------------------
// gen_inlist
// Find generator in gen list
//---------------------------------------------------------
static SFGen* gen_inlist (int gen, const QList<SFGen*>& genlist)
{
foreach(SFGen* p, genlist) {
if (p == 0)
break;
if (gen == p->id)
return p;
}
return 0;
}
/* delete zone from zone list */
static void sfont_zone_delete (QList<Zone*>* l, Zone * zone)
{
l->removeOne(zone);
delete zone;
}
/* -------------------------------------------------------------------
* preset generator loader
* generator (per preset) loading rules:
* Zones with no generators or modulators shall be annihilated
* Global zone must be 1st zone, discard additional ones (instrumentless zones)
*
* generator (per zone) loading rules (in order of decreasing precedence):
* KeyRange is 1st in list (if exists), else discard
* if a VelRange exists only preceded by a KeyRange, else discard
* if a generator follows an instrument discard it
* if a duplicate generator exists replace previous one
* ------------------------------------------------------------------- */
void SFont::load_pgen (int size)
{
foreach(Preset* p, presets) {
bool gzone = false;
bool discarded = false;
QList<Zone*>& zl = p->zones;
for (int k = 0; k < zl.size(); ++k) {
int level = 0;
Zone* z = zl[k];
int i = 0;
for (; i < z->gen.size();) {
SFGen* dup = 0;
bool skip = false;
bool drop = false;
if ((size -= SFGENSIZE) < 0)
throw(QString("1:Preset generator chunk size mismatch"));
unsigned short genid = READW();
SFGenAmount genval;
if (genid == Gen_KeyRange) { /* nothing precedes */
if (level == 0) {
level = 1;
genval.range.lo = READB();
genval.range.hi = READB();
}
else
skip = true;
}
else if (genid == Gen_VelRange) { // only KeyRange precedes
if (level <= 1) {
level = 2;
genval.range.lo = READB();
genval.range.hi = READB();
}
else
skip = true;
}
else if (genid == Gen_Instrument) { /* inst is last gen */
level = 3;
genval.uword = READW();
z->instIdx = genval.uword + 1;
break; /* break out of generator loop */
}
else {
level = 2;
if (gen_validp (genid)) { /* generator valid? */
genval.sword = READW();
dup = gen_inlist (genid, z->gen);
}
else
skip = true;
}
if (!skip) {
SFGen *g;
if (!dup) { /* if gen ! dup alloc new */
g = new SFGen;
g->id = genid;
z->gen[i] = g;
}
else {
g = dup; // ptr to orig gen
drop = true;
}
g->amount = genval;
}
else { /* Skip this generator */
discarded = true;
drop = true;
FSKIPW ();
}
if (!drop)
++i;
else {
z->gen.removeAt(i); // drop place holder
}
}
if (level == 3)
z->gen.removeAt(i); // drop place holder
else { // congratulations its a global zone
if (!gzone) { // Prior global zones?
gzone = true;
// if global zone is not 1st zone, relocate
if (k != 0) {
Zone* save = zl.takeAt(k);
qDebug("Preset \"%s\": Global zone is not first zone", qPrintable(p->name));
zl.prepend(save);
continue;
}
}
else { // previous global zone exists, discard
qDebug("Preset \"%s\": Discarding invalid global zone", qPrintable(p->name));
sfont_zone_delete(&zl, z);
}
}
for (; i < z->gen.size();) {
discarded = true;
if ((size -= SFGENSIZE) < 0)
throw(QString("2:Preset generator chunk size mismatch"));
FSKIP (SFGENSIZE);
z->gen.removeAt(i);
}
}
if (discarded)
qDebug("Preset \"%s\": Some invalid generators were discarded", qPrintable(p->name));
}
/* in case there isn't a terminal record */
if (size == 0)
return;
size -= SFGENSIZE;
if (size != 0)
throw(QString("3:Preset generator chunk size mismatch"));
FSKIP (SFGENSIZE); /* terminal gen */
}
/* instrument header loader */
void SFont::load_ihdr(int size)
{
Instrument *p, *pr = 0; /* ptr to current & previous instrument */
unsigned short zndx, pzndx = 0;
if (size % SFIHDRSIZE || size == 0) /* chunk size is valid? */
throw(QString("Instrumentrument header has invalid size"));
size = size / SFIHDRSIZE - 1;
if (size == 0) { /* at least one preset + term record */
qWarning("File contains no instruments");
FSKIP (SFIHDRSIZE);
return;
}
for (int i = 0; i < size; i++) { /* load all instrument headers */
p = new Instrument;
instruments.append(p);
char buffer[21];
READSTR (buffer); /* Possible read failure ^ */
zndx = READW();
if (pr) { /* not first instrument? */
if (zndx < pzndx)
throw(QString("Instrument header indices not monotonic"));
int i2 = zndx - pzndx;
while (i2--)
pr->zones.prepend(0);
}
else if (zndx > 0) { /* 1st inst, warn if ofs >0 */
qWarning("%d instrument zones not referenced, discarding", zndx);
}
pzndx = zndx;
pr = p; /* update instrument ptr */
}
FSKIP (20);
zndx = READW();
if (zndx < pzndx)
throw(QString("Instrumentrument header indices not monotonic"));
int i2 = zndx - pzndx;
while (i2--)
pr->zones.prepend(0);
}
/* instrument bag loader */
void SFont::load_ibag(int size)
{
Zone *z, *pz = 0;
unsigned short genndx, modndx, pgenndx = 0, pmodndx = 0;
if (size % SFBAGSIZE || size == 0) /* size is multiple of SFBAGSIZE? */
throw(QString("Instrumentrument bag chunk size is invalid"));
foreach(Instrument* in, instruments) {
int n = in->zones.size();
for (int i = 0; i < n; ++i) {
if ((size -= SFBAGSIZE) < 0) {
throw(QString("Instrument bag chunk size mismatch"));
}
z = new Zone;
in->zones[i] = z;
genndx = READW();
modndx = READW();
z->sample = 0;
if (pz) { /* if not first zone */
if (genndx < pgenndx)
throw(QString("Instrumentrument generator indices not monotonic"));
if (modndx < pmodndx)
throw(QString("Instrumentrument modulator indices not monotonic"));
int i2 = genndx - pgenndx;
while (i2--)
pz->gen.prepend(0);
i2 = modndx - pmodndx;
while (i2--)
pz->mod.prepend(0);
}
pz = z; /* update previous zone ptr */
pgenndx = genndx;
pmodndx = modndx;
}
}
size -= SFBAGSIZE;
if (size != 0)
throw(QString("Instrumentrument chunk size mismatch"));
genndx = READW();
modndx = READW();
if (!pz) { /* in case that all are no zoners */
if (genndx > 0)
qWarning("No instrument generators and terminal index not 0");
if (modndx > 0)
qWarning("No instrument modulators and terminal index not 0");
return;
}
if (genndx < pgenndx)
throw(QString("Instrumentrument generator indices not monotonic"));
if (modndx < pmodndx)
throw(QString("Instrumentrument modulator indices not monotonic"));
int i = genndx - pgenndx;
while (i--)
pz->gen.prepend(0);
i = modndx - pmodndx;
while (i--)
pz->mod.prepend(0);
}
/* instrument modulator loader */
void SFont::load_imod(int size)
{
foreach(Instrument* i, instruments) {
foreach(Zone* p2, i->zones) {
for (int k = 0; k < p2->mod.size(); ++k) {
if ((size -= SFMODSIZE) < 0)
throw(QString("Instrumentrument modulator chunk size mismatch"));
SFMod* m = new SFMod;
m->src = READW();
m->dest = READW();
m->amount = READW();
m->amtsrc = READW();
m->trans = READW();
p2->mod[k] = m;
}
}
}
/*
If there isn't even a terminal record
Hmmm, the specs say there should be one, but..
*/
if (size == 0)
return;
size -= SFMODSIZE;
if (size != 0)
throw(QString("Instrumentrument modulator chunk size mismatch"));
FSKIP (SFMODSIZE); /* terminal mod */
}
//---------------------------------------------------------
// load_igen
// load instrument generators
// (see load_pgen for loading rules)
//---------------------------------------------------------
void SFont::load_igen (int size)
{
foreach(Instrument* instr, instruments) {
bool gzone = false;
bool discarded = false;
QList<Zone*> zl = instr->zones;
for (int k = 0; k < zl.size(); ++k) {
int level = 0;
Zone* z = zl[k];
int i = 0;
for (; i < z->gen.size();) {
SFGenAmount genval;
SFGen* dup = 0;
bool skip = false;
bool drop = false;
if ((size -= SFGENSIZE) < 0)
throw(QString("IGEN chunk size mismatch"));
unsigned short genid = READW();
if (genid == Gen_KeyRange) { /* nothing precedes */
if (level == 0) {
level = 1;
genval.range.lo = READB();
genval.range.hi = READB();
}
else
skip = true;
}
else if (genid == Gen_VelRange) { // only KeyRange precedes
if (level <= 1) {
level = 2;
genval.range.lo = READB();
genval.range.hi = READB();
}
else
skip = true;
}
else if (genid == Gen_SampleId) { // sample is last gen
level = 3;
genval.uword = READW();
z->sampIdx = genval.uword + 1;
break; /* break out of generator loop */
}
else {
level = 2;
if (gen_valid (genid)) { // gen valid?
genval.sword = READW();
dup = gen_inlist (genid, z->gen);
}
else
skip = true;
}
if (!skip) {
SFGen* g;
if (!dup) { /* if gen ! dup alloc new */
g = new SFGen;
g->id = genid;
z->gen[i] = g;
}
else {
g = dup;
drop = true;
}
g->amount = genval;
}
else { /* skip this generator */
discarded = true;
drop = true;
FSKIPW ();
}
if (!drop)
++i;
else
z->gen.removeAt(i);
}
if (level == 3)
z->gen.removeAt(i);
else { /* its a global zone */
if (!gzone) {
gzone = true;
/* if global zone is not 1st zone, relocate */
if (k != 0) {
Zone* save = zl.takeAt(k);
zl.prepend(save);
continue;
}
}
else { /* previous global zone exists, discard */
sfont_zone_delete (&zl, z);
}
}
for (; i < z->gen.size();) {
discarded = true;
if ((size -= SFGENSIZE) < 0)
throw(QString("Instrumentrument generator chunk size mismatch"));
FSKIP (SFGENSIZE);
z->gen.removeAt(i);
}
}
if (discarded && debugMode)
qWarning("Instrument: Some invalid generators were discarded");
}
/* for those non-terminal record cases, grr! */
if (size == 0)
return;
size -= SFGENSIZE;
if (size != 0)
throw(QString("IGEN chunk size mismatch"));
FSKIP (SFGENSIZE); /* terminal gen */
}
//---------------------------------------------------------
// load_shdr
// sample header loader
//---------------------------------------------------------
void SFont::load_shdr (int size)
{
if (size % SFSHDRSIZE || size == 0) /* size is multiple of SHDR size? */
throw(QString("Sample header has invalid size"));
size = size / SFSHDRSIZE - 1;
if (size == 0) { // at least one sample + term record?
qWarning("File contains no samples");
FSKIP (SFSHDRSIZE);
return;
}
/* load all sample headers */
for (int i = 0; i < size; i++) {
Sample* p = new Sample(this);
sample.append(p);
char buffer[21];
READSTR (buffer);
// READSTR (p->name);
READD (p->start);
READD (p->end); /* - end, loopstart and loopend */
READD (p->loopstart); /* - will be checked and turned into */
READD (p->loopend);
READD (p->samplerate);
p->origpitch = READB();
p->pitchadj = READC();
FSKIPW (); // skip sample link
p->sampletype = READW();
if (p->sampletype & FLUID_SAMPLETYPE_ROM) {
p->setValid(false);
continue;
}
if ((p->end > getSamplesize()) || (p->start > (p->end - 4))) {
qWarning("Sample start/end file positions are invalid, disabling");
p->setValid(false);
continue;
}
p->setValid(true);
if (p->sampletype & FLUID_SAMPLETYPE_OGG_VORBIS) {
}
else {
// loop is fowled?? (cluck cluck :)
if (p->loopend > p->end || p->loopstart >= p->loopend || p->loopstart <= p->start) {
/* can pad loop by 8 samples and ensure at least 4 for loop (2*8+4) */
if ((p->end - p->start) >= 20) {
p->loopstart = p->start + 8;
p->loopend = p->end - 8;
}
else { // loop is fowled, sample is tiny (can't pad 8 samples)
p->loopstart = p->start + 1;
p->loopend = p->end - 1;
}
}
if ((p->end - p->start) < 8)
p->setValid(false);
}
}
FSKIP (SFSHDRSIZE); /* skip terminal shdr */
}
//---------------------------------------------------------
// fixup_pgen
// "fixup" (inst # -> inst ptr) instrument references
// in preset list
//---------------------------------------------------------
void SFont::fixup_pgen()
{
foreach(Preset* p, presets) {
foreach(Zone* z, p->zones) {
if (z->instIdx) { // load instrument #
z->instrument = instruments[z->instIdx-1];
if (!z->instrument)
throw(QString("Preset %1 %2: Invalid instrument reference").arg(p->bank).arg(p->num));
}
}
}
}
/* "fixup" (sample # -> sample ptr) sample references in instrument list */
void SFont::fixup_igen()
{
foreach(Instrument* p, instruments) {
foreach(Zone* z, p->zones) {
if (z->sampIdx) {
z->sample = sample[z->sampIdx - 1];
if (!z->sample)
throw(QString("Instrument: Invalid sample reference"));
// throw(QString("Instrument <%1>: Invalid sample reference").arg(p->name));
}
}
}
}
//---------------------------------------------------------
// safe_fread
//---------------------------------------------------------
void SFont::safe_fread(void* buf, int count)
{
if (f.read((char*)buf, count) != count) {
if (f.atEnd())
throw(QString("EOF while attemping to read %1 bytes").arg(count));
else
throw(QString("File read failed"));
}
}
//---------------------------------------------------------
// safe_fseek
//---------------------------------------------------------
void SFont::safe_fseek(long ofs)
{
qint64 newpos = ofs + f.pos();
if (!f.seek(newpos))
throw(QString("File seek failed with offset = %1").arg(ofs));
}
}
|