LibMusicXML 3.22
xmlpart2guido.h
1/*
2 MusicXML Library
3 Copyright (C) Grame 2006-2013
4
5 This Source Code Form is subject to the terms of the Mozilla Public
6 License, v. 2.0. If a copy of the MPL was not distributed with this
7 file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
9 Grame Research Laboratory, 11, cours de Verdun Gensoul 69002 Lyon - France
10 research@grame.fr
11 */
12
13#ifndef __xmlpart2guido__
14#define __xmlpart2guido__
15
16#include <ostream>
17#include <stack>
18#include <queue>
19#include <map>
20#include <string>
21
22#include "clefvisitor.h"
23#include "exports.h"
24#include "guido.h"
25#include "keysignvisitor.h"
26#include "metronomevisitor.h"
27#include "notevisitor.h"
28#include "rational.h"
29#include "timesignvisitor.h"
30#include "typedefs.h"
31#include "visitor.h"
32#include "xml.h"
33
34namespace MusicXML2
35{
36
41
45//______________________________________________________________________________
46class EXP xmlpart2guido :
47public clefvisitor,
48public timesignvisitor,
49public notevisitor,
50public keysignvisitor,
51public visitor<S_backup>,
52public visitor<S_barline>,
53public visitor<S_coda>,
54public visitor<S_direction>,
55public visitor<S_divisions>,
56public visitor<S_ending>,
57public visitor<S_forward>,
58public visitor<S_measure>,
59public visitor<S_part>,
60public visitor<S_repeat>,
61public visitor<S_segno>,
62public visitor<S_sound>,
63public visitor<S_attributes> // to get clef, division, staves, time and key in order!
64{
65 // the guido elements stack
66 std::stack<Sguidoelement> fStack;
67 // structure to store delayed elements ie elements enclosed in direction with offset
68 typedef struct {
69 long delay;
70 Sguidoelement element;
71 } delayedElement;
72 vector<delayedElement> fDelayed;
73 // fields to controls the guido output generation
74 bool fGenerateComments, fGenerateBars, fGeneratePositions, fGenerateAutoMeasureNum, fLyricsManualSpacing;
75
76 // internal parsing state
77 bool fInCue, fInGrace, fInhibitNextBar, fPendingBar, fBeamOpened, fBeamGrouping, fMeasureEmpty, fWavyTrillOpened, fSingleScopeTrill, fNonStandardNoteHead, fDoubleBar, fTremoloInProgress, fShouldStopOctava;
78
79 int fCrescPending, fDiminPending; // XML Number of Crescendo or Diminuedo that have been opened (0 if none)
80
81 int fTextTagOpen;
82 int fCurrentOctavaShift; // 0 if none
83 int fTupletOpen; // Number of opened Tuplets
84 std::queue<int> fTiedOpen; // Number of ongoing opened Tied
85
86 std::vector< std::pair<int, int> > fSlurStack; // first int: Internal num, 2nd int: XML num
87
88 Sguidoelement fLyricOpened;
89
90 bool isProcessingChord;
91
92 S_measure fCurrentMeasure;
93 S_part fCurrentPart;
94
95 bool fNotesOnly; // a flag to generate notes only (used for several voices on the same staff)
96 bool fSkipDirection; // a flag to skip direction elements (for notes only mode or due to different staff)
97 int fCurrentStaffIndex; // the index of the current guido staff
98 int fCurrentStaff; // the staff we're currently generating events for (0 by default)
99 int fTargetStaff; // the musicxml target staff (0 by default)
100 int fTargetVoice; // the musicxml target voice (0 by default)
101
102 int fStartMeasure, fEndMeasure, fEndMeasureOffset; // Starting and Ending measures for Partial conversion. Default=0 meaning non-partial.
103
104 long fCurrentDivision; // the current measure division, expresses the time unit in division of the quarter note
105 long fCurrentOffset; // the current direction offset: represents an element relative displacement in current division unit
106 rational fCurrentMeasureLength; // the current measure length (max of the current measure positions)
107 rational fCurrentMeasurePosition;// the current position in the measure
108 rational fCurrentVoicePosition; // the current position within a voice
109 rational fCurrentScorePosition; // the current position within the parsing score
110 rational fCurrentTimeSign; // the current time signature
111 int fMeasNum; // Internal Measure Number mapping
112
113 int fCurrentStemDirection; // the current stems direction, used for stem direction changes
114 int fPendingPops; // elements to be popped at chord exit (like fermata, articulations...)
115
116 void start (Sguidoelement& elt) { fStack.push(elt); }
117 void add (Sguidoelement& elt) {
118 if (!checkMeasureRange()) return;
119 if (fStack.size())
120 fStack.top()->add(elt);
121 }
122 void addDelayed (Sguidoelement elt, long offset); // adding elements to the delayed elements
123 void checkDelayed (long time, bool before); // checks the delayed elements for ready elements
124 void push (Sguidoelement& elt) {
125 if (!checkMeasureRange()) return;
126 add(elt); fStack.push(elt);
127 }
128 void pop () {
129 if (!checkMeasureRange()) return;
130 fStack.pop();
131 }
132
133 void moveMeasureTime (long duration, bool moveVoiceToo=false);
134 void reset ();
135 void stackClean ();
136
137 int checkArticulation ( const notevisitor& note ); // returns the count of articulations pushed on the stack
138 void checkPostArticulation ( const notevisitor& note );
139
140 int checkChordOrnaments ( const notevisitor& note ); // returns the count of articulations pushed on the stack
141
142 deque<notevisitor> getChord ( const S_note& note ); // build a chord vector
143 std::vector<Sxmlelement> getChord ( const Sxmlelement& note ); // build a chord vector
144 void checkStaff (int staff ); // check for staff change
145 void checkStem ( const S_stem& stem );
146 void checkBeamBegin ( const std::vector<S_beam>& beams, const S_note& elt );
147 void checkBeamEnd ( const std::vector<S_beam>& beams );
148 void checkTupletBegin( const std::vector<S_tuplet>& tuplets,
149 const notevisitor& nv,
150 const S_note& elt);
151 void checkTupletEnd ( const std::vector<S_tuplet>& tuplets );
152 void checkCue ( const notevisitor& nv );
153 void checkGrace ( const notevisitor& nv );
154 void checkGraceEnd(const notevisitor& nv);
155 int checkFermata ( const notevisitor& stem );
156 void checkSlur ( const std::vector<S_slur>& slurs );
157 void checkSlurBegin ( const std::vector<S_slur>& slurs );
158 void checkSlurEnd ( const std::vector<S_slur>& slurs );
159 bool isSlurClosing(S_slur elt);
160 void checkTiedBegin ( const std::vector<S_tied>& tied );
161 void checkTiedEnd ( const std::vector<S_tied>& tied );
162 void checkVoiceTime ( const rational& currTime, const rational& voiceTime);
163 int checkRestFormat ( const notevisitor& nv );
164 bool checkNoteFormat ( const notevisitor& nv , rational posInMeasure);
165 void checkWavyTrillBegin ( const notevisitor& nv );
166 void checkWavyTrillEnd ( const notevisitor& nv );
167 void checkTextEnd();
168 void newNote ( const notevisitor& nv, rational posInMeasure, const std::vector<Sxmlelement>& fingerings);
169 void newChord (const deque<notevisitor>& nvs, rational posInMeasure);
170
171 int checkTremolo(const notevisitor& note, const S_note& elt);
172
173 std::string noteName ( const notevisitor& nv );
174 guidonoteduration noteDuration ( const notevisitor& nv);
175
176 std::vector<S_beam>::const_iterator findValue ( const std::vector<S_beam>& beams, const std::string& val ) const;
177 std::vector<S_slur>::const_iterator findTypeValue ( const std::vector<S_slur>& slurs, const std::string& val ) const;
178 std::vector<S_tied>::const_iterator findTypeValue ( const std::vector<S_tied>& tied, const std::string& val ) const;
179 std::vector< std::pair<int, int> >::const_iterator findSlur ( const int xmlnum ) const;
180
182 void checkLyricBegin ( const std::vector<S_lyric>& lyrics );
183 void checkLyricEnd ( const std::vector<S_lyric>& lyrics );
184 std::vector<S_lyric>::const_iterator findValue ( const std::vector<S_lyric>& lyrics,
185 const std::string& val ) const;
186 std::string lyricParams;
187
188
189 static std::string alter2accident ( float alter );
190
191protected:
192 enum { kStemUndefined, kStemUp, kStemDown, kStemNone };
193 enum { kLeaveChord=-1, kNoChord, kEnterChord } chordState;
194
195 virtual void visitStart( S_backup& elt);
196 virtual void visitStart( S_barline& elt);
197 virtual void visitStart( S_coda& elt);
198 virtual void visitStart( S_direction& elt);
199 virtual void visitStart( S_divisions& elt);
200 virtual void visitStart( S_forward& elt);
201 virtual void visitStart( S_measure& elt);
202 virtual void visitStart( S_note& elt);
203 virtual void visitStart( S_part& elt);
204 virtual void visitStart( S_segno& elt);
205 virtual void visitStart( S_attributes& elt);
206
207 virtual void visitEnd ( S_direction& elt);
208 virtual void visitEnd ( S_ending& elt);
209 virtual void visitEnd ( S_key& elt);
210 virtual void visitEnd ( S_measure& elt);
211 virtual void visitEnd ( S_note& elt);
212 virtual void visitEnd ( S_repeat& elt);
213 virtual void visitEnd ( S_sound& elt);
214 virtual void visitEnd ( S_time& elt);
215 virtual void visitEnd ( S_part& elt);
216
217 std::string parseMetronome ( metronomevisitor &mv );
218
219 void parseWedge(MusicXML2::xmlelement *elt, int staff);
220
221 void parseOctaveShift(MusicXML2::xmlelement *elt, int staff);
224 void parseOctaveShift(int type);
225
226 bool findNextNote(ctree<xmlelement>::iterator& elt, ctree<xmlelement>::iterator &nextnote);
227 float getNoteDistanceFromStaffTop(const notevisitor& nv);
228
229 rational durationInCue;
230
231 std::map<int, float> fStaffDistance;
232
233public:
234 xmlpart2guido(bool generateComments, bool generateStem, bool generateBar = true, int startMeasure = 0, int endMeasure = 0, int endMeasureOffset = 0);
235 virtual ~xmlpart2guido() {}
236
237 Sguidoelement& current () { return fStack.top(); }
238 void initialize (Sguidoelement seq, int staff, int guidostaff, int voice, bool notesonly, rational defaultTimeSign);
239 void generatePositions (bool state) { fGeneratePositions = state; }
240 const rational& getTimeSign () const { return fCurrentTimeSign; }
241 bool hasLyrics() const {return fHasLyrics;}
242
244 std::multimap<int, std::pair< int, std::pair< rational, string > > > staffClefMap;
247
248 rational fStartPosition, fEndPosition;
249
250 int lastMeasureNumber() { return fMeasNum; }
251
252 std::map<double, int> measurePositionMap;
253
254 double totalPartDuration() { return fCurrentScorePosition.toDouble(); }
255
257 std::vector<int> processedDirections;
258
259 // Octavas must be re-applied across voices in the same staff. This property tracks them.
261 std::map<std::string, std::map<rational, int>> octavas;
262
263private:
264 bool fHasLyrics;
265
266 std::string getClef(int staffIndex, rational pos, int measureNum);
267
268 void addPosYforNoteHead(const notevisitor& nv, Sxmlelement elt, Sguidoelement& tag, float offset);
269 void addPosYforNoteHead(const notevisitor& nv, float xmlY, Sguidoelement& tag, float offset);
270
272 void addPositionOrPlacementToNote(const notevisitor& nv, Sxmlelement elt, Sguidoelement& tag, float offset);
273
276 void addDyFromNoteOrStaff(const notevisitor& nv, Sxmlelement elt, Sguidoelement& tag);
277
279 float distanceFromStaffTopForNote(const notevisitor& nv);
280
281 bool checkMeasureRange();
282
283 void parseTime(ctree<xmlelement>::iterator &iter);
284 void parseKey(ctree<xmlelement>::iterator &iter);
285
286 void checkOctavaBegin();
287 void checkOctavaEnd();
288
289 Sguidoelement lastKey; // Storage used for Partial Conversions
290 Sguidoelement lastMeter; // Storage used for Partial Conversions
291 double fPreviousPedalYPos; // Used for musicxml pedal change
292
293 // To skip already visited Directions when looking ahead because of grace notes
294 std::queue<int> fDirectionEraserStack;
295
297 bool findNextNote(MusicXML2::xmlelement *elt, MusicXML2::xmlelement *from);
298};
299
300
301} // namespace MusicXML2
302
303
304#endif
treeIterator< treePtr > iterator
the top -> bottom iterator type
Definition ctree.h:129
A guido note duration representation.
Definition guido.h:164
key element data
Definition metronomevisitor.h:37
Definition visitor.h:27
A generic xml element representation.
Definition xml.h:86
MusicXMLTimePositions timePositions
Containing default-x positions on a fCurrentVoicePosition (double) of measure(int)
Definition xmlpart2guido.h:246
std::multimap< int, std::pair< int, std::pair< rational, string > > > staffClefMap
Map for staffNum, measureNum, voice-Position and Clef.
Definition xmlpart2guido.h:244
std::vector< int > processedDirections
List of already processed Directions in other voices/staves to skip.
Definition xmlpart2guido.h:257
void parseOctaveShift(int type)
std::map< std::string, std::map< rational, int > > octavas
Map containing octavas on a staff: measureNumberString, PositionInMeasure, Type (0 for stop)
Definition xmlpart2guido.h:261
Definition timePositions.h:20
Rational number representation.
Definition rational.h:25