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
|
//=============================================================================
// MuseScore
// Music Composition & Notation
//
// Copyright (C) 2008-2011 Werner Schweer
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2
// as published by the Free Software Foundation and appearing in
// the file LICENCE.GPL
//=============================================================================
#include "instrument.h"
#include "xml.h"
#include "drumset.h"
#include "articulation.h"
#include "utils.h"
#include "stringdata.h"
#include "instrtemplate.h"
#include "mscore.h"
#include "part.h"
#include "score.h"
#include "synthesizer/synthesizer.h"
#include "synthesizer/midipatch.h"
namespace Ms {
Instrument InstrumentList::defaultInstrument;
const std::initializer_list<Channel::Prop> PartChannelSettingsLink::excerptProperties {
Channel::Prop::SOLOMUTE,
Channel::Prop::SOLO,
Channel::Prop::MUTE,
};
//---------------------------------------------------------
// write
//---------------------------------------------------------
void NamedEventList::write(XmlWriter& xml, const QString& n) const
{
xml.stag(QString("%1 name=\"%2\"").arg(n).arg(name));
if (!descr.isEmpty())
xml.tag("descr", descr);
for (const MidiCoreEvent& e : events)
e.write(xml);
xml.etag();
}
//---------------------------------------------------------
// read
//---------------------------------------------------------
void NamedEventList::read(XmlReader& e)
{
name = e.attribute("name");
while (e.readNextStartElement()) {
const QStringRef& tag(e.name());
if (tag == "program") {
MidiCoreEvent ev(ME_CONTROLLER, 0, CTRL_PROGRAM, e.intAttribute("value", 0));
events.push_back(ev);
e.skipCurrentElement();
}
else if (tag == "controller") {
MidiCoreEvent ev;
ev.setType(ME_CONTROLLER);
ev.setDataA(e.intAttribute("ctrl", 0));
ev.setDataB(e.intAttribute("value", 0));
events.push_back(ev);
e.skipCurrentElement();
}
else if (tag == "descr")
descr = e.readElementText();
else
e.unknown();
}
}
//---------------------------------------------------------
// operator
//---------------------------------------------------------
bool MidiArticulation::operator==(const MidiArticulation& i) const
{
return (i.name == name) && (i.velocity == velocity) && (i.gateTime == gateTime);
}
//---------------------------------------------------------
// Instrument
//---------------------------------------------------------
Instrument::Instrument()
{
Channel* a = new Channel;
a->setName(Channel::DEFAULT_NAME);
_channel.append(a);
_minPitchA = 0;
_maxPitchA = 127;
_minPitchP = 0;
_maxPitchP = 127;
_useDrumset = false;
_drumset = 0;
_singleNoteDynamics = true;
}
Instrument::Instrument(const Instrument& i)
{
_longNames = i._longNames;
_shortNames = i._shortNames;
_trackName = i._trackName;
_minPitchA = i._minPitchA;
_maxPitchA = i._maxPitchA;
_minPitchP = i._minPitchP;
_maxPitchP = i._maxPitchP;
_transpose = i._transpose;
_instrumentId = i._instrumentId;
_stringData = i._stringData;
_drumset = 0;
setDrumset(i._drumset);
_useDrumset = i._useDrumset;
_stringData = i._stringData;
_midiActions = i._midiActions;
_articulation = i._articulation;
_singleNoteDynamics = i._singleNoteDynamics;
for (Channel* c : i._channel)
_channel.append(new Channel(*c));
_clefType = i._clefType;
}
void Instrument::operator=(const Instrument& i)
{
qDeleteAll(_channel);
_channel.clear();
delete _drumset;
_longNames = i._longNames;
_shortNames = i._shortNames;
_trackName = i._trackName;
_minPitchA = i._minPitchA;
_maxPitchA = i._maxPitchA;
_minPitchP = i._minPitchP;
_maxPitchP = i._maxPitchP;
_transpose = i._transpose;
_instrumentId = i._instrumentId;
_stringData = i._stringData;
_drumset = 0;
setDrumset(i._drumset);
_useDrumset = i._useDrumset;
_stringData = i._stringData;
_midiActions = i._midiActions;
_articulation = i._articulation;
_singleNoteDynamics = i._singleNoteDynamics;
for (Channel* c : i._channel)
_channel.append(new Channel(*c));
_clefType = i._clefType;
}
//---------------------------------------------------------
// ~Instrument
//---------------------------------------------------------
Instrument::~Instrument()
{
qDeleteAll(_channel);
delete _drumset;
}
//---------------------------------------------------------
// StaffName
//---------------------------------------------------------
StaffName::StaffName(const QString& s, int p) : _name(s), _pos(p)
{
Text::validateText(_name); // enforce HTML encoding
}
//---------------------------------------------------------
// StaffName::write
//---------------------------------------------------------
void StaffName::write(XmlWriter& xml, const char* tag) const
{
if (!name().isEmpty()) {
if (pos() == 0)
xml.writeXml(QString("%1").arg(tag), name());
else
xml.writeXml(QString("%1 pos=\"%2\"").arg(tag).arg(pos()), name());
}
}
//---------------------------------------------------------
// read
//---------------------------------------------------------
void StaffName::read(XmlReader& e)
{
_pos = e.intAttribute("pos", 0);
_name = e.readXml();
if (_name.startsWith("<html>")) {
// compatibility to old html implementation:
_name = QTextDocumentFragment::fromHtml(_name).toPlainText();
}
}
//---------------------------------------------------------
// Instrument::write
//---------------------------------------------------------
void Instrument::write(XmlWriter& xml, const Part* part) const
{
xml.stag("Instrument");
_longNames.write(xml, "longName");
_shortNames.write(xml, "shortName");
// if (!_trackName.empty())
xml.tag("trackName", _trackName);
if (_minPitchP > 0)
xml.tag("minPitchP", _minPitchP);
if (_maxPitchP < 127)
xml.tag("maxPitchP", _maxPitchP);
if (_minPitchA > 0)
xml.tag("minPitchA", _minPitchA);
if (_maxPitchA < 127)
xml.tag("maxPitchA", _maxPitchA);
if (_transpose.diatonic)
xml.tag("transposeDiatonic", _transpose.diatonic);
if (_transpose.chromatic)
xml.tag("transposeChromatic", _transpose.chromatic);
if (!_instrumentId.isEmpty())
xml.tag("instrumentId", _instrumentId);
if (_useDrumset) {
xml.tag("useDrumset", _useDrumset);
_drumset->save(xml);
}
for (int i = 0; i < _clefType.size(); ++i) {
ClefTypeList ct = _clefType[i];
if (ct._concertClef == ct._transposingClef) {
if (ct._concertClef != ClefType::G) {
QString tag = ClefInfo::tag(ct._concertClef);
if (i)
xml.tag(QString("clef staff=\"%1\"").arg(i+1), tag);
else
xml.tag("clef", tag);
}
}
else {
QString tag1 = ClefInfo::tag(ct._concertClef);
QString tag2 = ClefInfo::tag(ct._transposingClef);
if (i) {
xml.tag(QString("concertClef staff=\"%1\"").arg(i+1), tag1);
xml.tag(QString("transposingClef staff=\"%1\"").arg(i+1), tag2);
}
else {
xml.tag("concertClef", tag1);
xml.tag("transposingClef", tag2);
}
}
}
if (_singleNoteDynamics != getSingleNoteDynamicsFromTemplate())
xml.tag("singleNoteDynamics", _singleNoteDynamics);
if (!(_stringData == StringData()))
_stringData.write(xml);
for (const NamedEventList& a : _midiActions)
a.write(xml, "MidiAction");
for (const MidiArticulation& a : _articulation)
a.write(xml);
for (const Channel* a : _channel)
a->write(xml, part);
xml.etag();
}
//---------------------------------------------------------
// Instrument::read
//---------------------------------------------------------
void Instrument::read(XmlReader& e, Part* part)
{
bool customDrumset = false;
bool readSingleNoteDynamics = false;
_channel.clear(); // remove default channel
while (e.readNextStartElement()) {
const QStringRef& tag(e.name());
if (tag == "singleNoteDynamics") {
_singleNoteDynamics = e.readBool();
readSingleNoteDynamics = true;
}
else if (!readProperties(e, part, &customDrumset))
e.unknown();
}
if (!readSingleNoteDynamics)
setSingleNoteDynamicsFromTemplate();
if (_useDrumset) {
if (_channel[0]->bank() == 0 && _channel[0]->synti().toLower() != "zerberus")
_channel[0]->setBank(128);
}
}
//---------------------------------------------------------
// Instrument::readProperties
//---------------------------------------------------------
bool Instrument::readProperties(XmlReader& e, Part* part, bool* customDrumset)
{
const QStringRef& tag(e.name());
if (tag == "longName") {
StaffName name;
name.read(e);
_longNames.append(name);
}
else if (tag == "shortName") {
StaffName name;
name.read(e);
_shortNames.append(name);
}
else if (tag == "trackName")
_trackName = e.readElementText();
else if (tag == "minPitch") { // obsolete
_minPitchP = _minPitchA = e.readInt();
}
else if (tag == "maxPitch") { // obsolete
_maxPitchP = _maxPitchA = e.readInt();
}
else if (tag == "minPitchA")
_minPitchA = e.readInt();
else if (tag == "minPitchP")
_minPitchP = e.readInt();
else if (tag == "maxPitchA")
_maxPitchA = e.readInt();
else if (tag == "maxPitchP")
_maxPitchP = e.readInt();
else if (tag == "transposition") { // obsolete
_transpose.chromatic = e.readInt();
_transpose.diatonic = chromatic2diatonic(_transpose.chromatic);
}
else if (tag == "transposeChromatic")
_transpose.chromatic = e.readInt();
else if (tag == "transposeDiatonic")
_transpose.diatonic = e.readInt();
else if (tag == "instrumentId")
_instrumentId = e.readElementText();
else if (tag == "useDrumset") {
_useDrumset = e.readInt();
if (_useDrumset) {
delete _drumset;
_drumset = new Drumset(*smDrumset);
}
}
else if (tag == "Drum") {
// if we see on of this tags, a custom drumset will
// be created
if (!_drumset)
_drumset = new Drumset(*smDrumset);
if (!(*customDrumset)) {
const_cast<Drumset*>(_drumset)->clear();
*customDrumset = true;
}
const_cast<Drumset*>(_drumset)->load(e);
}
// support tag "Tablature" for a while for compatibility with existent 2.0 scores
else if (tag == "Tablature" || tag == "StringData")
_stringData.read(e);
else if (tag == "MidiAction") {
NamedEventList a;
a.read(e);
_midiActions.append(a);
}
else if (tag == "Articulation") {
MidiArticulation a;
a.read(e);
_articulation.append(a);
}
else if (tag == "Channel" || tag == "channel") {
Channel* a = new Channel;
a->read(e, part);
_channel.append(a);
}
else if (tag == "clef") { // sets both transposing and concert clef
int idx = e.intAttribute("staff", 1) - 1;
QString val(e.readElementText());
ClefType ct = Clef::clefType(val);
setClefType(idx, ClefTypeList(ct, ct));
}
else if (tag == "concertClef") {
int idx = e.intAttribute("staff", 1) - 1;
QString val(e.readElementText());
setClefType(idx, ClefTypeList(Clef::clefType(val), clefType(idx)._transposingClef));
}
else if (tag == "transposingClef") {
int idx = e.intAttribute("staff", 1) - 1;
QString val(e.readElementText());
setClefType(idx, ClefTypeList(clefType(idx)._concertClef, Clef::clefType(val)));
}
else
return false;
return true;
}
//---------------------------------------------------------
// action
//---------------------------------------------------------
NamedEventList* Instrument::midiAction(const QString& s, int channelIdx) const
{
// first look in channel list
foreach(const NamedEventList& a, _channel[channelIdx]->midiActions) {
if (s == a.name)
return const_cast<NamedEventList*>(&a);
}
foreach(const NamedEventList& a, _midiActions) {
if (s == a.name)
return const_cast<NamedEventList*>(&a);
}
return 0;
}
const char *Channel::DEFAULT_NAME = "normal";
//---------------------------------------------------------
// Channel
//---------------------------------------------------------
Channel::Channel()
{
for(int i = 0; i < int(A::INIT_COUNT); ++i)
_init.push_back(MidiCoreEvent());
_synti = "Fluid"; // default synthesizer
_channel = -1;
_program = -1;
_bank = 0;
_volume = 100;
_pan = 64; // actually 63.5 for center
_chorus = 0;
_reverb = 0;
_color = DEFAULT_COLOR;
_mute = false;
_solo = false;
_soloMute = false;
// qDebug("construct Channel ");
}
//---------------------------------------------------------
// initList
//---------------------------------------------------------
std::vector<MidiCoreEvent>& Channel::initList() const
{
if (_mustUpdateInit) {
updateInitList();
_mustUpdateInit = false;
}
return _init;
}
//---------------------------------------------------------
// setVolume
//---------------------------------------------------------
void Channel::setVolume(char value)
{
if (_volume != value) {
_volume = value;
firePropertyChanged(Prop::VOLUME);
}
_mustUpdateInit = true;
}
//---------------------------------------------------------
// setPan
//---------------------------------------------------------
void Channel::setPan(char value)
{
if (_pan != value) {
_pan = value;
firePropertyChanged(Prop::PAN);
}
_mustUpdateInit = true;
}
//---------------------------------------------------------
// setChorus
//---------------------------------------------------------
void Channel::setChorus(char value)
{
if (_chorus != value) {
_chorus = value;
firePropertyChanged(Prop::CHORUS);
}
_mustUpdateInit = true;
}
//---------------------------------------------------------
// setReverb
//---------------------------------------------------------
void Channel::setReverb(char value)
{
if (_reverb != value) {
_reverb = value;
firePropertyChanged(Prop::REVERB);
}
_mustUpdateInit = true;
}
//---------------------------------------------------------
// setName
//---------------------------------------------------------
void Channel::setName(const QString& value)
{
if (_name != value) {
_name = value;
firePropertyChanged(Prop::NAME);
}
}
//---------------------------------------------------------
// setDescr
//---------------------------------------------------------
void Channel::setDescr(const QString& value)
{
if (_descr != value) {
_descr = value;
firePropertyChanged(Prop::DESCR);
}
}
//---------------------------------------------------------
// setSynti
//---------------------------------------------------------
void Channel::setSynti(const QString& value)
{
if (_synti != value) {
_synti = value;
firePropertyChanged(Prop::SYNTI);
}
}
//---------------------------------------------------------
// setColor
//---------------------------------------------------------
void Channel::setColor(int value)
{
if (_color != value) {
_color = value;
firePropertyChanged(Prop::COLOR);
}
}
//---------------------------------------------------------
// setProgram
//---------------------------------------------------------
void Channel::setProgram(int value)
{
if (_program != value) {
_program = value;
firePropertyChanged(Prop::PROGRAM);
}
_mustUpdateInit = true;
}
//---------------------------------------------------------
// setBank
//---------------------------------------------------------
void Channel::setBank(int value)
{
if (_bank != value) {
_bank = value;
firePropertyChanged(Prop::BANK);
}
_mustUpdateInit = true;
}
//---------------------------------------------------------
// setChannel
//---------------------------------------------------------
void Channel::setChannel(int value)
{
if (_channel != value) {
_channel = value;
firePropertyChanged(Prop::CHANNEL);
}
}
//---------------------------------------------------------
// setSoloMute
//---------------------------------------------------------
void Channel::setSoloMute(bool value)
{
if (_soloMute != value) {
_soloMute = value;
firePropertyChanged(Prop::SOLOMUTE);
}
}
//---------------------------------------------------------
// setMute
//---------------------------------------------------------
void Channel::setMute(bool value)
{
if (_mute != value) {
_mute = value;
firePropertyChanged(Prop::MUTE);
}
}
//---------------------------------------------------------
// setSolo
//---------------------------------------------------------
void Channel::setSolo(bool value)
{
if (_solo != value) {
_solo = value;
firePropertyChanged(Prop::SOLO);
}
}
//---------------------------------------------------------
// setUserBankController
//---------------------------------------------------------
void Channel::setUserBankController(bool val)
{
if (_userBankController != val) {
_userBankController = val;
firePropertyChanged(Prop::USER_BANK_CONTROL);
}
}
//---------------------------------------------------------
// write
//---------------------------------------------------------
void Channel::write(XmlWriter& xml, const Part* part) const
{
if (_name.isEmpty() || _name == DEFAULT_NAME)
xml.stag("Channel");
else
xml.stag(QString("Channel name=\"%1\"").arg(_name));
if (!_descr.isEmpty())
xml.tag("descr", _descr);
if (_color != DEFAULT_COLOR)
xml.tag("color", _color);
for (const MidiCoreEvent& e : initList()) {
if (e.type() == ME_INVALID)
continue;
if (e.type() == ME_CONTROLLER) {
// Don't write bank if automatically switched, but always write if switched by the user
if (e.dataA() == CTRL_HBANK && e.dataB() == 0 && !_userBankController)
continue;
if (e.dataA() == CTRL_LBANK && e.dataB() == 0 && !_userBankController)
continue;
if (e.dataA() == CTRL_VOLUME && e.dataB() == 100)
continue;
if (e.dataA() == CTRL_PANPOT && e.dataB() == 64)
continue;
if (e.dataA() == CTRL_REVERB_SEND && e.dataB() == 0)
continue;
if (e.dataA() == CTRL_CHORUS_SEND && e.dataB() == 0)
continue;
}
e.write(xml);
}
if (!MScore::testMode)
// xml.tag("synti", ::synti->name(synti));
xml.tag("synti", _synti);
if (_mute)
xml.tag("mute", _mute);
if (_solo)
xml.tag("solo", _solo);
if (part && part->masterScore()->exportMidiMapping() && part->score() == part->masterScore()) {
xml.tag("midiPort", part->masterScore()->midiMapping(_channel)->port());
xml.tag("midiChannel", part->masterScore()->midiMapping(_channel)->channel());
}
for (const NamedEventList& a : midiActions)
a.write(xml, "MidiAction");
for (const MidiArticulation& a : articulation)
a.write(xml);
xml.etag();
}
//---------------------------------------------------------
// read
//---------------------------------------------------------
void Channel::read(XmlReader& e, Part* part)
{
// synti = 0;
_name = e.attribute("name");
if (_name == "")
_name = DEFAULT_NAME;
int midiPort = -1;
int midiChannel = -1;
while (e.readNextStartElement()) {
const QStringRef& tag(e.name());
if (tag == "program") {
_program = e.intAttribute("value", -1);
if (_program == -1)
_program = e.readInt();
else
e.readNext();
}
else if (tag == "controller") {
int value = e.intAttribute("value", 0);
int ctrl = e.intAttribute("ctrl", 0);
switch (ctrl) {
case CTRL_HBANK:
_bank = (value << 7) + (_bank & 0x7f);
_userBankController = true;
break;
case CTRL_LBANK:
_bank = (_bank & ~0x7f) + (value & 0x7f);
_userBankController = true;
break;
case CTRL_VOLUME:
_volume = value;
break;
case CTRL_PANPOT:
_pan = value;
break;
case CTRL_CHORUS_SEND:
_chorus = value;
break;
case CTRL_REVERB_SEND:
_reverb = value;
break;
default:
{
Event ev(ME_CONTROLLER);
ev.setOntime(-1);
ev.setChannel(0);
ev.setDataA(ctrl);
ev.setDataB(value);
_init.push_back(ev);
}
break;
}
e.readNext();
}
else if (tag == "Articulation") {
MidiArticulation a;
a.read(e);
articulation.append(a);
}
else if (tag == "MidiAction") {
NamedEventList a;
a.read(e);
midiActions.append(a);
}
else if (tag == "synti")
_synti = e.readElementText();
else if (tag == "descr")
_descr = e.readElementText();
else if (tag == "color")
_color = e.readInt();
else if (tag == "mute")
_mute = e.readInt();
else if (tag == "solo")
_solo = e.readInt();
else if (tag == "midiPort") {
midiPort = e.readInt();
}
else if (tag == "midiChannel") {
midiChannel = e.readInt();
}
else
e.unknown();
}
if (128 == _bank && "zerberus" == _synti.toLower())
_bank = 0;
_mustUpdateInit = true;
if ((midiPort != -1 || midiChannel != -1) && part && part->score()->isMaster())
part->masterScore()->addMidiMapping(this, part, midiPort, midiChannel);
}
//---------------------------------------------------------
// switchExpressive
// Switches channel from non-expressive to expressive patch or vice versa
// This works only with MuseScore General soundfont
//---------------------------------------------------------
void Channel::switchExpressive(Synthesizer* synth, bool expressive, bool force /* = false */)
{
if ((_userBankController && !force) || !synth)
return;
// Check that we're actually changing the MuseScore General soundfont
const auto fontsInfo = synth->soundFontsInfo();
if (fontsInfo.empty())
return;
const auto& info = fontsInfo.front();
if (!info.fontName.contains("MuseScore_General")) {
qDebug().nospace() << "Soundfont '" << info.fontName << "' is not MuseScore General, cannot update expressive";
return;
}
// Work out where the new expressive patch will be
// All expressive instruments are +1 bank higher than the
// normal counterparts, except on bank 0, where they are placed on bank 17
// and on bank 8, which uses bank 18 instead.
int searchBankNum;
int newBankNum;
if (expressive) {
int relativeBank = bank() % 129;
if (relativeBank == 0)
newBankNum = 17;
else if (relativeBank == 8)
newBankNum = 18;
else
newBankNum = relativeBank + 1;
}
else {
int relativeBank = bank() % 129;
if (relativeBank == 17)
newBankNum = 0;
else if (relativeBank == 18)
newBankNum = 8;
else
newBankNum = relativeBank - 1;
}
// Floor bank num to multiple of 129 and add new num to get bank num of new patch
searchBankNum = (bank() / 129) * 129 + newBankNum;
const auto& pl = synth->getPatchInfo();
for (const MidiPatch* p : pl) {
if (p->synti == "Fluid") {
if (searchBankNum == p->bank && program() == p->prog) {
setBank(p->bank);
return;
}
}
}
}
//---------------------------------------------------------
// updateInitList
//---------------------------------------------------------
void Channel::updateInitList() const
{
MidiCoreEvent e;
if (_program != -1) {
e.setType(ME_CONTROLLER);
e.setDataA(CTRL_PROGRAM);
e.setDataB(_program);
_init[int(A::PROGRAM)] = e;
}
e.setData(ME_CONTROLLER, CTRL_HBANK, (_bank >> 7) & 0x7f);
_init[int(A::HBANK)] = e;
e.setData(ME_CONTROLLER, CTRL_LBANK, _bank & 0x7f);
_init[int(A::LBANK)] = e;
e.setData(ME_CONTROLLER, CTRL_VOLUME, volume());
_init[int(A::VOLUME)] = e;
e.setData(ME_CONTROLLER, CTRL_PANPOT, pan());
_init[int(A::PAN)] = e;
e.setData(ME_CONTROLLER, CTRL_CHORUS_SEND, chorus());
_init[int(A::CHORUS)] = e;
e.setData(ME_CONTROLLER, CTRL_REVERB_SEND, reverb());
_init[int(A::REVERB)] = e;
}
//---------------------------------------------------------
// addListener
//---------------------------------------------------------
void Channel::addListener(ChannelListener* l)
{
_notifier.addListener(l);
}
//---------------------------------------------------------
// removeListener
//---------------------------------------------------------
void Channel::removeListener(ChannelListener* l)
{
_notifier.removeListener(l);
}
//---------------------------------------------------------
// PartChannelSettingsLink
//---------------------------------------------------------
PartChannelSettingsLink::PartChannelSettingsLink(Channel* main, Channel* bound, bool excerpt)
: _main(main), _bound(bound), _excerpt(excerpt)
{
if (excerpt) {
for (Channel::Prop p : excerptProperties)
applyProperty(p, /* from */ bound, /* to */ main);
}
// Maybe it would be good to assign common properties if the link
// is constructed in non-excerpt mode. But it is not currently
// necessary as playback channels are currently recreated on each
// MIDI remapping.
main->addListener(this);
}
//---------------------------------------------------------
// PartChannelSettingsLink
//---------------------------------------------------------
PartChannelSettingsLink::PartChannelSettingsLink(PartChannelSettingsLink&& other)
: ChannelListener(), // swap() will set the notifier instead
_main(nullptr), _bound(nullptr), _excerpt(false)
{
swap(*this, other);
}
//---------------------------------------------------------
// PartChannelSettingsLink::operator=
//---------------------------------------------------------
PartChannelSettingsLink& PartChannelSettingsLink::operator=(PartChannelSettingsLink&& other)
{
if (this != &other)
swap(*this, other);
return *this;
}
//---------------------------------------------------------
// swap
//---------------------------------------------------------
void swap(PartChannelSettingsLink& l1, PartChannelSettingsLink& l2)
{
Ms::swap(static_cast<ChannelListener&>(l1), static_cast<ChannelListener&>(l2));
using std::swap;
swap(l1._main, l2._main);
swap(l1._bound, l2._bound);
swap(l1._excerpt, l2._excerpt);
}
//---------------------------------------------------------
// PartChannelSettingsLink::applyProperty
//---------------------------------------------------------
void PartChannelSettingsLink::applyProperty(Channel::Prop p, const Channel* from, Channel* to)
{
switch (p) {
case Channel::Prop::VOLUME:
to->setVolume(from->volume());
break;
case Channel::Prop::PAN:
to->setPan(from->pan());
break;
case Channel::Prop::CHORUS:
to->setChorus(from->chorus());
break;
case Channel::Prop::REVERB:
to->setReverb(from->reverb());
break;
case Channel::Prop::NAME:
to->setName(from->name());
break;
case Channel::Prop::DESCR:
to->setDescr(from->descr());
break;
case Channel::Prop::PROGRAM:
to->setProgram(from->program());
break;
case Channel::Prop::BANK:
to->setBank(from->bank());
break;
case Channel::Prop::COLOR:
to->setColor(from->color());
break;
case Channel::Prop::SOLOMUTE:
to->setSoloMute(from->soloMute());
break;
case Channel::Prop::SOLO:
to->setSolo(from->solo());
break;
case Channel::Prop::MUTE:
to->setMute(from->mute());
break;
case Channel::Prop::SYNTI:
to->setSynti(from->synti());
break;
case Channel::Prop::CHANNEL:
to->setChannel(from->channel());
break;
case Channel::Prop::USER_BANK_CONTROL:
to->setUserBankController(from->userBankController());
break;
};
}
//---------------------------------------------------------
// PartChannelSettingsLink::propertyChanged
//---------------------------------------------------------
void PartChannelSettingsLink::propertyChanged(Channel::Prop p)
{
if (isExcerptProperty(p) == _excerpt)
applyProperty(p, _main, _bound);
}
//---------------------------------------------------------
// channelIdx
//---------------------------------------------------------
int Instrument::channelIdx(const QString& s) const
{
int idx = 0;
for (const Channel* a : _channel) {
if (a->name().isEmpty() && s == Channel::DEFAULT_NAME)
return idx;
if (s == a->name())
return idx;
++idx;
}
return -1;
}
//---------------------------------------------------------
// write
//---------------------------------------------------------
void MidiArticulation::write(XmlWriter& xml) const
{
if (name.isEmpty())
xml.stag("Articulation");
else
xml.stag(QString("Articulation name=\"%1\"").arg(name));
if (!descr.isEmpty())
xml.tag("descr", descr);
xml.tag("velocity", velocity);
xml.tag("gateTime", gateTime);
xml.etag();
}
//---------------------------------------------------------
// read
//---------------------------------------------------------
void MidiArticulation::read(XmlReader& e)
{
name = e.attribute("name");
while (e.readNextStartElement()) {
const QStringRef& tag(e.name());
if (tag == "velocity") {
QString text(e.readElementText());
if (text.endsWith("%"))
text = text.left(text.size()-1);
velocity = text.toInt();
}
else if (tag == "gateTime") {
QString text(e.readElementText());
if (text.endsWith("%"))
text = text.left(text.size()-1);
gateTime = text.toInt();
}
else if (tag == "descr")
descr = e.readElementText();
else
e.unknown();
}
}
//---------------------------------------------------------
// updateVelocity
//---------------------------------------------------------
void Instrument::updateVelocity(int* velocity, int /*channelIdx*/, const QString& name)
{
for (const MidiArticulation& a : _articulation) {
if (a.name == name) {
*velocity = *velocity * a.velocity / 100;
break;
}
}
}
//---------------------------------------------------------
// updateGateTime
//---------------------------------------------------------
void Instrument::updateGateTime(int* gateTime, int /*channelIdx*/, const QString& name)
{
for (const MidiArticulation& a : _articulation) {
if (a.name == name) {
*gateTime = a.gateTime;
break;
}
}
}
//---------------------------------------------------------
// updateGateTime
//---------------------------------------------------------
void Instrument::switchExpressive(MasterScore* score, Synthesizer* synth, bool expressive, bool force /* = false */)
{
// Only switch to expressive where necessary
if (!synth || (expressive && !singleNoteDynamics()))
return;
for (Channel* c : channel()) {
c->switchExpressive(synth, expressive, force);
if (score->playbackChannel(c))
score->playbackChannel(c)->switchExpressive(synth, expressive, force);
}
}
//---------------------------------------------------------
// operator==
//---------------------------------------------------------
bool Instrument::operator==(const Instrument& i) const
{
int n = _longNames.size();
if (i._longNames.size() != n)
return false;
for (int k = 0; k < n; ++k) {
if (!(i._longNames[k] == _longNames[k]))
return false;
}
n = _shortNames.size();
if (i._shortNames.size() != n)
return false;
for (int k = 0; k < n; ++k) {
if (!(i._shortNames[k] == _shortNames[k].name()))
return false;
}
n = _channel.size();
if (i._channel.size() != n)
return false;
for (int k = 0; k < n; ++k) {
if (!(*i._channel[k] == *_channel[k]))
return false;
}
return i._minPitchA == _minPitchA
&& i._maxPitchA == _maxPitchA
&& i._minPitchP == _minPitchP
&& i._maxPitchP == _maxPitchP
&& i._useDrumset == _useDrumset
&& i._midiActions == _midiActions
&& i._articulation == _articulation
&& i._transpose.diatonic == _transpose.diatonic
&& i._transpose.chromatic == _transpose.chromatic
&& i._trackName == _trackName
&& *i.stringData() == *stringData()
&& i._singleNoteDynamics == _singleNoteDynamics;
}
//---------------------------------------------------------
// operator==
//---------------------------------------------------------
bool StaffName::operator==(const StaffName& i) const
{
return (i._pos == _pos) && (i._name == _name);
}
//---------------------------------------------------------
// setUseDrumset
//---------------------------------------------------------
void Instrument::setUseDrumset(bool val)
{
_useDrumset = val;
if (val && !_drumset)
_drumset = new Drumset(*smDrumset);
}
//---------------------------------------------------------
// setDrumset
//---------------------------------------------------------
void Instrument::setDrumset(const Drumset* ds)
{
delete _drumset;
if (ds) {
_useDrumset = true;
_drumset = new Drumset(*ds);
}
else {
_useDrumset = false;
_drumset = 0;
}
}
//---------------------------------------------------------
// setLongName
// f is in richtext format
//---------------------------------------------------------
void Instrument::setLongName(const QString& f)
{
_longNames.clear();
if (f.length() > 0)
_longNames.append(StaffName(f, 0));
}
//---------------------------------------------------------
// setShortName
// f is in richtext format
//---------------------------------------------------------
void Instrument::setShortName(const QString& f)
{
_shortNames.clear();
if (f.length() > 0)
_shortNames.append(StaffName(f, 0));
}
//---------------------------------------------------------
// addLongName
//---------------------------------------------------------
void Instrument::addLongName(const StaffName& f)
{
_longNames.append(f);
}
//---------------------------------------------------------
// addShortName
//---------------------------------------------------------
void Instrument::addShortName(const StaffName& f)
{
_shortNames.append(f);
}
//---------------------------------------------------------
// clefType
//---------------------------------------------------------
ClefTypeList Instrument::clefType(int staffIdx) const
{
if (staffIdx >= _clefType.size()) {
if (_clefType.empty())
return ClefTypeList(staffIdx == 1 ? ClefType::F : ClefType::G);
return _clefType[0];
}
return _clefType[staffIdx];
}
//---------------------------------------------------------
// setClefType
//---------------------------------------------------------
void Instrument::setClefType(int staffIdx, const ClefTypeList& c)
{
while (_clefType.size() <= staffIdx)
_clefType.append(ClefTypeList());
_clefType[staffIdx] = c;
}
//---------------------------------------------------------
// minPitchP
//---------------------------------------------------------
int Instrument::minPitchP() const
{
return _minPitchP;
}
//---------------------------------------------------------
// maxPitchP
//---------------------------------------------------------
int Instrument::maxPitchP() const
{
return _maxPitchP;
}
//---------------------------------------------------------
// minPitchA
//---------------------------------------------------------
int Instrument::minPitchA() const
{
return _minPitchA;
}
//---------------------------------------------------------
// maxPitchA
//---------------------------------------------------------
int Instrument::maxPitchA() const
{
return _maxPitchA;
}
//---------------------------------------------------------
// instrumentId
//---------------------------------------------------------
QString Instrument::instrumentId() const
{
return _instrumentId;
}
//---------------------------------------------------------
// instrument
//---------------------------------------------------------
const Instrument* InstrumentList::instrument(int tick) const
{
if (empty())
return &defaultInstrument;
auto i = upper_bound(tick);
if (i == begin())
return &defaultInstrument;
--i;
return i->second;
}
//---------------------------------------------------------
// instrument
//---------------------------------------------------------
Instrument* InstrumentList::instrument(int tick)
{
if (empty())
return &defaultInstrument;
auto i = upper_bound(tick);
if (i == begin())
return &defaultInstrument;
--i;
return i->second;
}
//---------------------------------------------------------
// setInstrument
//---------------------------------------------------------
void InstrumentList::setInstrument(Instrument* instr, int tick)
{
if (!insert({tick, instr}).second)
(*this)[tick] = instr;
}
//---------------------------------------------------------
// longName
//---------------------------------------------------------
const QList<StaffName>& Instrument::longNames() const
{
return _longNames;
}
//---------------------------------------------------------
// shortName
//---------------------------------------------------------
const QList<StaffName>& Instrument::shortNames() const
{
return _shortNames;
}
//---------------------------------------------------------
// longName
//---------------------------------------------------------
QList<StaffName>& Instrument::longNames()
{
return _longNames;
}
//---------------------------------------------------------
// shortName
//---------------------------------------------------------
QList<StaffName>& Instrument::shortNames()
{
return _shortNames;
}
//---------------------------------------------------------
// trackName
//---------------------------------------------------------
QString Instrument::trackName() const
{
return _trackName;
}
void Instrument::setTrackName(const QString& s)
{
_trackName = s;
}
//---------------------------------------------------------
// fromTemplate
//---------------------------------------------------------
Instrument Instrument::fromTemplate(const InstrumentTemplate* t)
{
Instrument instr;
instr.setAmateurPitchRange(t->minPitchA, t->maxPitchA);
instr.setProfessionalPitchRange(t->minPitchP, t->maxPitchP);
for (StaffName sn : t->longNames)
instr.addLongName(StaffName(sn.name(), sn.pos()));
for (StaffName sn : t->shortNames)
instr.addShortName(StaffName(sn.name(), sn.pos()));
instr.setTrackName(t->trackName);
instr.setTranspose(t->transpose);
instr.setInstrumentId(t->musicXMLid);
if (t->useDrumset)
instr.setDrumset(t->drumset ? t->drumset : smDrumset);
for (int i = 0; i < t->nstaves(); ++i)
instr.setClefType(i, t->clefTypes[i]);
instr.setMidiActions(t->midiActions);
instr.setArticulation(t->articulation);
instr._channel.clear();
for (const Channel& c : t->channel)
instr._channel.append(new Channel(c));
instr.setStringData(t->stringData);
instr.setSingleNoteDynamics(t->singleNoteDynamics);
return instr;
}
//---------------------------------------------------------
// Instrument::playbackChannel
//---------------------------------------------------------
const Channel* Instrument::playbackChannel(int idx, const MasterScore* score) const
{
return score->playbackChannel(channel(idx));
}
//---------------------------------------------------------
// Instrument::playbackChannel
//---------------------------------------------------------
Channel* Instrument::playbackChannel(int idx, MasterScore* score)
{
return score->playbackChannel(channel(idx));
}
//---------------------------------------------------------
// getSingleNoteDynamicsFromTemplate
//---------------------------------------------------------
bool Instrument::getSingleNoteDynamicsFromTemplate() const
{
QString templateName = trackName().toLower().replace(" ", "-").replace("♭", "b");
InstrumentTemplate* tp = searchTemplate(templateName);
if (tp)
return tp->singleNoteDynamics;
return true;
}
//---------------------------------------------------------
// setSingleNoteDynamicsFromTemplate
//---------------------------------------------------------
void Instrument::setSingleNoteDynamicsFromTemplate()
{
setSingleNoteDynamics(getSingleNoteDynamicsFromTemplate());
}
//---------------------------------------------------------
// StaffNameList::write
//---------------------------------------------------------
void StaffNameList::write(XmlWriter& xml, const char* name) const
{
for (const StaffName& sn : *this)
sn.write(xml, name);
}
}
|