-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathARMeter.cpp
More file actions
195 lines (173 loc) · 5.93 KB
/
ARMeter.cpp
File metadata and controls
195 lines (173 loc) · 5.93 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
/*
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
*/
#include <iostream>
#include <sstream>
#include <regex>
#include "ARMeter.h"
#include "TagParameterStrings.h"
#include "TagParameterString.h"
#include "TagParameterFloat.h"
using namespace std;
//--------------------------------------------------------------------------------------
ARMeter::ARMeter() : fMeterDuration(0,1), fSingleUnit(true), fGroupComplex(false)
{
setupTagParameters (gMaps->sARMeterMap);
fType = NONE;
fAutoBarlines = true;
fAutoMeasuresNum = kNoAutoMeasureNum;
}
//--------------------------------------------------------------------------------------
ARMeter::ARMeter(int num, int dnum) : fMeterDuration(num, dnum), fSingleUnit(true)
{
setupTagParameters (gMaps->sARMeterMap);
std::stringstream bufferSStream;
bufferSStream << fMeterDuration;
fMeterName = bufferSStream.str().c_str();
fType = NUMERIC;
fAutoBarlines = true;
fAutoMeasuresNum = kNoAutoMeasureNum;
}
//--------------------------------------------------------------------------------------
// converts a meter string in the form "n[/d]" to a fraction
// warning: the [/d] part is optional are when not present, the denominator is set to 0
Fraction ARMeter::str2meter(string str) const
{
size_t n = str.find("/");
int num = 0;
int dnum = 0;
if (n == string::npos)
num = std::stoi(str);
else {
num = std::stoi(str.substr(0,n));
dnum = std::stoi(str.substr(n+1));
}
return Fraction (num, dnum);
}
//--------------------------------------------------------------------------------------
// check if the meters set has a single unit (denominator)
bool ARMeter::singleUnit (const std::vector<Fraction>& meters) const
{
int unit = 0;
for (size_t i=0; i < meters.size(); i++) {
int d = meters[i].getDenominator();
if (!d) continue; // unit is implicit
if (!unit) unit = d;
else if (d != unit) return false;
}
return true;
}
//--------------------------------------------------------------------------------------
// parse a meters string in the form "n[/d] [+] n[/d] [+] ... [+] n[/d]"
// where parts in brackets [] are optionnal
// note that the method is relaxed regarding extra characters that are actually ignored
const vector<Fraction> ARMeter::finalizeMeters(const std::vector<Fraction>& meters) const
{
vector<Fraction> out;
int n = int(meters.size() - 1);
int lastdnum = 4; // default denominator
while (n >= 0) {
Fraction m = meters[n--];
int d = m.getDenominator();
if (!d) m.setDenominator(lastdnum);
else lastdnum = d;
out.insert (out.begin(), m);
}
return out;
}
//--------------------------------------------------------------------------------------
// parse a meters string in the form "n[/d] [+] n[/d] [+] ... [+] n[/d]"
// where parts in brackets [] are optionnal
// note that the method is relaxed regarding extra characters that are actually ignored
const vector<Fraction> ARMeter::parseMeters(std::string str) const
{
vector<Fraction> meters;
string exp = "([1-9][0-9]*([ ]*/[1-9][0-9]*){0,1})+";
std::regex e (exp);
smatch m;
while (regex_search (str, m, e, regex_constants::match_any)) {
string match = *m.begin();
meters.push_back(str2meter (match));
str = m.suffix().str();
}
if (meters.size()) meters = finalizeMeters (meters);
return meters;
}
//--------------------------------------------------------------------------------------
Fraction ARMeter::metersDuration (const std::vector<Fraction>& meters) const
{
Fraction out (0,1);
if (fSingleUnit) { // intended to avoid fraction normalization
int n = 0, d = 1;
for (size_t i=0; i < meters.size(); i++) {
n += meters[i].getNumerator();
d = meters[i].getDenominator();
}
out.set (n, d);
}
else {
for (size_t i=0; i<meters.size(); i++)
out += meters[i];
out.normalize();
}
return out;
}
//--------------------------------------------------------------------------------------
void ARMeter::setMeter (const string& meter)
{
fMeterName = meter;
if ((meter == "C") || (meter == "c")) {
fType = C;
fMetersVector.push_back(Fraction(4,4));
}
else if ((meter == "C/") || (meter == "c/")) {
fType = C2;
fMetersVector.push_back(Fraction(2,2));
}
else if ((meter == "") || !isNumeric(meter)) {
fType = NONE;
fMetersVector.push_back(Fraction(4,4));
}
else {
fType = NUMERIC;
fMetersVector = parseMeters(meter);
fSingleUnit = singleUnit (fMetersVector);
fMeterDuration = metersDuration(fMetersVector);
}
if (fMetersVector.empty()) {
fType = NONE;
fMetersVector.push_back(Fraction(4,4));
}
}
//--------------------------------------------------------------------------------------
void ARMeter::setTagParameters (const TagParameterMap& params)
{
const TagParameterString* p = getParameter<TagParameterString>(kTypeStr);
if (p) setMeter (p->getValue());
else return;
string am = getParameter<TagParameterString>(kAutoMeasNumStr, true)->getValue();
if (am == "on") fAutoMeasuresNum = kAutoMeasureNum;
else if (am == "page") fAutoMeasuresNum = kAutoMeasureNumPage;
else if (am == "system") fAutoMeasuresNum = kAutoMeasureNumSystem;
fHidden = getParameter<TagParameterString>(kHiddenStr, true)->getBool();
fAutoBarlines = getParameter<TagParameterString>(kAutoBarlinesStr, true)->getBool();
fGroupComplex = getParameter<TagParameterString>(kGroupStr, true)->getBool();
fMeterDuration = metersDuration(fMetersVector);
}
//--------------------------------------------------------------------------------------
bool ARMeter::isNumeric (const std::string& meter) const
{
const char* ptr = meter.c_str();
while (*ptr) {
int c = *ptr++;
if (!isdigit(c) && !isblank(c) && (c != '+') && (c != '/')) return false;
}
return true;
}