-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNoteBatchEdits.cs
733 lines (648 loc) · 31.1 KB
/
NoteBatchEdits.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using OpenUtau.Core.Ustx;
using OpenUtau.Core.Util;
namespace OpenUtau.Core.Editing {
public class AddTailNote : BatchEdit {
public string Name => name;
private string lyric;
private string name;
public AddTailNote(string lyric, string name) {
this.lyric = lyric;
this.name = name;
}
public void Run(UProject project, UVoicePart part, List<UNote> selectedNotes, DocManager docManager) {
List<UNote> toAdd = new List<UNote>();
var notes = selectedNotes.Count > 0 ? selectedNotes : part.notes.ToList();
foreach (var note in notes) {
if (note.lyric != lyric && (note.Next == null || note.Next.position > note.End + 120)) {
var addNote = project.CreateNote(note.tone, note.End, 120);
foreach(var exp in note.phonemeExpressions.OrderBy(exp => exp.index)) {
addNote.SetExpression(project, project.tracks[part.trackNo], exp.abbr, new float?[] { exp.value });
}
toAdd.Add(addNote);
}
}
if (toAdd.Count == 0) {
return;
}
docManager.StartUndoGroup(true);
foreach (var note in toAdd) {
note.lyric = lyric;
docManager.ExecuteCmd(new AddNoteCommand(part, note));
}
docManager.EndUndoGroup();
}
}
public class RemoveTailNote : BatchEdit {
public string Name => name;
private string lyric;
private string name;
public RemoveTailNote(string lyric, string name) {
this.lyric = lyric;
this.name = name;
}
bool NeedToBeRemoved(UNote note) {
return note.lyric == lyric
&& (note.Next == null || note.Next.position > note.End);
}
public void Run(UProject project, UVoicePart part, List<UNote> selectedNotes, DocManager docManager) {
var notes = selectedNotes.Count > 0 ? selectedNotes : part.notes.ToList();
List<UNote> toRemove = notes.Where(NeedToBeRemoved).ToList();
if (toRemove.Count == 0) {
return;
}
docManager.StartUndoGroup(true);
foreach (var note in toRemove) {
note.lyric = lyric;
docManager.ExecuteCmd(new RemoveNoteCommand(part, note));
}
docManager.EndUndoGroup();
}
}
public class AddBreathNote : BatchEdit {
public string Name => name;
private string lyric;
private string name;
public AddBreathNote(string lyric) {
this.lyric = lyric;
this.name = "pianoroll.menu.notes.addbreath";
}
public void Run(UProject project, UVoicePart part, List<UNote> selectedNotes, DocManager docManager) {
List<UNote> toAdd = new List<UNote>();
var notes = selectedNotes.Count > 0 ? selectedNotes : part.notes.ToList();
foreach (var note in notes) {
if (note.lyric != lyric) {
int duration;
if (note.Prev == null) {
duration = 480;
} else if (note.Prev.lyric == lyric || note.position - 120 <= note.Prev.End) {
continue;
} else if (note.Prev.End < note.position - 960) {
duration = 480;
} else {
duration = note.position - note.Prev.End;
}
var addNote = project.CreateNote(note.tone, note.position - duration, duration);
foreach (var exp in note.phonemeExpressions.Where(exp => exp.index == 0)) {
addNote.SetExpression(project, project.tracks[part.trackNo], exp.abbr, new float?[] { exp.value });
}
toAdd.Add(addNote);
}
}
if (toAdd.Count == 0) {
return;
}
docManager.StartUndoGroup(true);
foreach (var note in toAdd) {
note.lyric = lyric;
docManager.ExecuteCmd(new AddNoteCommand(part, note));
}
docManager.EndUndoGroup();
}
}
public class Transpose : BatchEdit {
public string Name => name;
private int deltaNoteNum;
private string name;
public Transpose(int deltaNoteNum, string name) {
this.deltaNoteNum = deltaNoteNum;
this.name= name;
}
public void Run(UProject project, UVoicePart part, List<UNote> selectedNotes, DocManager docManager) {
var notes = selectedNotes.Count > 0 ? selectedNotes : part.notes.ToList();
docManager.StartUndoGroup(true);
foreach (var note in notes) {
docManager.ExecuteCmd(new MoveNoteCommand(part, note, 0, deltaNoteNum));
}
docManager.EndUndoGroup();
}
}
public class QuantizeNotes : BatchEdit {
public virtual string Name => name;
private int quantize;
private string name;
public QuantizeNotes(int quantize) {
this.quantize = quantize;
name = $"pianoroll.menu.notes.quantize{quantize}";
}
public void Run(UProject project, UVoicePart part, List<UNote> selectedNotes, DocManager docManager) {
var notes = selectedNotes.Count > 0 ? selectedNotes : part.notes.ToList();
docManager.StartUndoGroup(true);
foreach (var note in notes) {
int pos = note.position;
int end = note.End;
int newPos = (int)Math.Round(1.0 * pos / quantize) * quantize;
int newEnd = (int)Math.Round(1.0 * end / quantize) * quantize;
if (newPos != pos) {
docManager.ExecuteCmd(new MoveNoteCommand(part, note, newPos - pos, 0));
docManager.ExecuteCmd(new ResizeNoteCommand(part, note, newEnd - newPos - note.duration));
} else if (newEnd != end) {
docManager.ExecuteCmd(new ResizeNoteCommand(part, note, newEnd - newPos - note.duration));
}
}
docManager.EndUndoGroup();
}
}
public class AutoLegato : BatchEdit {
public virtual string Name => name;
private string name;
public AutoLegato() {
name = $"pianoroll.menu.notes.autolegato";
}
public void Run(UProject project, UVoicePart part, List<UNote> selectedNotes, DocManager docManager) {
var notes = selectedNotes.Count > 0 ? selectedNotes : part.notes.ToList();
notes.Sort((a, b) => a.position.CompareTo(b.position));
docManager.StartUndoGroup(true);
for (int i = 0; i < notes.Count - 1; i++) {
docManager.ExecuteCmd(new ResizeNoteCommand(part, notes[i], notes[i + 1].position - notes[i].position - notes[i].duration));
}
docManager.EndUndoGroup();
}
}
public class FixOverlap: BatchEdit {
/// <summary>
/// Fix overlapping notes.
/// If multiple notes start at the same time, only the one with the highest tone will be kept
/// If one notes's end is overlapped by another note, the end will be moved to the start of the next note
/// </summary>
public virtual string Name => name;
private string name;
public FixOverlap() {
name = $"pianoroll.menu.notes.fixoverlap";
}
public void Run(UProject project, UVoicePart part, List<UNote> selectedNotes, DocManager docManager) {
var notes = selectedNotes.Count > 0 ? selectedNotes : part.notes.ToList();
if(notes.Count == 0){
return;
}
docManager.StartUndoGroup();
var currentNote = notes[0];
foreach(var note in notes.Skip(1)){
if(note.position == currentNote.position){
if(note.tone > currentNote.tone){
docManager.ExecuteCmd(new RemoveNoteCommand(part, currentNote));
currentNote = note;
}else{
docManager.ExecuteCmd(new RemoveNoteCommand(part, note));
}
}else if(note.position < currentNote.End){
docManager.ExecuteCmd(new ResizeNoteCommand(part, currentNote, note.position - currentNote.End));
currentNote = note;
}else{
currentNote = note;
}
}
docManager.EndUndoGroup();
}
}
public class HanziToPinyin : BatchEdit {
public virtual string Name => name;
private string name;
public HanziToPinyin() {
name = "pianoroll.menu.notes.hanzitopinyin";
}
public void Run(UProject project, UVoicePart part, List<UNote> selectedNotes, DocManager docManager) {
var pinyinResult = BaseChinesePhonemizer.Romanize(selectedNotes.Select(note=>note.lyric));
docManager.StartUndoGroup(true);
foreach(var t in Enumerable.Zip(selectedNotes, pinyinResult,
(note, pinyin) => Tuple.Create(note, pinyin))) {
docManager.ExecuteCmd(new ChangeNoteLyricCommand(part, t.Item1, t.Item2));
}
docManager.EndUndoGroup();
}
}
public class ResetPitchBends : BatchEdit {
public virtual string Name => name;
private string name;
public ResetPitchBends() {
name = "pianoroll.menu.notes.reset.pitchbends";
}
public void Run(UProject project, UVoicePart part, List<UNote> selectedNotes, DocManager docManager) {
var notes = selectedNotes.Count > 0 ? selectedNotes : part.notes.ToList();
docManager.StartUndoGroup(true);
foreach (var note in notes) {
docManager.ExecuteCmd(new ResetPitchPointsCommand(part, note));
}
docManager.EndUndoGroup();
}
}
public class ResetAllExpressions : BatchEdit {
public virtual string Name => name;
private string name;
public ResetAllExpressions() {
name = "pianoroll.menu.notes.reset.exps";
}
public void Run(UProject project, UVoicePart part, List<UNote> selectedNotes, DocManager docManager) {
var notes = selectedNotes.Count > 0 ? selectedNotes : part.notes.ToList();
docManager.StartUndoGroup(true);
foreach (var note in notes) {
if (note.phonemeExpressions.Count > 0) {
docManager.ExecuteCmd(new ResetExpressionsCommand(part, note));
}
}
var curveAbbrs = part.curves.Select(c => c.abbr).ToArray();
foreach (var abbr in curveAbbrs) {
docManager.ExecuteCmd(new ClearCurveCommand(part, abbr));
}
docManager.EndUndoGroup();
}
}
public class ClearVibratos : BatchEdit {
public virtual string Name => name;
private string name;
public ClearVibratos() {
name = "pianoroll.menu.notes.clear.vibratos";
}
public void Run(UProject project, UVoicePart part, List<UNote> selectedNotes, DocManager docManager) {
var notes = selectedNotes.Count > 0 ? selectedNotes : part.notes.ToList();
docManager.StartUndoGroup(true);
foreach (var note in notes) {
if (note.vibrato.length > 0) {
docManager.ExecuteCmd(new VibratoLengthCommand(part, note, 0));
}
}
docManager.EndUndoGroup();
}
}
public class ResetVibratos : BatchEdit {
public virtual string Name => name;
private string name;
public ResetVibratos() {
name = "pianoroll.menu.notes.reset.vibratos";
}
public void Run(UProject project, UVoicePart part, List<UNote> selectedNotes, DocManager docManager) {
var notes = selectedNotes.Count > 0 ? selectedNotes : part.notes.ToList();
docManager.StartUndoGroup(true);
foreach (var note in notes) {
docManager.ExecuteCmd(new VibratoPeriodCommand(part, note, NotePresets.Default.DefaultVibrato.VibratoPeriod));
docManager.ExecuteCmd(new VibratoDepthCommand(part, note, NotePresets.Default.DefaultVibrato.VibratoDepth));
docManager.ExecuteCmd(new VibratoFadeInCommand(part, note, NotePresets.Default.DefaultVibrato.VibratoIn));
docManager.ExecuteCmd(new VibratoFadeOutCommand(part, note, NotePresets.Default.DefaultVibrato.VibratoOut));
docManager.ExecuteCmd(new VibratoShiftCommand(part, note, NotePresets.Default.DefaultVibrato.VibratoShift));
docManager.ExecuteCmd(new VibratoDriftCommand(part, note, NotePresets.Default.DefaultVibrato.VibratoDrift));
if (NotePresets.Default.AutoVibratoToggle && note.duration >= NotePresets.Default.AutoVibratoNoteDuration) {
docManager.ExecuteCmd(new VibratoLengthCommand(part, note, NotePresets.Default.DefaultVibrato.VibratoLength));
} else {
docManager.ExecuteCmd(new VibratoLengthCommand(part, note, 0));
}
}
docManager.EndUndoGroup();
}
}
public class ClearTimings : BatchEdit {
public virtual string Name => name;
private string name;
public ClearTimings() {
name = "pianoroll.menu.notes.reset.phonemetimings";
}
public void Run(UProject project, UVoicePart part, List<UNote> selectedNotes, DocManager docManager) {
var notes = selectedNotes.Count > 0 ? selectedNotes : part.notes.ToList();
docManager.StartUndoGroup(true);
foreach (var note in notes) {
bool shouldClear = false;
foreach (var o in note.phonemeOverrides) {
if (o.offset != null || o.preutterDelta != null || o.overlapDelta != null) {
shouldClear = true;
break;
}
}
if (shouldClear) {
docManager.ExecuteCmd(new ClearPhonemeTimingCommand(part, note));
}
}
docManager.EndUndoGroup();
}
}
public class ResetAliases : BatchEdit {
public virtual string Name => name;
private string name;
public ResetAliases() {
name = "pianoroll.menu.notes.reset.aliases";
}
public void Run(UProject project, UVoicePart part, List<UNote> selectedNotes, DocManager docManager) {
var notes = selectedNotes.Count > 0 ? selectedNotes : part.notes.ToList();
docManager.StartUndoGroup(true);
foreach (var note in notes) {
foreach (var o in note.phonemeOverrides) {
if (o.phoneme!=null) {
docManager.ExecuteCmd(new ChangePhonemeAliasCommand(part, note, o.index, null));
}
}
}
docManager.EndUndoGroup();
}
}
public class LengthenCrossfade : BatchEdit {
public virtual string Name => name;
private string name;
private double ratio;
public LengthenCrossfade(double ratio) {
name = "pianoroll.menu.notes.lengthencrossfade";
this.ratio = ratio;
}
public void Run(UProject project, UVoicePart part, List<UNote> selectedNotes, DocManager docManager) {
var notes = selectedNotes.Count > 0 ? selectedNotes : part.notes.ToList();
if (notes.Count == 0) {
return;
}
docManager.StartUndoGroup(true);
var track = project.tracks[part.trackNo];
foreach (var note in notes) {
foreach (UPhoneme phoneme in part.phonemes) {
if (phoneme.Parent == note && phoneme.Prev != null && phoneme.PositionMs == phoneme.Prev.EndMs) {
double consonantStretch = Math.Pow(2f, 1.0f - phoneme.GetExpression(project, track, Format.Ustx.VEL).Item1 / 100f);
double maxPreutter = phoneme.oto.Preutter * consonantStretch;
double prevDur = phoneme.Prev.DurationMs;
double preutter = phoneme.preutter;
if (maxPreutter > prevDur * 0.9f) {
maxPreutter = prevDur * 0.9f;
}
if(maxPreutter > phoneme.preutter) {
docManager.ExecuteCmd(new PhonemePreutterCommand(part, note, phoneme.index, (float)(maxPreutter - phoneme.autoPreutter)));
preutter = maxPreutter;
}
var overlap = preutter * ratio;
if (overlap > phoneme.autoOverlap) {
docManager.ExecuteCmd(new PhonemeOverlapCommand(part, note, phoneme.index, (float)(overlap - phoneme.autoOverlap)));
}
}
}
}
docManager.EndUndoGroup();
}
}
public class LoadRenderedPitch : BatchEdit {
public virtual string Name => name;
private string name;
public LoadRenderedPitch() {
name = "pianoroll.menu.notes.loadrenderedpitch";
}
public void Run(UProject project, UVoicePart part, List<UNote> selectedNotes, DocManager docManager) {
var renderer = project.tracks[part.trackNo].RendererSettings.Renderer;
if (renderer == null || !renderer.SupportsRenderPitch) {
docManager.ExecuteCmd(new ErrorMessageNotification("Not supported"));
return;
}
var notes = selectedNotes.Count > 0 ? selectedNotes : part.notes.ToList();
var positions = notes.Select(n => n.position + part.position).ToHashSet();
var phrases = part.renderPhrases.Where(phrase => phrase.notes.Any(n => positions.Contains(phrase.position + n.position)));
docManager.StartUndoGroup(true);
float minPitD = -1200;
if (project.expressions.TryGetValue(Format.Ustx.PITD, out var descriptor)) {
minPitD = descriptor.min;
}
foreach (var phrase in phrases) {
var result = renderer.LoadRenderedPitch(phrase);
if (result == null) {
continue;
}
int? lastX = null;
int? lastY = null;
// TODO: Optimize interpolation and command.
for (int i = 0; i < result.tones.Length; i++) {
if (result.tones[i] < 0) {
continue;
}
int x = phrase.position - part.position + (int)result.ticks[i];
int pitchIndex = Math.Clamp((x - (phrase.position - part.position - phrase.leading)) / 5, 0, phrase.pitches.Length - 1);
float basePitch = phrase.pitchesBeforeDeviation[pitchIndex];
int y = (int)(result.tones[i] * 100 - basePitch);
lastX ??= x;
lastY ??= y;
if (y > minPitD) {
docManager.ExecuteCmd(new SetCurveCommand(
project, part, Format.Ustx.PITD, x, y, lastX.Value, lastY.Value));
}
lastX = x;
lastY = y;
}
}
docManager.EndUndoGroup();
}
}
public class BakePitch: BatchEdit {
public virtual string Name => name;
private string name;
public BakePitch() {
name = "pianoroll.menu.notes.bakepitch";
}
struct Point{
public int X;
public double Y;
public PitchPointShape shape;
public Point(int X, double Y, PitchPointShape shape = PitchPointShape.l) {
this.X = X;
this.Y = Y;
this.shape = shape;
}
public Point ChangeShape(PitchPointShape shape) {
return new Point(X, Y, shape);
}
}
double deltaY(Point pt, Point lineStart, Point lineEnd, PitchPointShape shape){
return pt.Y - MusicMath.InterpolateShape(lineStart.X, lineEnd.X, lineStart.Y, lineEnd.Y, pt.X, shape);
}
PitchPointShape DetermineShape(Point start, Point middle, Point end){
if(start.Y==end.Y){
return PitchPointShape.l;
}
var k = (middle.Y-start.Y)/(end.Y-start.Y);
if(k > 0.67){
return PitchPointShape.o;
}
if(k < 0.33){
return PitchPointShape.i;
}
return PitchPointShape.l;
}
//reference: https://github.com/sdercolin/utaformatix3/blob/0f026f7024386ca8362972043c3471c6f2ac9859/src/main/kotlin/process/RdpSimplification.kt#L43
/*
* The Ramer–Douglas–Peucker algorithm is a line simplification algorithm
* for reducing the number of points used to define its shape.
*
* Wikipedia: https://en.wikipedia.org/wiki/Ramer%E2%80%93Douglas%E2%80%93Peucker_algorithm
* Implementation reference: https://rosettacode.org/wiki/Ramer-Douglas-Peucker_line_simplification
* */
//perpendicularDistance is replaced with deltaY, because the units of X and Y are different.
//result doesn't contain the last point to enhance performance in recursion
List<Point> simplifyShape(List<Point> pointList, Double epsilon) {
if (pointList.Count <= 2) {
return pointList;
}
// Determine line shape
var middlePoint = pointList[pointList.Count / 2];
var startPoint = pointList[0];
var endPoint = pointList[^1];
var shape = DetermineShape(startPoint, middlePoint, endPoint);
// Find the point with the maximum distance from line between start and end
var dmax = 0.0;
var index = 0;
var end = pointList.Count - 1;
for (var i = 1; i < end; i++) {
var d = Math.Abs(deltaY(pointList[i], pointList[0], pointList[end], shape));
if (d > dmax) {
index = i;
dmax = d;
}
}
// If max distance is greater than epsilon, recursively simplify
List<Point> results = new List<Point>();
if (dmax > epsilon) {
// Recursive call
var recResults1 = simplifyShape(pointList.GetRange(0, index + 1), epsilon);
var recResults2 = simplifyShape(pointList.GetRange(index, pointList.Count - index), epsilon);
// Build the result list
results.AddRange(recResults1);
results.AddRange(recResults2);
if (results.Count < 2) {
throw new Exception("Problem assembling output");
}
} else {
//Just return the start point
results.Add(pointList[0].ChangeShape(shape));
}
return results;
}
public static int LastIndexOfMin<T>(IList<T> self, Func<T, double> selector, int startIndex, int endIndex)
{
if (self == null) {
throw new ArgumentNullException("self");
}
if (self.Count == 0) {
throw new ArgumentException("List is empty.", "self");
}
var min = selector(self[endIndex-1]);
int minIndex = endIndex - 1;
for (int i = endIndex - 1; i >= startIndex; --i) {
var value = selector(self[i]);
if (value < min) {
min = value;
minIndex = i;
}
}
return minIndex;
}
public void Run(UProject project, UVoicePart part, List<UNote> selectedNotes, DocManager docManager) {
TimeAxis timeAxis = project.timeAxis;
const int pitchInterval = 5;
var notes = selectedNotes.Count > 0 ? selectedNotes : part.notes.ToList();
var positions = notes.Select(n => n.position + part.position).ToHashSet();
var phrases = part.renderPhrases.Where(phrase => phrase.notes.Any(n => positions.Contains(phrase.position + n.position)));
float minPitD = -1200;
if (project.expressions.TryGetValue(Format.Ustx.PITD, out var descriptor)) {
minPitD = descriptor.min;
}
//Dictionary from note start tick to pitch point
//value is a tuple of (starttick, endtick, pitch points)
//Here starttick and endtick are project absolute tick, and pitch points are ms relative to the starttick
var pitchPointsPerNote = new Dictionary<int, Tuple<int,int,List<PitchPoint>>>();
foreach (var phrase in phrases) {
var pitchStart = -phrase.leading;
//var ticks = Enumerable.Range(0, phrase.duration).Select(i => i * 5).ToArray();
var pitches = phrase.pitches;
var points = Enumerable.Zip(
Enumerable.Range(0, pitches.Length),
pitches,
(i, pitch) => new Point(pitchStart + i * pitchInterval, pitch)
).ToList();
//Reduce pitch point
var mustIncludeIndices = phrase.notes
.SelectMany(n => new[] {
n.position,
n.duration>160 ? n.end-80 : n.position+n.duration/2 })
.Select(t=>(t-pitchStart)/pitchInterval)
.Prepend(0)
.Append(points.Count-1)
.ToList();
//pairwise(mustIncludePointIndices)
points = mustIncludeIndices.Zip(mustIncludeIndices.Skip(1),
(a, b) => simplifyShape(points.GetRange(a,b-a),10))
.SelectMany(x=>x).Append(points[^1]).ToList();
//determine where to distribute pitch point
int idx = 0;
//note_boundary[i] is the index of the first pitch point after the end of note i
var note_boundaries = new int[phrase.notes.Length + 1];
note_boundaries[0] = 2;
foreach(int i in Enumerable.Range(0,phrase.notes.Length)) {
var note = phrase.notes[i];
while(idx<points.Count
&& points[idx].X<note.end){
idx++;
}
note_boundaries[i + 1] = idx;
}
//if there is zero point in the note, adjusted_boundaries is the index of the last zero point
//otherwise, it is the index of the pitch point with minimal y-distance to the note
var adjusted_boundaries = new int[phrase.notes.Length + 1];
adjusted_boundaries[0] = 2;
foreach(int i in Enumerable.Range(0,phrase.notes.Length - 1)){
var note = phrase.notes[i];
var notePitch = note.tone*100;
//var zero_point = points.FindIndex(note_boundaries[i], note_boundaries[i + 1] - note_boundaries[i], p => p.Y == 0);
var zero_point = Enumerable.Range(0,note_boundaries[i + 1] - note_boundaries[i])
.Select(j=>note_boundaries[i+1]-1-j)
.Where(j => (points[j].Y-notePitch) * (points[j-1].Y-notePitch) <= 0)
.DefaultIfEmpty(-1)
.First();
if(zero_point != -1){
adjusted_boundaries[i + 1] = zero_point + 1;
}else{
adjusted_boundaries[i + 1] = LastIndexOfMin(points, p => Math.Abs(p.Y - notePitch), note_boundaries[i], note_boundaries[i + 1]) + 2;
}
}
adjusted_boundaries[^1] = note_boundaries[^1];
//distribute pitch point to each note
foreach(int i in Enumerable.Range(0,phrase.notes.Length)) {
var note = phrase.notes[i];
var pitch = points.GetRange(adjusted_boundaries[i]-2,adjusted_boundaries[i + 1]-(adjusted_boundaries[i]-2))
.Select(p => new PitchPoint(
(float)timeAxis.MsBetweenTickPos(note.position + part.position, p.X + part.position),
(float)(p.Y - note.tone * 100) / 10,
p.shape))
.ToList();
pitchPointsPerNote[note.position + phrase.position - part.position]
= Tuple.Create(
points[adjusted_boundaries[i] - 2].X + phrase.position,
points[adjusted_boundaries[i + 1] - 1].X + phrase.position,
pitch);
}
}
docManager.StartUndoGroup(true);
//Apply pitch points to notes
foreach(var note in notes) {
if (pitchPointsPerNote.TryGetValue(note.position, out var tickRangeAndPitch)) {
var pitch = tickRangeAndPitch.Item3;
docManager.ExecuteCmd(new ResetPitchPointsCommand(part, note));
int index = 0;
foreach(var point in pitch) {
docManager.ExecuteCmd(new AddPitchPointCommand(part, note, point, index));
index++;
}
docManager.ExecuteCmd(new DeletePitchPointCommand(part, note, index));
docManager.ExecuteCmd(new DeletePitchPointCommand(part, note, index));
var lastPitch = note.pitch.data[^1];
docManager.ExecuteCmd(new MovePitchPointCommand(part, lastPitch ,0, -lastPitch.Y));
}
}
//Erase PITD curve that has been converted to pitch points
foreach(var note in notes) {
if (pitchPointsPerNote.TryGetValue(note.position, out var tickRangeAndPitch)) {
var start = tickRangeAndPitch.Item1 - part.position;
var end = tickRangeAndPitch.Item2 - part.position;
docManager.ExecuteCmd(new SetCurveCommand(project, part, Format.Ustx.PITD,
start, 0,
start, 0));
docManager.ExecuteCmd(new SetCurveCommand(project, part, Format.Ustx.PITD,
end, 0,
end, 0));
docManager.ExecuteCmd(new SetCurveCommand(project, part, Format.Ustx.PITD,
start, 0,
end, 0));
}
}
docManager.EndUndoGroup();
}
}
}