forked from rism-digital/verovio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdurationinterface.cpp
194 lines (167 loc) · 5.96 KB
/
durationinterface.cpp
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
/////////////////////////////////////////////////////////////////////////////
// Name: durationinterface.cpp
// Author: Laurent Pugin
// Created: 2011
// Copyright (c) Authors and others. All rights reserved.
/////////////////////////////////////////////////////////////////////////////
#include "durationinterface.h"
//----------------------------------------------------------------------------
#include <assert.h>
#include <math.h>
#include <stdlib.h>
//----------------------------------------------------------------------------
#include "beam.h"
#include "chord.h"
#include "mensur.h"
#include "note.h"
#include "vrv.h"
namespace vrv {
//----------------------------------------------------------------------------
// DurationInterface
//----------------------------------------------------------------------------
DurationInterface::DurationInterface()
: Interface()
, AttAugmentDots()
, AttBeamSecondary()
, AttDurationGestural()
, AttDurationLogical()
, AttDurationRatio()
, AttFermataPresent()
, AttStaffIdent()
{
RegisterInterfaceAttClass(ATT_AUGMENTDOTS);
RegisterInterfaceAttClass(ATT_BEAMSECONDARY);
RegisterInterfaceAttClass(ATT_DURATIONGESTURAL);
RegisterInterfaceAttClass(ATT_DURATIONLOGICAL);
RegisterInterfaceAttClass(ATT_DURATIONRATIO);
RegisterInterfaceAttClass(ATT_FERMATAPRESENT);
RegisterInterfaceAttClass(ATT_STAFFIDENT);
Reset();
}
DurationInterface::~DurationInterface() {}
void DurationInterface::Reset()
{
ResetAugmentDots();
ResetBeamSecondary();
ResetDurationGestural();
ResetDurationLogical();
ResetDurationRatio();
ResetFermataPresent();
ResetStaffIdent();
}
double DurationInterface::GetInterfaceAlignmentDuration(int num, int numbase)
{
int noteDur = this->GetDurGes() != DURATION_NONE ? this->GetActualDurGes() : this->GetActualDur();
if (this->HasNum()) num *= this->GetNum();
if (this->HasNumbase()) numbase *= this->GetNumbase();
double duration = DUR_MAX / pow(2.0, (double)(noteDur - 2.0)) * numbase / num;
int noteDots = (this->HasDotsGes()) ? this->GetDotsGes() : this->GetDots();
if (noteDots != -1) {
duration = 2 * duration - (duration / pow(2, noteDots));
}
// LogDebug("Duration %d; Dot %d; Alignement %f", noteDur, GetDots(), duration);
return duration;
}
double DurationInterface::GetInterfaceAlignmentMensuralDuration(int num, int numbase, Mensur *currentMensur)
{
int noteDur = this->GetDurGes() != DURATION_NONE ? this->GetActualDurGes() : this->GetActualDur();
if (!currentMensur) {
LogWarning("No current mensur for calculating duration");
return DUR_MENSURAL_REF;
}
if (this->HasNum()) num *= this->GetNum();
if (this->HasNumbase()) numbase *= this->GetNumbase();
if (currentMensur->HasNum()) num *= currentMensur->GetNum();
if (currentMensur->HasNumbase()) numbase *= currentMensur->GetNumbase();
double ratio = 0.0;
double duration = (double)DUR_MENSURAL_REF;
switch (noteDur) {
case DUR_MX:
duration *= (double)abs(currentMensur->GetModusminor()) * (double)abs(currentMensur->GetModusmaior());
break;
case DUR_LG: duration *= (double)abs(currentMensur->GetModusminor()); break;
case DUR_BR: break;
case DUR_1: duration /= (double)abs(currentMensur->GetTempus()); break;
default:
ratio = pow(2.0, (double)(noteDur - DUR_2));
duration /= (double)abs(currentMensur->GetTempus()) * (double)abs(currentMensur->GetProlatio()) * ratio;
break;
}
duration *= (double)numbase / (double)num;
// LogDebug("Duration %d; %d/%d; Alignement %f; Ratio %f", noteDur, num, numbase, duration, ratio);
duration = durRound(duration);
return duration;
}
bool DurationInterface::IsFirstInBeam(LayerElement *noteOrRest)
{
Beam *beam = dynamic_cast<Beam *>(noteOrRest->GetFirstAncestor(BEAM, MAX_BEAM_DEPTH));
if (!beam) {
return false;
}
const ArrayOfObjects *notesOrRests = beam->GetList(beam);
ArrayOfObjects::const_iterator iter = notesOrRests->begin();
if (*iter == noteOrRest) {
return true;
}
return false;
}
bool DurationInterface::IsLastInBeam(LayerElement *noteOrRest)
{
Beam *beam = dynamic_cast<Beam *>(noteOrRest->GetFirstAncestor(BEAM, MAX_BEAM_DEPTH));
if (!beam) {
return false;
}
const ArrayOfObjects *notesOrRests = beam->GetList(beam);
ArrayOfObjects::const_reverse_iterator iter = notesOrRests->rbegin();
if (*iter == noteOrRest) {
return true;
}
return false;
}
int DurationInterface::GetActualDur() const
{
// maxima (-1) is a mensural only value
if (this->GetDur() == DURATION_maxima) return DUR_MX;
return (this->GetDur() & DUR_MENSURAL_MASK);
}
int DurationInterface::GetActualDurGes() const
{
// maxima (-1) is a mensural only value
if (this->GetDurGes() == DURATION_maxima) return DUR_MX;
return (this->GetDurGes() & DUR_MENSURAL_MASK);
}
int DurationInterface::GetNoteOrChordDur(LayerElement *element)
{
if (element->Is(CHORD)) {
return this->GetActualDur();
}
else if (element->Is(NOTE)) {
Note *note = dynamic_cast<Note *>(element);
assert(note);
Chord *chord = note->IsChordTone();
if (chord && !this->HasDur())
return chord->GetActualDur();
else
return this->GetActualDur();
}
return this->GetActualDur();
}
bool DurationInterface::IsMensuralDur()
{
// maxima (-1) is a mensural only value
if (this->GetDur() == DURATION_maxima) return true;
return (this->GetDur() > DUR_MENSURAL_MASK);
}
bool DurationInterface::HasIdenticalDurationInterface(DurationInterface *otherDurationInterface)
{
// This should never happen because it is fully implemented
LogError("DurationInterface::HasIdenticalDurationInterface missing");
assert(false);
return false;
/*
if (!otherDurationInterface) {
return false;
}
*/
}
} // namespace vrv