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
|
//=============================================================================
// MuseScore
// Music Composition & Notation
//
// Copyright (C) 2002-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 "score.h"
#include "excerpt.h"
#include "instrument.h"
#include "part.h"
namespace Ms {
//---------------------------------------------------------
// rebuildMidiMapping
//---------------------------------------------------------
void MasterScore::rebuildMidiMapping()
{
Score* playbackScore = _playbackScore ? _playbackScore : this;
setPlaybackScore(nullptr);
removeDeletedMidiMapping();
int maxport = updateMidiMapping();
reorderMidiMapping();
rebuildExcerptsMidiMapping();
masterScore()->setMidiPortCount(maxport);
setPlaybackScore(playbackScore);
}
//---------------------------------------------------------
// checkMidiMapping
// midi mapping is simple if all ports and channels
// don't decrease and don't have 'holes' except drum tracks
//---------------------------------------------------------
void MasterScore::checkMidiMapping()
{
isSimpleMidiMaping = true;
rebuildMidiMapping();
QList<bool> drum;
drum.reserve(int(_midiMapping.size()));
for (Part* part : parts()) {
const InstrumentList* il = part->instruments();
for (auto i = il->begin(); i != il->end(); ++i) {
const Instrument* instr = i->second;
for (int j = 0; j < instr->channel().size(); ++j)
drum.append(instr->useDrumset());
}
}
int lastChannel = -1; // port*16+channel
int lastDrumPort = -1;
int index = 0;
for (const MidiMapping& m : _midiMapping) {
if (index >= drum.size())
break;
if (drum[index]) {
lastDrumPort++;
if (m.port() != lastDrumPort) {
isSimpleMidiMaping = false;
return;
}
}
else {
lastChannel++;
if (lastChannel % 16 == 9)
lastChannel++;
int p = lastChannel / 16;
int c = lastChannel % 16;
if (m.port() != p || m.channel() != c) {
isSimpleMidiMaping = false;
return;
}
}
index++;
}
}
//---------------------------------------------------------
// getNextFreeMidiMapping
//---------------------------------------------------------
int MasterScore::getNextFreeMidiMapping(int p, int ch)
{
if (ch != -1 && p != -1)
return p*16+ch;
else if (ch != -1 && p == -1) {
for (int port = 0;; port++) {
if (!occupiedMidiChannels.contains(port*16+ch)) {
occupiedMidiChannels.insert(port*16+ch);
return port*16+ch;
}
}
}
else if (ch == -1 && p != -1) {
for (int channel = 0; channel < 16; channel++) {
if (channel != 9 && !occupiedMidiChannels.contains(p*16+channel)) {
occupiedMidiChannels.insert(p*16+channel);
return p*16+channel;
}
}
}
for (;;searchMidiMappingFrom++) {
if (searchMidiMappingFrom % 16 != 9 && !occupiedMidiChannels.contains(searchMidiMappingFrom)) {
occupiedMidiChannels.insert(searchMidiMappingFrom);
return searchMidiMappingFrom;
}
}
}
//---------------------------------------------------------
// getNextFreeDrumMidiMapping
//---------------------------------------------------------
int MasterScore::getNextFreeDrumMidiMapping()
{
for (int i = 0;; i++) {
if (!occupiedMidiChannels.contains(i*16+9)) {
occupiedMidiChannels.insert(i*16+9);
return i*16+9;
}
}
}
//---------------------------------------------------------
// rebuildExcerptsMidiMapping
//---------------------------------------------------------
void MasterScore::rebuildExcerptsMidiMapping()
{
for (Excerpt* ex : excerpts()) {
for (Part* p : ex->partScore()->parts()) {
const Part* masterPart = p->masterPart();
if (!masterPart->score()->isMaster()) {
qWarning("reorderMidiMapping: no part in master score is linked");
continue;
}
Q_ASSERT(p->instruments()->size() == masterPart->instruments()->size());
for (const auto& item : *masterPart->instruments()) {
const Instrument* iMaster = item.second;
const int tick = item.first;
Instrument* iLocal = p->instrument(Fraction::fromTicks(tick));
const int nchannels = iMaster->channel().size();
if (iLocal->channel().size() != nchannels) {
// may happen, e.g., if user changes an instrument
(*iLocal) = (*iMaster);
continue;
}
for (int c = 0; c < nchannels; ++c) {
Channel* cLocal = iLocal->channel(c);
const Channel* cMaster = iMaster->channel(c);
cLocal->setChannel(cMaster->channel());
}
}
}
}
}
//---------------------------------------------------------
// reorderMidiMapping
// Set mappings in order you see in Add->Instruments
//---------------------------------------------------------
void MasterScore::reorderMidiMapping()
{
using std::swap;
int sequenceNumber = 0;
for (Part* part : parts()) {
const InstrumentList* il = part->instruments();
for (auto i = il->begin(); i != il->end(); ++i) {
const Instrument* instr = i->second;
for (Channel* channel : instr->channel()) {
if (!(_midiMapping[sequenceNumber].part() == part
&& _midiMapping[sequenceNumber].masterChannel == channel)) {
int shouldBe = channel->channel();
swap(_midiMapping[sequenceNumber], _midiMapping[shouldBe]);
_midiMapping[sequenceNumber].articulation()->setChannel(sequenceNumber);
channel->setChannel(sequenceNumber);
_midiMapping[shouldBe].articulation()->setChannel(shouldBe);
}
sequenceNumber++;
}
}
}
}
//---------------------------------------------------------
// removeDeletedMidiMapping
// Remove mappings to deleted instruments
//---------------------------------------------------------
void MasterScore::removeDeletedMidiMapping()
{
int removeOffset = 0;
int mappingSize = int(_midiMapping.size());
for (int index = 0; index < mappingSize; index++) {
Part* p = midiMapping(index)->part();
if (!parts().contains(p)) {
removeOffset++;
continue;
}
// Not all channels could exist
bool channelExists = false;
const InstrumentList* il = p->instruments();
for (auto i = il->begin(); i != il->end() && !channelExists; ++i) {
const Instrument* instr = i->second;
channelExists = (_midiMapping[index].articulation()->channel() != -1
&& instr->channel().contains(_midiMapping[index].masterChannel)
&& !(_midiMapping[index].port() == -1 && _midiMapping[index].channel() == -1));
if (channelExists)
break;
}
if (!channelExists) {
removeOffset++;
continue;
}
// Let's do a left shift by 'removeOffset' items if necessary
if (index != 0 && removeOffset != 0) {
_midiMapping[index-removeOffset] = std::move(_midiMapping[index]);
const int chanVal = _midiMapping[index-removeOffset].articulation()->channel();
_midiMapping[index-removeOffset].articulation()->setChannel(chanVal - removeOffset);
}
}
// We have 'removeOffset' deleted instruments, let's remove their mappings
for (int index = 0; index < removeOffset; index++)
_midiMapping.pop_back();
}
//---------------------------------------------------------
// updateMidiMapping
// Add mappings to new instruments and repair existing ones
//---------------------------------------------------------
int MasterScore::updateMidiMapping()
{
int maxport = 0;
occupiedMidiChannels.clear();
searchMidiMappingFrom = 0;
occupiedMidiChannels.reserve(int(_midiMapping.size())); // Bringing down the complexity of insertion to amortized O(1)
for (const MidiMapping& mm :_midiMapping) {
if (mm.port() == -1 || mm.channel() == -1)
continue;
occupiedMidiChannels.insert((int)(mm.port())*16+(int)mm.channel());
if (maxport < mm.port())
maxport = mm.port();
}
for (Part* part : parts()) {
const InstrumentList* il = part->instruments();
for (auto i = il->begin(); i != il->end(); ++i) {
const Instrument* instr = i->second;
bool drum = instr->useDrumset();
for (Channel* channel : instr->channel()) {
bool channelExists = false;
for (const MidiMapping& mapping: _midiMapping) {
if (channel == mapping.masterChannel && channel->channel() != -1) {
channelExists = true;
break;
}
}
// Channel could already exist, but have unassigned port or channel. Repair and continue
if (channelExists) {
if (_midiMapping[channel->channel()].port() == -1) {
const int nm = getNextFreeMidiMapping(-1, _midiMapping[channel->channel()].channel());
_midiMapping[channel->channel()]._port = nm / 16;
}
else if (_midiMapping[channel->channel()].channel() == -1) {
if (drum) {
_midiMapping[channel->channel()]._port = getNextFreeDrumMidiMapping() / 16;
_midiMapping[channel->channel()]._channel = 9;
continue;
}
int nm = getNextFreeMidiMapping(_midiMapping[channel->channel()].port());
_midiMapping[channel->channel()]._port = nm / 16;
_midiMapping[channel->channel()]._channel = nm % 16;
}
continue;
}
int midiPort;
int midiChannel;
if (drum) {
midiPort = getNextFreeDrumMidiMapping() / 16;
midiChannel = 9;
}
else {
int nm = getNextFreeMidiMapping();
midiPort = nm / 16;
midiChannel = nm % 16;
}
if (midiPort > maxport)
maxport = midiPort;
addMidiMapping(channel, part, midiPort, midiChannel);
}
}
}
return maxport;
}
//---------------------------------------------------------
// addMidiMapping
//---------------------------------------------------------
void MasterScore::addMidiMapping(Channel* channel, Part* part, int midiPort, int midiChannel)
{
if (!part->score()->isMaster())
return;
MidiMapping mm;
mm._part = part;
mm.masterChannel = channel;
mm._articulation.reset(new Channel(*channel));
mm.link = PartChannelSettingsLink(mm.articulation(), mm.masterChannel, /* excerpt */ false);
mm._port = midiPort;
mm._channel = midiChannel;
const int mscoreChannel = int(_midiMapping.size());
mm._articulation->setChannel(mscoreChannel);
mm.masterChannel->setChannel(mscoreChannel);
_midiMapping.push_back(std::move(mm));
}
//---------------------------------------------------------
// updateMidiMapping
//---------------------------------------------------------
void MasterScore::updateMidiMapping(Channel* channel, Part* part, int midiPort, int midiChannel)
{
const int c = channel->channel();
if (c < 0)
return;
if (c >= int(masterScore()->midiMapping().size())) {
qDebug("Can't set midi channel: midiMapping is empty!");
return;
}
MidiMapping& mm = _midiMapping[c];
if (midiChannel != -1)
mm._channel = midiChannel;
if (midiPort != -1)
mm._port = midiPort;
if (part)
mm._part = part->masterPart();
}
} // namespace Ms
|