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
|
// -*- c-basic-offset: 4 -*-
/*
Rosegarden
A sequencer and musical notation editor.
This program is Copyright 2000-2008
Guillaume Laurent <[email protected]>,
Chris Cannam <[email protected]>,
Richard Bown <[email protected]>
The moral right of the authors to claim authorship of this work
has been asserted.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version. See the file
COPYING included with this distribution for more information.
*/
#include "SegmentPerformanceHelper.h"
#include "BaseProperties.h"
#include <iostream>
namespace Rosegarden
{
using std::endl;
using std::string;
using namespace BaseProperties;
SegmentPerformanceHelper::~SegmentPerformanceHelper() { }
SegmentPerformanceHelper::iteratorcontainer
SegmentPerformanceHelper::getTiedNotes(iterator i)
{
iteratorcontainer c;
c.push_back(i);
Event *e = *i;
if (!e->isa(Note::EventType)) return c;
Segment::iterator j(i);
bool tiedBack = false, tiedForward = false;
e->get<Bool>(TIED_BACKWARD, tiedBack);
e->get<Bool>(TIED_FORWARD, tiedForward);
timeT d = e->getNotationDuration();
timeT t = e->getNotationAbsoluteTime();
if (!e->has(PITCH)) return c;
int pitch = e->get<Int>(PITCH);
bool valid = false;
if (tiedBack) {
// #1171463: If we can find no preceding TIED_FORWARD event,
// then we remove this property
while (j != begin()) {
--j;
if (!(*j)->isa(Note::EventType)) continue;
e = *j; // can reuse e because this branch always returns
timeT t2 = e->getNotationAbsoluteTime() + e->getNotationDuration();
if (t2 < t) break;
if (t2 > t || !e->has(PITCH) ||
e->get<Int>(PITCH) != pitch) continue;
bool prevTiedForward = false;
if (!e->get<Bool>(TIED_FORWARD, prevTiedForward) ||
!prevTiedForward) break;
valid = true;
break;
}
if (valid) {
return iteratorcontainer();
} else {
(*i)->unset(TIED_BACKWARD);
return c;
}
}
else if (!tiedForward) return c;
for (;;) {
while (++j != end() && !(*j)->isa(Note::EventType));
if (j == end()) return c;
e = *j;
timeT t2 = e->getNotationAbsoluteTime();
if (t2 > t + d) break;
else if (t2 < t + d || !e->has(PITCH) ||
e->get<Int>(PITCH) != pitch) continue;
if (!e->get<Bool>(TIED_BACKWARD, tiedBack) ||
!tiedBack) break;
d += e->getNotationDuration();
c.push_back(j);
valid = true;
if (!e->get<Bool>(TIED_FORWARD, tiedForward) ||
!tiedForward) return c;
}
if (!valid) {
// Related to #1171463: If we can find no following
// TIED_BACKWARD event, then we remove this property
(*i)->unset(TIED_FORWARD);
}
return c;
}
bool
SegmentPerformanceHelper::getGraceAndHostNotes(iterator i,
iteratorcontainer &graceNotes,
iteratorcontainer &hostNotes,
bool &isHostNote)
{
if (i == end() || !(*i)->isa(Note::EventType)) return false;
Segment::iterator j = i;
Segment::iterator firstGraceNote = i;
Segment::iterator firstHostNote = i;
if ((*i)->has(IS_GRACE_NOTE) && (*i)->get<Bool>(IS_GRACE_NOTE)) {
// i is a grace note. Find the first host note following it
j = i;
while (++j != end()) {
if ((*j)->getNotationAbsoluteTime() >
(*i)->getNotationAbsoluteTime()) break;
if ((*j)->getSubOrdering() < 0) continue;
if ((*j)->isa(Note::EventType)) {
firstHostNote = j;
break;
}
}
if (firstHostNote == i) {
std::cerr << "SegmentPerformanceHelper::getGraceAndHostNotes: REMARK: Grace note at " << (*i)->getAbsoluteTime() << " has no host note" << std::endl;
return false;
}
} else {
// i is a host note, but we need to ensure we have the first
// one, not just any one
j = i;
while (j != begin()) {
--j;
if ((*j)->getNotationAbsoluteTime() <
(*i)->getNotationAbsoluteTime()) break;
if ((*j)->getSubOrdering() <
(*i)->getSubOrdering()) break;
if ((*j)->isa(Note::EventType)) {
firstHostNote = j;
break;
}
}
}
// firstHostNote now points to the first host note, which is
// either the first non-grace note after i (if i was a grace note)
// or the first note with the same time and subordering as i (if i
// was not a grace note).
if ((*firstHostNote)->getSubOrdering() < 0) {
std::cerr << "SegmentPerformanceHelper::getGraceAndHostNotes: WARNING: Note at " << (*firstHostNote)->getAbsoluteTime() << " has subordering " << (*i)->getSubOrdering() << " but is not a grace note" << std::endl;
return false;
}
j = firstHostNote;
while (j != begin()) {
--j;
if ((*j)->getNotationAbsoluteTime() <
(*firstHostNote)->getNotationAbsoluteTime()) break;
if ((*j)->getSubOrdering() >= 0) continue;
if (!(*j)->isa(Note::EventType)) continue;
if (!(*j)->has(IS_GRACE_NOTE) || !(*j)->get<Bool>(IS_GRACE_NOTE)) {
std::cerr << "SegmentPerformanceHelper::getGraceAndHostNotes: WARNING: Note at " << (*j)->getAbsoluteTime() << " (in trackback) has subordering " << (*j)->getSubOrdering() << " but is not a grace note" << std::endl;
break;
}
firstGraceNote = j;
}
if (firstGraceNote == firstHostNote) {
std::cerr << "SegmentPerformanceHelper::getGraceAndHostNotes: REMARK: Note at " << (*firstHostNote)->getAbsoluteTime() << " has no grace notes" << std::endl;
return false;
}
j = firstGraceNote;
// push all of the grace notes, and notes with the same time as
// the first host note, onto the container
isHostNote = false;
while (j != end()) {
if ((*j)->isa(Note::EventType)) {
if ((*j)->getSubOrdering() < 0) {
if ((*j)->has(IS_GRACE_NOTE) && (*j)->get<Bool>(IS_GRACE_NOTE)) {
graceNotes.push_back(j);
}
} else {
hostNotes.push_back(j);
if (j == i) isHostNote = true;
}
}
if ((*j)->getNotationAbsoluteTime() >
(*firstHostNote)->getNotationAbsoluteTime()) break;
++j;
}
return true;
}
timeT
SegmentPerformanceHelper::getSoundingAbsoluteTime(iterator i)
{
timeT t = 0;
timeT discard;
// std::cerr << "SegmentPerformanceHelper::getSoundingAbsoluteTime at " << (*i)->getAbsoluteTime() << std::endl;
if ((*i)->has(IS_GRACE_NOTE)) {
// std::cerr << "it's a grace note" << std::endl;
if (getGraceNoteTimeAndDuration(false, i, t, discard)) return t;
}
if ((*i)->has(MAY_HAVE_GRACE_NOTES)) {
// std::cerr << "it's a candidate host note" << std::endl;
if (getGraceNoteTimeAndDuration(true, i, t, discard)) return t;
}
return (*i)->getAbsoluteTime();
}
timeT
SegmentPerformanceHelper::getSoundingDuration(iterator i)
{
timeT d = 0;
timeT discard;
// std::cerr << "SegmentPerformanceHelper::getSoundingDuration at " << (*i)->getAbsoluteTime() << std::endl;
if ((*i)->has(IS_GRACE_NOTE)) {
// std::cerr << "it's a grace note" << std::endl;
if (getGraceNoteTimeAndDuration(false, i, discard, d)) return d;
}
if ((*i)->has(MAY_HAVE_GRACE_NOTES)) {
// std::cerr << "it's a candidate host note" << std::endl;
if (getGraceNoteTimeAndDuration(true, i, discard, d)) return d;
}
if ((*i)->has(TIED_BACKWARD)) {
// Formerly we just returned d in this case, but now we check
// with getTiedNotes so as to remove any bogus backward ties
// that have no corresponding forward tie. Unfortunately this
// is quite a bit slower.
//!!! optimize. at least we should add a marker property to
//anything we've already processed from this helper this time
//around.
iteratorcontainer c(getTiedNotes(i));
if (c.empty()) { // the tie back is valid
return 0;
}
}
if (!(*i)->has(TIED_FORWARD) || !(*i)->isa(Note::EventType)) {
d = (*i)->getDuration();
} else {
// tied forward but not back
iteratorcontainer c(getTiedNotes(i));
for (iteratorcontainer::iterator ci = c.begin();
ci != c.end(); ++ci) {
d += (**ci)->getDuration();
}
}
return d;
}
// In theory we can do better with tuplets, because real time has
// finer precision than timeT time. With a timeT resolution of 960ppq
// however the difference is probably not audible
RealTime
SegmentPerformanceHelper::getRealAbsoluteTime(iterator i)
{
return segment().getComposition()->getElapsedRealTime
(getSoundingAbsoluteTime(i));
}
// In theory we can do better with tuplets, because real time has
// finer precision than timeT time. With a timeT resolution of 960ppq
// however the difference is probably not audible
//
// (If we did want to do this, it'd help to have abstime->realtime
// conversion methods that accept double args in Composition)
RealTime
SegmentPerformanceHelper::getRealSoundingDuration(iterator i)
{
timeT t0 = getSoundingAbsoluteTime(i);
timeT t1 = t0 + getSoundingDuration(i);
if (t1 > segment().getEndMarkerTime()) {
t1 = segment().getEndMarkerTime();
}
return segment().getComposition()->getRealTimeDifference(t0, t1);
}
bool
SegmentPerformanceHelper::getGraceNoteTimeAndDuration(bool host, iterator i,
timeT &t, timeT &d)
{
// [This code currently assumes appoggiatura. Acciaccatura later.]
// For our present purposes, we will assume that grace notes start
// at the same time as their host note was intended to, and
// "steal" a proportion of the duration of their host note. This
// causes the host note to start later, and be shorter, by that
// same proportion.
// If a host note has more than one (consecutive) grace note, they
// should take a single cut from the grace note and divide it
// amongst themselves.
// To begin with we will set the proportion to 1/4, but we will
// probably want it to be (a) something different [because I don't
// really know what I'm doing], (b) adaptive [e.g. shorter host
// note or more grace notes = longer proportion], (c)
// configurable, or (d) all of the above.
// Of course we also ought to be taking into account the notated
// duration of the grace notes -- though in my working examples it
// generally doesn't seem to be the case that we can always just
// follow those. I wonder if we can always use the grace notes'
// notated duration if the ratio of grace note duration to host
// note duration is less than some value? Whatever we do, we
// should be dividing the grace note duration up in proportion to
// the durations of the grace notes, in situations where we have
// more than one grace note consecutively of different durations;
// that isn't handled at all here.
if (i == end()) return false;
iteratorcontainer graceNotes, hostNotes;
bool isHostNote;
if (!getGraceAndHostNotes(i, graceNotes, hostNotes, isHostNote)) {
std::cerr << "SegmentPerformanceHelper::getGraceNoteTimeAndDuration: REMARK: Note at " << (*i)->getAbsoluteTime() << " is not a grace note, or has no grace notes" << std::endl;
return false;
}
if (!isHostNote) {
if (!(*i)->has(IS_GRACE_NOTE) || !(*i)->get<Bool>(IS_GRACE_NOTE)) {
std::cerr << "SegmentPerformanceHelper::getGraceNoteTimeAndDuration: WARNING: Note at " << (*i)->getAbsoluteTime() << " is neither grace nor host note, but was reported as suitable by getGraceAndHostNotes" << std::endl;
return false;
}
}
if (hostNotes.empty()) {
std::cerr << "SegmentPerformanceHelper::getGraceNoteTimeAndDuration: REMARK: Grace note at " << (*i)->getAbsoluteTime() << " has no host note" << std::endl;
return false;
}
if (graceNotes.empty()) {
std::cerr << "SegmentPerformanceHelper::getGraceNoteTimeAndDuration: REMARK: Note at " << (*i)->getAbsoluteTime() << " has no grace notes" << std::endl;
return false;
}
timeT hostNoteEarliestTime = 0;
timeT hostNoteShortestDuration = 0;
timeT hostNoteNotationDuration = 0;
for (iteratorcontainer::iterator j = hostNotes.begin();
j != hostNotes.end(); ++j) {
if (j == hostNotes.begin() ||
(**j)->getAbsoluteTime() < hostNoteEarliestTime) {
hostNoteEarliestTime = (**j)->getAbsoluteTime();
}
if (j == hostNotes.begin() ||
(**j)->getDuration() < hostNoteShortestDuration) {
hostNoteShortestDuration = (**j)->getDuration();
}
if (j == hostNotes.begin() ||
(**j)->getNotationDuration() > hostNoteNotationDuration) {
hostNoteNotationDuration = (**j)->getNotationDuration();
}
(**j)->set<Bool>(MAY_HAVE_GRACE_NOTES, true);
}
timeT graceNoteTime = hostNoteEarliestTime;
timeT graceNoteDuration = hostNoteNotationDuration / 4;
if (graceNoteDuration > hostNoteShortestDuration / 2) {
graceNoteDuration = hostNoteShortestDuration / 2;
}
if (isHostNote) {
t = (*i)->getAbsoluteTime() + graceNoteDuration;
d = (*i)->getDuration() - graceNoteDuration;
} else {
int count = 0, index = 0;
bool found = false;
int prevSubOrdering = 0;
for (iteratorcontainer::iterator j = graceNotes.begin();
j != graceNotes.end(); ++j) {
bool newChord = false;
if ((**j)->getSubOrdering() != prevSubOrdering) {
newChord = true;
prevSubOrdering = (**j)->getSubOrdering();
}
if (newChord) ++count;
if (*j == i) found = true;
if (!found) {
if (newChord) ++index;
}
}
if (index == count) index = 0;
if (count == 0) count = 1; // should not happen
d = graceNoteDuration / count;
t = hostNoteEarliestTime + d * index;
}
return true;
}
}
|