1 //=============================================================================
2 //  MusE Score
3 //  Linux Music Score Editor
4 //
5 //  Copyright (C) 2010 Werner Schweer and others
6 //
7 //  This program is free software; you can redistribute it and/or modify
8 //  it under the terms of the GNU General Public License version 2.
9 //
10 //  This program is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU General Public License for more details.
14 //
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program; if not, write to the Free Software
17 //  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 //=============================================================================
19 
20 #ifndef __IMPORTGTP_H__
21 #define __IMPORTGTP_H__
22 
23 #include <libmscore/score.h>
24 #include <libmscore/mscore.h>
25 #include <libmscore/fraction.h>
26 #include <libmscore/fret.h>
27 #include <libmscore/chordrest.h>
28 #include <libmscore/slur.h>
29 #include <libmscore/clef.h>
30 #include <libmscore/keysig.h>
31 #include <libmscore/chordrest.h>
32 #include <libmscore/clef.h>
33 #include <libmscore/keysig.h>
34 #include <libmscore/hairpin.h>
35 #include <libmscore/ottava.h>
36 #include <libmscore/vibrato.h>
37 #include <libmscore/drumset.h>
38 
39 namespace Ms {
40 
41 class Score;
42 class Chord;
43 class Note;
44 class Segment;
45 class Measure;
46 class Tuplet;
47 class Volta;
48 class LetRing;
49 class PalmMute;
50 class Vibrato;
51 
52 static const int GP_MAX_LYRIC_LINES = 5;
53 static const int GP_MAX_TRACK_NUMBER = 32;
54 static const int GP_MAX_STRING_NUMBER = 7;
55 static const int GP_DEFAULT_PERCUSSION_CHANNEL = 9;
56 static const int GP_INVALID_KEYSIG = 127;
57 static const int GP_VOLTA_BINARY = 1;
58 static const int GP_VOLTA_FLAGS = 2;
59 
60 Score::FileError importGTP(Score* score, const QString& filename, const char* data, unsigned int data_len);
61 
62 enum class Repeat : char;
63 
64 struct GpTrack {
65       int patch;
66       uchar volume, pan, chorus, reverb, phase, tremolo;
67       };
68 
69 struct GPVolta {
70       int voltaType;
71       QList<int> voltaInfo;
72 };
73 
74 /* How the fermatas are represented in Guitar Pro is two integers, the
75  * first is an index value and the second is the time division that
76  * index value refers to, and they are givin with respect to a
77  * measure. Time division 0 means a minim, 1 is a crotchet, 2 is a
78  * quaver and so on, with the index (counting from 0) referring to how
79  * many time divisions occur before the fermata. These numbers are
80  * separated in GP6 with a '/' character. For example, a note
81  * occurring on the third beat of a measure in a 4/4 bar would be
82  * represented as 2/1.
83  */
84 
85 struct GPFermata {
86       int index;
87       int timeDivision;
88       QString type;
89       };
90 
91 struct GPLyrics {
92       QStringList lyrics;
93       std::vector<Segment*> segments;
94       int fromBeat;
95       int beatCounter;
96       int lyricTrack;
97       };
98 
99 struct GpBar {
100       Fraction timesig;
101       bool freeTime;
102       int keysig;
103       QString marker;
104       BarLineType barLine;
105       Repeat repeatFlags;
106       int repeats;
107       GPVolta volta;
108       QString direction;
109       QString directionStyle;
110 
111       QString section[2];
112 
113       std::vector<QString> directions;
114 
115       GpBar();
116       };
117 
118 //---------------------------------------------------------
119 //   GuitarPro
120 //---------------------------------------------------------
121 
122 class GuitarPro {
123    protected:
124       std::list<Note*> slideList; //list of start slide notes
125 
126       // note effect bit masks
127       static const uchar EFFECT_BEND = 0x1;
128       static const uchar EFFECT_STACATTO = 0x1;
129       static const uchar EFFECT_HAMMER = 0x2;
130       static const uchar EFFECT_PALM_MUTE = 0x2;
131       static const uchar EFFECT_TREMOLO = 0x4;
132       static const uchar EFFECT_LET_RING = 0x8;
133       static const uchar EFFECT_SLIDE_OLD = 0x4;
134       static const uchar EFFECT_SLIDE = 0x8;
135       static const uchar EFFECT_GRACE = 0x10;
136       static const uchar EFFECT_ARTIFICIAL_HARMONIC = 0x10;
137       static const uchar EFFECT_TRILL = 0x20;
138       static const uchar EFFECT_GHOST = 0x01;
139 
140       // arpeggio direction masks
141       static const uchar ARPEGGIO_UP = 0xa;
142       static const uchar ARPEGGIO_DOWN = 0x2;
143 
144       // note bit masks
145       static const uchar NOTE_GHOST = 0x04; // 2
146       static const uchar NOTE_DEAD = 0x20; //5
147       static const uchar NOTE_DYNAMIC = 0x10; // 4
148       static const uchar NOTE_FRET = 0x20; //5
149       static const uchar NOTE_FINGERING = 0x80; //7
150       static const uchar NOTE_MARCATO = 0x02; //1
151       static const uchar NOTE_SFORZATO = 0x40; //6
152       static const uchar NOTE_SLUR = 0x8;//3
153       static const uchar NOTE_APPOGIATURA = 0x02;//1
154 
155       // beat bit masks
156       static const uchar BEAT_VIBRATO_TREMOLO = 0x02;
157       static const uchar BEAT_FADE = 0x10;
158       static const uchar BEAT_EFFECT = 0x20;
159       static const uchar BEAT_TREMOLO = 0x04;
160       static const uchar BEAT_ARPEGGIO = 0x40;
161       static const uchar BEAT_STROKE_DIR = 0x02;
162       static const uchar BEAT_DOTTED = 0x01;
163       static const uchar BEAT_PAUSE = 0x40;
164       static const uchar BEAT_TUPLET = 0x20;
165       static const uchar BEAT_LYRICS = 0x4;
166       static const uchar BEAT_EFFECTS = 0x8;
167       static const uchar BEAT_MIX_CHANGE = 0x10;
168       static const uchar BEAT_CHORD = 0x2;
169 
170       // score bit masks
171       static const uchar SCORE_TIMESIG_NUMERATOR = 0x1;
172       static const uchar SCORE_TIMESIG_DENOMINATOR = 0x2;
173       static const uchar SCORE_REPEAT_START = 0x4;
174       static const uchar SCORE_REPEAT_END = 0x8;
175       static const uchar SCORE_MARKER = 0x20;
176       static const uchar SCORE_VOLTA = 0x10;
177       static const uchar SCORE_KEYSIG = 0x40;
178       static const uchar SCORE_DOUBLE_BAR = 0x80;
179 
180       // slide kinds
181       static const int SHIFT_SLIDE = 1;
182       static const int LEGATO_SLIDE = 2;
183       static const int SLIDE_OUT_DOWN = 4;
184       static const int SLIDE_OUT_UP = 8;
185       static const int SLIDE_IN_ABOVE = 16;
186       static const int SLIDE_IN_BELOW = 32;
187 
188       static const int MAX_PITCH = 127;
189       static const char* const errmsg[];
190       int version;
191       int key { 0 };
192 
193       Segment* last_segment   { nullptr };
194       Measure* last_measure   { nullptr };
195       int last_tempo          { -1 };
196 
197       QMap<int, QList<GPFermata>*> fermatas;
198       std::vector<Ottava*> ottava;
199       Hairpin** hairpins;
200       MasterScore* score;
201       QFile* f;
202       int curPos;
203       int previousTempo;
204       int previousDynamic;
205       std::vector<int> ottavaFound;
206       std::vector<QString> ottavaValue;
207       std::map<int, std::pair<int, bool>> tempoMap;
208       int tempo;
209       QMap<int,int> slides;
210 
211       GPLyrics gpLyrics;
212       int slide;
213       int voltaSequence;
214       QTextCodec* _codec { 0 };
215       Slur** slurs       { nullptr };
216 
217       void skip(qint64 len);
218       void read(void* p, qint64 len);
219       int readUChar();
220       int readChar();
221       QString readPascalString(int);
222       QString readWordPascalString();
223       QString readBytePascalString();
224       int readInt();
225       QString readDelphiString();
226       void readVolta(GPVolta*, Measure*);
227       virtual void readBend(Note*);
228       virtual bool readMixChange(Measure* measure);
229       virtual int readBeatEffects(int track, Segment*) = 0;
230       void readLyrics();
231       void readChannels();
232       void setTuplet(Tuplet* tuplet, int tuple);
233       void setupTupletStyle(Tuplet* tuplet);
234       Fraction len2fraction(int len);
235       void addDynamic(Note*, int d);
236       void createMeasures();
237       void applyBeatEffects(Chord*, int beatEffects);
238       void readTremoloBar(int track, Segment*);
239       void readChord(Segment* seg, int track, int numStrings, QString name, bool gpHeader);
240       void restsForEmptyBeats(Segment* seg, Measure* measure, ChordRest* cr, Fraction& l, int track, const Fraction& tick);
241       void createSlur(bool hasSlur, int staffIdx, ChordRest* cr);
242       void createOttava(bool hasOttava, int track, ChordRest* cr, QString value);
243       void createSlide(int slide, ChordRest* cr, int staffIdx, Note* note = nullptr);
244       void createCrecDim(int staffIdx, int track, const Fraction& tick, bool crec);
245       Text* addTextToNote(QString, Align, Note*);
246       void addPalmMute(Note*);
247       void addLetRing(Note*);
248       void addVibrato(Note*, Vibrato::Type type = Vibrato::Type::GUITAR_VIBRATO);
249       void addTap(Note*);
250       void addSlap(Note*);
251       void addPop(Note*);
252       void createTuningString(int strings, int tuning[]);
253 
254       std::vector<PalmMute*> _palmMutes;
255       std::vector<LetRing*> _letRings;
256       std::vector<Vibrato*> _vibratos;
257 
258    public:
259       std::vector<std::string> tunings;
260 
261       void setTempo(int n, Measure* measure);
262       void initGuitarProDrumset();
263       QString title, subtitle, artist, album, composer;
264       QString transcriber, instructions;
265       QStringList comments;
266       GpTrack channelDefaults[GP_MAX_TRACK_NUMBER * 2];
267       int staves;
268       int measures;
269       QList<GpBar> bars;
270 
271       enum class GuitarProError : char { GP_NO_ERROR, GP_UNKNOWN_FORMAT,
272          GP_EOF, GP_BAD_NUMBER_OF_STRINGS
273             };
274 
275       GuitarPro(MasterScore*, int v);
276       virtual ~GuitarPro();
277       virtual bool read(QFile*) = 0;
error(GuitarProError n)278       QString error(GuitarProError n) const { return QString(errmsg[int(n)]); }
279       };
280 
281 //---------------------------------------------------------
282 //   GuitarPro1
283 //---------------------------------------------------------
284 
285 class GuitarPro1 : public GuitarPro {
286 
287    protected:
288       bool readNote(int string, Note* note);
289       virtual int readBeatEffects(int track, Segment*);
290 
291    public:
GuitarPro1(MasterScore * s,int v)292       GuitarPro1(MasterScore* s, int v) : GuitarPro(s, v) {}
293       virtual bool read(QFile*);
294       };
295 
296 //---------------------------------------------------------
297 //   GuitarPro2
298 //---------------------------------------------------------
299 
300 class GuitarPro2 : public GuitarPro1 {
301 
302    public:
GuitarPro2(MasterScore * s,int v)303       GuitarPro2(MasterScore* s, int v) : GuitarPro1(s, v) {}
304       virtual bool read(QFile*);
305       };
306 
307 //---------------------------------------------------------
308 //   GuitarPro3
309 //---------------------------------------------------------
310 
311 class GuitarPro3 : public GuitarPro1 {
312       virtual int readBeatEffects(int track, Segment* segment);
313 
314    public:
GuitarPro3(MasterScore * s,int v)315       GuitarPro3(MasterScore* s, int v) : GuitarPro1(s, v) {}
316       virtual bool read(QFile*);
317       };
318 
319 //---------------------------------------------------------
320 //   GuitarPro4
321 //---------------------------------------------------------
322 
323 class GuitarPro4 : public GuitarPro {
324       std::vector<int> curDynam;
325       std::vector<int> tupleKind;
326       void readInfo();
327       bool readNote(int string, int staffIdx, Note* note);
328       virtual int readBeatEffects(int track, Segment* segment);
329       virtual bool readMixChange(Measure* measure);
330       int convertGP4SlideNum(int slide);
331 
332    public:
GuitarPro4(MasterScore * s,int v)333       GuitarPro4(MasterScore* s, int v) : GuitarPro(s, v) {}
334       virtual bool read(QFile*);
335       };
336 
337 //---------------------------------------------------------
338 //   GuitarPro5
339 //---------------------------------------------------------
340 
341 class GuitarPro5 : public GuitarPro {
342       std::map<std::pair<int, int>, bool> dead_end;
343       int _beat_counter{ 0 };
344       void readInfo();
345       void readPageSetup();
346       virtual int readBeatEffects(int track, Segment* segment);
347       bool readNote(int string, Note* note);
348       virtual bool readMixChange(Measure* measure);
349       void readMeasure(Measure* measure, int staffIdx, Tuplet*[], bool mixChange);
350       int readArtificialHarmonic();
351       bool readTracks();
352       void readMeasures(int startingTempo);
353       Fraction readBeat(const Fraction& tick, int voice, Measure* measure, int staffIdx, Tuplet** tuplets, bool mixChange);
354       bool readNoteEffects(Note*);
355 
356    public:
GuitarPro5(MasterScore * s,int v)357       GuitarPro5(MasterScore* s, int v) : GuitarPro(s, v) {}
358       virtual bool read(QFile*);
359       };
360 
361 //---------------------------------------------------------
362 //   GuitarPro6
363 //---------------------------------------------------------
364 
365 class GuitarPro6 : public GuitarPro {
366 
367       Fraction _lastTick;
368       Volta* _lastVolta{ nullptr };
369       // an integer stored in the header indicating that the file is not compressed (BCFS).
370       const int GPX_HEADER_UNCOMPRESSED = 1397113666;
371       // an integer stored in the header indicating that the file is not compressed (BCFZ).
372       const int GPX_HEADER_COMPRESSED = 1514554178;
373       int position = 0;
374       // a constant storing the amount of bits per byte
375       const int BITS_IN_BYTE = 8;
376       // contains all the information about notes that will go in the parts
377       struct GPPartInfo {
378             QDomNode masterBars;
379             QDomNode bars;
380             QDomNode voices;
381             QDomNode beats;
382             QDomNode notes;
383             QDomNode rhythms;
384             };
385       Slur** legatos;
386       // a mapping from identifiers to fret diagrams
387       QMap<int, FretDiagram*> fretDiagrams;
388       void parseFile(const char* filename, QByteArray* data);
389       int readBit(QByteArray* buffer);
390       QByteArray getBytes(QByteArray* buffer, int offset, int length);
391       void readGPX(QByteArray* buffer);
392       int readInteger(QByteArray* buffer, int offset);
393       QByteArray readString(QByteArray* buffer, int offset, int length);
394       int readBits(QByteArray* buffer, int bitsToRead);
395       int readBitsReversed(QByteArray* buffer, int bitsToRead);
396       void readScore(QDomNode* metadata);
397       void readChord(QDomNode* diagram, int track);
398       int findNumMeasures(GPPartInfo* partInfo);
399       void readMasterTracks(QDomNode* masterTrack);
400       void readDrumNote(Note* note, int element, int variation);
401       Fraction readBeats(QString beats, GPPartInfo* partInfo, Measure* measure, const Fraction& startTick, int staffIdx, int voiceNum, Tuplet* tuplets[], int measureCounter);
402       void readBars(QDomNode* barList, Measure* measure, ClefType oldClefId[], GPPartInfo* partInfo, int measureCounter);
403       virtual void readTracks(QDomNode* tracks);
404       void readMasterBars(GPPartInfo* partInfo);
405       Fraction rhythmToDuration(QString value);
406       Fraction fermataToFraction(int numerator, int denominator);
407       QDomNode getNode(const QString& id, QDomNode currentDomNode);
408       void unhandledNode(QString nodeName);
409       void makeTie(Note* note);
410       void addTremoloBar(Segment* segment, int track, int whammyOrigin, int whammyMiddle, int whammyEnd);
411 
412       std::map<std::pair<int, int>, Note*> slideMap;
413 
414    protected:
415       const static std::map<QString, QString> instrumentMapping;
416       int* previousDynamic;
417       void readGpif(QByteArray* data);
418       void readNote(int string, Note* note);
419       virtual int readBeatEffects(int track, Segment*);
420       void readTrackProperties(const QDomNode& currentNode, Part* part, int trackCounter, bool& hasTuning);
421 
422    public:
GuitarPro6(MasterScore * s)423       GuitarPro6(MasterScore* s) : GuitarPro(s, 6) {}
GuitarPro6(MasterScore * s,int v)424       GuitarPro6(MasterScore* s, int v) : GuitarPro(s, v) {}
425       virtual bool read(QFile*);
426       };
427 
428 class GuitarPro7 : public GuitarPro6 {
429       virtual void readTracks(QDomNode* tracks);
430 
431    public:
GuitarPro7(MasterScore * s)432       GuitarPro7(MasterScore* s) : GuitarPro6(s, 7) {}
433       virtual bool read(QFile*);
434       };
435 
436 } // namespace Ms
437 #endif
438