LCOV - code coverage report
Current view: top level - src/importexport/guitarpro/internal - importgtp.cpp Coverage Total Hit
Test: filtered_coverage.info Lines: 53.1 % 1818 966
Test Date: 2026-11-19 04:14:36

            Line data    Source code
       1              : /*
       2              :  * SPDX-License-Identifier: GPL-3.0-only
       3              :  * MuseScore-Studio-CLA-applies
       4              :  *
       5              :  * MuseScore Studio
       6              :  * Music Composition & Notation
       7              :  *
       8              :  * Copyright (C) 2021 MuseScore Limited and others
       9              :  *
      10              :  * This program is free software: you can redistribute it and/or modify
      11              :  * it under the terms of the GNU General Public License version 3 as
      12              :  * published by the Free Software Foundation.
      13              :  *
      14              :  * This program is distributed in the hope that it will be useful,
      15              :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      16              :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17              :  * GNU General Public License for more details.
      18              :  *
      19              :  * You should have received a copy of the GNU General Public License
      20              :  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
      21              :  */
      22              : 
      23              : #include "importgtp.h"
      24              : 
      25              : #include <cmath>
      26              : 
      27              : #include "importptb.h"
      28              : #include "internal/guitarprodrumset.h"
      29              : #include "utils.h"
      30              : 
      31              : #include "engraving/dom/arpeggio.h"
      32              : #include "engraving/dom/articulation.h"
      33              : #include "engraving/dom/barline.h"
      34              : #include "engraving/dom/bend.h"
      35              : #include "engraving/dom/box.h"
      36              : #include "engraving/dom/bracket.h"
      37              : #include "engraving/dom/chord.h"
      38              : #include "engraving/dom/chordline.h"
      39              : #include "engraving/dom/clef.h"
      40              : #include "engraving/dom/dynamic.h"
      41              : #include "engraving/dom/excerpt.h"
      42              : #include "engraving/dom/factory.h"
      43              : #include "engraving/dom/fret.h"
      44              : #include "engraving/dom/glissando.h"
      45              : #include "engraving/dom/harmony.h"
      46              : #include "engraving/dom/instrchange.h"
      47              : #include "engraving/dom/instrtemplate.h"
      48              : #include "engraving/dom/keysig.h"
      49              : #include "engraving/dom/lyrics.h"
      50              : #include "engraving/dom/masterscore.h"
      51              : #include "engraving/dom/measure.h"
      52              : #include "engraving/dom/measurebase.h"
      53              : #include "engraving/dom/note.h"
      54              : #include "engraving/dom/notedot.h"
      55              : #include "engraving/dom/part.h"
      56              : #include "engraving/dom/rehearsalmark.h"
      57              : #include "engraving/dom/rest.h"
      58              : #include "engraving/dom/segment.h"
      59              : #include "engraving/dom/slur.h"
      60              : #include "engraving/dom/staff.h"
      61              : #include "engraving/dom/stafftext.h"
      62              : #include "engraving/dom/stafftype.h"
      63              : #include "engraving/dom/stringdata.h"
      64              : #include "engraving/dom/tapping.h"
      65              : #include "engraving/dom/tempotext.h"
      66              : #include "engraving/dom/text.h"
      67              : #include "engraving/dom/textline.h"
      68              : #include "engraving/dom/tie.h"
      69              : #include "engraving/dom/timesig.h"
      70              : #include "engraving/dom/tuplet.h"
      71              : #include "engraving/dom/volta.h"
      72              : #include "engraving/dom/stringtunings.h"
      73              : #include "engraving/dom/capo.h"
      74              : #include "engraving/rw/xmlwriter.h"
      75              : #include "engraving/types/symid.h"
      76              : 
      77              : #include "engraving/editing/editchord.h"
      78              : 
      79              : #include "log.h"
      80              : 
      81              : using namespace muse;
      82              : using namespace muse::io;
      83              : using namespace mu::engraving;
      84              : 
      85              : namespace mu::iex::guitarpro {
      86              : //---------------------------------------------------------
      87              : //   errmsg
      88              : //---------------------------------------------------------
      89              : 
      90              : const char* const GuitarPro::errmsg[] = {
      91              :     "no error",
      92              :     "unknown file format",
      93              :     "unexpected end of file",
      94              :     "bad number of strings",
      95              : };
      96              : 
      97              : #ifdef _MSC_VER
      98              : #pragma optimize("", off)
      99              : #endif
     100              : 
     101              : //---------------------------------------------------------
     102              : //   GuitarPro
     103              : //---------------------------------------------------------
     104              : 
     105          295 : GuitarPro::GuitarPro(MasterScore* s, int v, const modularity::ContextPtr& iocCtx)
     106          295 :     : muse::Contextable(iocCtx)
     107              : {
     108          295 :     score   = s;
     109          295 :     version = v;
     110          295 :     voltaSequence = 1;
     111          295 :     tempo = -1;
     112          295 : }
     113              : 
     114              : //---------------------------------------------------------
     115              : //   skip
     116              : //---------------------------------------------------------
     117              : 
     118         5319 : void GuitarPro::skip(int64_t len)
     119              : {
     120         5319 :     f->seek(f->pos() + len);
     121              :     /*char c;
     122              :     while (len--)
     123              :           read(&c, 1);*/
     124         5319 : }
     125              : 
     126              : //---------------------------------------------------------
     127              : //   createTuningString
     128              : //---------------------------------------------------------
     129              : static const std::unordered_map<uint64_t, std::string> flatPresets = {
     130              :     // 63 58 54 49 44 39
     131              :     { 0x3f3a36312c27, { "Eb Ab Db Gb Bb Eb" } },
     132              :     // 60 55 51 46 41 36
     133              :     { 0x3c37332e2924, { "C F Bb Eb G C" } },
     134              :     // 63 58 54 49 44 37
     135              :     { 0x3f3a36312c25, { "Db Ab Db Gb Bb Eb" } },
     136              : };
     137              : 
     138           78 : bool GuitarPro::createTuningString(int strings, int tuning[])
     139              : {
     140           78 :     bool useFlats = false;
     141           78 :     const char* tune[] = { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };
     142           78 :     std::vector<int> pitch;
     143           78 :     uint64_t k = 0;
     144          544 :     for (int i = 0; i < strings; ++i) {
     145          466 :         pitch.push_back(tuning[i]);
     146          466 :         k |= (uint64_t)tuning[i] << 8 * i;
     147              :     }
     148           78 :     if (auto preset = flatPresets.find(k); preset != flatPresets.end()) {
     149            0 :         tunings.push_back(preset->second);
     150            0 :         useFlats = true;
     151            0 :         return useFlats;
     152              :     }
     153           78 :     std::string t;
     154          544 :     for (auto i : pitch) {
     155          466 :         t += tune[i % 12];
     156          466 :         t += " ";
     157              :     }
     158           78 :     tunings.push_back(t);
     159           78 :     return useFlats;
     160           78 : }
     161              : 
     162           74 : void GuitarPro::initDynamics(size_t stavesNum)
     163              : {
     164           74 :     previousDynamicByTrack.resize(stavesNum * mu::engraving::VOICES);
     165           74 :     std::fill(previousDynamicByTrack.begin(), previousDynamicByTrack.end(), INVALID_DYNAMIC);
     166           74 : }
     167              : 
     168              : //---------------------------------------------------------
     169              : //    read
     170              : //---------------------------------------------------------
     171              : 
     172        78477 : void GuitarPro::read(void* p, int64_t len)
     173              : {
     174        78477 :     if (len == 0) {
     175          996 :         return;
     176              :     }
     177        77481 :     int64_t rv = f->read((uint8_t*)p, len);
     178        77481 :     if (rv != len) {
     179            0 :         assert(rv == len);     //to have assert in debug and no warnings from AppVeyor in release
     180              :     }
     181        77481 :     curPos += len;
     182              : }
     183              : 
     184              : //---------------------------------------------------------
     185              : //   readChar
     186              : //---------------------------------------------------------
     187              : 
     188         4278 : int GuitarPro::readChar()
     189              : {
     190              :     signed char c;
     191         4278 :     read(&c, 1);
     192         4278 :     return c;
     193              : }
     194              : 
     195              : //---------------------------------------------------------
     196              : //   readUInt8
     197              : //---------------------------------------------------------
     198              : 
     199        39092 : uint8_t GuitarPro::readUInt8()
     200              : {
     201              :     uint8_t c;
     202        39092 :     read(&c, 1);
     203        39092 :     return c;
     204              : }
     205              : 
     206              : //---------------------------------------------------------
     207              : //   readPascalString
     208              : //---------------------------------------------------------
     209              : 
     210           80 : String GuitarPro::readPascalString(int n)
     211              : {
     212           80 :     uint8_t l = readUInt8();
     213           80 :     std::vector<char> s(l + 1);
     214           80 :     read(&s[0], l);
     215           80 :     s[l] = 0;
     216           80 :     if (n - l > 0) {
     217           80 :         skip(n - l);
     218              :     }
     219           80 :     std::string_view sw(&s[0], l);
     220           80 :     if (UtfCodec::isValidUtf8(sw)) {
     221           80 :         return String::fromUtf8(&s[0]);
     222              :     }
     223            0 :     return String::fromAscii(&s[0], l);
     224           80 : }
     225              : 
     226              : //---------------------------------------------------------
     227              : //   readWordPascalString
     228              : //---------------------------------------------------------
     229              : 
     230          335 : String GuitarPro::readWordPascalString()
     231              : {
     232          335 :     int l = readInt();
     233          335 :     std::vector<char> c(l + 1);
     234          335 :     read(&c[0], l);
     235          335 :     c[l] = 0;
     236          335 :     std::string_view sw(&c[0], l);
     237          335 :     if (UtfCodec::isValidUtf8(sw)) {
     238          335 :         return String::fromUtf8(&c[0]);
     239              :     }
     240            0 :     return String::fromAscii(&c[0], l);
     241          335 : }
     242              : 
     243              : //---------------------------------------------------------
     244              : //   readBytePascalString
     245              : //---------------------------------------------------------
     246              : 
     247          440 : String GuitarPro::readBytePascalString()
     248              : {
     249          440 :     int l = readUInt8();
     250          440 :     std::vector<char> c(l + 1);
     251          440 :     read(&c[0], l);
     252          440 :     c[l] = 0;
     253          440 :     std::string_view sw(&c[0], l);
     254          440 :     if (UtfCodec::isValidUtf8(sw)) {
     255          440 :         return String::fromUtf8(&c[0]);
     256              :     }
     257            0 :     return String::fromAscii(&c[0], l);
     258          440 : }
     259              : 
     260              : //---------------------------------------------------------
     261              : //   readDelphiString
     262              : //---------------------------------------------------------
     263              : 
     264          740 : String GuitarPro::readDelphiString()
     265              : {
     266          740 :     int maxl = readInt();
     267          740 :     int l    = readUInt8();
     268          740 :     if (maxl != l + 1 && maxl > 255) {
     269            0 :         ASSERT_X("readDelphiString: first word doesn't match second byte");
     270              :         l = maxl - 1;
     271              :     }
     272          740 :     std::vector<char> c(l + 1);
     273          740 :     read(&c[0], l);
     274          740 :     c[l] = 0;
     275         1480 :     return String::fromAscii(&c[0]);
     276          740 : }
     277              : 
     278              : //---------------------------------------------------------
     279              : //   readInt
     280              : //---------------------------------------------------------
     281              : 
     282         8378 : int GuitarPro::readInt()
     283              : {
     284              :     uint8_t x;
     285         8378 :     read(&x, 1);
     286         8378 :     int r = x;
     287         8378 :     read(&x, 1);
     288         8378 :     r += x << 8;
     289         8378 :     read(&x, 1);
     290         8378 :     r += x << 16;
     291         8378 :     read(&x, 1);
     292         8378 :     r += x << 24;
     293         8378 :     return r;
     294              : }
     295              : 
     296              : //---------------------------------------------------------
     297              : //   addPalmMate
     298              : //---------------------------------------------------------
     299              : 
     300          930 : void GuitarPro::addPalmMute(ChordRest* cr, bool hasPalmMute)
     301              : {
     302          930 :     m_continiousElementsBuilder->buildContiniousElement(cr, ElementType::PALM_MUTE, ContiniousElementsBuilder::ImportType::PALM_MUTE,
     303              :                                                         hasPalmMute);
     304          930 : }
     305              : 
     306              : //---------------------------------------------------------
     307              : //   addLetRing
     308              : //---------------------------------------------------------
     309              : 
     310          930 : void GuitarPro::addLetRing(ChordRest* cr, bool hasLetRing)
     311              : {
     312          930 :     m_continiousElementsBuilder->buildContiniousElement(cr, ElementType::LET_RING, ContiniousElementsBuilder::ImportType::LET_RING,
     313              :                                                         hasLetRing);
     314          930 : }
     315              : 
     316              : //---------------------------------------------------------
     317              : //   addTrill
     318              : //---------------------------------------------------------
     319              : 
     320          839 : void GuitarPro::addTrill(ChordRest* cr, bool hasTrill)
     321              : {
     322          839 :     m_continiousElementsBuilder->buildContiniousElement(cr, ElementType::TRILL, ContiniousElementsBuilder::ImportType::TRILL, hasTrill);
     323          839 : }
     324              : 
     325              : //---------------------------------------------------------
     326              : //   addHammerOnPullOff
     327              : //---------------------------------------------------------
     328              : 
     329          839 : void GuitarPro::addHammerOnPullOff(ChordRest* cr, bool hasHammerOnPullOff)
     330              : {
     331          839 :     m_continiousElementsBuilder->buildContiniousElement(cr, ElementType::HAMMER_ON_PULL_OFF,
     332              :                                                         ContiniousElementsBuilder::ImportType::HAMMER_ON_PULL_OFF, hasHammerOnPullOff);
     333          839 : }
     334              : 
     335              : //---------------------------------------------------------
     336              : //   addRasgueado
     337              : //---------------------------------------------------------
     338              : 
     339          545 : void GuitarPro::addRasgueado(ChordRest* cr, bool hasRasgueado)
     340              : {
     341          545 :     m_continiousElementsBuilder->buildContiniousElement(cr, ElementType::RASGUEADO, ContiniousElementsBuilder::ImportType::RASGUEADO,
     342              :                                                         hasRasgueado);
     343          545 : }
     344              : 
     345              : //---------------------------------------------------------
     346              : //   addVibratoLeftHand
     347              : //---------------------------------------------------------
     348              : 
     349          930 : void GuitarPro::addVibratoLeftHand(ChordRest* cr, bool hasVibratoLeftHand)
     350              : {
     351          930 :     m_continiousElementsBuilder->buildContiniousElement(cr, ElementType::VIBRATO,
     352              :                                                         ContiniousElementsBuilder::ImportType::VIBRATO_LEFT_HAND_SLIGHT, hasVibratoLeftHand,
     353              :                                                         ContiniousElementsBuilder::VibratoSubType::LEFT_HAND_SLIGHT);
     354          930 : }
     355              : 
     356              : //---------------------------------------------------------
     357              : //   addVibratoWTremBar
     358              : //---------------------------------------------------------
     359              : 
     360          930 : void GuitarPro::addVibratoWTremBar(ChordRest* cr, bool hasVibratoWTremBar)
     361              : {
     362          930 :     m_continiousElementsBuilder->buildContiniousElement(cr, ElementType::VIBRATO,
     363              :                                                         ContiniousElementsBuilder::ImportType::VIBRATO_W_TREM_BAR_SLIGHT,
     364              :                                                         hasVibratoWTremBar, ContiniousElementsBuilder::VibratoSubType::W_TREM_BAR_SLIGHT);
     365          930 : }
     366              : 
     367              : //---------------------------------------------------------
     368              : //   addHarmonicMarks
     369              : //---------------------------------------------------------
     370              : 
     371          839 : void GuitarPro::addHarmonicMarks(ChordRest* cr, bool hasHarmonicArtificial, bool hasHarmonicPinch, bool hasHarmonicTap,
     372              :                                  bool hasHarmonicSemi)
     373              : {
     374          839 :     m_continiousElementsBuilder->buildContiniousElement(cr, ElementType::HARMONIC_MARK,
     375              :                                                         ContiniousElementsBuilder::ImportType::HARMONIC_ARTIFICIAL,
     376              :                                                         hasHarmonicArtificial, ContiniousElementsBuilder::HarmonicMarkSubType::ARTIFICIAL);
     377              : 
     378          839 :     m_continiousElementsBuilder->buildContiniousElement(cr, ElementType::HARMONIC_MARK,
     379              :                                                         ContiniousElementsBuilder::ImportType::HARMONIC_PINCH,
     380              :                                                         hasHarmonicPinch, ContiniousElementsBuilder::HarmonicMarkSubType::PINCH);
     381              : 
     382          839 :     m_continiousElementsBuilder->buildContiniousElement(cr, ElementType::HARMONIC_MARK,
     383              :                                                         ContiniousElementsBuilder::ImportType::HARMONIC_TAP,
     384              :                                                         hasHarmonicTap, ContiniousElementsBuilder::HarmonicMarkSubType::TAP);
     385              : 
     386          839 :     m_continiousElementsBuilder->buildContiniousElement(cr, ElementType::HARMONIC_MARK,
     387              :                                                         ContiniousElementsBuilder::ImportType::HARMONIC_SEMI,
     388              :                                                         hasHarmonicSemi, ContiniousElementsBuilder::HarmonicMarkSubType::SEMI);
     389          839 : }
     390              : 
     391              : //---------------------------------------------------------
     392              : //   addTap
     393              : //---------------------------------------------------------
     394              : 
     395            1 : void GuitarPro::addTap(Chord* chord)
     396              : {
     397            1 :     Tapping* tapping = Factory::createTapping(chord);
     398            1 :     tapping->setTrack(chord->track());
     399            1 :     tapping->setHand(TappingHand::RIGHT);
     400            1 :     chord->add(tapping);
     401            1 : }
     402              : 
     403              : //---------------------------------------------------------
     404              : //   addSlap
     405              : //---------------------------------------------------------
     406              : 
     407            4 : void GuitarPro::addSlap(Note* note)
     408              : {
     409            4 :     addTextArticulation(note, ArticulationTextType::SLAP);
     410            4 : }
     411              : 
     412              : //---------------------------------------------------------
     413              : //   addPop
     414              : //---------------------------------------------------------
     415              : 
     416            4 : void GuitarPro::addPop(Note* note)
     417              : {
     418            4 :     addTextArticulation(note, ArticulationTextType::POP);
     419            4 : }
     420              : 
     421            8 : void GuitarPro::addTextArticulation(Note* note, ArticulationTextType type)
     422              : {
     423            8 :     if (type == ArticulationTextType::NO_TEXT) {
     424            0 :         return;
     425              :     }
     426              : 
     427            8 :     if (Chord* ch = toChord(note->parent())) {
     428            8 :         Articulation* art = mu::engraving::Factory::createArticulation(ch);
     429            8 :         art->setTextType(type);
     430            8 :         ch->add(art);
     431              :     }
     432              : }
     433              : 
     434              : //---------------------------------------------------------
     435              : //   addTextToNote
     436              : //---------------------------------------------------------
     437              : 
     438            0 : void GuitarPro::addTextToNote(String string, Note* note)
     439              : {
     440            0 :     Segment* segment = note->chord()->segment();
     441            0 :     StaffText* text = Factory::createStaffText(segment);
     442              : 
     443            0 :     if (!string.isEmpty()) {
     444            0 :         bool useHarmony = string[string.size() - 1] == '\\';
     445            0 :         if (useHarmony) {
     446            0 :             string.chop(1);
     447              :         }
     448              :     }
     449            0 :     text->setPlainText(string);
     450            0 :     text->setTrack(note->chord()->track());
     451            0 :     segment->add(text);
     452            0 : }
     453              : 
     454            0 : void GuitarPro::setupTupletStyle(Tuplet* tuplet)
     455              : {
     456              :     bool real;
     457            0 :     switch (tuplet->ratio().numerator()) {
     458            0 :     case 2: real = (tuplet->ratio().denominator() == 3);
     459            0 :         break;
     460            0 :     case 3:
     461            0 :     case 4: real = (tuplet->ratio().denominator() == 2);
     462            0 :         break;
     463            0 :     case 5:
     464              :     case 6:
     465            0 :     case 7: real = (tuplet->ratio().denominator() == 4);
     466            0 :         break;
     467            0 :     case 9:
     468              :     case 10:
     469              :     case 11:
     470              :     case 12:
     471            0 :     case 13: real = (tuplet->ratio().denominator() == 8);
     472            0 :         break;
     473            0 :     default: real = false;
     474              :     }
     475            0 :     if (!real) {
     476            0 :         tuplet->setNumberType(TupletNumberType::SHOW_RELATION);
     477            0 :         tuplet->setPropertyFlags(Pid::NUMBER_TYPE, PropertyFlags::UNSTYLED);
     478              :     }
     479            0 : }
     480              : 
     481              : //---------------------------------------------------------
     482              : //   setTuplet
     483              : //---------------------------------------------------------
     484              : 
     485           20 : void GuitarPro::setTuplet(Tuplet* tuplet, int tuple)
     486              : {
     487           20 :     switch (tuple) {
     488           20 :     case 3:
     489           20 :         tuplet->setRatio(Fraction(3, 2));
     490           20 :         break;
     491            0 :     case 5:
     492            0 :         tuplet->setRatio(Fraction(5, 4));
     493            0 :         break;
     494            0 :     case 6:
     495            0 :         tuplet->setRatio(Fraction(6, 4));
     496            0 :         break;
     497            0 :     case 7:
     498            0 :         tuplet->setRatio(Fraction(7, 4));
     499            0 :         break;
     500            0 :     case 9:
     501            0 :         tuplet->setRatio(Fraction(9, 8));
     502            0 :         break;
     503            0 :     case 10:
     504            0 :         tuplet->setRatio(Fraction(10, 8));
     505            0 :         break;
     506            0 :     case 11:
     507            0 :         tuplet->setRatio(Fraction(11, 8));
     508            0 :         break;
     509            0 :     case 12:
     510            0 :         tuplet->setRatio(Fraction(12, 8));
     511            0 :         break;
     512            0 :     case 13:
     513            0 :         tuplet->setRatio(Fraction(13, 8));
     514            0 :         break;
     515            0 :     default:
     516            0 :         ASSERT_X(String(u"unsupported tuplet %d\n").arg(tuple));
     517              :     }
     518           20 : }
     519              : 
     520              : //---------------------------------------------------------
     521              : //   addDynamic
     522              : //---------------------------------------------------------
     523              : 
     524          148 : void GuitarPro::addDynamic(Note* note, int d)
     525              : {
     526              :     // guitar pro only allows their users to go from ppp to fff
     527          148 :     static const std::array<String, 9> map_dyn { u"f", u"ppp", u"pp", u"p", u"mp", u"mf", u"f", u"ff", u"fff" };
     528          296 :     if (d < 0 || static_cast<size_t>(d) >= map_dyn.size()) {
     529            2 :         return;
     530              :     }
     531          146 :     if (!note->chord()) {
     532            0 :         LOGD() << "addDynamics: No chord associated with this note";
     533            0 :         return;
     534              :     }
     535          146 :     Segment* s = nullptr;
     536          146 :     if (note->chord()->isGrace()) {
     537            0 :         Chord* parent = toChord(note->chord()->parent());
     538            0 :         s = parent->segment();
     539              :     } else {
     540          146 :         s = note->chord()->segment();
     541              :     }
     542          146 :     if (!s->findAnnotation(ElementType::DYNAMIC, note->staffIdx() * VOICES, note->staffIdx() * VOICES + VOICES - 1)) {
     543          111 :         Dynamic* dyn = new Dynamic(s);
     544          111 :         dyn->setDynamicType(map_dyn[d]);
     545          111 :         dyn->setTrack(note->track());
     546          111 :         s->add(dyn);
     547              :     }
     548              : }
     549              : 
     550              : //---------------------------------------------------------
     551              : //   readVolta
     552              : //---------------------------------------------------------
     553              : 
     554          234 : void GuitarPro::readVolta(GPVolta* gpVolta, Measure* m)
     555              : {
     556              :     /* Volta information is at most eight bits
     557              :      * signifying which numbers should appear in the
     558              :      * volta. A single bit 1 represents we should show
     559              :      * 1, 100 represents 3, 10000 represents 6, 10101
     560              :      * represents 1,3,5 etc. */
     561          234 :     if (gpVolta->voltaInfo.size() != 0) {
     562              :         // we have volta information - set up a volta
     563           16 :         mu::engraving::Volta* volta = new mu::engraving::Volta(score->dummy());
     564           16 :         volta->endings().clear();
     565           16 :         String voltaTextString = u"";
     566              :         // initialise count to 1 as the first bit processed with represent first time volta
     567           16 :         int count = 0;
     568           16 :         int binaryNumber = 0;
     569              :         // iterate through the volta information and determine the decimal numbers for voltas
     570           16 :         auto iter = gpVolta->voltaInfo.begin();
     571           77 :         while (iter != gpVolta->voltaInfo.end()) {
     572           45 :             switch (gpVolta->voltaType) {
     573            9 :             case GP_VOLTA_FLAGS:
     574            9 :                 count++;
     575            9 :                 if (*iter == 1) {                 // we want this number to be displayed in the volta
     576            5 :                     if (voltaTextString.empty()) {
     577            2 :                         voltaTextString += String::number(count);
     578              :                     } else {
     579            3 :                         voltaTextString += u',' + String::number(count);
     580              :                     }
     581              :                     // add the decimal number to the endings field of voltas as well as the text
     582            5 :                     volta->endings().push_back(count);
     583              :                 }
     584            9 :                 ++iter;
     585            9 :                 break;
     586           36 :             case GP_VOLTA_BINARY:
     587              :                 // find the binary number in decimal
     588           36 :                 if (*iter == 1) {
     589           20 :                     binaryNumber += pow(2, count);
     590              :                 }
     591           36 :                 ++iter;
     592           36 :                 if (iter == gpVolta->voltaInfo.end()) {
     593              :                     // display all numbers in the volta from voltaSequence to the decimal
     594           42 :                     while (voltaSequence <= binaryNumber) {
     595           28 :                         if (voltaTextString.empty()) {
     596           14 :                             voltaTextString = String::number(voltaSequence);
     597              :                         } else {
     598           14 :                             voltaTextString += u',' + String::number(voltaSequence);
     599              :                         }
     600           28 :                         volta->endings().push_back(voltaSequence);
     601           28 :                         voltaSequence++;
     602              :                     }
     603              :                 }
     604           36 :                 count++;
     605           36 :                 break;
     606              :             }
     607              :         }
     608           16 :         volta->setText(XmlWriter::xmlString(voltaTextString));
     609           16 :         volta->setTick(m->tick());
     610           16 :         volta->setTick2(m->endTick());
     611           16 :         score->addElement(volta);
     612           16 :     }
     613          234 : }
     614              : 
     615           22 : std::vector<PitchValue> GuitarPro::readBendDataFromFile()
     616              : {
     617           22 :     std::vector<PitchValue> bendData;
     618              : 
     619           22 :     readUInt8();                          // icon
     620           22 :     /*int amplitude =*/ readInt();        // shown amplitude
     621           22 :     int numPoints = readInt();            // the number of points in the bend
     622              : 
     623          105 :     for (int i = 0; i < numPoints; ++i) {
     624           83 :         int bendTime  = readInt();
     625           83 :         int bendPitch = readInt();
     626           83 :         int bendVibrato = readUInt8();
     627           83 :         bendData.emplace_back(bendTime, bendPitch, bendVibrato);
     628              :     }
     629              : 
     630           22 :     return bendData;
     631            0 : }
     632              : 
     633           22 : void GuitarPro::createBend(Note* note, std::vector<PitchValue>& bendData)
     634              : {
     635           22 :     if (bendData.size() < 2) {
     636            0 :         return;
     637              :     }
     638              : 
     639              :     /// not adding "hold" on 0 pitch
     640           22 :     int maxPitch = (std::max_element(bendData.begin(), bendData.end(), [](const PitchValue& l, const PitchValue& r) {
     641           61 :         return l.pitch < r.pitch;
     642           22 :     }))->pitch;
     643              : 
     644           22 :     if (maxPitch == 0) {
     645            0 :         return;
     646              :     }
     647              : 
     648           22 :     m_guitarBendImporter->collectBend(note, bendData);
     649              : }
     650              : 
     651              : //---------------------------------------------------------
     652              : //   readBend
     653              : //    bend graph
     654              : //---------------------------------------------------------
     655              : 
     656           10 : void GuitarPro::readBend(Note* note)
     657              : {
     658           10 :     std::vector<PitchValue> bendData = readBendDataFromFile();
     659           10 :     createBend(note, bendData);
     660           10 : }
     661              : 
     662              : //---------------------------------------------------------
     663              : //   readLyrics
     664              : //---------------------------------------------------------
     665              : 
     666           67 : void GuitarPro::readLyrics()
     667              : {
     668           67 :     gpLyrics.lyricTrack = static_cast<size_t>(readInt());          // lyric track
     669           67 :     gpLyrics.fromBeat = static_cast<size_t>(readInt());
     670           67 :     gpLyrics.beatCounter = 0;
     671              : 
     672           67 :     String lyrics = readWordPascalString();
     673           67 :     lyrics.replace(u'\n', u' ');
     674           67 :     lyrics.replace(u'\r', u' ');
     675           67 :     auto sl = lyrics.split(u' ', KeepEmptyParts);
     676          134 :     for (auto& str : sl) {
     677           67 :         gpLyrics.lyrics.push_back(str);
     678              :     }
     679              : 
     680          335 :     for (int i = 0; i < 4; ++i) {
     681          268 :         readInt();
     682          268 :         readWordPascalString();
     683              :     }
     684           67 : }
     685              : 
     686              : //---------------------------------------------------------
     687              : //   createSlide
     688              : //---------------------------------------------------------
     689              : 
     690           16 : void GuitarPro::createSlide(int sl, ChordRest* cr, int staffIdx, Note* note)
     691              : {
     692              :     UNUSED(note);
     693              :     // shift / legato slide
     694           16 :     if (sl == SHIFT_SLIDE || sl == LEGATO_SLIDE) {
     695            0 :         Segment* prevSeg = cr->segment()->prev1(SegmentType::ChordRest);
     696            0 :         EngravingItem* prevElem = prevSeg ? prevSeg->element(cr->track()) : nullptr;
     697            0 :         if (prevElem && prevElem->isChord()) {
     698            0 :             Chord* prevChord = toChord(prevElem);
     699            0 :             Chord* chord = toChord(cr);
     700              :             /** TODO we should not just take the top notes here
     701              :             * but the /correct/ notes need to check whether GP
     702              :             * supports multi-note gliss. I think it can in modern
     703              :             * versions */
     704            0 :             Note* startNote = prevChord->upNote();
     705            0 :             Note* endNote = chord->upNote();
     706              : 
     707            0 :             Glissando* s = mu::engraving::Factory::createGlissando(startNote);
     708            0 :             s->setStartElement(startNote);
     709            0 :             s->setTick(prevSeg->tick());
     710            0 :             s->setTrack(startNote->track());
     711            0 :             s->setEndElement(endNote);
     712            0 :             s->setTick2(chord->segment()->tick());
     713            0 :             s->setTrack2(endNote->track());
     714            0 :             s->setText(u"");
     715            0 :             s->setGlissandoType(GlissandoType::STRAIGHT);
     716            0 :             s->setGlissandoShift(sl == SHIFT_SLIDE);
     717            0 :             s->setGlissandoStyle(s->part()->instrument(s->tick())->glissandoStyle());
     718            0 :             score->addElement(s);
     719              : 
     720            0 :             if (sl == LEGATO_SLIDE) {
     721            0 :                 createSlur(true, staffIdx, prevChord);
     722              :             }
     723              :         }
     724              : 
     725            0 :         if (sl == LEGATO_SLIDE) {
     726            0 :             createSlur(false, staffIdx, cr);
     727              :         }
     728              :     }
     729              :     // slide out downwards (fall)
     730           16 :     if (sl & SLIDE_OUT_DOWN) {
     731            4 :         ChordLine* sld = Factory::createChordLine(mu::engraving::toChord(cr));
     732            4 :         sld->setChordLineType(ChordLineType::FALL);
     733            4 :         cr->add(sld);
     734              :     }
     735              :     // slide out upwards (doit)
     736           16 :     if (sl & SLIDE_OUT_UP) {
     737            4 :         ChordLine* slu = Factory::createChordLine(mu::engraving::toChord(cr));
     738            4 :         slu->setChordLineType(ChordLineType::DOIT);
     739            4 :         cr->add(slu);
     740              :     }
     741              :     // slide in from below (plop)
     742           16 :     if (sl & SLIDE_IN_BELOW) {
     743            4 :         ChordLine* slb = Factory::createChordLine(mu::engraving::toChord(cr));
     744            4 :         slb->setChordLineType(ChordLineType::PLOP);
     745            4 :         cr->add(slb);
     746              :     }
     747              :     // slide in from above (scoop)
     748           16 :     if (sl & SLIDE_IN_ABOVE) {
     749            4 :         ChordLine* sla = Factory::createChordLine(mu::engraving::toChord(cr));
     750            4 :         sla->setChordLineType(ChordLineType::SCOOP);
     751            4 :         cr->add(sla);
     752              :     }
     753           16 : }
     754              : 
     755              : //---------------------------------------------------------
     756              : //   readChannels
     757              : //---------------------------------------------------------
     758              : 
     759           67 : void GuitarPro::readChannels()
     760              : {
     761         4355 :     for (int i = 0; i < GP_MAX_TRACK_NUMBER * 2; ++i) {
     762         4288 :         channelDefaults[i].patch   = readInt();
     763         4288 :         channelDefaults[i].volume  = readUInt8() * 8 - 1;
     764         4288 :         channelDefaults[i].pan     = readUInt8() * 8 - 1;
     765         4288 :         channelDefaults[i].chorus  = readUInt8() * 8 - 1;
     766         4288 :         channelDefaults[i].reverb  = readUInt8() * 8 - 1;
     767         4288 :         channelDefaults[i].phase   = readUInt8() * 8 - 1;
     768         4288 :         channelDefaults[i].tremolo = readUInt8() * 8 - 1;
     769              : 
     770              :         // defaults of 255, or any value above 127, are set to 0 (Musescore range is 0-127)
     771         4288 :         if (channelDefaults[i].patch > 127) {
     772            0 :             channelDefaults[i].patch = 0;
     773              :         }
     774         4288 :         if (channelDefaults[i].volume > 127) {
     775         2470 :             channelDefaults[i].volume = 0;
     776              :         }
     777         4288 :         if (channelDefaults[i].pan > 127) {
     778         2470 :             channelDefaults[i].pan = 0;
     779              :         }
     780         4288 :         if (channelDefaults[i].chorus > 127) {
     781         4288 :             channelDefaults[i].chorus = 0;
     782              :         }
     783         4288 :         if (channelDefaults[i].reverb > 127) {
     784         4288 :             channelDefaults[i].reverb = 0;
     785              :         }
     786         4288 :         if (channelDefaults[i].phase > 127) {
     787         4288 :             channelDefaults[i].phase = 0;
     788              :         }
     789         4288 :         if (channelDefaults[i].tremolo > 127) {
     790         4288 :             channelDefaults[i].tremolo = 0;
     791              :         }
     792              : 
     793              :         // skip over blank information included for backwards compatibility with 3.0
     794         4288 :         skip(2);
     795              :     }
     796           67 : }
     797              : 
     798              : //---------------------------------------------------------
     799              : //   len2fraction
     800              : //---------------------------------------------------------
     801              : 
     802         1083 : Fraction GuitarPro::len2fraction(int len)
     803              : {
     804         1083 :     Fraction l;
     805         1083 :     switch (len) {
     806           24 :     case -2: l.set(1, 1);
     807           24 :         break;
     808          115 :     case -1: l.set(1, 2);
     809          115 :         break;
     810          778 :     case  0: l.set(1, 4);
     811          778 :         break;
     812          119 :     case  1: l.set(1, 8);
     813          119 :         break;
     814           47 :     case  2: l.set(1, 16);
     815           47 :         break;
     816            0 :     case  3: l.set(1, 32);
     817            0 :         break;
     818            0 :     case  4: l.set(1, 64);
     819            0 :         break;
     820            0 :     case  5: l.set(1, 128);
     821            0 :         break;
     822              :     // set to len - in some cases we get whacky numbers for this (40, 28...)
     823            0 :     default:
     824            0 :         l.set(1, len);
     825              :     }
     826         1083 :     return l;
     827              : }
     828              : 
     829              : //---------------------------------------------------------
     830              : //   readMixChange
     831              : //---------------------------------------------------------
     832              : 
     833            1 : bool GuitarPro::readMixChange(Measure* measure)
     834              : {
     835              :     /*char patch   =*/
     836            1 :     readChar();
     837            1 :     signed char volume  = readChar();
     838            1 :     signed char pan     = readChar();
     839            1 :     signed char chorus  = readChar();
     840            1 :     signed char reverb  = readChar();
     841            1 :     signed char phase   = readChar();
     842            1 :     signed char tremolo = readChar();
     843            1 :     int temp    = readInt();
     844              : 
     845            1 :     if (volume >= 0) {
     846            0 :         readChar();
     847              :     }
     848            1 :     if (pan >= 0) {
     849            0 :         readChar();
     850              :     }
     851            1 :     if (chorus >= 0) {
     852            0 :         readChar();
     853              :     }
     854            1 :     if (reverb >= 0) {
     855            0 :         readChar();
     856              :     }
     857            1 :     if (phase >= 0) {
     858            0 :         readChar();
     859              :     }
     860            1 :     if (tremolo >= 0) {
     861            0 :         readChar();
     862              :     }
     863            1 :     if (temp >= 0) {
     864            1 :         if (temp != previousTempo) {
     865            1 :             previousTempo = temp;
     866            1 :             setTempo(temp, measure);
     867              :         }
     868            1 :         readChar();
     869              :     }
     870            1 :     return true;
     871              : }
     872              : 
     873              : //---------------------------------------------------------
     874              : //   createMeasures
     875              : //---------------------------------------------------------
     876              : 
     877           67 : void GuitarPro::createMeasures()
     878              : {
     879           67 :     Fraction tick = Fraction(0, 1);
     880           67 :     Fraction ts;
     881           67 :     LOGD("measures %zu bars.size %zu", measures, bars.size());
     882              : 
     883              :     //      for (int i = 0; i < measures; ++i) {
     884          278 :     for (size_t i = 0; i < bars.size(); ++i) {     // ?? (ws)
     885          211 :         Fraction nts = bars[i].timesig;
     886          211 :         Measure* m = Factory::createMeasure(score);
     887          211 :         m->setTick(tick);
     888          211 :         m->setTimesig(nts);
     889          211 :         m->setTicks(nts);
     890              : 
     891          211 :         if (i == 0 || ts.numerator() != nts.numerator() || ts.denominator() != nts.denominator()) {
     892          139 :             for (size_t staffIdx = 0; staffIdx < staves; ++staffIdx) {
     893           72 :                 const Staff* staff = score->staff(staffIdx);
     894           72 :                 const StaffType* staffType = staff->staffType(Fraction(0, 1));             // at tick 0
     895           72 :                 if (staffType->genTimesig()) {
     896           72 :                     Segment* s = m->getSegment(SegmentType::TimeSig, tick);
     897           72 :                     TimeSig* t = Factory::createTimeSig(s);
     898           72 :                     t->setTrack(staffIdx * VOICES);
     899           72 :                     t->setSig(nts);
     900           72 :                     s->add(t);
     901              :                 }
     902              :             }
     903              :         }
     904          211 :         if (i == 0 || (bars[i].keysig != GP_INVALID_KEYSIG)) {
     905          259 :             for (size_t staffIdx = 0; staffIdx < staves; ++staffIdx) {
     906          132 :                 int keysig = bars[i].keysig != GP_INVALID_KEYSIG ? bars[i].keysig : key;
     907          132 :                 if (tick.isZero() || (int)score->staff(staffIdx)->key(tick) != (int)Key(keysig)) {
     908          132 :                     Segment* s = m->getSegment(SegmentType::KeySig, tick);
     909          132 :                     KeySig* t = Factory::createKeySig(s);
     910          132 :                     t->setKey(Key(keysig));
     911          132 :                     t->setTrack(staffIdx * VOICES);
     912          132 :                     s->add(t);
     913              :                 }
     914              :             }
     915              :         }
     916          211 :         readVolta(&bars[i].volta, m);
     917          211 :         m->setRepeatEnd(bars[i].repeatFlags == mu::engraving::Repeat::END);
     918          211 :         m->setRepeatStart(bars[i].repeatFlags == mu::engraving::Repeat::START);
     919          211 :         m->setRepeatJump(bars[i].repeatFlags == mu::engraving::Repeat::JUMP);
     920              :         //            m->setRepeatFlags(bars[i].repeatFlags);
     921          211 :         m->setRepeatCount(bars[i].repeats);           // supported in gp5
     922              : 
     923          211 :         if (bars[i].repeatFlags == (mu::engraving::Repeat::START | mu::engraving::Repeat::END)) {
     924            0 :             m->setRepeatEnd(true);
     925            0 :             m->setRepeatStart(true);
     926            0 :             voltaSequence = 1;
     927              :         }
     928              :         // reset the volta sequence if we have an opening repeat
     929          211 :         if (bars[i].repeatFlags == mu::engraving::Repeat::START) {
     930            1 :             voltaSequence = 1;
     931              :         }
     932              :         // otherwise, if we see an end repeat symbol, only reset if the bar after it does not contain a volta
     933          210 :         else if (bars[i].repeatFlags == mu::engraving::Repeat::END && i < bars.size() - 1) {
     934            3 :             if (bars[i + 1].volta.voltaInfo.size() == 0) {
     935            1 :                 voltaSequence = 1;              // reset  the volta count
     936              :             }
     937              :         }
     938              : 
     939          211 :         score->measures()->append(m);
     940          211 :         tick += nts;
     941          211 :         ts = nts;
     942              :     }
     943           67 : }
     944              : 
     945              : //---------------------------------------------------------
     946              : //   applyBeatEffects
     947              : //---------------------------------------------------------
     948              : 
     949          797 : void GuitarPro::applyBeatEffects(Chord* chord, int beatEffect, bool& hasVibratoLeftHand, bool& hasVibratoWTremBar)
     950              : {
     951              :     /* slap/pop implemented as text until SMuFL has
     952              :      * specifications and we can add them to fonts. Note that
     953              :      * slap/pop are just added to the top note in the chord,
     954              :      * technically these can be applied to individual notes on the
     955              :      * UI, but Guitar Pro has no way to express that on the
     956              :      * score. To get the same result, we should just add the marking
     957              :      * to above the top note.
     958              :      */
     959          797 :     if (beatEffect == 1) {
     960            1 :         if (version > 300) {
     961            1 :             addTap(chord);
     962              :         } else {
     963            0 :             hasVibratoLeftHand = true;
     964              :         }
     965          796 :     } else if (beatEffect == 2) {
     966            4 :         addSlap(chord->upNote());
     967          792 :     } else if (beatEffect == 3) {
     968            4 :         addPop(chord->upNote());
     969          788 :     } else if (beatEffect == 4) {
     970            2 :         if (version >= 400) {
     971            2 :             Articulation* a = Factory::createArticulation(chord);
     972            2 :             a->setSymId(SymId::guitarFadeIn);
     973            2 :             a->setAnchor(ArticulationAnchor::TOP);
     974            2 :             a->setPropertyFlags(Pid::ARTICULATION_ANCHOR, PropertyFlags::UNSTYLED);
     975            2 :             chord->add(a);
     976              :         }
     977              :         //TODO-ws       else for (auto n : chord->notes())
     978              :         //          n->setHarmonic(true);
     979          786 :     } else if (beatEffect == 5) {
     980            2 :         Articulation* a = Factory::createArticulation(chord);
     981            2 :         a->setSymId(SymId::stringsUpBow);
     982            2 :         chord->add(a);
     983          784 :     } else if (beatEffect == 6) {
     984            2 :         Articulation* art = Factory::createArticulation(chord);
     985            2 :         art->setSymId(SymId::stringsDownBow);
     986            2 :         chord->add(art);
     987          782 :     } else if (beatEffect == 7) {
     988            6 :         hasVibratoWTremBar = true;
     989              :     }
     990          797 : }
     991              : 
     992              : #ifdef _MSC_VER
     993              : #pragma optimize("", on)
     994              : #endif
     995              : 
     996              : //---------------------------------------------------------
     997              : //   read
     998              : //---------------------------------------------------------
     999              : 
    1000            0 : bool GuitarPro1::read(IODevice* io)
    1001              : {
    1002            0 :     m_continiousElementsBuilder = std::make_unique<ContiniousElementsBuilder>(score);
    1003            0 :     m_guitarBendImporter = std::make_unique<GuitarBendImporter>(score);
    1004              : 
    1005            0 :     f      = io;
    1006            0 :     curPos = 30;
    1007              : 
    1008            0 :     title  = readDelphiString();
    1009            0 :     artist = readDelphiString();
    1010            0 :     readDelphiString();
    1011              : 
    1012            0 :     int temp = readInt();
    1013            0 :     /*uint8_t num =*/ readUInt8();        // Shuffle rhythm feel
    1014              : 
    1015              :     // int octave = 0;
    1016            0 :     key    = 0;
    1017            0 :     if (version > 102) {
    1018            0 :         key = readInt();        // key
    1019              :     }
    1020            0 :     staves  = version > 102 ? 8 : 1;
    1021            0 :     slurs.resize(staves, nullptr);
    1022              : 
    1023              :     //
    1024              :     // create a part for every staff
    1025              :     //
    1026            0 :     for (size_t staffIdx = 0; staffIdx < staves; ++staffIdx) {
    1027            0 :         Part* part = new Part(score);
    1028            0 :         Staff* s = Factory::createStaff(part);
    1029              : 
    1030            0 :         score->appendStaff(s);
    1031            0 :         score->appendPart(part);
    1032              :     }
    1033              : 
    1034            0 :     for (size_t i = 0; i < staves; ++i) {
    1035              :         int tuning[GP_MAX_STRING_NUMBER];
    1036              : 
    1037            0 :         int strings  = version > 101 ? readInt() : 6;
    1038            0 :         if (strings <= 0 || strings > GP_MAX_STRING_NUMBER) {
    1039            0 :             return false;
    1040              :         }
    1041              : 
    1042            0 :         for (int j = 0; j < strings; ++j) {
    1043            0 :             tuning[j] = readInt();
    1044              :         }
    1045            0 :         std::vector<int> tuning2(strings);
    1046            0 :         for (int k = 0; k < strings; ++k) {
    1047            0 :             tuning2[strings - k - 1] = tuning[k];
    1048              :         }
    1049              : 
    1050            0 :         int frets = 32;       // TODO
    1051            0 :         bool useFlats = createTuningString(strings, &tuning2[0]);
    1052            0 :         StringData stringData(frets, strings, &tuning2[0], useFlats);
    1053            0 :         Part* part = score->staff(i)->part();
    1054            0 :         Instrument* instr = part->instrument();
    1055            0 :         instr->setStringData(stringData);
    1056            0 :         instr->setSingleNoteDynamics(false);
    1057            0 :     }
    1058              : 
    1059            0 :     measures = readInt();
    1060              : 
    1061            0 :     Fraction ts;
    1062            0 :     Fraction tick = { 0, 1 };
    1063            0 :     for (size_t i = 0; i < measures; ++i) {
    1064            0 :         Fraction nts = bars[i].timesig;
    1065            0 :         Measure* m = Factory::createMeasure(score);
    1066            0 :         m->setTick(tick);
    1067            0 :         m->setTimesig(nts);
    1068            0 :         m->setTicks(nts);
    1069              : 
    1070            0 :         if (i == 0 || ts != nts) {
    1071            0 :             for (size_t staffIdx = 0; staffIdx < staves; ++staffIdx) {
    1072            0 :                 Segment* s = m->getSegment(SegmentType::TimeSig, tick);
    1073            0 :                 TimeSig* t = Factory::createTimeSig(s);
    1074            0 :                 t->setTrack(staffIdx * VOICES);
    1075            0 :                 t->setSig(nts);
    1076            0 :                 s->add(t);
    1077              :             }
    1078              :         }
    1079              : 
    1080            0 :         score->measures()->append(m);
    1081            0 :         tick += nts;
    1082            0 :         ts = nts;
    1083              :     }
    1084              : 
    1085            0 :     previousTempo = temp;
    1086            0 :     Measure* measure = score->firstMeasure();
    1087            0 :     bool mixChange = false;
    1088            0 :     for (size_t bar = 0; bar < measures; ++bar, measure = measure->nextMeasure()) {
    1089            0 :         const GpBar& gpbar = bars[bar];
    1090              : 
    1091            0 :         if (!gpbar.marker.isEmpty()) {
    1092            0 :             Segment* segment = measure->getSegment(SegmentType::ChordRest, measure->tick());
    1093            0 :             RehearsalMark* s = new RehearsalMark(segment);
    1094            0 :             s->setPlainText(gpbar.marker.trimmed());
    1095            0 :             s->setTrack(0);
    1096            0 :             segment->add(s);
    1097              :         }
    1098            0 :         std::vector<Tuplet*> tuplets(staves);
    1099            0 :         for (size_t staffIdx = 0; staffIdx < staves; ++staffIdx) {
    1100            0 :             tuplets[staffIdx] = 0;
    1101              :         }
    1102              : 
    1103            0 :         for (size_t staffIdx = 0; staffIdx < staves; ++staffIdx) {
    1104            0 :             Fraction measureLen = { 0, 1 };
    1105            0 :             track_idx_t track = staffIdx * VOICES;
    1106            0 :             Fraction fraction  = measure->tick();
    1107            0 :             int beats = readInt();
    1108            0 :             for (int beat = 0; beat < beats; ++beat) {
    1109              :                 //                        int pause = 0;
    1110            0 :                 uint8_t beatBits = readUInt8();
    1111            0 :                 bool dotted = beatBits & BEAT_DOTTED;
    1112            0 :                 if (beatBits & BEAT_PAUSE) {
    1113              :                     /*pause =*/
    1114            0 :                     readUInt8();
    1115              :                 }
    1116            0 :                 int len = readChar();
    1117            0 :                 int tuple = 0;
    1118            0 :                 if (beatBits & BEAT_TUPLET) {
    1119            0 :                     tuple = readInt();
    1120              :                 }
    1121            0 :                 Segment* segment = measure->getSegment(SegmentType::ChordRest, fraction);
    1122            0 :                 if (beatBits & BEAT_CHORD) {
    1123            0 :                     int numStrings = static_cast<int>(score->staff(staffIdx)->part()->instrument()->stringData()->strings());
    1124            0 :                     int header = readUInt8();
    1125            0 :                     String name;
    1126            0 :                     if ((header & 1) == 0) {
    1127            0 :                         name = readDelphiString();
    1128            0 :                         readChord(segment, static_cast<int>(track), numStrings, name, false);
    1129              :                     } else {
    1130            0 :                         skip(25);
    1131            0 :                         name = readPascalString(34);
    1132            0 :                         readChord(segment, static_cast<int>(track), numStrings, name, true);
    1133            0 :                         skip(36);
    1134              :                     }
    1135            0 :                 }
    1136            0 :                 Lyrics* lyrics = 0;
    1137            0 :                 if (beatBits & BEAT_LYRICS) {
    1138            0 :                     lyrics = Factory::createLyrics(score->dummy());
    1139            0 :                     lyrics->setPlainText(readDelphiString());
    1140              :                 }
    1141            0 :                 if (beatBits & BEAT_EFFECTS) {
    1142            0 :                     readBeatEffects(static_cast<int>(track), segment);
    1143              :                 }
    1144              : 
    1145            0 :                 if (beatBits & BEAT_MIX_CHANGE) {
    1146            0 :                     readMixChange(measure);
    1147            0 :                     mixChange = true;
    1148              :                 }
    1149              : 
    1150            0 :                 int strings = readUInt8();           // used strings mask
    1151              : 
    1152            0 :                 Fraction l = len2fraction(len);
    1153              :                 ChordRest* cr;
    1154            0 :                 if (strings) {
    1155            0 :                     cr = Factory::createChord(score->dummy());
    1156              :                 } else {
    1157            0 :                     cr = Factory::createRest(score->dummy());
    1158              :                 }
    1159            0 :                 cr->setTrack(track);
    1160            0 :                 if (lyrics) {
    1161            0 :                     cr->add(lyrics);
    1162              :                 }
    1163              : 
    1164            0 :                 TDuration d(l);
    1165            0 :                 d.setDots(dotted ? 1 : 0);
    1166              : 
    1167            0 :                 if (dotted) {
    1168            0 :                     l = l + (l * Fraction(1, 2));
    1169              :                 }
    1170              : 
    1171            0 :                 if (tuple) {
    1172            0 :                     Tuplet* tuplet = tuplets[staffIdx];
    1173            0 :                     if ((tuplet == 0) || (tuplet->elementsDuration() == tuplet->baseLen().fraction() * tuplet->ratio().numerator())) {
    1174            0 :                         tuplet = Factory::createTuplet(measure);
    1175            0 :                         tuplet->setTick(fraction);
    1176            0 :                         tuplet->setTrack(cr->track());
    1177            0 :                         tuplets[staffIdx] = tuplet;
    1178            0 :                         setTuplet(tuplet, tuple);
    1179              :                     }
    1180            0 :                     tuplet->setTrack(track);
    1181            0 :                     tuplet->setBaseLen(l);
    1182            0 :                     tuplet->setTicks(l * tuplet->ratio().denominator());
    1183            0 :                     cr->setTuplet(tuplet);
    1184            0 :                     tuplet->add(cr);            //TODOxxx
    1185              :                 }
    1186              : 
    1187            0 :                 cr->setTicks(l);
    1188            0 :                 cr->setDurationType(d);
    1189            0 :                 segment->add(cr);
    1190            0 :                 Staff* staff = cr->staff();
    1191            0 :                 int numStrings = static_cast<int>(staff->part()->instrument()->stringData()->strings());
    1192            0 :                 ReadNoteResult readResult;
    1193            0 :                 bool hasLetRing = false;
    1194            0 :                 bool hasPalmMute = false;
    1195            0 :                 for (int i = 6; i >= 0; --i) {
    1196            0 :                     if (strings & (1 << i) && ((6 - i) < numStrings)) {
    1197            0 :                         Note* note = Factory::createNote(toChord(cr));
    1198            0 :                         toChord(cr)->add(note);
    1199            0 :                         readResult = readNote(6 - i, note);
    1200            0 :                         hasLetRing = readResult.letRing || hasLetRing;
    1201            0 :                         hasPalmMute = readResult.palmMute || hasPalmMute;
    1202            0 :                         note->setTpcFromPitch();
    1203              :                     }
    1204              :                 }
    1205            0 :                 restsForEmptyBeats(segment, measure, cr, l, static_cast<int>(track), fraction);
    1206            0 :                 fraction += cr->actualTicks();
    1207            0 :                 measureLen += cr->actualTicks();
    1208            0 :                 addLetRing(cr, hasLetRing);
    1209            0 :                 addPalmMute(cr, hasPalmMute);
    1210              :             }
    1211            0 :             if (measureLen < measure->ticks()) {
    1212            0 :                 score->setRest(fraction, track, measure->ticks() - measureLen, false, nullptr, false);
    1213            0 :                 m_continiousElementsBuilder->notifyUncompletedMeasure();
    1214              :             }
    1215              :         }
    1216            0 :         if (bar == 1 && !mixChange) {
    1217            0 :             setTempo(temp, score->firstMeasure());
    1218              :         }
    1219            0 :     }
    1220              : 
    1221            0 :     m_continiousElementsBuilder->addElementsToScore();
    1222            0 :     addTunings();
    1223            0 :     return true;
    1224              : }
    1225              : 
    1226              : //---------------------------------------------------------
    1227              : //   setTempo
    1228              : //---------------------------------------------------------
    1229              : 
    1230          359 : void GuitarPro::setTempo(int newTempo, Measure* measure)
    1231              : {
    1232          359 :     if (!measure || measure == last_measure) {
    1233          308 :         return;
    1234              :     }
    1235              : 
    1236           51 :     last_measure = measure;
    1237              : 
    1238           51 :     if (last_tempo != newTempo) {
    1239           51 :         Segment* segment = measure->getSegment(SegmentType::ChordRest, measure->tick());
    1240           51 :         TempoText* tt = Factory::createTempoText(segment);
    1241           51 :         tt->setTempo(BeatsPerSecond::fromBPM(newTempo));
    1242           51 :         tt->setXmlText(String(u"<sym>metNoteQuarterUp</sym> = %1").arg(newTempo));
    1243           51 :         tt->setTrack(0);
    1244           51 :         segment->add(tt);
    1245           51 :         tempo = newTempo;
    1246           51 :         last_tempo = newTempo;
    1247              :     }
    1248              : }
    1249              : 
    1250              : //---------------------------------------------------------
    1251              : //   readChord
    1252              : //---------------------------------------------------------
    1253              : 
    1254            4 : void GuitarPro::readChord(Segment* seg, int track, int numStrings, String name, bool gpHeader)
    1255              : {
    1256            4 :     int firstFret = readInt();
    1257            4 :     if (firstFret || gpHeader) {
    1258              :         /// showing chord instead of fret diagrams
    1259              :         if (true) {
    1260            4 :             StaffText* staffText = Factory::createStaffText(seg);
    1261            4 :             staffText->setTrack(track);
    1262            4 :             staffText->setPlainText(name);
    1263            4 :             seg->add(staffText);
    1264            4 :             skip(4 * (gpHeader ? 7 : 6));
    1265              :         } else {
    1266              :             FretDiagram* fret = Factory::createFretDiagram(seg);
    1267              :             fret->setTrack(track);
    1268              :             fret->setStrings(numStrings);
    1269              :             fret->setFretOffset(firstFret - 1);
    1270              :             for (int i = 0; i < (gpHeader ? 7 : 6); ++i) {
    1271              :                 int currentFret =  readInt();
    1272              :                 // read the frets and add them to the fretboard
    1273              :                 // subtract 1 extra from numStrings as we count from 0
    1274              :                 if (i > numStrings - 1) {
    1275              :                 } else if (currentFret > 0) {
    1276              :                     fret->setDot(numStrings - 1 - i, currentFret - firstFret + 1, true);
    1277              :                 } else if (currentFret == 0) {
    1278              :                     fret->setDot(numStrings - 1 - i, 0, true);
    1279              :                     fret->setMarker(numStrings - 1 - i, FretMarkerType::CIRCLE);
    1280              :                 } else if (currentFret == -1) {
    1281              :                     fret->setDot(numStrings - 1 - i, 0, true);
    1282              :                     fret->setMarker(numStrings - 1 - i, FretMarkerType::CROSS);
    1283              :                 }
    1284              :             }
    1285              :             seg->add(fret);
    1286              :             if (!name.isEmpty()) {
    1287              :                 Harmony* harmony = new Harmony(seg);
    1288              :                 harmony->setHarmony(name);
    1289              :                 harmony->setTrack(track);
    1290              :                 fret->add(harmony);
    1291              :             }
    1292              :         }
    1293            4 :     } else if (!name.isEmpty()) {
    1294            0 :         Harmony* harmony = new Harmony(seg);
    1295            0 :         harmony->setHarmony(name);
    1296            0 :         harmony->setTrack(track);
    1297            0 :         seg->add(harmony);
    1298              :     }
    1299            4 : }
    1300              : 
    1301              : //---------------------------------------------------------
    1302              : //   restsForEmptyBeats
    1303              : //---------------------------------------------------------
    1304              : 
    1305         1083 : void GuitarPro::restsForEmptyBeats(Segment* seg, Measure* measure, ChordRest* cr, Fraction& l, int track, const Fraction& tick)
    1306              : {
    1307              :     /* this can happen as Guitar Pro versions 5 and below allows
    1308              :      * users to create empty segments. Here, we create rests and
    1309              :      * make them invisible so users get the same visual if they are
    1310              :      * at a valid tick of the score. */
    1311         1083 :     if (seg->empty()) {
    1312           27 :         if (tick < measure->endTick()) {
    1313            0 :             cr = Factory::createRest(seg);
    1314            0 :             cr->setTrack(track);
    1315            0 :             TDuration d(l);
    1316            0 :             cr->setDurationType(d);
    1317            0 :             cr->setVisible(false);
    1318            0 :             seg->add(cr);
    1319              :         } else {
    1320           27 :             measure->remove(seg);
    1321              :         }
    1322              :     }
    1323         1083 : }
    1324              : 
    1325              : //---------------------------------------------------------
    1326              : //   createSlur
    1327              : //---------------------------------------------------------
    1328              : 
    1329          934 : void GuitarPro::createSlur(bool hasSlur, staff_idx_t staffIdx, ChordRest* cr)
    1330              : {
    1331          934 :     if (hasSlur && (slurs[staffIdx] == 0)) {
    1332            4 :         Slur* slur = Factory::createSlur(score->dummy());
    1333            4 :         slur->setTrack(cr->track());
    1334            4 :         slur->setTrack2(cr->track());
    1335            4 :         slur->setTick(cr->tick());
    1336            4 :         slur->setTick2(cr->tick());
    1337            4 :         slur->setStartElement(cr);
    1338            4 :         slur->setEndElement(cr);
    1339            4 :         slurs[staffIdx] = slur;
    1340            4 :         score->addElement(slur);
    1341          930 :     } else if (slurs[staffIdx] && !hasSlur) {
    1342            3 :         Slur* s = slurs[staffIdx];
    1343            3 :         slurs[staffIdx] = 0;
    1344            3 :         s->setEndElement(cr);
    1345            3 :         s->setTick2(cr->tick());
    1346            3 :         s->setTrack2(cr->track());
    1347              :     }
    1348          934 : }
    1349              : 
    1350              : //---------------------------------------------------------
    1351              : //   read
    1352              : //---------------------------------------------------------
    1353              : 
    1354            0 : bool GuitarPro2::read(IODevice* io)
    1355              : {
    1356            0 :     m_continiousElementsBuilder = std::make_unique<ContiniousElementsBuilder>(score);
    1357            0 :     m_guitarBendImporter = std::make_unique<GuitarBendImporter>(score);
    1358              : 
    1359            0 :     f      = io;
    1360            0 :     curPos = 30;
    1361              : 
    1362            0 :     title        = readDelphiString();
    1363            0 :     subtitle     = readDelphiString();
    1364            0 :     artist       = readDelphiString();
    1365            0 :     album        = readDelphiString();
    1366            0 :     composer     = readDelphiString();
    1367            0 :     String copyright = readDelphiString();
    1368            0 :     if (!copyright.isEmpty()) {
    1369            0 :         score->setMetaTag(u"copyright", copyright);
    1370              :     }
    1371              : 
    1372            0 :     readDelphiString(); // transcriber
    1373            0 :     readDelphiString(); // instructions
    1374            0 :     int n = readInt();
    1375            0 :     for (int i = 0; i < n; ++i) {
    1376            0 :         comments.append(readDelphiString());
    1377              :     }
    1378              : 
    1379            0 :     /*uint8_t num =*/ readUInt8();        // Shuffle rhythm feel
    1380              : 
    1381            0 :     int temp = readInt();
    1382              : 
    1383              :     // int octave = 0;
    1384            0 :     /*int key =*/ readInt();      // key
    1385              : 
    1386            0 :     for (int i = 0; i < GP_MAX_TRACK_NUMBER * 2; ++i) {
    1387            0 :         channelDefaults[i].patch   = readInt();
    1388            0 :         channelDefaults[i].volume  = readUInt8() * 8 - 1;
    1389            0 :         channelDefaults[i].pan     = readUInt8() * 8 - 1;
    1390            0 :         channelDefaults[i].chorus  = readUInt8() * 8 - 1;
    1391            0 :         channelDefaults[i].reverb  = readUInt8() * 8 - 1;
    1392            0 :         channelDefaults[i].phase   = readUInt8() * 8 - 1;
    1393            0 :         channelDefaults[i].tremolo = readUInt8() * 8 - 1;
    1394            0 :         readUInt8();          // padding
    1395            0 :         readUInt8();
    1396              :     }
    1397            0 :     measures   = readInt();
    1398            0 :     staves = readInt();
    1399            0 :     initDynamics(staves);
    1400              : 
    1401            0 :     int tnumerator   = 4;
    1402            0 :     int tdenominator = 4;
    1403            0 :     for (size_t i = 0; i < measures; ++i) {
    1404            0 :         GpBar bar;
    1405            0 :         uint8_t barBits = readUInt8();
    1406            0 :         if (barBits & SCORE_TIMESIG_NUMERATOR) {
    1407            0 :             tnumerator = readUInt8();
    1408              :         }
    1409            0 :         if (barBits & SCORE_TIMESIG_DENOMINATOR) {
    1410            0 :             tdenominator = readUInt8();
    1411              :         }
    1412            0 :         if (barBits & SCORE_REPEAT_START) {
    1413            0 :             bar.repeatFlags = bar.repeatFlags | mu::engraving::Repeat::START;
    1414              :         }
    1415            0 :         if (barBits & SCORE_REPEAT_END) {
    1416            0 :             bar.repeatFlags = bar.repeatFlags | mu::engraving::Repeat::END;
    1417            0 :             bar.repeats = readUInt8() + 1;
    1418              :         }
    1419            0 :         if (barBits & SCORE_VOLTA) {
    1420            0 :             uint8_t voltaNumber = readUInt8();
    1421            0 :             while (voltaNumber > 0) {
    1422              :                 // volta information is represented as a binary number
    1423            0 :                 bar.volta.voltaType = GP_VOLTA_BINARY;
    1424            0 :                 bar.volta.voltaInfo.push_back(voltaNumber & 1);
    1425            0 :                 voltaNumber >>= 1;
    1426              :             }
    1427              :         }
    1428            0 :         if (barBits & SCORE_MARKER) {
    1429            0 :             bar.marker = readDelphiString();           // new section?
    1430            0 :             /*int color =*/ readInt();          // color?
    1431              :         }
    1432            0 :         if (barBits & SCORE_KEYSIG) {
    1433            0 :             bar.keysig = readUInt8();
    1434            0 :             /*uint8_t c    =*/ readUInt8();              // minor
    1435              :         }
    1436            0 :         if (barBits & SCORE_DOUBLE_BAR) {
    1437            0 :             bar.barLine = BarLineType::DOUBLE;
    1438              :         }
    1439            0 :         bar.timesig = Fraction(tnumerator, tdenominator);
    1440            0 :         bars.push_back(bar);
    1441            0 :     }
    1442              : 
    1443              :     //
    1444              :     // create a part for every staff
    1445              :     //
    1446            0 :     for (size_t staffIdx = 0; staffIdx < staves; ++staffIdx) {
    1447            0 :         Part* part = new Part(score);
    1448            0 :         Staff* s = Factory::createStaff(part);
    1449              : 
    1450            0 :         score->appendStaff(s);
    1451            0 :         score->appendPart(part);
    1452              :     }
    1453              : 
    1454            0 :     Fraction ts;
    1455            0 :     Fraction tick = { 0, 1 };
    1456            0 :     for (size_t i = 0; i < measures; ++i) {
    1457            0 :         Fraction nts = bars[i].timesig;
    1458            0 :         Measure* m = Factory::createMeasure(score);
    1459            0 :         m->setTick(tick);
    1460            0 :         m->setTimesig(nts);
    1461            0 :         m->setTicks(nts);
    1462              : 
    1463            0 :         if (i == 0 || ts != nts) {
    1464            0 :             for (size_t staffIdx = 0; staffIdx < staves; ++staffIdx) {
    1465            0 :                 Segment* s = m->getSegment(SegmentType::TimeSig, tick);
    1466            0 :                 TimeSig* t = Factory::createTimeSig(s);
    1467            0 :                 t->setTrack(staffIdx * VOICES);
    1468            0 :                 t->setSig(nts);
    1469            0 :                 s->add(t);
    1470              :             }
    1471              :         }
    1472              : 
    1473            0 :         score->measures()->append(m);
    1474            0 :         tick += nts;
    1475            0 :         ts = nts;
    1476              :     }
    1477              : 
    1478            0 :     for (size_t i = 0; i < staves; ++i) {
    1479              :         int tuning[GP_MAX_STRING_NUMBER];
    1480              : 
    1481            0 :         uint8_t c      = readUInt8();       // simulations bitmask
    1482            0 :         if (c & 0x2) {                    // 12 stringed guitar
    1483              :         }
    1484            0 :         if (c & 0x4) {                    // banjo track
    1485              :         }
    1486            0 :         String name = readPascalString(40);
    1487            0 :         int strings  = readInt();
    1488            0 :         if (strings <= 0 || strings > GP_MAX_STRING_NUMBER) {
    1489            0 :             return false;
    1490              :         }
    1491            0 :         for (int j = 0; j < strings; ++j) {
    1492            0 :             tuning[j] = readInt();
    1493              :         }
    1494            0 :         for (int j = strings; j < GP_MAX_STRING_NUMBER; ++j) {
    1495            0 :             readInt();
    1496              :         }
    1497            0 :         /*int midiPort     =*/ readInt();     //  - 1;
    1498            0 :         int midiChannel  = readInt() - 1;
    1499            0 :         if (midiChannel < 0 || midiChannel >= static_cast<int>(channelDefaults.size())) {
    1500            0 :             LOGE() << "midiChannel " << midiChannel << " out of range 0-" << channelDefaults.size();
    1501            0 :             return false;
    1502              :         }
    1503            0 :         /*int midiChannel2 =*/ readInt();     // - 1;
    1504            0 :         int frets        = readInt();
    1505            0 :         int capo         = readInt();
    1506            0 :         /*int color        =*/ readInt();
    1507              : 
    1508            0 :         std::vector<int> tuning2(strings);
    1509              :         //int tuning2[strings];
    1510            0 :         for (int k = 0; k < strings; ++k) {
    1511            0 :             tuning2[strings - k - 1] = tuning[k];
    1512              :         }
    1513            0 :         bool useFlats = createTuningString(strings, &tuning2[0]);
    1514            0 :         StringData stringData(frets, strings, &tuning2[0], useFlats);
    1515            0 :         Part* part = score->staff(i)->part();
    1516            0 :         Instrument* instr = part->instrument();
    1517            0 :         instr->setStringData(stringData);
    1518            0 :         instr->setSingleNoteDynamics(false);
    1519            0 :         part->setPlainLongName(name);
    1520              : 
    1521              :         //
    1522              :         // determine clef
    1523              :         //
    1524            0 :         Staff* staff = score->staff(i);
    1525            0 :         int patch = channelDefaults[midiChannel].patch;
    1526            0 :         ClefType clefId = ClefType::G;
    1527            0 :         if (midiChannel == GP_DEFAULT_PERCUSSION_CHANNEL) {
    1528            0 :             clefId = ClefType::PERC;
    1529            0 :             StaffTypes type = StaffTypes::PERC_DEFAULT;
    1530            0 :             if (auto it = drumset::PERC_STAFF_LINES_FROM_INSTRUMENT.find(name.toStdString());
    1531            0 :                 it != drumset::PERC_STAFF_LINES_FROM_INSTRUMENT.end()) {
    1532            0 :                 drumset::initGuitarProPercussionSet(it->second);
    1533            0 :                 drumset::setInstrumentDrumset(instr, it->second);
    1534            0 :                 switch (it->second.numLines) {
    1535            0 :                 case 1:
    1536            0 :                     type = StaffTypes::PERC_1LINE;
    1537            0 :                     break;
    1538            0 :                 case 2:
    1539            0 :                     type = StaffTypes::PERC_2LINE;
    1540            0 :                     break;
    1541            0 :                 case 3:
    1542            0 :                     type = StaffTypes::PERC_3LINE;
    1543            0 :                     break;
    1544            0 :                 default:
    1545            0 :                     type = StaffTypes::PERC_DEFAULT;
    1546            0 :                     break;
    1547              :                 }
    1548              :             } else {
    1549            0 :                 drumset::initGuitarProDrumset();
    1550            0 :                 instr->setDrumset(drumset::gpDrumset);
    1551              :             }
    1552            0 :             staff->setStaffType(Fraction(0, 1), *StaffType::preset(type));
    1553              :         } else {
    1554            0 :             clefId = defaultClef(patch);
    1555              :         }
    1556            0 :         Measure* measure = score->firstMeasure();
    1557            0 :         Segment* segment = measure->getSegment(SegmentType::HeaderClef, Fraction(0, 1));
    1558            0 :         Clef* clef = Factory::createClef(segment);
    1559            0 :         clef->setClefType(clefId);
    1560            0 :         clef->setTrack(i * VOICES);
    1561            0 :         segment->add(clef);
    1562              : 
    1563            0 :         if (capo > 0 && !engravingConfiguration()->guitarProImportExperimental()) {
    1564            0 :             Segment* s = measure->getSegment(SegmentType::ChordRest, measure->tick());
    1565            0 :             const size_t track = i * VOICES;
    1566              : 
    1567            0 :             CapoParams params;
    1568            0 :             params.active = true;
    1569            0 :             params.transposeMode = CapoParams::TransposeMode::TAB_ONLY;
    1570            0 :             params.fretPosition = capo;
    1571              : 
    1572            0 :             Capo* capoEl = Factory::createCapo(s);
    1573            0 :             capoEl->setTrack(track);
    1574            0 :             capoEl->setParams(params);
    1575            0 :             s->add(capoEl);
    1576              : 
    1577            0 :             staff->insertCapoParams({ 0, 1 }, params, true);
    1578            0 :         }
    1579              : 
    1580            0 :         InstrChannel* ch = instr->channel(0);
    1581            0 :         if (midiChannel == int(StaffTypes::PERC_DEFAULT)) {
    1582            0 :             ch->setProgram(0);
    1583            0 :             ch->setBank(128);
    1584              :         } else {
    1585            0 :             ch->setProgram(patch);
    1586            0 :             ch->setBank(0);
    1587              :         }
    1588            0 :         ch->setVolume(channelDefaults[midiChannel].volume);
    1589            0 :         ch->setPan(channelDefaults[midiChannel].pan);
    1590            0 :         ch->setChorus(channelDefaults[midiChannel].chorus);
    1591            0 :         ch->setReverb(channelDefaults[midiChannel].reverb);
    1592              :         // missing: phase, tremolo
    1593            0 :     }
    1594              : 
    1595            0 :     previousTempo = temp;
    1596            0 :     Measure* measure = score->firstMeasure();
    1597            0 :     bool mixChange = false;
    1598            0 :     for (size_t bar = 0; bar < measures; ++bar, measure = measure->nextMeasure()) {
    1599            0 :         const GpBar& gpbar = bars[bar];
    1600              : 
    1601            0 :         if (!gpbar.marker.isEmpty()) {
    1602            0 :             Segment* segment = measure->getSegment(SegmentType::ChordRest, measure->tick());
    1603            0 :             RehearsalMark* s = new RehearsalMark(segment);
    1604            0 :             s->setPlainText(gpbar.marker.trimmed());
    1605            0 :             s->setTrack(0);
    1606            0 :             segment->add(s);
    1607              :         }
    1608              : 
    1609            0 :         std::vector<Tuplet*> tuplets(staves);
    1610              :         // Tuplet* tuplets[staves];
    1611            0 :         for (size_t staffIdx = 0; staffIdx < staves; ++staffIdx) {
    1612            0 :             tuplets[staffIdx] = 0;
    1613              :         }
    1614              : 
    1615            0 :         for (size_t staffIdx = 0; staffIdx < staves; ++staffIdx) {
    1616            0 :             Fraction measureLen = { 0, 1 };
    1617            0 :             track_idx_t track = staffIdx * VOICES;
    1618            0 :             Fraction fraction = measure->tick();
    1619            0 :             int beats = readInt();
    1620            0 :             for (int beat = 0; beat < beats; ++beat) {
    1621              :                 //                        int pause = 0;
    1622            0 :                 uint8_t beatBits = readUInt8();
    1623            0 :                 bool dotted = beatBits & BEAT_DOTTED;
    1624            0 :                 if (beatBits & BEAT_PAUSE) {
    1625              :                     /*pause =*/
    1626            0 :                     readUInt8();
    1627              :                 }
    1628            0 :                 int len = readChar();
    1629            0 :                 int tuple = 0;
    1630            0 :                 if (beatBits & BEAT_TUPLET) {
    1631            0 :                     tuple = readInt();
    1632              :                 }
    1633            0 :                 Segment* segment = measure->getSegment(SegmentType::ChordRest, fraction);
    1634            0 :                 if (beatBits & BEAT_CHORD) {
    1635            0 :                     int numStrings = static_cast<int>(score->staff(staffIdx)->part()->instrument()->stringData()->strings());
    1636            0 :                     int header = readUInt8();
    1637            0 :                     String name;
    1638            0 :                     if ((header & 1) == 0) {
    1639            0 :                         name = readDelphiString();
    1640            0 :                         readChord(segment, static_cast<int>(track), numStrings, name, false);
    1641              :                     } else {
    1642            0 :                         skip(25);
    1643            0 :                         name = readPascalString(34);
    1644            0 :                         readChord(segment, static_cast<int>(track), numStrings, name, true);
    1645            0 :                         skip(36);
    1646              :                     }
    1647            0 :                 }
    1648            0 :                 Lyrics* lyrics = 0;
    1649            0 :                 if (beatBits & BEAT_LYRICS) {
    1650            0 :                     String txt = readDelphiString();
    1651            0 :                     lyrics = Factory::createLyrics(score->dummy());
    1652            0 :                     lyrics->setPlainText(txt);
    1653            0 :                 }
    1654            0 :                 if (beatBits & BEAT_EFFECTS) {
    1655            0 :                     readBeatEffects(static_cast<int>(track), segment);
    1656              :                 }
    1657              : 
    1658            0 :                 if (beatBits & BEAT_MIX_CHANGE) {
    1659            0 :                     readMixChange(measure);
    1660            0 :                     mixChange = true;
    1661              :                 }
    1662              : 
    1663            0 :                 int strings = readUInt8();           // used strings mask
    1664              : 
    1665            0 :                 Fraction l = len2fraction(len);
    1666              :                 ChordRest* cr;
    1667            0 :                 if (strings) {
    1668            0 :                     cr = Factory::createChord(segment);
    1669              :                 } else {
    1670            0 :                     cr = Factory::createRest(segment);
    1671              :                 }
    1672            0 :                 cr->setTrack(track);
    1673            0 :                 if (lyrics) {
    1674            0 :                     cr->add(lyrics);
    1675              :                 }
    1676              : 
    1677            0 :                 TDuration d(l);
    1678            0 :                 d.setDots(dotted ? 1 : 0);
    1679              : 
    1680            0 :                 if (dotted) {
    1681            0 :                     l = l + (l * Fraction(1, 2));
    1682              :                 }
    1683              : 
    1684            0 :                 if (tuple) {
    1685            0 :                     Tuplet* tuplet = tuplets[staffIdx];
    1686            0 :                     if ((tuplet == 0) || (tuplet->elementsDuration() == tuplet->baseLen().fraction() * tuplet->ratio().numerator())) {
    1687            0 :                         tuplet = Factory::createTuplet(measure);
    1688            0 :                         tuplet->setTick(fraction);
    1689            0 :                         tuplet->setTrack(cr->track());
    1690            0 :                         tuplets[staffIdx] = tuplet;
    1691            0 :                         setTuplet(tuplet, tuple);
    1692              :                     }
    1693            0 :                     tuplet->setTrack(track);
    1694            0 :                     tuplet->setBaseLen(l);
    1695            0 :                     tuplet->setTicks(l * tuplet->ratio().denominator());
    1696            0 :                     cr->setTuplet(tuplet);
    1697            0 :                     tuplet->add(cr);
    1698              :                 }
    1699              : 
    1700            0 :                 cr->setTicks(l);
    1701            0 :                 cr->setDurationType(d);
    1702            0 :                 segment->add(cr);
    1703            0 :                 Staff* staff = cr->staff();
    1704            0 :                 int numStrings = static_cast<int>(staff->part()->instrument()->stringData()->strings());
    1705            0 :                 ReadNoteResult readResult;
    1706            0 :                 bool hasLetRing = false;
    1707            0 :                 bool hasPalmMute = false;
    1708            0 :                 for (int i = 6; i >= 0; --i) {
    1709            0 :                     if (strings & (1 << i) && ((6 - i) < numStrings)) {
    1710            0 :                         Note* note = Factory::createNote(toChord(cr));
    1711            0 :                         toChord(cr)->add(note);
    1712            0 :                         readResult = readNote(6 - i, note);
    1713            0 :                         hasLetRing = readResult.letRing || hasLetRing;
    1714            0 :                         hasPalmMute = readResult.palmMute || hasPalmMute;
    1715            0 :                         note->setTpcFromPitch();
    1716              :                     }
    1717              :                 }
    1718            0 :                 restsForEmptyBeats(segment, measure, cr, l, static_cast<int>(track), fraction);
    1719            0 :                 fraction += cr->actualTicks();
    1720            0 :                 measureLen += cr->actualTicks();
    1721            0 :                 addLetRing(cr, hasLetRing);
    1722            0 :                 addPalmMute(cr, hasPalmMute);
    1723              :             }
    1724            0 :             if (measureLen < measure->ticks()) {
    1725            0 :                 score->setRest(fraction, track, measure->ticks() - measureLen, false, nullptr, false);
    1726            0 :                 m_continiousElementsBuilder->notifyUncompletedMeasure();
    1727              :             }
    1728              :         }
    1729            0 :         if (bar == 1 && !mixChange) {
    1730            0 :             setTempo(temp, score->firstMeasure());
    1731              :         }
    1732            0 :     }
    1733              : 
    1734            0 :     m_continiousElementsBuilder->addElementsToScore();
    1735            0 :     addTunings();
    1736            0 :     return true;
    1737            0 : }
    1738              : 
    1739              : //---------------------------------------------------------
    1740              : //   readNote
    1741              : //---------------------------------------------------------
    1742              : 
    1743           87 : GuitarPro::ReadNoteResult GuitarPro1::readNote(int string, Note* note)
    1744              : {
    1745           87 :     ReadNoteResult readResult;
    1746           87 :     uint8_t noteBits = readUInt8();
    1747           87 :     if (noteBits & NOTE_GHOST) {
    1748            0 :         if (version == 300) {
    1749            0 :             note->setGhost(true);
    1750              :         } else {
    1751            0 :             note->setHeadGroup(NoteHeadGroup::HEAD_CROSS);
    1752            0 :             note->setGhost(true);
    1753              :         }
    1754              :     }
    1755              : 
    1756           87 :     bool tieNote = false;
    1757           87 :     uint8_t variant = 1;
    1758           87 :     if (noteBits & NOTE_DEAD) {
    1759           87 :         variant = readUInt8();
    1760           87 :         if (variant == 1) {         // normal note
    1761            5 :         } else if (variant == 2) {
    1762            2 :             tieNote = true;
    1763            3 :         } else if (variant == 3) {                   // dead notes
    1764            3 :             note->setHeadGroup(NoteHeadGroup::HEAD_CROSS);
    1765            3 :             note->setDeadNote(true);
    1766              :         } else {
    1767            0 :             LOGD("unknown note variant: %d", variant);
    1768              :         }
    1769              :     }
    1770              : 
    1771              :     //
    1772              :     // noteBits:
    1773              :     //    7 - Right hand or left hand fingering;
    1774              :     //    6 - Accentuated note
    1775              :     //    5 - Note type (rest, empty note, normal note);
    1776              :     //    4 - note dynamic;
    1777              :     //    3 - Presence of effects linked to the note;
    1778              :     //    2 - Ghost note;
    1779              :     //    1 - Dotted note;  ?
    1780              :     //    0 - Time-independent duration
    1781              : 
    1782           87 :     if (noteBits & 0x1) {                 // note != beat
    1783            0 :         int a = readUInt8();              // length
    1784            0 :         int b = readUInt8();              // t
    1785            0 :         LOGD("Time independent note len, len %d t %d", a, b);
    1786              :     }
    1787           87 :     if (noteBits & 0x2) {                 // note is dotted
    1788              :         //readUInt8();
    1789              :     }
    1790              : 
    1791           87 :     int& previousDynamic = previousDynamicByTrack[note->track()];
    1792              : 
    1793              :     // set dynamic information on note if different from previous note
    1794           87 :     if (noteBits & NOTE_DYNAMIC) {
    1795           17 :         int d = readChar();
    1796           17 :         if (previousDynamic != d) {
    1797            4 :             previousDynamic = d;
    1798            4 :             addDynamic(note, d);
    1799              :         }
    1800           70 :     } else if (previousDynamic != DEFAULT_DYNAMIC) {
    1801            4 :         previousDynamic = DEFAULT_DYNAMIC;
    1802            4 :         addDynamic(note, previousDynamic);
    1803              :     }
    1804              : 
    1805           87 :     int fretNumber = -1;
    1806           87 :     if (noteBits & NOTE_FRET) {
    1807           87 :         fretNumber = readUInt8();
    1808              :     }
    1809              : 
    1810           87 :     if (noteBits & NOTE_FINGERING) {                // fingering
    1811            0 :         int a = readUInt8();
    1812            0 :         int b = readUInt8();
    1813            0 :         LOGD("Fingering=========%d %d", a, b);
    1814              :     }
    1815           87 :     if (noteBits & BEAT_EFFECTS) {
    1816            6 :         uint8_t modMask1 = readUInt8();
    1817            6 :         uint8_t modMask2 = 0;
    1818            6 :         if (version >= 400) {
    1819            0 :             modMask2 = readUInt8();
    1820              :         }
    1821            6 :         if (modMask1 & EFFECT_BEND) {
    1822            5 :             readBend(note);
    1823              :         }
    1824            6 :         if (modMask1 & EFFECT_GRACE) {
    1825              :             // GP3 grace note
    1826            0 :             int fret = readUInt8();                  // grace fret
    1827            0 :             int dynamic = readUInt8();                  // grace dynamic
    1828            0 :             int transition = readUInt8();                  // grace transition
    1829            0 :             int duration = readUInt8();                  // grace duration
    1830              : 
    1831            0 :             int grace_len = Constants::DIVISION / 8;
    1832            0 :             if (duration == 1) {
    1833            0 :                 grace_len = Constants::DIVISION / 8;       //32nd
    1834            0 :             } else if (duration == 2) {
    1835            0 :                 grace_len = Constants::DIVISION / 6;       //24th
    1836            0 :             } else if (duration == 3) {
    1837            0 :                 grace_len = Constants::DIVISION / 4;       //16th
    1838              :             }
    1839            0 :             Note* gn = Factory::createNote(score->dummy());
    1840              : 
    1841            0 :             if (fret == 255) {
    1842            0 :                 gn->setHeadGroup(NoteHeadGroup::HEAD_CROSS);
    1843            0 :                 gn->setGhost(true);
    1844              :             }
    1845            0 :             if (fret == 255) {
    1846            0 :                 fret = 0;
    1847              :             }
    1848            0 :             gn->setFret(fret);
    1849            0 :             gn->setString(string);
    1850            0 :             int grace_pitch = note->staff()->part()->instrument()->stringData()->getPitch(string, fret, note->staff(), note->tick());
    1851            0 :             gn->setPitch(grace_pitch);
    1852            0 :             gn->setTpcFromPitch();
    1853              : 
    1854            0 :             Chord* gc = nullptr;
    1855            0 :             if (note->chord()->graceNotes().size()) {
    1856            0 :                 gc = note->chord()->graceNotes().front();
    1857              :             }
    1858            0 :             if (!gc) {
    1859            0 :                 gc = Factory::createChord(score->dummy());
    1860            0 :                 TDuration d;
    1861            0 :                 d.setVal(grace_len);
    1862            0 :                 if (grace_len == Constants::DIVISION / 6) {
    1863            0 :                     d.setDots(1);
    1864              :                 }
    1865            0 :                 gc->setDurationType(d);
    1866            0 :                 gc->setTicks(d.fraction());
    1867            0 :                 gc->setNoteType(NoteType::ACCIACCATURA);
    1868            0 :                 gc->mutldata()->setMag(note->chord()->staff()->staffMag(Fraction(0, 1)) * score->style().styleD(Sid::graceNoteMag));
    1869            0 :                 note->chord()->add(gc);         // sets parent + track
    1870            0 :                 addDynamic(gn, dynamic);
    1871              :             }
    1872              : 
    1873            0 :             gc->add(gn);
    1874              : 
    1875            0 :             if (transition == 0) {
    1876              :                 // no transition
    1877            0 :             } else if (transition == 1) {
    1878              :                 //note->setSlideNote(gn);
    1879            0 :                 Glissando* glis = new Glissando(gn);
    1880            0 :                 glis->setGlissandoType(GlissandoType::STRAIGHT);
    1881            0 :                 glis->setStartElement(gn);
    1882            0 :                 glis->setTick(gn->chord()->tick());
    1883            0 :                 glis->setTrack(gn->track());
    1884            0 :                 glis->setEndElement(note);
    1885            0 :                 glis->setTick2(note->chord()->tick());
    1886            0 :                 glis->setTrack2(note->track());
    1887            0 :                 score->addElement(glis);
    1888              :                 //HammerOn here??? Maybe version...
    1889              : 
    1890            0 :                 Slur* slur1 = Factory::createSlur(score->dummy());
    1891            0 :                 slur1->setStartElement(gc);
    1892            0 :                 slur1->setEndElement(note->chord());
    1893            0 :                 slur1->setTick(gc->tick());
    1894            0 :                 slur1->setTick2(note->chord()->tick());
    1895            0 :                 slur1->setTrack(gc->track());
    1896            0 :                 slur1->setTrack2(note->track());
    1897            0 :                 score->addElement(slur1);
    1898              : 
    1899              :                 //TODO: Add a 'slide' guitar effect when implemented
    1900            0 :             } else if (transition == 2 && fretNumber >= 0 && fretNumber <= 255 && fretNumber != gn->fret()) {
    1901              :                 /*QList<PitchValue> points;
    1902              :                 points.append(PitchValue(0,0, false));
    1903              :                 points.append(PitchValue(60,(fretNumber-gn->fret())*100, false));
    1904              : 
    1905              :                 Bend* b = Factory::createBend(note->score());
    1906              :                 b->setPoints(points);
    1907              :                 b->setTrack(gn->track());
    1908              :                 gn->add(b);*/
    1909            0 :             } else if (transition == 3) {
    1910              :                 // TODO:
    1911              :                 //     major: replace with a 'hammer-on' guitar effect when implemented
    1912              :                 //     minor: make slurs for parts
    1913              : 
    1914            0 :                 ChordRest* cr1 = toChord(gc);
    1915            0 :                 ChordRest* cr2 = toChord(note->chord());
    1916              : 
    1917            0 :                 Slur* slur1 = Factory::createSlur(score->dummy());
    1918            0 :                 slur1->setStartElement(cr1);
    1919            0 :                 slur1->setEndElement(cr2);
    1920            0 :                 slur1->setTick(cr1->tick());
    1921            0 :                 slur1->setTick2(cr2->tick());
    1922            0 :                 slur1->setTrack(cr1->track());
    1923            0 :                 slur1->setTrack2(cr2->track());
    1924            0 :                 score->addElement(slur1);
    1925              :             }
    1926              :         }
    1927            6 :         if (modMask1 & EFFECT_HAMMER) {         // hammer on / pull off
    1928            0 :             readResult.slur = true;
    1929              :         }
    1930            6 :         if (modMask1 & EFFECT_LET_RING) {       // let ring
    1931            0 :             readResult.letRing = true;
    1932              :         }
    1933            6 :         if (modMask1 & EFFECT_SLIDE_OLD) {
    1934            1 :             slideList.push_back(note);
    1935              :         }
    1936              : 
    1937            6 :         if (version >= 400) {
    1938            0 :             if (modMask2 & EFFECT_STACCATO) {
    1939              :             }
    1940            0 :             if (modMask2 & EFFECT_PALM_MUTE) {
    1941            0 :                 readResult.palmMute = true;
    1942              :             }
    1943            0 :             if (modMask2 & EFFECT_TREMOLO) {
    1944            0 :                 readUInt8();
    1945              :             }
    1946            0 :             if (modMask2 & EFFECT_ARTIFICIAL_HARMONIC) {
    1947              :                 /*int type =*/
    1948            0 :                 readUInt8();
    1949              :                 //TODO-ws           if (type == 1 || type == 4 || type == 5)
    1950              :                 //                    note->setHarmonic(true);
    1951              :             }
    1952            0 :             if (modMask2 & EFFECT_TRILL) {
    1953              :                 //TODO-ws                        note->setTrillFret(readUInt8());      // trill fret
    1954            0 :                 readUInt8();              // trill length
    1955              :             }
    1956              :         }
    1957              :     }
    1958           87 :     if (fretNumber == -1) {
    1959            0 :         LOGD("Note: no fret number, tie %d", tieNote);
    1960              :     }
    1961           87 :     Staff* staff = note->staff();
    1962           87 :     if (fretNumber == 255) {
    1963            0 :         fretNumber = 0;
    1964            0 :         note->setHeadGroup(NoteHeadGroup::HEAD_CROSS);
    1965            0 :         note->setGhost(true);
    1966              :     }
    1967              :     // dead note represented as high numbers - fix to zero
    1968           87 :     if (fretNumber > 99 || fretNumber == -1) {
    1969            0 :         fretNumber = 0;
    1970              :     }
    1971           87 :     int pitch = staff->part()->instrument()->stringData()->getPitch(string, fretNumber, staff, note->tick());
    1972              : 
    1973              :     /* it's possible to specify extraordinarily high pitches by
    1974              :     specifying fret numbers that don't exist. This is an issue that
    1975              :     comes from tuxguitar. Just set to maximum pitch. GP6 actually
    1976              :     sets the fret number to 0 also, so that's what I've opted to do
    1977              :     here. */
    1978           87 :     if (pitch > MAX_PITCH) {
    1979            0 :         fretNumber = 0;
    1980            0 :         pitch = MAX_PITCH;
    1981              :     }
    1982              : 
    1983           87 :     note->setFret(fretNumber);
    1984           87 :     note->setString(string);
    1985           87 :     note->setPitch(pitch);
    1986              : 
    1987           87 :     if (tieNote) {
    1988            2 :         bool found = false;
    1989            2 :         Chord* chord     = note->chord();
    1990            2 :         Segment* segment = chord->segment()->prev1(SegmentType::ChordRest);
    1991            2 :         track_idx_t track = note->track();
    1992            2 :         std::vector<Chord*> chords;
    1993            2 :         Note* true_note = nullptr;
    1994            2 :         while (segment) {
    1995            2 :             EngravingItem* e = segment->element(track);
    1996            2 :             if (e) {
    1997            2 :                 if (e->isChord()) {
    1998            2 :                     Chord* chord2 = toChord(e);
    1999            2 :                     for (Note* note2 : chord2->notes()) {
    2000            2 :                         if (note2->string() == string) {
    2001            2 :                             if (chords.empty()) {
    2002            2 :                                 Tie* tie = Factory::createTie(note2);
    2003            2 :                                 tie->setEndNote(note);
    2004            2 :                                 note2->add(tie);
    2005              :                             }
    2006            2 :                             note->setFret(note2->fret());
    2007            2 :                             note->setPitch(note2->pitch());
    2008            2 :                             found = true;
    2009            2 :                             true_note = note2;
    2010            2 :                             break;
    2011              :                         }
    2012              :                     }
    2013            2 :                     if (!found) {
    2014            0 :                         chords.push_back(chord2);
    2015              :                     }
    2016              :                 }
    2017            2 :                 if (found) {
    2018            2 :                     break;
    2019              :                 }
    2020              :             }
    2021            0 :             segment = segment->prev1(SegmentType::ChordRest);
    2022              :         }
    2023              : 
    2024            2 :         if (chords.size() && true_note) {
    2025            0 :             Note* end_note = note;
    2026            0 :             for (unsigned int i = 0; i < chords.size(); ++i) {
    2027            0 :                 Note* note2 = Factory::createNote(chords[i]);
    2028            0 :                 note2->setString(true_note->string());
    2029            0 :                 note2->setFret(true_note->fret());
    2030            0 :                 note2->setPitch(true_note->pitch());
    2031            0 :                 note2->setTpcFromPitch();
    2032            0 :                 chords[i]->add(note2);
    2033            0 :                 Tie* tie = Factory::createTie(note2);
    2034            0 :                 tie->setEndNote(end_note);
    2035            0 :                 end_note = note2;
    2036            0 :                 note2->add(tie);
    2037              :             }
    2038            0 :             Tie* tie = Factory::createTie(true_note);
    2039            0 :             tie->setEndNote(end_note);
    2040            0 :             true_note->add(tie);
    2041              :         }
    2042            2 :     }
    2043              : 
    2044           87 :     return readResult;
    2045              : }
    2046              : 
    2047              : //---------------------------------------------------------
    2048              : //   readBeatEffects
    2049              : //---------------------------------------------------------
    2050              : 
    2051            0 : int GuitarPro1::readBeatEffects(int, Segment*)
    2052              : {
    2053            0 :     uint8_t fxBits1 = readUInt8();
    2054            0 :     if (fxBits1 & BEAT_EFFECT) {
    2055            0 :         uint8_t num = readUInt8();
    2056            0 :         switch (num) {
    2057            0 :         case 0:                     // tremolo bar
    2058            0 :             readInt();
    2059            0 :             break;
    2060            0 :         default:
    2061            0 :             readInt();
    2062            0 :             break;
    2063              :         }
    2064              :     }
    2065            0 :     if (fxBits1 & BEAT_ARPEGGIO) {
    2066            0 :         readUInt8();                // down stroke length
    2067            0 :         readUInt8();                // up stroke length
    2068              :     }
    2069            0 :     return 0;
    2070              : }
    2071              : 
    2072              : //---------------------------------------------------------
    2073              : //   read
    2074              : //---------------------------------------------------------
    2075              : 
    2076            7 : bool GuitarPro3::read(IODevice* io)
    2077              : {
    2078            7 :     m_continiousElementsBuilder = std::make_unique<ContiniousElementsBuilder>(score);
    2079            7 :     m_guitarBendImporter = std::make_unique<GuitarBendImporter>(score);
    2080              : 
    2081            7 :     f      = io;
    2082            7 :     curPos = 30;
    2083              : 
    2084            7 :     title        = readDelphiString();
    2085            7 :     subtitle     = readDelphiString();
    2086            7 :     artist       = readDelphiString();
    2087            7 :     album        = readDelphiString();
    2088            7 :     composer     = readDelphiString();
    2089            7 :     String copyright = readDelphiString();
    2090              : 
    2091            7 :     if (!copyright.isEmpty()) {
    2092            2 :         score->setMetaTag(u"copyright", copyright);
    2093              :     }
    2094              : 
    2095            7 :     readDelphiString(); // transcriber
    2096            7 :     readDelphiString(); // instructions
    2097           11 :     for (int i = 0, n = readInt(); i < n; ++i) {
    2098            4 :         comments.append(readDelphiString());
    2099              :     }
    2100              : 
    2101            7 :     /*uint8_t num =*/ readUInt8();        // Shuffle rhythm feel
    2102              : 
    2103            7 :     int temp = readInt();
    2104              : 
    2105              :     // int octave = 0;
    2106            7 :     key = readInt();      // key
    2107              : 
    2108          455 :     for (int i = 0; i < GP_MAX_TRACK_NUMBER * 2; ++i) {
    2109          448 :         channelDefaults[i].patch   = readInt();
    2110          448 :         channelDefaults[i].volume  = readUInt8() * 8 - 1;
    2111          448 :         channelDefaults[i].pan     = readUInt8() * 8 - 1;
    2112          448 :         channelDefaults[i].chorus  = readUInt8() * 8 - 1;
    2113          448 :         channelDefaults[i].reverb  = readUInt8() * 8 - 1;
    2114          448 :         channelDefaults[i].phase   = readUInt8() * 8 - 1;
    2115          448 :         channelDefaults[i].tremolo = readUInt8() * 8 - 1;
    2116          448 :         readUInt8();          // padding
    2117          448 :         readUInt8();
    2118              :     }
    2119            7 :     measures   = readInt();
    2120            7 :     staves = readInt();
    2121            7 :     initDynamics(staves);
    2122              : 
    2123            7 :     slurs.resize(staves, nullptr);
    2124              : 
    2125            7 :     int tnumerator   = 4;
    2126            7 :     int tdenominator = 4;
    2127           30 :     for (size_t i = 0; i < measures; ++i) {
    2128           23 :         GpBar bar;
    2129           23 :         uint8_t barBits = readUInt8();
    2130           23 :         if (barBits & SCORE_TIMESIG_NUMERATOR) {
    2131            7 :             tnumerator = readUInt8();
    2132              :         }
    2133           23 :         if (barBits & SCORE_TIMESIG_DENOMINATOR) {
    2134            7 :             tdenominator = readUInt8();
    2135              :         }
    2136           23 :         if (barBits & SCORE_REPEAT_START) {
    2137            1 :             bar.repeatFlags = bar.repeatFlags | mu::engraving::Repeat::START;
    2138              :         }
    2139           23 :         if (barBits & SCORE_REPEAT_END) {                    // number of repeats
    2140            2 :             bar.repeatFlags = bar.repeatFlags | mu::engraving::Repeat::END;
    2141            2 :             bar.repeats = readUInt8() + 1;
    2142              :         }
    2143           23 :         if (barBits & SCORE_VOLTA) {                          // a volta
    2144            7 :             uint8_t voltaNumber = readUInt8();
    2145              :             // voltas are represented as a binary number
    2146            7 :             bar.volta.voltaType = GP_VOLTA_BINARY;
    2147           25 :             while (voltaNumber > 0) {
    2148           18 :                 bar.volta.voltaInfo.push_back(voltaNumber & 1);
    2149           18 :                 voltaNumber >>= 1;
    2150              :             }
    2151              :         }
    2152           23 :         if (barBits & SCORE_MARKER) {
    2153            0 :             bar.marker = readDelphiString();           // new section?
    2154            0 :             /*int color =*/ readInt();          // color?
    2155              :         }
    2156           23 :         if (barBits & SCORE_KEYSIG) {
    2157            0 :             int currentKey = readUInt8();
    2158              :             /* key signatures are specified as
    2159              :              * 1# = 1, 2# = 2, ..., 7# = 7
    2160              :              * 1b = 255, 2b = 254, ... 7b = 249 */
    2161            0 :             bar.keysig = currentKey <= 7 ? currentKey : -256 + currentKey;
    2162            0 :             readUInt8();              // specifies major/minor mode
    2163              :         }
    2164              : 
    2165           23 :         if (barBits & SCORE_DOUBLE_BAR) {
    2166            0 :             bar.barLine = BarLineType::DOUBLE;
    2167              :         }
    2168           23 :         bar.timesig = Fraction(tnumerator, tdenominator);
    2169           23 :         bars.push_back(bar);
    2170           23 :     }
    2171              : 
    2172              :     //
    2173              :     // create a part for every staff
    2174              :     //
    2175           14 :     for (size_t staffIdx = 0; staffIdx < staves; ++staffIdx) {
    2176            7 :         Part* part = new Part(score);
    2177            7 :         Staff* s = Factory::createStaff(part);
    2178              : 
    2179            7 :         score->appendStaff(s);
    2180            7 :         score->appendPart(part);
    2181              :     }
    2182              : 
    2183            7 :     Fraction ts;
    2184            7 :     Fraction tick = { 0, 1 };
    2185           30 :     for (size_t i = 0; i < measures; ++i) {
    2186           23 :         Fraction nts = bars[i].timesig;
    2187           23 :         Measure* m = Factory::createMeasure(score);
    2188           23 :         m->setTick(tick);
    2189           23 :         m->setTimesig(nts);
    2190           23 :         m->setTicks(nts);
    2191              : 
    2192           23 :         if (i == 0 || ts != nts) {
    2193           14 :             for (size_t staffIdx = 0; staffIdx < staves; ++staffIdx) {
    2194            7 :                 Segment* s = m->getSegment(SegmentType::TimeSig, tick);
    2195            7 :                 TimeSig* t = Factory::createTimeSig(s);
    2196            7 :                 t->setTrack(staffIdx * VOICES);
    2197            7 :                 t->setSig(nts);
    2198            7 :                 s->add(t);
    2199              :             }
    2200              :         }
    2201           23 :         if (i == 0 && key) {
    2202            0 :             for (size_t staffIdx = 0; staffIdx < staves; ++staffIdx) {
    2203            0 :                 Segment* s = m->getSegment(SegmentType::KeySig, tick);
    2204            0 :                 KeySig* t = Factory::createKeySig(s);
    2205            0 :                 t->setKey(Key(key));
    2206            0 :                 t->setTrack(staffIdx * VOICES);
    2207            0 :                 s->add(t);
    2208              :             }
    2209              :         }
    2210              : 
    2211           23 :         readVolta(&bars[i].volta, m);
    2212           23 :         m->setRepeatEnd(bars[i].repeatFlags == mu::engraving::Repeat::END);
    2213           23 :         m->setRepeatStart(bars[i].repeatFlags == mu::engraving::Repeat::START);
    2214           23 :         m->setRepeatJump(bars[i].repeatFlags == mu::engraving::Repeat::JUMP);
    2215              :         //            m->setRepeatFlags(bars[i].repeatFlags);
    2216           23 :         m->setRepeatCount(bars[i].repeats);
    2217              : 
    2218              :         // reset the volta sequence if we have an opening repeat
    2219           23 :         if (bars[i].repeatFlags == mu::engraving::Repeat::START) {
    2220            1 :             voltaSequence = 1;
    2221              :         }
    2222              :         // otherwise, if we see an end repeat symbol, only reset if the bar after it does not contain a volta
    2223           22 :         else if (bars[i].repeatFlags == mu::engraving::Repeat::END && i < bars.size() - 1) {
    2224            2 :             if (bars[i + 1].volta.voltaInfo.size() == 0) {
    2225            1 :                 voltaSequence = 1;
    2226              :             }
    2227              :         }
    2228              : 
    2229           23 :         score->measures()->append(m);
    2230           23 :         tick += nts;
    2231           23 :         ts = nts;
    2232              :     }
    2233              : 
    2234           14 :     for (size_t i = 0; i < staves; ++i) {
    2235              :         int tuning[GP_MAX_STRING_NUMBER];
    2236              : 
    2237            7 :         uint8_t c      = readUInt8();       // simulations bitmask
    2238            7 :         if (c & 0x2) {                    // 12 stringed guitar
    2239              :         }
    2240            7 :         if (c & 0x4) {                    // banjo track
    2241              :         }
    2242            7 :         String name = readPascalString(40);
    2243            7 :         int strings  = readInt();
    2244            7 :         if (strings <= 0 || strings > GP_MAX_STRING_NUMBER) {
    2245            0 :             return false;
    2246              :         }
    2247           47 :         for (int j = 0; j < strings; ++j) {
    2248           40 :             tuning[j] = readInt();
    2249              :         }
    2250           16 :         for (int j = strings; j < GP_MAX_STRING_NUMBER; ++j) {
    2251            9 :             readInt();
    2252              :         }
    2253            7 :         int midiPort     = readInt() - 1;
    2254            7 :         int midiChannel  = readInt() - 1;
    2255           14 :         if (midiChannel < 0 || midiChannel >= static_cast<int>(channelDefaults.size())) {
    2256            0 :             LOGE() << "midiChannel " << midiChannel << " out of range 0-" << channelDefaults.size();
    2257            0 :             return false;
    2258              :         }
    2259            7 :         /*int midiChannel2 =*/ readInt();     // - 1;
    2260            7 :         int frets        = readInt();
    2261            7 :         int capo         = readInt();
    2262            7 :         /*int color        =*/ readInt();
    2263              : 
    2264            7 :         std::vector<int> tuning2(strings);
    2265              :         //int tuning2[strings];
    2266           47 :         for (int k = 0; k < strings; ++k) {
    2267           40 :             tuning2[strings - k - 1] = tuning[k];
    2268              :         }
    2269            7 :         bool useFlats = createTuningString(strings, &tuning2[0]);
    2270            7 :         StringData stringData(frets, strings, &tuning2[0], useFlats);
    2271            7 :         Part* part = score->staff(i)->part();
    2272            7 :         Instrument* instr = part->instrument();
    2273            7 :         instr->setStringData(stringData);
    2274            7 :         instr->setSingleNoteDynamics(false);
    2275            7 :         part->setPlainLongName(name);
    2276              :         //
    2277              :         // determine clef
    2278              :         //
    2279            7 :         Staff* staff = score->staff(i);
    2280            7 :         int patch = channelDefaults[midiChannel].patch;
    2281            7 :         ClefType clefId = ClefType::G;
    2282            7 :         if (midiChannel == GP_DEFAULT_PERCUSSION_CHANNEL) {
    2283            0 :             clefId = ClefType::PERC;
    2284            0 :             StaffTypes type = StaffTypes::PERC_DEFAULT;
    2285            0 :             if (auto it = drumset::PERC_STAFF_LINES_FROM_INSTRUMENT.find(name.toStdString());
    2286            0 :                 it != drumset::PERC_STAFF_LINES_FROM_INSTRUMENT.end()) {
    2287            0 :                 drumset::initGuitarProPercussionSet(it->second);
    2288            0 :                 drumset::setInstrumentDrumset(instr, it->second);
    2289            0 :                 switch (it->second.numLines) {
    2290            0 :                 case 1:
    2291            0 :                     type = StaffTypes::PERC_1LINE;
    2292            0 :                     break;
    2293            0 :                 case 2:
    2294            0 :                     type = StaffTypes::PERC_2LINE;
    2295            0 :                     break;
    2296            0 :                 case 3:
    2297            0 :                     type = StaffTypes::PERC_3LINE;
    2298            0 :                     break;
    2299            0 :                 default:
    2300            0 :                     type = StaffTypes::PERC_DEFAULT;
    2301            0 :                     break;
    2302              :                 }
    2303              :             } else {
    2304            0 :                 drumset::initGuitarProDrumset();
    2305            0 :                 instr->setDrumset(drumset::gpDrumset);
    2306              :             }
    2307            0 :             staff->setStaffType(Fraction(0, 1), *StaffType::preset(type));
    2308              :         } else {
    2309            7 :             clefId = defaultClef(patch);
    2310              :         }
    2311            7 :         Measure* measure = score->firstMeasure();
    2312            7 :         Segment* segment = measure->getSegment(SegmentType::HeaderClef, Fraction(0, 1));
    2313            7 :         Clef* clef = Factory::createClef(segment);
    2314            7 :         clef->setClefType(clefId);
    2315            7 :         clef->setTrack(i * VOICES);
    2316            7 :         segment->add(clef);
    2317              : 
    2318            7 :         if (capo > 0) {
    2319            1 :             Segment* s = measure->getSegment(SegmentType::ChordRest, measure->tick());
    2320            1 :             const size_t track = i * VOICES;
    2321              : 
    2322            1 :             CapoParams params;
    2323            1 :             params.active = true;
    2324            1 :             params.transposeMode = CapoParams::TransposeMode::TAB_ONLY;
    2325            1 :             params.fretPosition = capo;
    2326              : 
    2327            1 :             Capo* capoEl = Factory::createCapo(s);
    2328            1 :             capoEl->setTrack(track);
    2329            1 :             capoEl->setParams(params);
    2330            1 :             s->add(capoEl);
    2331              : 
    2332            1 :             staff->insertCapoParams({ 0, 1 }, params, true);
    2333            1 :         }
    2334              : 
    2335            7 :         InstrChannel* ch = instr->channel(0);
    2336            7 :         if (midiChannel == GP_DEFAULT_PERCUSSION_CHANNEL) {
    2337            0 :             ch->setProgram(0);
    2338            0 :             ch->setBank(128);
    2339              :         } else {
    2340            7 :             ch->setProgram(patch);
    2341            7 :             ch->setBank(0);
    2342              :         }
    2343            7 :         ch->setVolume(channelDefaults[midiChannel].volume);
    2344            7 :         ch->setPan(channelDefaults[midiChannel].pan);
    2345            7 :         ch->setChorus(channelDefaults[midiChannel].chorus);
    2346            7 :         ch->setReverb(channelDefaults[midiChannel].reverb);
    2347            7 :         staff->part()->setMidiChannel(midiChannel, midiPort);
    2348              :         // missing: phase, tremolo
    2349            7 :     }
    2350              : 
    2351            7 :     previousTempo = temp;
    2352            7 :     Measure* measure = score->firstMeasure();
    2353            7 :     bool mixChange = false;
    2354           30 :     for (size_t bar = 0; bar < measures; ++bar, measure = measure->nextMeasure()) {
    2355           23 :         const GpBar& gpbar = bars[bar];
    2356              : 
    2357           23 :         if (!gpbar.marker.isEmpty()) {
    2358            0 :             Segment* segment = measure->getSegment(SegmentType::ChordRest, measure->tick());
    2359            0 :             RehearsalMark* s = new RehearsalMark(segment);
    2360            0 :             s->setPlainText(gpbar.marker.trimmed());
    2361            0 :             s->setTrack(0);
    2362            0 :             segment->add(s);
    2363              :         }
    2364              : 
    2365           23 :         std::vector<Tuplet*> tuplets(staves);
    2366           46 :         for (size_t staffIdx = 0; staffIdx < staves; ++staffIdx) {
    2367           23 :             tuplets[staffIdx] = 0;
    2368              :         }
    2369              : 
    2370           46 :         for (size_t staffIdx = 0; staffIdx < staves; ++staffIdx) {
    2371           23 :             Fraction measureLen = { 0, 1 };
    2372           23 :             track_idx_t track = staffIdx * VOICES;
    2373           23 :             Fraction fraction = measure->tick();
    2374           23 :             int beats = readInt();
    2375           23 :             if (beats > 200) {
    2376            0 :                 return false;
    2377              :             }
    2378          114 :             for (int beat = 0; beat < beats; ++beat) {
    2379           91 :                 bool hasSlur = false;
    2380           91 :                 bool hasLetRing = false;
    2381           91 :                 bool hasPalmMute = false;
    2382           91 :                 bool hasVibratoLeftHand = false;
    2383           91 :                 bool hasVibratoWTremBar = false;
    2384              : 
    2385              :                 //                        int pause = 0;
    2386           91 :                 uint8_t beatBits = readUInt8();
    2387           91 :                 bool dotted = beatBits & BEAT_DOTTED;
    2388           91 :                 if (beatBits & BEAT_PAUSE) {
    2389              :                     /*pause =*/
    2390            8 :                     readUInt8();
    2391              :                 }
    2392              : 
    2393           91 :                 slide = -1;
    2394           91 :                 if (muse::contains(slides, static_cast<int>(track))) {
    2395            0 :                     slide = muse::take(slides, static_cast<int>(track));
    2396              :                 }
    2397              : 
    2398           91 :                 int len = readChar();
    2399           91 :                 int tuple = 0;
    2400           91 :                 if (beatBits & BEAT_TUPLET) {
    2401            0 :                     tuple = readInt();
    2402              :                 }
    2403              : 
    2404           91 :                 Segment* segment = measure->getSegment(SegmentType::ChordRest, fraction);
    2405           91 :                 if (beatBits & BEAT_CHORD) {
    2406            0 :                     int numStrings = static_cast<int>(score->staff(staffIdx)->part()->instrument()->stringData()->strings());
    2407            0 :                     int header = readUInt8();
    2408            0 :                     String name;
    2409            0 :                     if ((header & 1) == 0) {
    2410            0 :                         name = readDelphiString();
    2411            0 :                         readChord(segment, static_cast<int>(track), numStrings, name, false);
    2412              :                     } else {
    2413            0 :                         skip(25);
    2414            0 :                         name = readPascalString(34);
    2415            0 :                         readChord(segment, static_cast<int>(track), numStrings, name, false);
    2416            0 :                         skip(36);
    2417              :                     }
    2418            0 :                 }
    2419           91 :                 Lyrics* lyrics = 0;
    2420           91 :                 if (beatBits & BEAT_LYRICS) {
    2421            0 :                     String txt = readDelphiString();
    2422            0 :                     Lyrics* lyrics2 = Factory::createLyrics(score->dummy());
    2423            0 :                     lyrics2->setPlainText(txt);
    2424            0 :                 }
    2425           91 :                 int beatEffects = 0;
    2426              : 
    2427           91 :                 if (beatBits & BEAT_EFFECTS) {
    2428            0 :                     beatEffects = readBeatEffects(static_cast<int>(track), segment);
    2429              :                 }
    2430           91 :                 bool vibrato = beatEffects & 0x1 || beatEffects & 0x2;
    2431              : 
    2432           91 :                 if (beatBits & BEAT_MIX_CHANGE) {
    2433            1 :                     readMixChange(measure);
    2434            1 :                     mixChange = true;
    2435              :                 }
    2436              : 
    2437           91 :                 int strings = readUInt8();           // used strings mask
    2438              : 
    2439           91 :                 Fraction l = len2fraction(len);
    2440              : 
    2441              :                 // Some beat effects could add a Chord before this
    2442           91 :                 ChordRest* cr = segment->cr(track);
    2443              :                 // if (!pause || strings)
    2444           91 :                 if (strings) {
    2445           83 :                     if (!segment->cr(track)) {
    2446           83 :                         cr = Factory::createChord(segment);
    2447              :                     }
    2448              :                 } else {
    2449            8 :                     if (segment->cr(track)) {
    2450            0 :                         segment->remove(segment->cr(track));
    2451            0 :                         delete cr;
    2452            0 :                         cr = 0;
    2453              :                     }
    2454            8 :                     cr = Factory::createRest(segment);
    2455              :                 }
    2456              : 
    2457           91 :                 cr->setTrack(track);
    2458           91 :                 if (lyrics) {
    2459            0 :                     cr->add(lyrics);
    2460              :                 }
    2461              : 
    2462           91 :                 TDuration d(l);
    2463           91 :                 d.setDots(dotted ? 1 : 0);
    2464              : 
    2465           91 :                 if (dotted) {
    2466            1 :                     l = l + (l * Fraction(1, 2));
    2467              :                 }
    2468              : 
    2469           91 :                 if (tuple) {
    2470            0 :                     Tuplet* tuplet = tuplets[staffIdx];
    2471            0 :                     if ((tuplet == 0) || (tuplet->elementsDuration() == tuplet->baseLen().fraction() * tuplet->ratio().numerator())) {
    2472            0 :                         tuplet = Factory::createTuplet(measure);
    2473            0 :                         tuplet->setTick(fraction);
    2474            0 :                         tuplet->setTrack(cr->track());
    2475            0 :                         tuplets[staffIdx] = tuplet;
    2476            0 :                         setTuplet(tuplet, tuple);
    2477              :                     }
    2478            0 :                     tuplet->setTrack(track);
    2479            0 :                     tuplet->setBaseLen(l);
    2480            0 :                     tuplet->setTicks(l * tuplet->ratio().denominator());
    2481            0 :                     cr->setTuplet(tuplet);
    2482            0 :                     tuplet->add(cr);
    2483              :                 }
    2484              : 
    2485           91 :                 cr->setTicks(l);
    2486              : 
    2487           91 :                 if (cr->isRest() && l >= measure->ticks()) {
    2488            1 :                     cr->setDurationType(DurationType::V_MEASURE);
    2489            1 :                     cr->setTicks(measure->ticks());
    2490              :                 } else {
    2491           90 :                     cr->setDurationType(d);
    2492              :                 }
    2493              : 
    2494           91 :                 if (!segment->cr(track)) {
    2495           91 :                     segment->add(cr);
    2496              :                 }
    2497              : 
    2498           91 :                 Staff* staff = cr->staff();
    2499           91 :                 int numStrings = static_cast<int>(staff->part()->instrument()->stringData()->strings());
    2500          728 :                 for (int i = 6; i >= 0; --i) {
    2501          637 :                     if (strings & (1 << i) && ((6 - i) < numStrings)) {
    2502           87 :                         Note* note = Factory::createNote(toChord(cr));
    2503           87 :                         toChord(cr)->add(note);
    2504           87 :                         if (vibrato) {
    2505            0 :                             hasVibratoLeftHand = true;
    2506              :                         }
    2507           87 :                         if (dotted) {
    2508            1 :                             NoteDot* dot = Factory::createNoteDot(note);
    2509              :                             // there is at most one dotted note in this guitar pro version - set 0 index
    2510            1 :                             dot->setTrack(track);                // needed to know the staff it belongs to (and detect tablature)
    2511            1 :                             dot->setVisible(true);
    2512            1 :                             note->add(dot);
    2513              :                         }
    2514           87 :                         ReadNoteResult readResult = readNote(6 - i, note);
    2515           87 :                         hasSlur = readResult.slur || hasSlur;
    2516           87 :                         hasLetRing = readResult.letRing || hasLetRing;
    2517           87 :                         hasPalmMute = readResult.palmMute || hasPalmMute;
    2518           87 :                         hasVibratoLeftHand = readResult.vibrato || hasVibratoLeftHand;
    2519           87 :                         note->setTpcFromPitch();
    2520              :                     }
    2521              :                 }
    2522           91 :                 if (cr && cr->isChord() && toChord(cr)->notes().empty()) {
    2523            0 :                     if (segment->cr(track)) {
    2524            0 :                         segment->remove(cr);
    2525              :                     }
    2526            0 :                     delete cr;
    2527            0 :                     cr = Factory::createRest(segment);
    2528            0 :                     cr->setTicks(l);
    2529            0 :                     cr->setTrack(track);
    2530            0 :                     cr->setDurationType(d);
    2531            0 :                     segment->add(cr);
    2532              :                 }
    2533           91 :                 createSlur(hasSlur, staffIdx, cr);
    2534              : 
    2535           91 :                 if (cr && (cr->isChord())) {
    2536           83 :                     if (beatEffects >= 200) {
    2537            0 :                         beatEffects -= 200;
    2538            0 :                         Articulation* art = Factory::createArticulation(cr);
    2539            0 :                         art->setSymId(SymId::guitarFadeOut);
    2540            0 :                         art->setAnchor(ArticulationAnchor::TOP);
    2541            0 :                         art->setPropertyFlags(Pid::ARTICULATION_ANCHOR, PropertyFlags::UNSTYLED);
    2542            0 :                         if (!EditChord::toggleArticulation(score, cr, art)) {
    2543            0 :                             delete art;
    2544              :                         }
    2545              :                     }
    2546              : 
    2547           83 :                     bool hasVibratoLeftHandOnBeat = false;
    2548           83 :                     bool hasVibratoWTremBarOnBeat = false;
    2549           83 :                     applyBeatEffects(toChord(cr), beatEffects, hasVibratoLeftHandOnBeat, hasVibratoWTremBarOnBeat);
    2550           83 :                     hasVibratoLeftHand = hasVibratoLeftHand || hasVibratoLeftHandOnBeat;
    2551           83 :                     hasVibratoWTremBar = hasVibratoWTremBar || hasVibratoWTremBarOnBeat;
    2552           83 :                     if (slide > 0) {
    2553            0 :                         createSlide(slide, cr, static_cast<int>(staffIdx));
    2554              :                     }
    2555              :                 }
    2556              : 
    2557           91 :                 if (cr) {
    2558           91 :                     addLetRing(cr, hasLetRing);
    2559           91 :                     addPalmMute(cr, hasPalmMute);
    2560           91 :                     addVibratoLeftHand(cr, hasVibratoLeftHand);
    2561           91 :                     addVibratoWTremBar(cr, hasVibratoWTremBar);
    2562              :                 }
    2563              : 
    2564           91 :                 restsForEmptyBeats(segment, measure, cr, l, static_cast<int>(track), fraction);
    2565           91 :                 fraction += cr->actualTicks();
    2566           91 :                 measureLen += cr->actualTicks();
    2567              :             }
    2568           23 :             if (measureLen < measure->ticks()) {
    2569            0 :                 score->setRest(fraction, track, measure->ticks() - measureLen, false, nullptr, false);
    2570            0 :                 m_continiousElementsBuilder->notifyUncompletedMeasure();
    2571              :             }
    2572           23 :             bool removeRests = true;
    2573           23 :             int counter = 0;
    2574           23 :             Rest* lastRest = nullptr;
    2575           44 :             for (auto seg = measure->first(); seg; seg = seg->next()) {
    2576           43 :                 if (seg->segmentType() == SegmentType::ChordRest) {
    2577           29 :                     auto cr = seg->cr(track);
    2578           29 :                     if (cr && cr->isChord()) {
    2579           22 :                         removeRests = false;
    2580           22 :                         break;
    2581            7 :                     } else if (cr) {
    2582            7 :                         ++counter;
    2583            7 :                         lastRest = toRest(cr);
    2584              :                     }
    2585              :                 }
    2586              :             }
    2587           23 :             if (removeRests && counter < 2) {
    2588            1 :                 removeRests = false;
    2589            1 :                 if (counter == 1) {
    2590            1 :                     lastRest->setTicks(measure->timesig());
    2591            1 :                     lastRest->setDurationType(DurationType::V_MEASURE);
    2592              :                 }
    2593              :             }
    2594           23 :             if (removeRests) {
    2595            0 :                 auto seg = measure->first();
    2596            0 :                 while (seg && seg != measure->last())
    2597              :                 {
    2598            0 :                     if (seg->segmentType() == SegmentType::ChordRest) {
    2599            0 :                         auto cr = seg->cr(track);
    2600            0 :                         if (cr) {
    2601            0 :                             seg->remove(cr);
    2602            0 :                             delete cr;
    2603              :                         }
    2604              :                     }
    2605            0 :                     seg = seg->next();
    2606              :                 }
    2607            0 :                 auto cr = Factory::createRest(seg);
    2608            0 :                 cr->setTicks(measure->timesig());
    2609            0 :                 cr->setDurationType(DurationType::V_MEASURE);
    2610            0 :                 cr->setTrack(track);
    2611            0 :                 seg->add(cr);
    2612              :             }
    2613              :         }
    2614           23 :         if (bar == 1 && !mixChange) {
    2615            4 :             setTempo(temp, score->firstMeasure());
    2616              :         }
    2617           23 :     }
    2618            8 :     for (auto n : slideList) {
    2619            1 :         auto segment = n->chord()->segment();
    2620            1 :         auto measure1 = segment->measure();
    2621            1 :         while ((segment = segment->next1(SegmentType::ChordRest))
    2622            1 :                || ((measure1 = measure1->nextMeasure()) && (segment = measure1->first()))) {
    2623              :             // bool br = false;
    2624            1 :             auto crest = segment->cr(n->track());
    2625            1 :             if (!crest) {
    2626            0 :                 continue;
    2627              :             }
    2628            1 :             if (crest->type() == mu::engraving::ElementType::REST) {
    2629            0 :                 break;
    2630              :             }
    2631            1 :             auto cr = toChord(crest);
    2632            1 :             if (!cr) {
    2633            0 :                 continue;
    2634              :             }
    2635            1 :             if (cr->graceNotes().size()) {
    2636            0 :                 cr = cr->graceNotes().front();
    2637              :             }
    2638            1 :             if (cr) {
    2639            1 :                 for (auto nt : cr->notes()) {
    2640            1 :                     if (nt->string() == n->string()) {
    2641              :                         // auto mg = nt->magS();
    2642            1 :                         Glissando* s = new Glissando(n);
    2643            1 :                         s->setStartElement(n);
    2644            1 :                         s->setTick(n->chord()->segment()->tick());
    2645            1 :                         s->setTrack(n->track());
    2646            1 :                         s->setGlissandoType(GlissandoType::STRAIGHT);
    2647            1 :                         s->setEndElement(nt);
    2648            1 :                         s->setTick2(cr->segment()->tick());
    2649            1 :                         s->setTrack2(n->track());
    2650            1 :                         score->addElement(s);
    2651            1 :                         break;
    2652              :                     }
    2653              :                 }
    2654              :             }
    2655            1 :             break;
    2656              :         }
    2657              :     }
    2658              : 
    2659            7 :     m_continiousElementsBuilder->addElementsToScore();
    2660            7 :     m_guitarBendImporter->addElementsToScore();
    2661            7 :     addTunings();
    2662              : 
    2663            7 :     return true;
    2664            7 : }
    2665              : 
    2666              : //---------------------------------------------------------
    2667              : //   readBeatEffects
    2668              : //---------------------------------------------------------
    2669              : 
    2670            0 : int GuitarPro3::readBeatEffects(int track, Segment* segment)
    2671              : {
    2672            0 :     int effects = 0;
    2673            0 :     uint8_t fxBits = readUInt8();
    2674              : 
    2675            0 :     if (fxBits & BEAT_EFFECT) {
    2676            0 :         effects = readUInt8();          // effect 1-tapping, 2-slapping, 3-popping
    2677            0 :         readInt();     // we don't need this integer
    2678              :     }
    2679              : 
    2680            0 :     if (fxBits & BEAT_ARPEGGIO) {
    2681            0 :         int strokeup = readUInt8();                // up stroke length
    2682            0 :         int strokedown = readUInt8();                // down stroke length
    2683              : 
    2684            0 :         Arpeggio* a = Factory::createArpeggio(score->dummy());
    2685            0 :         if (strokeup > 0) {
    2686            0 :             a->setArpeggioType(ArpeggioType::UP_STRAIGHT);
    2687            0 :         } else if (strokedown > 0) {
    2688            0 :             a->setArpeggioType(ArpeggioType::DOWN_STRAIGHT);
    2689              :         } else {
    2690            0 :             delete a;
    2691            0 :             a = 0;
    2692              :         }
    2693              : 
    2694            0 :         if (a) {
    2695            0 :             ChordRest* cr = Factory::createChord(segment);
    2696            0 :             cr->setTrack(track);
    2697            0 :             cr->add(a);
    2698            0 :             segment->add(cr);
    2699              :         }
    2700              :     }
    2701            0 :     if (fxBits & BEAT_TREMOLO) {
    2702              :     }
    2703            0 :     if (fxBits & BEAT_FADE) {
    2704            0 :         effects += 200;
    2705              :     }
    2706            0 :     if (fxBits & BEAT_DOTTED) {
    2707              :     }
    2708            0 :     if (fxBits & BEAT_CHORD) {
    2709              :     }
    2710            0 :     if (effects == 0) {
    2711            0 :         return fxBits;
    2712              :     }
    2713            0 :     return effects;
    2714              : }
    2715              : 
    2716              : //---------------------------------------------------------
    2717              : //   readTremoloBar
    2718              : //---------------------------------------------------------
    2719              : 
    2720            3 : void GuitarPro::readTremoloBar(int /*track*/, Segment* /*segment*/)
    2721              : {
    2722              :     /*int a1 =*/
    2723            3 :     readChar();
    2724            3 :     /*int a2 =*/ readChar();
    2725            3 :     /*int a3 =*/ readChar();
    2726            3 :     /*int a4 =*/ readChar();
    2727            3 :     /*int a5 =*/ readChar();
    2728            3 :     int n  =  readInt();
    2729            3 :     std::vector<PitchValue> points;
    2730           12 :     for (int i = 0; i < n; ++i) {
    2731            9 :         int time    = readInt();
    2732            9 :         int pitch   = readInt();
    2733            9 :         int vibrato = readUInt8();
    2734            9 :         points.push_back(PitchValue(time, pitch, vibrato));
    2735              :     }
    2736            3 : }
    2737              : 
    2738           33 : void GuitarPro::addTunings()
    2739              : {
    2740           33 :     const Measure* m = score->firstMeasure();
    2741              : 
    2742              :     // NOTE: GP doesn't support multiple tunings on one part
    2743              :     // We're safe to just take the very first chord rest segment
    2744              :     // and check if it has any non-standard tuning
    2745           33 :     const Fraction& frac{ 0, 1 };
    2746              : 
    2747           66 :     for (auto p : score->parts()) {
    2748           66 :         for (auto s : p->staves()) {
    2749           33 :             if (!s->isPrimaryStaff() || p->instrument()->useDrumset()) {
    2750           31 :                 continue;
    2751              :             }
    2752              : 
    2753           33 :             Segment* seg = m->findSegment(SegmentType::ChordRest, frac);
    2754              : 
    2755           33 :             IF_ASSERT_FAILED(seg) {
    2756            0 :                 LOGE() << "First measure MUST has a chord rest segment after import";
    2757            0 :                 break;
    2758              :             }
    2759              : 
    2760           33 :             const StringData sd = *p->instrument()->stringData();
    2761           33 :             std::vector<int> tuning(sd.strings());
    2762          230 :             for (size_t i = 0; i < tuning.size(); ++i) {
    2763          197 :                 tuning[i] = sd.stringList().at(i).pitch + p->instrument()->transpose().chromatic;
    2764              :             }
    2765              : 
    2766           33 :             if (utils::isStandardTuning(p->instrument()->channel(0)->program(), tuning)) {
    2767           31 :                 continue;
    2768              :             }
    2769              : 
    2770            2 :             StringTunings* tun = Factory::createStringTunings(seg);
    2771            2 :             tun->setStringData(sd);
    2772            2 :             tun->setTrack(staff2track(s->idx()));
    2773            2 :             seg->add(tun);
    2774              :             // Instrument string data should be set to the standard
    2775            2 :             tuning = utils::standardTuningFor(p->instrument()->channel(0)->program(), (int)sd.strings());
    2776            2 :             StringData instrumentSD(sd.frets(), (int)tuning.size(), tuning.data());
    2777            2 :             p->instrument()->setStringData(instrumentSD);
    2778           64 :         }
    2779              :     }
    2780           33 : }
    2781              : 
    2782              : //---------------------------------------------------------
    2783              : //   addMetaInfo
    2784              : //---------------------------------------------------------
    2785              : 
    2786           73 : static void addMetaInfo(MasterScore* score, GuitarPro* gp, bool experimental)
    2787              : {
    2788          219 :     std::vector<String> fieldNames = { gp->title, gp->subtitle, gp->artist,
    2789          511 :                                        gp->album, gp->composer };
    2790              : 
    2791              :     bool createTitleField
    2792          410 :         = std::any_of(fieldNames.begin(), fieldNames.end(), [](const String& fieldName) { return !fieldName.isEmpty(); });
    2793              : 
    2794           73 :     if (!createTitleField && !experimental) {
    2795           64 :         return;
    2796              :     }
    2797              : 
    2798            9 :     MeasureBase* m = nullptr;
    2799            9 :     if (!score->measures()->first()) {
    2800            0 :         m = Factory::createTitleVBox(score);
    2801            0 :         score->measures()->append(m);
    2802              :     } else {
    2803            9 :         m = score->measures()->first();
    2804            9 :         if (!m->isVBox()) {
    2805            9 :             MeasureBase* mb = Factory::createTitleVBox(score);
    2806            9 :             score->addMeasure(mb, m);
    2807            9 :             m = mb;
    2808              :         }
    2809              :     }
    2810            9 :     if (!gp->title.isEmpty() || experimental) {
    2811            9 :         Text* s = Factory::createText(m, TextStyleType::TITLE);
    2812            9 :         s->setPlainText(gp->title);
    2813            9 :         m->add(s);
    2814              :     }
    2815            9 :     if (!gp->subtitle.isEmpty() || !gp->artist.isEmpty() || !gp->album.isEmpty() || experimental) {
    2816            4 :         Text* s = Factory::createText(m, TextStyleType::SUBTITLE);
    2817            4 :         String str;
    2818            4 :         if (!gp->subtitle.isEmpty()) {
    2819            1 :             str.append(gp->subtitle);
    2820              :         }
    2821            4 :         if (!gp->artist.isEmpty()) {
    2822            2 :             if (!str.isEmpty()) {
    2823            1 :                 str.append(u'\n');
    2824              :             }
    2825            2 :             str.append(gp->artist);
    2826              :         }
    2827            4 :         if (!gp->album.isEmpty()) {
    2828            2 :             if (!str.isEmpty()) {
    2829            2 :                 str.append(u'\n');
    2830              :             }
    2831            2 :             str.append(gp->album);
    2832              :         }
    2833            4 :         s->setPlainText(str);
    2834            4 :         m->add(s);
    2835            4 :     }
    2836            9 :     if (!gp->composer.isEmpty()) {
    2837            3 :         Text* s = Factory::createText(m, TextStyleType::COMPOSER);
    2838            3 :         s->setPlainText(gp->composer);
    2839            3 :         m->add(s);
    2840              :     }
    2841              : 
    2842            9 :     if (!gp->poet.isEmpty() || experimental) {
    2843            3 :         Text* s = Factory::createText(m, TextStyleType::LYRICIST);
    2844            3 :         if (!gp->poet.isEmpty()) {
    2845            1 :             s->setPlainText(muse::mtrc("iex_guitarpro", "Words by %1").arg(gp->poet));
    2846              :         }
    2847              : 
    2848            3 :         m->add(s);
    2849              :     }
    2850          146 : }
    2851              : 
    2852              : //---------------------------------------------------------
    2853              : //   importGTP
    2854              : //---------------------------------------------------------
    2855              : 
    2856          301 : Err importGTP(MasterScore* score, muse::io::IODevice* io, const muse::modularity::ContextPtr& iocCtx, bool experimental)
    2857              : {
    2858          301 :     if (!io->open(IODevice::ReadOnly)) {
    2859            0 :         return Err::FileOpenError;
    2860              :     }
    2861              : 
    2862          301 :     score->checkChordList();
    2863          301 :     io->seek(0);
    2864              :     char header[5];
    2865          301 :     io->read((uint8_t*)(header), 4);
    2866          301 :     header[4] = 0;
    2867          301 :     io->seek(0);
    2868          301 :     if (strcmp(header, "ptab") == 0) {
    2869            3 :         PowerTab ptb(io, score);
    2870            3 :         return ptb.read();
    2871            3 :     }
    2872              : 
    2873          298 :     GuitarPro* gp = nullptr;
    2874          298 :     bool readResult = false;
    2875          298 :     bool isVersionAbove6 = false;
    2876              :     // check to see if we are dealing with a GP file via the extension
    2877          298 :     if (strcmp(header, "PK\x3\x4") == 0) {
    2878          145 :         gp = new GuitarPro7(score, iocCtx);
    2879          145 :         readResult = gp->read(io);
    2880          145 :         gp->setTempo(0, 0);
    2881          145 :         isVersionAbove6 = true;
    2882              :     }
    2883              :     // check to see if we are dealing with a GPX file via the extension
    2884          153 :     else if (strcmp(header, "BCFZ") == 0) {
    2885           76 :         gp = new GuitarPro6(score, iocCtx);
    2886           76 :         drumset::initGuitarProDrumset();
    2887           76 :         readResult = gp->read(io);
    2888           76 :         gp->setTempo(0, 0);
    2889           76 :         isVersionAbove6 = true;
    2890              :     }
    2891              :     // otherwise it's an older version - check the header
    2892           77 :     else if (strcmp(&header[1], "FIC") == 0) {
    2893              :         uint8_t l;
    2894           76 :         io->read((uint8_t*)&l, 1);
    2895              :         char ss[30];
    2896           76 :         io->read((uint8_t*)(ss), 30);
    2897           76 :         ss[l] = 0;
    2898           76 :         String s = String::fromUtf8(ss);
    2899           76 :         if (s.startsWith(u"FICHIER GUITAR PRO ")) {
    2900           75 :             s = s.mid(20);
    2901            1 :         } else if (s.startsWith(u"FICHIER GUITARE PRO ")) {
    2902            0 :             s = s.mid(21);
    2903              :         } else {
    2904            1 :             LOGD("unknown gtp format <%s>", ss);
    2905            1 :             delete gp;
    2906            1 :             return Err::FileBadFormat;
    2907              :         }
    2908           75 :         int a = s.left(1).toInt();
    2909           75 :         int b = s.mid(2).toInt();
    2910           75 :         int version = a * 100 + b;
    2911           75 :         if (a == 1) {
    2912            0 :             gp = new GuitarPro1(score, version, iocCtx);
    2913           75 :         } else if (a == 2) {
    2914            0 :             gp = new GuitarPro2(score, version, iocCtx);
    2915           75 :         } else if (a == 3) {
    2916            7 :             gp = new GuitarPro3(score, version, iocCtx);
    2917           68 :         } else if (a == 4) {
    2918           27 :             gp = new GuitarPro4(score, version, iocCtx);
    2919           41 :         } else if (a == 5) {
    2920           40 :             gp = new GuitarPro5(score, version, iocCtx);
    2921              :         } else {
    2922            1 :             LOGD("unknown gtp format %d", version);
    2923            1 :             delete gp;
    2924            1 :             return Err::FileBadFormat;
    2925              :         }
    2926           74 :         readResult = gp->read(io);
    2927           74 :         gp->setTempo(0, 0);
    2928           76 :     } else {
    2929            1 :         delete gp;
    2930            1 :         return Err::FileBadFormat;
    2931              :     }
    2932          295 :     if (readResult == false) {
    2933            1 :         LOGD("guitar pro import error====");
    2934              :         // avoid another error message box
    2935            1 :         delete gp;
    2936            1 :         return Err::NoError;
    2937              :     }
    2938              : 
    2939          294 :     if (!isVersionAbove6) {
    2940           73 :         addMetaInfo(score, gp, experimental);
    2941              :     }
    2942              : 
    2943          294 :     int idx = 0;
    2944              : 
    2945          526 :     for (Measure* m1 = score->firstMeasure(); m1; m1 = m1->nextMeasure(), ++idx) {
    2946          453 :         if (gp->bars.empty()) {
    2947          221 :             break;
    2948              :         }
    2949          232 :         const GpBar& bar = gp->bars[idx];
    2950          232 :         if (bar.barLine != BarLineType::NORMAL && bar.barLine != BarLineType::END_REPEAT && bar.barLine != BarLineType::START_REPEAT
    2951            1 :             && bar.barLine != BarLineType::END_START_REPEAT) {
    2952            1 :             m1->setEndBarLineType(bar.barLine, 0);
    2953              :         }
    2954              :     }
    2955          294 :     if (score->lastMeasure() && score->lastMeasure()->endBarLineType() != BarLineType::NORMAL) {
    2956            0 :         score->lastMeasure()->setEndBarLineType(BarLineType::END, false);
    2957              :     }
    2958              : 
    2959          611 :     for (Part* part : score->parts()) {
    2960          651 :         for (const auto& pair : part->instruments()) {
    2961          334 :             pair.second->updateInstrumentId();
    2962              :         }
    2963              :     }
    2964              : 
    2965          294 :     score->updateTicksAndTimeSigMap();
    2966          611 :     for (Part* p : score->parts()) {
    2967          317 :         p->updateHarmonyChannels(false);
    2968              :     }
    2969              : 
    2970          294 :     delete gp;
    2971              : 
    2972          294 :     return Err::NoError;
    2973              : }
    2974              : } // namespace mu::iex::guitarpro
        

Generated by: LCOV version 2.0-1