MuseScore/libmscore/instrument.cpp

999 lines
32 KiB
C++
Raw Normal View History

2012-05-26 14:26:10 +02:00
//=============================================================================
// 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"
2012-05-26 14:26:10 +02:00
#include "xml.h"
#include "drumset.h"
#include "articulation.h"
#include "utils.h"
#include "stringdata.h"
2012-05-26 14:26:10 +02:00
#include "instrtemplate.h"
#include "mscore.h"
#include "part.h"
#include "score.h"
2012-05-26 14:26:10 +02:00
2013-05-13 18:49:17 +02:00
namespace Ms {
2012-05-26 14:26:10 +02:00
Instrument InstrumentList::defaultInstrument;
//---------------------------------------------------------
// write
//---------------------------------------------------------
2016-11-19 11:51:21 +01:00
void NamedEventList::write(XmlWriter& xml, const QString& n) const
2012-05-26 14:26:10 +02:00
{
xml.stag(QString("%1 name=\"%2\"").arg(n).arg(name));
if (!descr.isEmpty())
xml.tag("descr", descr);
2016-11-19 11:51:21 +01:00
for (const MidiCoreEvent& e : events)
2013-04-25 10:52:02 +02:00
e.write(xml);
2012-05-26 14:26:10 +02:00
xml.etag();
}
//---------------------------------------------------------
// read
//---------------------------------------------------------
2013-01-11 18:10:18 +01:00
void NamedEventList::read(XmlReader& e)
2012-05-26 14:26:10 +02:00
{
2013-01-11 18:10:18 +01:00
name = e.attribute("name");
while (e.readNextStartElement()) {
const QStringRef& tag(e.name());
2012-05-26 14:26:10 +02:00
if (tag == "program") {
MidiCoreEvent ev(ME_CONTROLLER, 0, CTRL_PROGRAM, e.intAttribute("value", 0));
events.push_back(ev);
2013-01-11 18:10:18 +01:00
e.skipCurrentElement();
2012-05-26 14:26:10 +02:00
}
else if (tag == "controller") {
MidiCoreEvent ev;
ev.setType(ME_CONTROLLER);
2013-04-08 10:31:17 +02:00
ev.setDataA(e.intAttribute("ctrl", 0));
ev.setDataB(e.intAttribute("value", 0));
events.push_back(ev);
2013-01-11 18:10:18 +01:00
e.skipCurrentElement();
2012-05-26 14:26:10 +02:00
}
else if (tag == "descr")
2013-01-11 18:10:18 +01:00
descr = e.readElementText();
2012-05-26 14:26:10 +02:00
else
2013-01-11 18:10:18 +01:00
e.unknown();
2012-05-26 14:26:10 +02:00
}
}
//---------------------------------------------------------
// operator
//---------------------------------------------------------
bool MidiArticulation::operator==(const MidiArticulation& i) const
{
return (i.name == name) && (i.velocity == velocity) && (i.gateTime == gateTime);
}
//---------------------------------------------------------
// Instrument
//---------------------------------------------------------
Instrument::Instrument()
2012-05-26 14:26:10 +02:00
{
Channel* a = new Channel;
a->name = "normal";
2012-05-26 14:26:10 +02:00
_channel.append(a);
_minPitchA = 0;
_maxPitchA = 127;
_minPitchP = 0;
_maxPitchP = 127;
_useDrumset = false;
2012-05-26 14:26:10 +02:00
_drumset = 0;
}
Instrument::Instrument(const Instrument& i)
2012-05-26 14:26:10 +02:00
{
_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;
2014-05-13 17:15:32 +02:00
_stringData = i._stringData;
2012-05-26 14:26:10 +02:00
_drumset = 0;
setDrumset(i._drumset);
_useDrumset = i._useDrumset;
_stringData = i._stringData;
_midiActions = i._midiActions;
_articulation = i._articulation;
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;
2014-05-13 17:15:32 +02:00
_stringData = i._stringData;
2012-05-26 14:26:10 +02:00
_midiActions = i._midiActions;
_articulation = i._articulation;
for (Channel* c : i._channel)
_channel.append(new Channel(*c));
2014-08-15 12:20:21 +02:00
_clefType = i._clefType;
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// ~Instrument
2012-05-26 14:26:10 +02:00
//---------------------------------------------------------
Instrument::~Instrument()
2012-05-26 14:26:10 +02:00
{
qDeleteAll(_channel);
2012-05-26 14:26:10 +02:00
delete _drumset;
}
//---------------------------------------------------------
2014-02-11 14:27:44 +01:00
// StaffName::write
2012-05-26 14:26:10 +02:00
//---------------------------------------------------------
2016-11-19 11:51:21 +01:00
void StaffName::write(XmlWriter& xml, const char* tag) const
2012-05-26 14:26:10 +02:00
{
2015-04-13 17:12:21 +02:00
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());
}
2014-02-12 12:51:00 +01:00
}
//---------------------------------------------------------
// read
//---------------------------------------------------------
void StaffName::read(XmlReader& e)
{
2015-04-13 17:12:21 +02:00
_pos = e.intAttribute("pos", 0);
_name = e.readXml();
if (_name.startsWith("<html>")) {
2014-02-12 12:51:00 +01:00
// compatibility to old html implementation:
2015-04-13 17:12:21 +02:00
_name = QTextDocumentFragment::fromHtml(_name).toPlainText();
2012-05-26 14:26:10 +02:00
}
2013-06-04 18:29:14 +02:00
}
//---------------------------------------------------------
// Instrument::write
2013-06-04 18:29:14 +02:00
//---------------------------------------------------------
2016-11-19 11:51:21 +01:00
void Instrument::write(XmlWriter& xml, Part* part) const
2013-06-04 18:29:14 +02:00
{
xml.stag("Instrument");
2015-04-13 17:12:21 +02:00
_longNames.write(xml, "longName");
_shortNames.write(xml, "shortName");
2016-02-06 22:03:43 +01:00
// if (!_trackName.empty())
2014-02-12 12:51:00 +01:00
xml.tag("trackName", _trackName);
2012-05-26 14:26:10 +02:00
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);
2012-05-26 14:26:10 +02:00
_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);
}
2014-08-07 17:43:42 +02:00
}
2014-08-07 16:32:41 +02:00
}
2014-05-13 17:15:32 +02:00
if (!(_stringData == StringData()))
_stringData.write(xml);
2012-05-26 14:26:10 +02:00
foreach(const NamedEventList& a, _midiActions)
a.write(xml, "MidiAction");
foreach(const MidiArticulation& a, _articulation)
a.write(xml);
for (const Channel* a : _channel)
a->write(xml, part);
2012-05-26 14:26:10 +02:00
xml.etag();
}
//---------------------------------------------------------
// Instrument::read
2012-05-26 14:26:10 +02:00
//---------------------------------------------------------
void Instrument::read(XmlReader& e, Part* part)
2012-05-26 14:26:10 +02:00
{
bool customDrumset = false;
_channel.clear(); // remove default channel
2013-01-11 18:10:18 +01:00
while (e.readNextStartElement()) {
if (!readProperties(e, part, &customDrumset))
e.unknown();
}
if (_useDrumset) {
if (_channel[0]->bank == 0 && _channel[0]->synti.toLower() != "zerberus")
_channel[0]->bank = 128;
_channel[0]->updateInitList();
}
}
2012-05-26 14:26:10 +02:00
//---------------------------------------------------------
// 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);
2014-08-07 16:32:41 +02:00
}
}
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;
2014-08-07 16:32:41 +02:00
}
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);
2012-05-26 14:26:10 +02:00
}
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);
2012-05-26 14:26:10 +02:00
_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));
2012-05-26 14:26:10 +02:00
}
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;
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// action
//---------------------------------------------------------
NamedEventList* Instrument::midiAction(const QString& s, int channelIdx) const
2012-05-26 14:26:10 +02:00
{
// first look in channel list
foreach(const NamedEventList& a, _channel[channelIdx]->midiActions) {
2012-05-26 14:26:10 +02:00
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;
}
//---------------------------------------------------------
// Channel
//---------------------------------------------------------
Channel::Channel()
{
for(int i = 0; i < int(A::INIT_COUNT); ++i)
init.push_back(MidiCoreEvent());
synti = "Fluid"; // default synthesizer
2012-05-26 14:26:10 +02:00
channel = -1;
program = -1;
bank = 0;
volume = 100;
pan = 64; // actually 63.5 for center
chorus = 0;
reverb = 0;
2012-05-26 14:26:10 +02:00
mute = false;
solo = false;
soloMute = false;
}
//---------------------------------------------------------
// write
//---------------------------------------------------------
2016-11-19 11:51:21 +01:00
void Channel::write(XmlWriter& xml, Part* part) const
2012-05-26 14:26:10 +02:00
{
if (name.isEmpty() || name == "normal")
2012-05-26 14:26:10 +02:00
xml.stag("Channel");
else
xml.stag(QString("Channel name=\"%1\"").arg(name));
if (!descr.isEmpty())
xml.tag("descr", descr);
updateInitList();
foreach(const MidiCoreEvent& e, init) {
if (e.type() == ME_INVALID)
continue;
if (e.type() == ME_CONTROLLER) {
2013-04-08 10:31:17 +02:00
if (e.dataA() == CTRL_HBANK && e.dataB() == 0)
continue;
2013-04-08 10:31:17 +02:00
if (e.dataA() == CTRL_LBANK && e.dataB() == 0)
continue;
2013-04-08 10:31:17 +02:00
if (e.dataA() == CTRL_VOLUME && e.dataB() == 100)
continue;
2013-04-08 10:31:17 +02:00
if (e.dataA() == CTRL_PANPOT && e.dataB() == 64)
continue;
2013-04-08 10:31:17 +02:00
if (e.dataA() == CTRL_REVERB_SEND && e.dataB() == 0)
continue;
2013-04-08 10:31:17 +02:00
if (e.dataA() == CTRL_CHORUS_SEND && e.dataB() == 0)
continue;
}
2013-04-25 10:52:02 +02:00
e.write(xml);
2012-05-26 14:26:10 +02:00
}
if (!MScore::testMode)
2013-04-04 12:12:10 +02:00
// xml.tag("synti", ::synti->name(synti));
xml.tag("synti", synti);
2012-05-26 14:26:10 +02:00
if (mute)
xml.tag("mute", mute);
if (solo)
xml.tag("solo", solo);
2016-06-01 23:24:05 +02:00
if (part && part->masterScore()->exportMidiMapping() && part->score() == part->masterScore()) {
2016-03-11 12:18:46 +01:00
xml.tag("midiPort", part->masterScore()->midiMapping(channel)->port);
xml.tag("midiChannel", part->masterScore()->midiMapping(channel)->channel);
}
2016-03-11 12:18:46 +01:00
for (const NamedEventList& a : midiActions)
2012-05-26 14:26:10 +02:00
a.write(xml, "MidiAction");
2016-03-11 12:18:46 +01:00
for (const MidiArticulation& a : articulation)
2012-05-26 14:26:10 +02:00
a.write(xml);
xml.etag();
}
//---------------------------------------------------------
// read
//---------------------------------------------------------
void Channel::read(XmlReader& e, Part* part)
2012-05-26 14:26:10 +02:00
{
2013-04-04 12:12:10 +02:00
// synti = 0;
2013-01-11 18:10:18 +01:00
name = e.attribute("name");
if (name == "")
name = "normal";
2013-01-11 18:10:18 +01:00
while (e.readNextStartElement()) {
const QStringRef& tag(e.name());
2012-05-26 14:26:10 +02:00
if (tag == "program") {
2013-01-11 18:10:18 +01:00
program = e.intAttribute("value", -1);
2012-05-26 14:26:10 +02:00
if (program == -1)
2013-01-11 18:10:18 +01:00
program = e.readInt();
2013-01-17 20:20:41 +01:00
else
e.readNext();
2012-05-26 14:26:10 +02:00
}
else if (tag == "controller") {
2013-01-11 18:10:18 +01:00
int value = e.intAttribute("value", 0);
int ctrl = e.intAttribute("ctrl", 0);
2012-05-26 14:26:10 +02:00
switch (ctrl) {
case CTRL_HBANK:
bank = (value << 7) + (bank & 0x7f);
break;
case CTRL_LBANK:
bank = (bank & ~0x7f) + (value & 0x7f);
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);
2012-05-26 14:26:10 +02:00
}
break;
}
2013-01-17 20:20:41 +01:00
e.readNext();
2012-05-26 14:26:10 +02:00
}
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();
2013-01-11 18:10:18 +01:00
else if (tag == "descr")
descr = e.readElementText();
else if (tag == "mute")
mute = e.readInt();
else if (tag == "solo")
solo = e.readInt();
else if (tag == "midiPort") {
int midiPort = e.readInt();
if (part) {
MidiMapping mm;
mm.port = midiPort;
mm.channel = -1;
mm.part = part;
mm.articulation = this;
2016-03-11 12:18:46 +01:00
part->masterScore()->midiMapping()->append(mm);
channel = part->masterScore()->midiMapping()->size() - 1;
}
}
else if (tag == "midiChannel") {
int midiChannel = e.readInt();
if (part)
2016-03-11 12:18:46 +01:00
part->masterScore()->midiMapping(channel)->channel = midiChannel;
}
2012-05-26 14:26:10 +02:00
else
2013-01-11 18:10:18 +01:00
e.unknown();
2012-05-26 14:26:10 +02:00
}
if (128 == bank && "zerberus" == synti.toLower())
bank = 0;
2012-05-26 14:26:10 +02:00
updateInitList();
}
//---------------------------------------------------------
// updateInitList
//---------------------------------------------------------
void Channel::updateInitList() const
{
MidiCoreEvent e;
2012-05-26 14:26:10 +02:00
if (program != -1) {
e.setType(ME_CONTROLLER);
2013-04-08 10:31:17 +02:00
e.setDataA(CTRL_PROGRAM);
e.setDataB(program);
init[int(A::PROGRAM)] = e;
2012-05-26 14:26:10 +02:00
}
2013-04-08 10:31:17 +02:00
e.setData(ME_CONTROLLER, CTRL_HBANK, (bank >> 7) & 0x7f);
init[int(A::HBANK)] = e;
2012-05-26 14:26:10 +02:00
2013-04-08 10:31:17 +02:00
e.setData(ME_CONTROLLER, CTRL_LBANK, bank & 0x7f);
init[int(A::LBANK)] = e;
2012-05-26 14:26:10 +02:00
2013-04-08 10:31:17 +02:00
e.setData(ME_CONTROLLER, CTRL_VOLUME, volume);
init[int(A::VOLUME)] = e;
2012-05-26 14:26:10 +02:00
2013-04-08 10:31:17 +02:00
e.setData(ME_CONTROLLER, CTRL_PANPOT, pan);
init[int(A::PAN)] = e;
2012-05-26 14:26:10 +02:00
2013-04-08 10:31:17 +02:00
e.setData(ME_CONTROLLER, CTRL_CHORUS_SEND, chorus);
init[int(A::CHORUS)] = e;
2012-05-26 14:26:10 +02:00
2013-04-08 10:31:17 +02:00
e.setData(ME_CONTROLLER, CTRL_REVERB_SEND, reverb);
init[int(A::REVERB)] = e;
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// channelIdx
//---------------------------------------------------------
int Instrument::channelIdx(const QString& s) const
2012-05-26 14:26:10 +02:00
{
int idx = 0;
for (const Channel* a : _channel) {
if (a->name.isEmpty() && s == "normal")
2012-05-26 14:26:10 +02:00
return idx;
if (s == a->name)
2012-05-26 14:26:10 +02:00
return idx;
++idx;
}
return -1;
}
//---------------------------------------------------------
// write
//---------------------------------------------------------
2016-11-19 11:51:21 +01:00
void MidiArticulation::write(XmlWriter& xml) const
2012-05-26 14:26:10 +02:00
{
if (name.isEmpty())
xml.stag("Articulation");
else
xml.stag(QString("Articulation name=\"%1\"").arg(name));
2012-05-26 14:26:10 +02:00
if (!descr.isEmpty())
xml.tag("descr", descr);
xml.tag("velocity", velocity);
xml.tag("gateTime", gateTime);
xml.etag();
}
//---------------------------------------------------------
// read
//---------------------------------------------------------
2013-01-11 18:10:18 +01:00
void MidiArticulation::read(XmlReader& e)
2012-05-26 14:26:10 +02:00
{
2013-01-11 18:10:18 +01:00
name = e.attribute("name");
while (e.readNextStartElement()) {
const QStringRef& tag(e.name());
2012-05-26 14:26:10 +02:00
if (tag == "velocity") {
2013-01-11 18:10:18 +01:00
QString text(e.readElementText());
2012-05-26 14:26:10 +02:00
if (text.endsWith("%"))
text = text.left(text.size()-1);
velocity = text.toInt();
}
else if (tag == "gateTime") {
2013-01-11 18:10:18 +01:00
QString text(e.readElementText());
2012-05-26 14:26:10 +02:00
if (text.endsWith("%"))
text = text.left(text.size()-1);
gateTime = text.toInt();
}
else if (tag == "descr")
2013-01-11 18:10:18 +01:00
descr = e.readElementText();
2012-05-26 14:26:10 +02:00
else
2013-01-11 18:10:18 +01:00
e.unknown();
2012-05-26 14:26:10 +02:00
}
}
//---------------------------------------------------------
// updateVelocity
//---------------------------------------------------------
void Instrument::updateVelocity(int* velocity, int /*channelIdx*/, const QString& name)
2012-05-26 14:26:10 +02:00
{
2016-10-06 16:36:50 +02:00
for (const MidiArticulation& a : _articulation) {
2012-05-26 14:26:10 +02:00
if (a.name == name) {
*velocity = *velocity * a.velocity / 100;
break;
}
}
}
//---------------------------------------------------------
// updateGateTime
//---------------------------------------------------------
void Instrument::updateGateTime(int* gateTime, int /*channelIdx*/, const QString& name)
2012-05-26 14:26:10 +02:00
{
2016-10-06 16:36:50 +02:00
for (const MidiArticulation& a : _articulation) {
2012-05-26 14:26:10 +02:00
if (a.name == name) {
2012-11-21 15:57:35 +01:00
*gateTime = a.gateTime;
2012-05-26 14:26:10 +02:00
break;
}
}
}
//---------------------------------------------------------
// operator==
//---------------------------------------------------------
bool Instrument::operator==(const Instrument& i) const
2012-05-26 14:26:10 +02:00
{
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) {
2015-04-13 17:12:21 +02:00
if (!(i._shortNames[k] == _shortNames[k].name()))
2012-05-26 14:26:10 +02:00
return false;
}
return i._minPitchA == _minPitchA
&& i._maxPitchA == _maxPitchA
&& i._minPitchP == _minPitchP
&& i._maxPitchP == _maxPitchP
&& i._useDrumset == _useDrumset
2013-04-25 10:52:02 +02:00
&& i._midiActions == _midiActions
2012-05-26 14:26:10 +02:00
&& i._channel == _channel
&& i._articulation == _articulation
&& i._transpose.diatonic == _transpose.diatonic
&& i._transpose.chromatic == _transpose.chromatic
&& i._trackName == _trackName
&& *i.stringData() == *stringData();
2012-05-26 14:26:10 +02:00
;
}
//---------------------------------------------------------
// operator==
//---------------------------------------------------------
2014-02-11 14:27:44 +01:00
bool StaffName::operator==(const StaffName& i) const
2012-05-26 14:26:10 +02:00
{
2015-04-13 17:12:21 +02:00
return (i._pos == _pos) && (i._name == _name);
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// setUseDrumset
//---------------------------------------------------------
void Instrument::setUseDrumset(bool val)
2012-05-26 14:26:10 +02:00
{
_useDrumset = val;
if (val && !_drumset)
_drumset = new Drumset(*smDrumset);
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// setDrumset
//---------------------------------------------------------
void Instrument::setDrumset(const Drumset* ds)
2012-05-26 14:26:10 +02:00
{
delete _drumset;
if (ds) {
_useDrumset = true;
_drumset = new Drumset(*ds);
}
else {
_useDrumset = false;
_drumset = 0;
}
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// setLongName
2015-04-13 17:12:21 +02:00
// f is in richtext format
2012-05-26 14:26:10 +02:00
//---------------------------------------------------------
void Instrument::setLongName(const QString& f)
2012-05-26 14:26:10 +02:00
{
_longNames.clear();
if (f.length() > 0)
_longNames.append(StaffName(f, 0));
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// setShortName
2015-04-13 17:12:21 +02:00
// f is in richtext format
2012-05-26 14:26:10 +02:00
//---------------------------------------------------------
void Instrument::setShortName(const QString& f)
2012-05-26 14:26:10 +02:00
{
_shortNames.clear();
if (f.length() > 0)
_shortNames.append(StaffName(f, 0));
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// addLongName
//---------------------------------------------------------
void Instrument::addLongName(const StaffName& f)
2012-05-26 14:26:10 +02:00
{
_longNames.append(f);
}
//---------------------------------------------------------
// addShortName
//---------------------------------------------------------
void Instrument::addShortName(const StaffName& f)
2012-05-26 14:26:10 +02:00
{
_shortNames.append(f);
}
//---------------------------------------------------------
// clefType
//---------------------------------------------------------
ClefTypeList Instrument::clefType(int staffIdx) const
{
if (staffIdx >= _clefType.size()) {
2016-02-06 22:03:43 +01:00
if (_clefType.empty())
2014-08-16 18:52:16 +02:00
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;
}
2012-05-26 14:26:10 +02:00
//---------------------------------------------------------
// minPitchP
//---------------------------------------------------------
int Instrument::minPitchP() const
{
return _minPitchP;
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// maxPitchP
//---------------------------------------------------------
int Instrument::maxPitchP() const
{
return _maxPitchP;
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// minPitchA
//---------------------------------------------------------
int Instrument::minPitchA() const
{
return _minPitchA;
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// maxPitchA
//---------------------------------------------------------
int Instrument::maxPitchA() const
{
return _maxPitchA;
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// instrumentId
//---------------------------------------------------------
QString Instrument::instrumentId() const
{
return _instrumentId;
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// instrument
//---------------------------------------------------------
const Instrument* InstrumentList::instrument(int tick) const
2012-05-26 14:26:10 +02:00
{
if (empty())
return &defaultInstrument;
2013-05-28 15:42:02 +02:00
auto i = upper_bound(tick);
2012-05-26 14:26:10 +02:00
if (i == begin())
return &defaultInstrument;
2012-05-26 14:26:10 +02:00
--i;
return i->second;
}
//---------------------------------------------------------
// instrument
//---------------------------------------------------------
Instrument* InstrumentList::instrument(int tick)
2012-05-26 14:26:10 +02:00
{
if (empty())
return &defaultInstrument;
2013-05-28 15:42:02 +02:00
auto i = upper_bound(tick);
2012-05-26 14:26:10 +02:00
if (i == begin())
return &defaultInstrument;
2012-05-26 14:26:10 +02:00
--i;
return i->second;
}
//---------------------------------------------------------
// setInstrument
//---------------------------------------------------------
void InstrumentList::setInstrument(Instrument* instr, int tick)
2012-05-26 14:26:10 +02:00
{
2013-05-28 15:42:02 +02:00
if (!insert({tick, instr}).second)
2012-05-26 14:26:10 +02:00
(*this)[tick] = instr;
}
//---------------------------------------------------------
// longName
//---------------------------------------------------------
2014-02-11 14:27:44 +01:00
const QList<StaffName>& Instrument::longNames() const
2012-05-26 14:26:10 +02:00
{
return _longNames;
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// shortName
//---------------------------------------------------------
2014-02-11 14:27:44 +01:00
const QList<StaffName>& Instrument::shortNames() const
2012-05-26 14:26:10 +02:00
{
return _shortNames;
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// longName
//---------------------------------------------------------
2014-02-11 14:27:44 +01:00
QList<StaffName>& Instrument::longNames()
2012-05-26 14:26:10 +02:00
{
return _longNames;
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// shortName
//---------------------------------------------------------
2014-02-11 14:27:44 +01:00
QList<StaffName>& Instrument::shortNames()
2012-05-26 14:26:10 +02:00
{
return _shortNames;
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// trackName
//---------------------------------------------------------
QString Instrument::trackName() const
{
return _trackName;
2012-05-26 14:26:10 +02:00
}
void Instrument::setTrackName(const QString& s)
{
_trackName = s;
2012-05-26 14:26:10 +02:00
}
//---------------------------------------------------------
// fromTemplate
//---------------------------------------------------------
Instrument Instrument::fromTemplate(const InstrumentTemplate* t)
{
Instrument instr;
instr.setAmateurPitchRange(t->minPitchA, t->maxPitchA);
instr.setProfessionalPitchRange(t->minPitchP, t->maxPitchP);
2015-04-13 17:12:21 +02:00
for (StaffName sn : t->longNames)
instr.addLongName(StaffName(sn.name(), sn.pos()));
for (StaffName sn : t->shortNames)
instr.addShortName(StaffName(sn.name(), sn.pos()));
2012-05-26 14:26:10 +02:00
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]);
2012-05-26 14:26:10 +02:00
instr.setMidiActions(t->midiActions);
instr.setArticulation(t->articulation);
instr._channel.clear();
for (const Channel& c : t->channel)
instr._channel.append(new Channel(c));
2014-05-13 17:15:32 +02:00
instr.setStringData(t->stringData);
2012-05-26 14:26:10 +02:00
return instr;
}
2015-04-13 17:12:21 +02:00
//---------------------------------------------------------
// StaffNameList::write
//---------------------------------------------------------
2016-11-19 11:51:21 +01:00
void StaffNameList::write(XmlWriter& xml, const char* name) const
2015-04-13 17:12:21 +02:00
{
for (const StaffName& sn : *this)
sn.write(xml, name);
}
2013-05-13 18:49:17 +02:00
}