-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathGRStaffManager.cpp
More file actions
3488 lines (3048 loc) · 106 KB
/
GRStaffManager.cpp
File metadata and controls
3488 lines (3048 loc) · 106 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
GUIDO Library
Copyright (C) 2002 Holger Hoos, Juergen Kilian, Kai Renz
Copyright (C) 2002-2017 Grame
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
Grame Research Laboratory, 11, cours de Verdun Gensoul 69002 Lyon - France
research@grame.fr
*/
#ifdef WIN32
# pragma warning (disable : 4800)
#endif
// - Standard C++
#include <typeinfo>
#include <cmath>
#include <iostream>
// - Guido Misc
#include "kf_vect.h"
// - Guido AR
#include "TagParameterFloat.h"
#include "ARClef.h"
#include "ARKey.h"
#include "ARMusic.h"
#include "ARMusicalTag.h"
#include "ARMusicalVoice.h"
#include "ARPageFormat.h"
#include "ARSystemFormat.h"
#include "ARAuto.h"
#include "ARDisplayDuration.h"
#include "ARAccolade.h"
// - Guido GR
#include "GRSystem.h"
#include "GRClef.h"
#include "GRKey.h"
#include "GRSpring.h"
#include "GRSpringForceIndex.h"
#include "GRVoice.h"
#include "GRMusic.h"
#include "GRPossibleBreakState.h"
#include "GRBar.h"
#include "GRTempo.h"
#include "GRPageText.h"
#include "GRRepeatEnd.h"
#include "GREmpty.h"
#include "GRMeter.h"
#include "GRChordTag.h"
#include "GRSystemSlice.h"
#include "GRSpacingMatrix.h"
#include "GRPage.h"
#include "GRBreakMatrix.h"
#include "GRSliceHeight.h"
#include "GRVoiceManager.h"
#include "GRStaffManager.h"
// if _DEBUGSFF is set, then the temporary SpaceForceFunctions
// for all potentail system breaks are written into files.
// also look into GRSystem for a similar output routine for
// the actual SpaceForceFunction being realised.
//#undef _DEBUGSFF
// #define _DEBUGSFF
#if 0
#define traceslice(a) a
#else
#define traceslice(a)
#endif
using namespace std;
const bool kIsGiesekingSpacing = true;
/** \brief The StaffManager is responsible for traversing the voice and
really doing everything.
The systems are created whenever real NewLines are
encountered (or automatic once are introduced).
*/
GRStaffManager::GRStaffManager(GRMusic * p_grmusic, ARPageFormat * inPageFormat, const GuidoLayoutSettings * aSettings)
: mSystemDistancePrev(-1.0f),
mSystemDistance(-1.0f),
staffposvect(0),
mCurSysFormat(NULL),
mSystemSlices(new sysslicelist(0)),
beg_sff_list(new bsfflist(1)),
beg_spr_list(new sprlist(1)),
staffTopVector(0),
staffBottomVector(0),
lastrod(NULL),
firstrod(NULL),
relativeTimePositionOfGR(0,1),
voiceSpringArr(-1),
evlist(1)
{
if(aSettings) {
// Keep the settings internally
this->settings = *aSettings;
// This code was in old function guido_applySettings. We have to do that to have the same comportement.
this->settings.neighborhoodSpacing = (aSettings->neighborhoodSpacing == 1 ? true : false);
this->settings.optimalPageFill = (aSettings->optimalPageFill == 1 ? true : false);
this->settings.proportionalRenderingForceMultiplicator =
(aSettings->proportionalRenderingForceMultiplicator < 0.0001 ? 0 : aSettings->proportionalRenderingForceMultiplicator);
}
else {
// Apply default layout settings
GuidoGetDefaultLayoutSettings (&this->settings);
}
mIsBreak = false;
mArAuto = NULL;
mStaffStateVect = NULL;
mTempSpringID = 1;
mSpringID = 1;
mLastSpringID = 0;
mNewLinePage = 0;
// These are pointers to the maximum width clef and the maximum width key. These are saved so that
// the beginning sff can be updated easily .....
mMaxClef = NULL;
mMaxKey = NULL;
mGrMusic = p_grmusic;
mArMusic = mGrMusic->getARMusic();
// now we create the very first page and also the very first system
mGrPage = new GRPage( mGrMusic, this, DURATION_0, settings, NULL );
if (inPageFormat)
mGrPage->setPageFormat(inPageFormat);
mGrMusic->addPage(mGrPage);
// a GRSystem is build later from the slices ...
// Or should we start with at least one system this is not yet clear -> we leave it NULL at first.
mGrSystem = NULL;
// the very first SystemSlice ....
mGrSystemSlice = new GRSystemSlice(this,DURATION_0);
// this is arguable: the 0 is the first glue, this is only for the very first start ....
mGrSystemSlice->mStartSpringID = 0;
mMyStaffs = new VStaff(0); // the Staves are not owned by the Manager
// An Array of Voice-Managers
mVoiceMgrList = new VoiceManagerList(1); // ownselements
// these are needed for backup, if newline is done "manually"
deletedElements = NULL;
// now we create the lists and vector. does own the elements!
mSimpleRods = new IRodList(1);
mComplexRods = new IRodList(1);
mSpringVector = new ISpringVector(1);
// The SpaceForceFunction ...
// The sff is used for the systemslices and to later find the optimum system breaks
cursff = new GRSpaceForceFunction2(settings.force);
// put one single (beginning) spring in the springvector, with id 0.
GRSpring * spr = new GRSpring(relativeTimePositionOfGR, DURATION_0, settings.spring, settings.proportionalRenderingForceMultiplicator);
spr->setID(0);
spr->change_const(50);
mSpringVector->Set(0,spr);
// PossibleBreakState List this will be reviewd shortly!
pblist = new GRPBList(1);
}
// ----------------------------------------------------------------------------
GRStaffManager::~GRStaffManager()
{
GRVoiceManager::resetCurrentNotesTP();
if (mSystemSlices)
{
// This is important for ABORT issues ...
// if the parse was aborted, the systemslices have not been transfered to the Systems yet
if (mSystemSlices->GetCount() > 0)
mSystemSlices->setOwnership( true );
delete mSystemSlices;
mSystemSlices = 0;
}
beg_sff_list->RemoveAll();
delete beg_sff_list;
delete beg_spr_list;
// never owned ...
delete mStaffStateVect;
// the staffs are not owned directly but are handled by the mGrSystemSlice which is deleted below .
delete mMyStaffs;
mMyStaffs = 0;
// already has ownership ...
delete mVoiceMgrList; mVoiceMgrList = 0;
delete deletedElements;
delete mSimpleRods;
delete mComplexRods;
delete mSpringVector;
delete cursff;
delete pblist;
delete mGrSystemSlice;
}
// ----------------------------------------------------------------------------
// this method has been created to structure the createStaves method
// D.F. Nov. 2016
// this method computes time positions as well as new line information,
// possible break value and a flag for tags management (conttagmode)
// returns a boolean value to indicate the end of all voices
bool GRStaffManager::nextTimePosition (int nvoices, bool filltagmode, TCreateStavesState& state)
{
TYPE_TIMEPOSITION timepos;
bool end = true;
for (int i = 0; i < nvoices; i++)
{
GRVoiceManager *voiceManager = mVoiceMgrList->Get(i);
timepos = state.timePos;
// this does the next timeposition ...
// behavior:
// 1. independant of filltagmode:
// if curtp > tmptp then tmptp is set to curtp and -1 is returned.
// if we are at the very end of a voice -5 is returned.
// 2. dependant on the filltagmode
// different behaviour occurs:
// filltagmode = 1:
// if there are tags or events with dur==0 at the current VoicePosition, then the
// respective NotationElements are created (tags that do not have a graphical representation
// are also parsed one after another anyways -> one could introduce an internal loop here...) and 0 is returned;
// if the tag is newSystem or newPage -3 and -4 are returned respectivly.
// if there is an event with dur>0 than -2 is returned.
// filltagmode = 0:
// if there is an event at tmptp then the GRNotationElement is created, the curtp is incremented;
// 0 is returned.
// if there is no event here, than -2 is returned -> switch to filltagmode!
// ret = tmp->Next(tmptp, filltagmode);
// see Iterate-description for documentation
int ret = voiceManager->Iterate(timepos, filltagmode);
// there is still a voice active ...
if (ret != GRVoiceManager::ENDOFVOICE)
end = false;
// minswitchtp remembers the next timeposition for an event. If the
// filltagmode is 0 after this loop, this value is taken for incrementing the curTP.
// This value is also important for potential Breakpoint determination.
if (ret == GRVoiceManager::CURTPBIGGER_ZEROFOLLOWS || ret == GRVoiceManager::DONE_ZEROFOLLOWS)
{
if (timepos < state.minswitchtp)
state.minswitchtp = timepos;
}
if (ret != GRVoiceManager::MODEERROR) {
if (filltagmode)
{
if (!state.conttagmode && ret == GRVoiceManager::DONE_ZEROFOLLOWS)
state.conttagmode = 1; // there has been at last one tag -> continue with filltagmode
if (ret == GRVoiceManager::NEWSYSTEM) { // newSystem.
if (state.newline == 0 || state.newline == 3)
state.newline = kNewSystem;
}
else if (ret == GRVoiceManager::NEWPAGE) // a newPage ...
state.newline = kNewPage; // this overides any other newline setting
else if (ret == GRVoiceManager::PBREAK) // a potential Break ...
{
if (state.newline == 0) {
state.newline = kPBreak;
state.pbreakval = voiceManager->pbreakval;
}
}
}
else { // no filltagmode (that is eventmode)
if (ret == GRVoiceManager::DONE_ZEROFOLLOWS || ret == GRVoiceManager::DONE_EVFOLLOWS ||
ret == GRVoiceManager::DONE || ret == GRVoiceManager::CURTPBIGGER_EVFOLLOWS ||
ret == GRVoiceManager::CURTPBIGGER_ZEROFOLLOWS )
{
// set the increment of timeposition
if (timepos < state.mintp) state.mintp = timepos;
}
}
}
}
return end;
}
// ----------------------------------------------------------------------------
float GRStaffManager::systemBreak (int newlineMode, float beginheight)
{
beginheight = FindOptimumBreaks( newlineMode, beginheight );
// we need to call the "break-algorithm" with the systemslice-list and just force
// the correct break at the given position....
// this means, we are dealing with a line or page-break at this point.
// Right now I just delete the rods ...
delete mSimpleRods; // TODO: optimise
mSimpleRods = new IRodList(1);
delete mComplexRods; // TODO: optimise
mComplexRods = new IRodList(1);
// the springs should be part of the whatever
delete mSpringVector;
// We need a new spring-vector .... we just start completely anew ....
mSpringVector = new ISpringVector(1);
// put one single (beginning) spring in the springvector, with id 0.
GRSpring * spr = new GRSpring(relativeTimePositionOfGR, DURATION_0, settings.spring, settings.proportionalRenderingForceMultiplicator);
spr->setID(0);
spr->change_const(50);
mSpringVector->Set(0,spr);
mTempSpringID = 1;
mSpringID = 1;
mLastSpringID = 0;
return beginheight;
}
// ----------------------------------------------------------------------------
int GRStaffManager::initVoices(int cnt)
{
ARMusicalVoice * voice;
GuidoPos pos = mArMusic->GetHeadPosition();
while (pos) {
voice = mArMusic->GetNext(pos);
// this is important, so that chords are not overread but the explicit events are read.
voice->setReadMode(ARMusicalVoice::EVENTMODE);
GRVoiceManager * voiceManager = new GRVoiceManager(mGrMusic, this, voice, cnt );
// this is the array of VoiceManagers ...
mVoiceMgrList->Set( cnt++, voiceManager );
// This call initializes the GRVoiceManager
// ATTENTION: realise the significance of STAFF-Tags at the very start!
voiceManager->BeginManageVoice();
voice->doAutoCluster();
}
return cnt;
}
// ----------------------------------------------------------------------------
/** \brief Creates and fills the staves.
it uses GRVoiceManagers to go through the individual voices.
*/
void GRStaffManager::createStaves()
{
// We have a ARmusic (armusic) and start from the beginning ...
// now we go through all voices and create graphical representations. We also
// add these to the respective staves (standard or handled by staff-tag)
int cnt = initVoices(0);
TCreateStavesState state;
state.timePos = relativeTimePositionOfGR;
state.newline = kNormalState;
state.pbreakval = 0;
// this parameter is used to capture explicit \newSystem tags. The height
// of the page is returned by FindOptimum in the case of a newSystem-call
float beginheight = 0;
// This setting controls if the VoiceManagers go to the next event.
int filltagmode = 1;
bool ender = false; // this is set if all voices end ....
// this remembers the current mSystemSize (very crude indeed!)
mSystemSize = 0;
do
{
state.conttagmode = 0; // this determines whether the filltagmode should be maintained, so that all tags can be read in ...
ender = true; // this is set to one and unset, if there is at least one voice still active
state.newline = kNormalState; // newline is reset.
// the time is incremented by the minimum amount of time...
state.mintp = MAX_DURATION;
state.minswitchtp = MAX_DURATION;
state.pbreakval = 0; // the pbreakval ...
// now, we go through all the voices sequentially
ender = nextTimePosition (cnt, filltagmode, state);
// now we need to check whether we need to switch to no-filltagmode
if (filltagmode)
{
// we had tags; now we check, whether tags follow
if (state.conttagmode == 0)
{
// no tags follow, so now we finish the Synchronization-Slice at the current tp.
// This handles spring-stuff...
FinishSyncSlice(state.timePos);
filltagmode = 0;
GRSpaceForceFunction2 * sff = 0;
if (state.newline != kNormalState)
{
// this builds the current Spring-Force-Function (that is, rods and springs and stuff is created).
sff = BuildSFF();
// I must create a new possiblebreakstate and add that to the syncslice
GRPossibleBreakState * pbs = new GRPossibleBreakState();
pbs->sff = sff;
pbs->copyofcompletesff = NULL;
float force = 0;
pbs->SaveState( mMyStaffs, mVoiceMgrList, this, state.timePos, force, state.pbreakval);
mGrSystemSlice->addPossibleBreakState(pbs);
// now we need to add the systemslice to the list of available system-slices ....
mGrSystemSlice->setNumber(mSystemSlices->GetCount() + 1);
mGrSystemSlice->mEndSpringID = mSpringID - 1;
// this is the location, where the systemslice is "officially" finished.
mGrSystemSlice->Finish();
mSystemSlices->AddTail(mGrSystemSlice);
// then we build a new mGrSystemSlice, and start a new ...
mGrSystemSlice = NULL;
mLastSpringID = mSpringID;
}
if (state.newline == kNewSystem || state.newline == kNewPage)
beginheight = systemBreak( state.newline, beginheight );
if (state.newline == kNewPage)
{
// a newpage
GRPage * newpage = new GRPage( mGrMusic, this, state.timePos, settings, mGrPage );
newpage->setColor(mGrPage->getColor());
mGrPage = newpage;
mGrMusic->addPage( mGrPage );
beginheight = 0;
}
if (state.newline != kNormalState)
{
// not sure of this ....
// now we tell each voice, that we have handled the break-condition
// remember that we are breaking righ now mIsBreak = true;
// we actually have to create a new systemslice so that we can put something together ....
mGrSystemSlice = new GRSystemSlice(this, state.timePos);
// This is important: a newline == 3 means, that this is only a potential breakpoint
// newline != 3 is a user-imposed real breakpoint
// In this case, the begin elements of all staves are already there and will not be
// added manually therefore, the first spring is the glue spring and will be present.
if (state.newline == kPBreak)
mGrSystemSlice->mStartSpringID = mSpringID;
else
mGrSystemSlice->mStartSpringID = 0;
delete mMyStaffs;
mMyStaffs = new VStaff(0); // the Staves are no longer valid and need to be created a new ...
for (int i = 0; i < cnt; i++)
{
GRVoiceManager * voiceManager = mVoiceMgrList->Get(i);
voiceManager->DoBreak( state.timePos, state.newline);
}
mSystemSize = 0;
filltagmode = 1;
}
}
}
else // no filltagmode...
{
if (state.mintp != MAX_DURATION)
state.timePos = state.mintp; // we increment the timeposition ...
else if (!ender)
assert(false); // there must have been a timeposition
// if we need to switch, then we switch
if (state.minswitchtp == state.timePos)
filltagmode = 1;
FinishSyncSlice(state.timePos);
}
// new: we do a very crude test, if the current space runs low ... This is in no ways accurate ( but it needn't be).
// It works as follows:
// Each time, an element is added to a staff we add the left- and right-space and compare
// it to a maximum value. All this is summed up for the whole system (the maximum for each slice).
// If we reach a value of twice the page-size, we issue a warning and FORCE A NEW SYSTEM! -> otherwise the algorithms run too long.
// is there an optimum force?
// if the force is alright
} while ( /* !sizer && */ !ender);
// checkAccidentalsCollisions();
finishStaves (state, beginheight);
}
//----------------------------------------------------------------------------------
void GRStaffManager::checkAccidentalsCollisions()
{
const int mini = mMyStaffs->GetMinimum();
const int maxi = mMyStaffs->GetMaximum();
for( int i = mini; i <= maxi; ++i )
{
GRStaff * staff = mMyStaffs->Get(i);
if (staff) {
cerr << "GRStaffManager::checkAccidentalsCollisions staff " << i << endl;
NEPointerList* elts = staff->getElements();
GuidoPos pos = elts->GetHeadPosition();
while (pos) {
GRNotationElement * e = elts->GetNext(pos);
if (e) {
cerr << " check " << e << endl;
}
}
}
}
}
//----------------------------------------------------------------------------------
// was initially at the end of createStaves
void GRStaffManager::finishStaves (const TCreateStavesState& state, float beginheight)
{
// I have to rethink the following ...
// it may be, that I have to finish the last slice before calling the optimum-Break-Routine ....
if (mSpringVector->GetMaximum() >= mSpringVector->GetMinimum())
{
// there is more than one spring ...
GRSpaceForceFunction2 * sff = 0;
if (mSpringID > mLastSpringID)
{
sff = BuildSFF();
// add the thing to the slice-list
GRPossibleBreakState * pbs = new GRPossibleBreakState();
pbs->sff = sff;
pbs->copyofcompletesff = NULL;
float force = 0;
pbs->SaveState(mMyStaffs, mVoiceMgrList, this, state.timePos, force, state.pbreakval);
mGrSystemSlice->addPossibleBreakState(pbs);
// now we need to add the systemslice to the list of available system-slices ....
mGrSystemSlice->setNumber(mSystemSlices->GetCount() + 1);
mGrSystemSlice->mEndSpringID = mSpringID-1;
mGrSystemSlice->Finish();
mSystemSlices->AddTail(mGrSystemSlice);
mGrSystemSlice = NULL;
}
}
FindOptimumBreaks( 0, beginheight );
}
/** \brief Called from the voice-managers.
The StaffManager has to make sure that the staff is there, otherwise it needs to create it.
*/
void GRStaffManager::prepareStaff(int staff)
{
GRStaff * curstaff = mMyStaffs->Get(staff);
if (curstaff == NULL) {
curstaff = new GRStaff(mGrSystemSlice, settings.proportionalRenderingForceMultiplicator);
if (mStaffStateVect) {
// this just copies the stateinformation ...
GRStaffState * myss = mStaffStateVect->Get(staff);
if (myss) {
// this sets the information explicitly that would otherwise be automatically
// copied with CreateBeginElements if an automatic Break instead of a
// forces break would occur.
curstaff->setStaffState(myss);
}
}
mGrSystemSlice->addStaff(curstaff,staff);
// We apply potential staff size defined with GuidoSetStaffSize API call
applyStaffSize(curstaff, staff);
}
// set the staff in Vector mMyStaffs.
mMyStaffs->Set(staff, curstaff);
}
GRStaff * GRStaffManager::getStaff(int staff)
{
GRStaff * curstaff = mMyStaffs->Get(staff);
if ( curstaff == NULL ) prepareStaff(staff);
return mMyStaffs->Get(staff);
}
// the functions below are never called D.F.
//
//void GRStaffManager::setRelativeEndTimePosition(const TYPE_TIMEPOSITION & tp)
//{
// mDurationOfGR = tp - relativeTimePositionOfGR;
//}
//
//void GRStaffManager::EndSystem(ARMusicalVoice * arVoice, GuidoPos pos)
//{
// // pos is the Position of the newLine -Tag
// setRelativeEndTimePosition( arVoice->GetAt(pos)->getRelativeEndTimePosition() );
//}
//
//void GRStaffManager::EndPage(ARMusicalVoice * voice, GuidoPos pos)
//{
// // pos is the Position of the newPage-Tag
// setRelativeEndTimePosition ( voice->GetAt(pos)->getRelativeEndTimePosition() );
//}
float GRStaffManager::getSystemWidthCm()
{
return mGrSystem ? mGrSystem->getSystemWidthCm() : 0;
}
/** \brief Returns mNewLinePage, which is set at the End of the Voice-Managing-PRocess.
It is 1, if a newLine ended the process
it is 2, if a newPage ended the process
it is 3, if a voice-end ended the process
it is 0, if a sizer ended the process.
*/
int GRStaffManager::getNewLinePage() const
{
return mNewLinePage;
}
/** \brief Sets a staff-state-tag. (like clef, meter, key)
If the staff is not already there, it is created first.
Remember, this just sets the state; no graphical elements are created here.
return false on error (i.e: input tag was not a staff-state tag)
*/
bool GRStaffManager::setStaffStateTag( const ARMusicalTag * tag, int staffnum )
{
if (tag == 0) return false;
GRStaff * grstaff = getStaff(staffnum);
bool result = true;
const type_info & ti = typeid(*tag);
// this is set if the beginning sff needs to be updated ....
bool needupdate = false;
GRStaffState & mystate = grstaff->getGRStaffState();
if (ti == typeid(ARClef)) // we should consider using a dynamic cast only
{
const ARClef * tmp = static_cast<const ARClef *>(tag);
needupdate = true;
// now check, wether the curclef is already set for the same timeposition and with different parameters.
if (mystate.curclef) {
if (mystate.curclef->getRelativeTimePosition() == tmp->getRelativeTimePosition() && !(*mystate.curclef == *tmp))
GuidoWarn("Adding a different clef to a staff at the same timeposition");
}
mystate.curclef = tmp;
}
else if (ti == typeid(ARMeter)) {
mystate.curmeter = static_cast<const ARMeter *>(tag); // was dynamic cast<ARMeter *>(tag);
}
else if (ti == typeid(ARKey)) {
needupdate = true;
mystate.curkey = static_cast<const ARKey *>(tag); // was dynamic cast<ARKey *>(tag);
}
else result = false; // was not a staff-state tag.
if (needupdate) UpdateBeginningSFF(staffnum);
return result;
}
/** \brief AddGRSyncElement is called by the GRVoiceManager-class to add those elements, that
need horizontal synchronization.
It uses a temporary spring-id and a data-structure, to keep track of equivalent tags.
When a slice is finished, FinishSyncSlice is called, which handles spring-ID-distribution and
spring-stretching.
The routine works as follows:
If the notationelement is a tag or an event with duration 0, than the type is looked up in a hash-table.
The hash-table has entries of the following type: (type,(grnotationelement,staff,voiceID),mSpringID):
If there is no element of this type, the temporary springid is incremented, and the type (and
element) is added to the hash-table. The entry voice-id of the voice-array is set to the new springid.
If there is an element of this type, it is checked, whether voice-array[voiceID] is
greater than the mSpringID of the entry of the hash-table. In this case, the mSpringID is
incremented and the hashtable-entry is replaced by a new entry. voice-array[voice-id] is set to the
new spring-id.
(There is a special case here: if the current springIDs of ALL voices in the old hash-table-entry are
smaller or equal to the mSpringID of the old hash-table-entry, than the old mSpringID
is REMOVED, that is, all the notationelements are moved to the new hash-table-entry. Each respective
voice-arr[voice-id] entry is set to the new mSpringID).
If the voice-array[voice-id] is smaller than the temporary mSpringID, the notation-element is added to the
same hash-table-entry. The mSpringID of voice-array[voice-id] is set to the value in the hash-table-entry.
*/
int GRStaffManager::AddGRSyncElement (GRNotationElement * grel, GRStaff * grstaff, int voiceID,
GRVoice * vce, GRNotationElement * sameel)
{
if (!grel) return -1;
if (grel->getNeedsSpring() == -1)
{
// then, the element has been added with a sharelocation or a sharestem-tag in mind.
// associated the element with an already assigned spring ...
// In this case, voiceID is really a mSpringID!!!!
// then there has been no spring assigned yet. we have to find the realspringid
GRSpring * spr = 0;
if (voiceID == -1)
{
// then we use sameel ....
assert(sameel != 0);
const type_info &ti = typeid(*sameel);
// convert it, so it can be looked up in the hash-table
NVstring tmp(ti.name());
HashEntry tmphash;
GuidoPos syncpos = syncHash.Lookup(tmp,tmphash);
if ( syncpos != NULL )
{
SubHash * sh = new SubHash();
sh->grel = grel;
sh->grstaff = grstaff;
sh->voiceID = voiceID;
sh->voice = vce;
tmphash.data->AddTail(sh);
}
else {
// we have a problem ....
}
}
else
{
spr = mSpringVector->Get(voiceID);
}
if (spr)
{
spr->addElement(grel,vce);
grel->tellSpringID(spr->getID());
}
grel->setNeedsSpring(0);
// spr->checkLocalCollisions();
return -1;
}
if (!grel->getNeedsSpring()) return -1;
// assert(grel->getDuration() == DURATION_0);
GREvent * theEv = GREvent::cast(grel);
if (theEv && grel->getDuration() > DURATION_0)
{
// here we deal with the events ...
GREvent * grev = theEv;
VoiceEvent * ve = new VoiceEvent;
ve->ev = grev;
ve->vce = vce;
evlist.AddTail(ve);
}
else
{
// and here, we deal with tags ...
const type_info & ti = typeid(*grel);
// convert it, so it can be looked up in the hash-table
NVstring tmp (ti.name());
HashEntry tmphash;
GuidoPos syncpos = syncHash.Lookup(tmp,tmphash);
if (syncpos != NULL )
{
// then, the entry is already there ...
// now, we have to check, wether the voice has a greater or equal current spring-id then the entry ...
if (voiceSpringArr.Get(voiceID)>=tmphash.mSpringID)
{
// now we need to check, wether ALL voices have smaller/equal current voiceIDs as
// the mSpringID of the hash-entry.
// in this case, the spring-ID is set to the bigger number ....
bool found = false;
// check, wether we are in the same voice ...
GuidoPos tmppos = tmphash.data->GetHeadPosition();
while (tmppos)
{
SubHash *sh = tmphash.data->GetNext(tmppos);
if (voiceSpringArr.Get(sh->voiceID)>tmphash.mSpringID || sh->voiceID == voiceID )
{
found = true;
break;
}
}
if (found)
{
// there must be a new entry
// we remove the key, so that the entry is no longer recognized but will be found later...
syncHash.Set(syncpos,"",tmphash);
// owns elements
SubHashList *shl = new SubHashList(1);
SubHash *sh = new SubHash();
sh->grel = grel;
sh->grstaff = grstaff;
sh->voiceID = voiceID;
sh->voice = vce;
shl->AddTail(sh);
tmphash.data = shl;
tmphash.mSpringID = ++mTempSpringID;
voiceSpringArr.Set(voiceID,mTempSpringID);
syncHash.Set(tmp,tmphash);
}
else
{
// we can replace the springIDs. remember to set the voidSpringArr-IDs
tmphash.mSpringID = ++mTempSpringID;
SubHash * sh = new SubHash();
sh->grel = grel;
sh->grstaff = grstaff;
sh->voiceID = voiceID;
sh->voice = vce;
tmphash.data->AddTail(sh);
// this sets the tmphash (because of changed mSpringID)
syncHash.Set(syncpos,tmp,tmphash);
GuidoPos tmppos = tmphash.data->GetHeadPosition();
while (tmppos)
{
SubHash * sh = tmphash.data->GetNext(tmppos);
voiceSpringArr.Set(sh->voiceID,mTempSpringID);
}
}
}
else
{
// is smaller; just add as an entry ...
SubHash * sh = new SubHash();
sh->grel = grel;
sh->grstaff = grstaff;
sh->voiceID = voiceID;
sh->voice = vce;
tmphash.data->AddTail(sh);
voiceSpringArr.Set(voiceID,tmphash.mSpringID);
}
}
else
{
// no entry yet
++mTempSpringID;
// create a new SubHash
SubHash * sh = new SubHash();
sh->grel = grel;
sh->grstaff = grstaff;
sh->voiceID = voiceID;
sh->voice = vce;
// owns elements ...
SubHashList * shl = new SubHashList(1);
shl->AddTail(sh);
tmphash.data = shl;
tmphash.mSpringID = mTempSpringID;
// this sets the SpringID of the voice
voiceSpringArr.Set(voiceID,mTempSpringID);
syncHash.Set(tmp,tmphash);
}
} // if duration_0
return 0;
}
/** \brief Called by the VoiceManager to add a SystemTag to the score.
A System tag (like e.g. \\tempo) is valid for the whole system. Each time, a SyncSlice is finished,
the collected systemtags must be checked for contraditing entries.
The Systemtags must also be assigned to already present springs.
*/
int GRStaffManager::AddSystemTag(GRNotationElement * grel, GRStaff * grstaff, int voiceid)
{
// first, we have to look, wether we already have an entry of the given type at the current time.
// if so, it is ignored (only one System-Tag per SyncSlice).
// if not present, we just add it.
const type_info & ti = typeid(*grel);
// convert it, so it can be looked up in the hash-table
NVstring tmp(ti.name());
GRNotationElement * mytag=0;
GuidoPos systempos = systemHash.Lookup(tmp,mytag);
if (systempos != NULL )
{
// there is an entry of this type already
GuidoWarn("Another SystemTag of the same type is already used");
delete grel;
}
else
systemHash.Set(tmp,grel);
return 0;
}
/** \brief Called by the GRVoiceManager to add those tags that are placed directly on
the page (like e.g. \\title, \\composer etc.).
The elements are just put where they would be put by default.
At the moment, there is no check, whether information is provided more than once ... this
could/should be done later.
*/
int GRStaffManager::AddPageTag(GRNotationElement * grel, GRStaff * grstaff,int voiceid)
{
GRPageText * grpgtxt = dynamic_cast<GRPageText *>(grel);
if (grpgtxt)
{
// the current page (this is known only here, not really in the voice-manager)
grpgtxt->setGRPage(mGrPage);
// this calls the routine to calculate the concrete Position within the page
grpgtxt->calcPosition();
}
// this adds the tag to the page ...
mGrPage->AddTail(grel);
return 0;
}
/** \brief FinishSyncSlice is called when a synchronization cycle is finished.
The routine clears the hashtable-entries and distributes the elements to the staves and
assigning REAL spring-ids.
(Afterwards, the rods can be created; no more ordering of springs is neccessary)
The routine works as follows:
Iterate through the hash-table (ordered by mSpringID).
For each entry, set the mSpringID and add the element to the staff. (This might
cause WARNINGS to be issued by the staff, as there might be two or more elements added with the
samed Spring-ID but conflicting meanings (as in clefs or keys)
*/
int GRStaffManager::FinishSyncSlice(const TYPE_TIMEPOSITION & tp)
{
// first, we sort the tmphash for the springIDs...
int startspringID = mSpringID;
// syncHash is the Hash of Elements ...
if (syncHash.GetCount() > 0)
{
syncHash.sortValues();
HashEntry tmphash;
GuidoPos pos = syncHash.GetHeadPosition();
int conflict = 0;
while(pos)
{
if (syncHash.GetNext(pos,tmphash))
{
// Now, we construct the Spring to put it in the SpaceForceFunction.
GRSpring * spr = new GRSpring(tp, DURATION_0, settings.spring, settings.proportionalRenderingForceMultiplicator);
spr->setID(mSpringID);
// now we need to check whether there are elements in the SAME staff that would be added to the same spring.
// If there are auto-Tags, then these are ignored. If not, only the first one is really used, the others are REMOVED!
GuidoPos tmppos = tmphash.data->GetHeadPosition();
while (tmppos)
{
// we need to compare each of the entries to each other.
SubHash *sh = tmphash.data->GetNext(tmppos);
GRTag * tag = dynamic_cast<GRTag *>(sh->grel);
if (!tag) continue;
if (tag->getError()) continue;
GuidoPos tmppos2 = tmppos;
while (tmppos2)
{
SubHash * sh2 = tmphash.data->GetNext(tmppos2);
if (sh->grstaff == sh2->grstaff)
{
GRStaffState & staffstate = sh->grstaff->getGRStaffState();
GRTag * tag2 = dynamic_cast<GRTag *>(sh2->grel);
if (!tag2) continue;
if (tag->getIsAuto() && !tag2->getIsAuto())
{
// check consistency between staff and clef baseline
GRClef * clef = dynamic_cast<GRClef*>(tag2);
if (clef && (staffstate.baseline != clef->getBaseLine()))
sh->grstaff->setClefParameters(clef);
tag->setError(1);
if ( *tag != *tag2)
conflict = 1;
break;
}
else
{
if (tag2->getIsAuto() && !tag->getIsAuto())
{
// check consistency between staff and clef baseline
GRClef * clef = dynamic_cast<GRClef*>(tag);
if (clef && (staffstate.baseline != clef->getBaseLine()))
sh->grstaff->setClefParameters(clef);
tag2->setError(1);
if (*tag != *tag2)
conflict = 1;
continue;
}