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
|
#include "importmidi_chordname.h"
#include "importmidi_inner.h"
#include "importmidi_chord.h"
#include "importmidi_fraction.h"
#include "libmscore/score.h"
#include "libmscore/staff.h"
#include "libmscore/measure.h"
#include "libmscore/harmony.h"
#include "midi/midifile.h"
#include "mscore/preferences.h"
// From XF Format Specifications V 2.01 (January 13, 1999, YAMAHA CORPORATION)
namespace Ms {
namespace MidiChordName {
int readFirstHalf(uchar byte)
{
return (byte >> 4) & 0xf;
}
int readSecondHalf(uchar byte)
{
return byte & 0xf;
}
QString readChordRoot(uchar byte)
{
static const std::vector<QString> inversions = {
"bbb", "bb", "b", "", "#", "##", "###"
};
static const std::vector<QString> notes = {
"", "C", "D", "E", "F", "G", "A", "B"
};
QString chordRoot;
const size_t noteIndex = readSecondHalf(byte);
if (noteIndex < notes.size())
chordRoot += notes[noteIndex];
const size_t inversionIndex = readFirstHalf(byte);
if (inversionIndex < inversions.size())
chordRoot += inversions[inversionIndex];
return chordRoot;
}
QString readChordType(uchar chordTypeIndex)
{
static const std::vector<QString> chordTypes = {
"" // Maj
, "6" // Maj6
, "Maj7"
, "Maj7#11" // Maj7(#11)
, "Maj9" // Maj(9)
, "Maj9" // Maj7(9)
, "6(9)" // Maj6(9)
, "aug"
, "m" // min
, "m6" // min6
, "m7" // min7
, "m7b5" // min7b5
, "m9" // min(9)
, "m9" // min7(9)
, "m7(11)" // min7(11)
, "mMaj7" // minMaj7
, "mMaj9" // minMaj7(9)
, "dim"
, "dim7"
, "7" // 7th
, "7sus4"
, "7b5"
, "9" // 7(9)
, "7#11" // 7(#11)
, "7(13)"
, "7b9" // 7(b9)
, "7b13" // 7(b13)
, "7#9" // 7(#9)
, "Maj7#5" // Maj7aug
, "7#5" // 7aug
, "" // 1+8
, "5" // 1+5
, "sus4"
, "sus2" // 1+2+5
, "N.C." // cc
};
if (chordTypeIndex < chordTypes.size())
return chordTypes[chordTypeIndex];
return "";
}
QString readChordName(const MidiEvent &e)
{
if (e.type() != ME_META || e.metaType() != META_SPECIFIC)
return "";
if (e.len() < 4)
return "";
const uchar *data = e.edata();
if (data[0] != 0x43 || data[1] != 0x7B || data[2] != 0x01)
return "";
QString chordName;
if (e.len() >= 4)
chordName += readChordRoot(data[3]);
if (e.len() >= 5)
chordName += readChordType(data[4]);
if (e.len() >= 6) {
const QString chordRoot = readChordRoot(data[5]);
if (!chordRoot.isEmpty()) {
QString chordType;
if (e.len() >= 7)
chordType = readChordType(data[6]);
if (chordRoot + chordType == chordName)
chordName += "/" + chordRoot;
else
chordName += "/" + chordRoot + chordType;
}
}
return chordName;
}
QString findChordName(
const QList<MidiNote> ¬es,
const std::multimap<ReducedFraction, QString> &chordNames)
{
for (const MidiNote ¬e: notes) {
const auto range = chordNames.equal_range(note.origOnTime);
if (range.second == range.first)
continue;
// found chord names (usually only one)
QString chordName;
for (auto it = range.first; it != range.second; ++it) {
if (it != range.first)
chordName += "\n" + it->second;
else
chordName += it->second;
}
return chordName;
}
return "";
}
void findChordNames(const std::multimap<int, MTrack> &tracks)
{
auto &data = *preferences.midiImportOperations.data();
for (const auto &track: tracks) {
for (const auto &event: track.second.mtrack->events()) {
const MidiEvent &e = event.second;
const QString chordName = readChordName(e);
if (!chordName.isEmpty()) {
const auto time = ReducedFraction::fromTicks(event.first);
data.chordNames.insert({time, chordName});
}
}
}
}
// all notes should be already placed to the score
void setChordNames(QList<MTrack> &tracks)
{
const auto &data = *preferences.midiImportOperations.data();
if (data.chordNames.empty() || !data.trackOpers.showChordNames.value())
return;
// chords here can have on time very different from the original one
// before quantization, so we look for original on times
// that are stored in notes
std::set<ReducedFraction> usedTimes; // don't use one tick for chord name twice
for (MTrack &track: tracks) {
for (const auto &chord: track.chords) {
if (usedTimes.find(chord.first) != usedTimes.end())
continue;
const MidiChord &c = chord.second;
const QString chordName = findChordName(c.notes, data.chordNames);
if (chordName.isEmpty())
continue;
Staff *staff = track.staff;
Score *score = staff->score();
const ReducedFraction &onTime = chord.first;
usedTimes.insert(onTime);
Measure* measure = score->tick2measure(onTime.ticks());
Segment* seg = measure->getSegment(Segment::Type::ChordRest,
onTime.ticks());
const int t = staff->idx() * VOICES;
Harmony* h = new Harmony(score);
h->setHarmony(chordName);
h->setTrack(t);
seg->add(h);
}
}
}
} // namespace MidiChordName
} // namespace Ms
|