-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
AutomationClip.cpp
1259 lines (990 loc) · 30.2 KB
/
AutomationClip.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
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
/*
* AutomationClip.cpp - implementation of class AutomationClip which
* holds dynamic values
*
* Copyright (c) 2008-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
* Copyright (c) 2006-2008 Javier Serrano Polo <jasp00/at/users.sourceforge.net>
*
* This file is part of LMMS - https://lmms.io
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program (see COPYING); if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*
*/
#include "AutomationClip.h"
#include "AutomationNode.h"
#include "AutomationClipView.h"
#include "AutomationTrack.h"
#include "LocaleHelper.h"
#include "Note.h"
#include "PatternStore.h"
#include "ProjectJournal.h"
#include "Song.h"
#include <cmath>
namespace lmms
{
int AutomationClip::s_quantization = 1;
const float AutomationClip::DEFAULT_MIN_VALUE = 0;
const float AutomationClip::DEFAULT_MAX_VALUE = 1;
AutomationClip::AutomationClip( AutomationTrack * _auto_track ) :
Clip( _auto_track ),
#if (QT_VERSION < QT_VERSION_CHECK(5,14,0))
m_clipMutex(QMutex::Recursive),
#endif
m_autoTrack( _auto_track ),
m_objects(),
m_tension( 1.0 ),
m_progressionType( ProgressionType::Discrete ),
m_dragging( false ),
m_isRecording( false ),
m_lastRecordedValue( 0 )
{
changeLength( TimePos( 1, 0 ) );
if( getTrack() )
{
switch( getTrack()->trackContainer()->type() )
{
case TrackContainer::Type::Pattern:
setAutoResize( true );
break;
case TrackContainer::Type::Song:
// move down
default:
setAutoResize( false );
break;
}
}
}
AutomationClip::AutomationClip( const AutomationClip & _clip_to_copy ) :
Clip( _clip_to_copy.m_autoTrack ),
#if (QT_VERSION < QT_VERSION_CHECK(5,14,0))
m_clipMutex(QMutex::Recursive),
#endif
m_autoTrack( _clip_to_copy.m_autoTrack ),
m_objects( _clip_to_copy.m_objects ),
m_tension( _clip_to_copy.m_tension ),
m_progressionType( _clip_to_copy.m_progressionType )
{
// Locks the mutex of the copied AutomationClip to make sure it
// doesn't change while it's being copied
QMutexLocker m(&_clip_to_copy.m_clipMutex);
for( timeMap::const_iterator it = _clip_to_copy.m_timeMap.begin();
it != _clip_to_copy.m_timeMap.end(); ++it )
{
// Copies the automation node (in/out values and in/out tangents)
m_timeMap[POS(it)] = it.value();
// Sets the node's clip to this one
m_timeMap[POS(it)].setClip(this);
}
if (!getTrack()){ return; }
switch( getTrack()->trackContainer()->type() )
{
case TrackContainer::Type::Pattern:
setAutoResize( true );
break;
case TrackContainer::Type::Song:
// move down
default:
setAutoResize( false );
break;
}
}
bool AutomationClip::addObject( AutomatableModel * _obj, bool _search_dup )
{
QMutexLocker m(&m_clipMutex);
if (_search_dup && std::find(m_objects.begin(), m_objects.end(), _obj) != m_objects.end())
{
return false;
}
// the automation track is unconnected and there is nothing in the track
if (m_objects.empty() && hasAutomation() == false)
{
// then initialize first value
putValue( TimePos(0), _obj->inverseScaledValue( _obj->value<float>() ), false );
}
m_objects.push_back(_obj);
connect( _obj, SIGNAL(destroyed(lmms::jo_id_t)),
this, SLOT(objectDestroyed(lmms::jo_id_t)),
Qt::DirectConnection );
emit dataChanged();
return true;
}
void AutomationClip::setProgressionType(
ProgressionType _new_progression_type )
{
QMutexLocker m(&m_clipMutex);
if ( _new_progression_type == ProgressionType::Discrete ||
_new_progression_type == ProgressionType::Linear ||
_new_progression_type == ProgressionType::CubicHermite )
{
m_progressionType = _new_progression_type;
emit dataChanged();
}
}
void AutomationClip::setTension( QString _new_tension )
{
QMutexLocker m(&m_clipMutex);
bool ok;
float nt = LocaleHelper::toFloat(_new_tension, & ok);
if( ok && nt > -0.01 && nt < 1.01 )
{
m_tension = nt;
}
}
const AutomatableModel * AutomationClip::firstObject() const
{
QMutexLocker m(&m_clipMutex);
AutomatableModel* model;
if (!m_objects.empty() && (model = m_objects.front()) != nullptr)
{
return model;
}
static FloatModel fm(0, DEFAULT_MIN_VALUE, DEFAULT_MAX_VALUE, 0.001f);
return &fm;
}
const AutomationClip::objectVector& AutomationClip::objects() const
{
QMutexLocker m(&m_clipMutex);
return m_objects;
}
TimePos AutomationClip::timeMapLength() const
{
QMutexLocker m(&m_clipMutex);
TimePos one_bar = TimePos(1, 0);
if (m_timeMap.isEmpty()) { return one_bar; }
timeMap::const_iterator it = m_timeMap.end();
auto last_tick = static_cast<tick_t>(POS(it - 1));
// if last_tick is 0 (single item at tick 0)
// return length as a whole bar to prevent disappearing Clip
if (last_tick == 0) { return one_bar; }
return TimePos(last_tick);
}
void AutomationClip::updateLength()
{
// Do not resize down in case user manually extended up
changeLength(std::max(length(), timeMapLength()));
}
/**
* @brief Puts an automation node on the timeMap with the given value.
* The inValue and outValue of the created node will be the same.
* @param TimePos time to add the node to
* @param Float inValue and outValue of the node
* @param Boolean True to quantize the position (defaults to true)
* @param Boolean True to ignore unquantized surrounding nodes (defaults to true)
* @return TimePos of the recently added automation node
*/
TimePos AutomationClip::putValue(
const TimePos & time,
const float value,
const bool quantPos,
const bool ignoreSurroundingPoints
)
{
QMutexLocker m(&m_clipMutex);
cleanObjects();
TimePos newTime = quantPos ? Note::quantized(time, quantization()) : time;
// Create a node or replace the existing one on newTime
m_timeMap[newTime] = AutomationNode(this, value, newTime);
timeMap::iterator it = m_timeMap.find(newTime);
// Remove control points that are covered by the new points
// quantization value. Control Key to override
if (!ignoreSurroundingPoints)
{
// We need to check that to avoid removing nodes from
// newTime + 1 to newTime (removing the node we are adding)
if (quantization() > 1)
{
// Remove nodes between the quantization points, them not
// being included
removeNodes(newTime + 1, newTime + quantization() - 1);
}
}
if (it != m_timeMap.begin()) { --it; }
generateTangents(it, 3);
updateLength();
emit dataChanged();
return newTime;
}
/**
* @brief Puts an automation node on the timeMap with the given inValue
* and outValue.
* @param TimePos time to add the node to
* @param Float inValue of the node
* @param Float outValue of the node
* @param Boolean True to quantize the position (defaults to true)
* @param Boolean True to ignore unquantized surrounding nodes (defaults to true)
* @return TimePos of the recently added automation node
*/
TimePos AutomationClip::putValues(
const TimePos & time,
const float inValue,
const float outValue,
const bool quantPos,
const bool ignoreSurroundingPoints
)
{
QMutexLocker m(&m_clipMutex);
cleanObjects();
TimePos newTime = quantPos ? Note::quantized(time, quantization()) : time;
// Create a node or replace the existing one on newTime
m_timeMap[newTime] = AutomationNode(this, inValue, outValue, newTime);
timeMap::iterator it = m_timeMap.find(newTime);
// Remove control points that are covered by the new points
// quantization value. Control Key to override
if (!ignoreSurroundingPoints)
{
// We need to check that to avoid removing nodes from
// newTime + 1 to newTime (removing the node we are adding)
if (quantization() > 1)
{
// Remove nodes between the quantization points, them not
// being included
removeNodes(newTime + 1, newTime + quantization() - 1);
}
}
if (it != m_timeMap.begin()) { --it; }
generateTangents(it, 3);
updateLength();
emit dataChanged();
return newTime;
}
void AutomationClip::removeNode(const TimePos & time)
{
QMutexLocker m(&m_clipMutex);
cleanObjects();
m_timeMap.remove( time );
timeMap::iterator it = m_timeMap.lowerBound(time);
if( it != m_timeMap.begin() )
{
--it;
}
generateTangents(it, 3);
updateLength();
emit dataChanged();
}
/**
* @brief Removes all automation nodes between the given ticks
* @param Int first tick of the range
* @param Int second tick of the range
*/
void AutomationClip::removeNodes(const int tick0, const int tick1)
{
if (tick0 == tick1)
{
removeNode(TimePos(tick0));
return;
}
auto start = TimePos(std::min(tick0, tick1));
auto end = TimePos(std::max(tick0, tick1));
// Make a list of TimePos with nodes to be removed
// because we can't simply remove the nodes from
// the timeMap while we are iterating it.
std::vector<TimePos> nodesToRemove;
for (auto it = m_timeMap.lowerBound(start), endIt = m_timeMap.upperBound(end); it != endIt; ++it)
{
nodesToRemove.push_back(POS(it));
}
for (auto node: nodesToRemove)
{
removeNode(node);
}
}
/**
* @brief Resets the outValues of all automation nodes between the given ticks
* @param Int first tick of the range
* @param Int second tick of the range
*/
void AutomationClip::resetNodes(const int tick0, const int tick1)
{
if (tick0 == tick1)
{
auto it = m_timeMap.find(TimePos(tick0));
if (it != m_timeMap.end()) { it.value().resetOutValue(); }
return;
}
auto start = TimePos(std::min(tick0, tick1));
auto end = TimePos(std::max(tick0, tick1));
for (auto it = m_timeMap.lowerBound(start), endIt = m_timeMap.upperBound(end); it != endIt; ++it)
{
it.value().resetOutValue();
}
}
void AutomationClip::resetTangents(const int tick0, const int tick1)
{
if (tick0 == tick1)
{
auto it = m_timeMap.find(TimePos(tick0));
if (it != m_timeMap.end())
{
it.value().setLockedTangents(false);
generateTangents(it, 1);
}
return;
}
TimePos start = TimePos(std::min(tick0, tick1));
TimePos end = TimePos(std::max(tick0, tick1));
for (auto it = m_timeMap.lowerBound(start), endIt = m_timeMap.upperBound(end); it != endIt; ++it)
{
it.value().setLockedTangents(false);
generateTangents(it, 1);
}
}
void AutomationClip::recordValue(TimePos time, float value)
{
QMutexLocker m(&m_clipMutex);
if( value != m_lastRecordedValue )
{
putValue( time, value, true );
m_lastRecordedValue = value;
}
else if( valueAt( time ) != value )
{
removeNode(time);
}
}
/**
* @brief Set the position of the point that is being dragged.
* Calling this function will also automatically set m_dragging to true.
* When applyDragValue() is called, m_dragging is set back to false.
* @param TimePos of the node being dragged
* @param Float with the value to assign to the point being dragged
* @param Boolean. True to snip x position
* @param Boolean. True to ignore unquantized surrounding nodes
* @return TimePos with current time of the dragged value
*/
TimePos AutomationClip::setDragValue(
const TimePos & time,
const float value,
const bool quantPos,
const bool controlKey
)
{
QMutexLocker m(&m_clipMutex);
if (m_dragging == false)
{
TimePos newTime = quantPos ? Note::quantized(time, quantization()) : time;
// We will keep the same outValue only if it's different from the
// inValue
m_dragKeepOutValue = false;
// We will set the tangents back to what they were if the node had
// its tangents locked
m_dragLockedTan = false;
// Check if we already have a node on the position we are dragging
// and if we do, store the outValue so the discrete jump can be kept
// and information about the tangents
timeMap::iterator it = m_timeMap.find(newTime);
if (it != m_timeMap.end())
{
// If we don't have a discrete jump, the outValue will be the
// same as the inValue
if (OFFSET(it) != 0)
{
m_dragKeepOutValue = true;
m_dragOutValue = OUTVAL(it);
}
// For the tangents, we will only keep them if the tangents were
// locked
if (LOCKEDTAN(it))
{
m_dragLockedTan = true;
m_dragInTan = INTAN(it);
m_dragOutTan = OUTTAN(it);
}
}
this->removeNode(newTime);
m_oldTimeMap = m_timeMap;
m_dragging = true;
}
//Restore to the state before it the point were being dragged
m_timeMap = m_oldTimeMap;
generateTangents();
TimePos returnedPos;
if (m_dragKeepOutValue)
{
returnedPos = this->putValues(time, value, m_dragOutValue, quantPos, controlKey);
}
else
{
returnedPos = this->putValue(time, value, quantPos, controlKey);
}
// Set the tangents on the newly created node if they were locked
// before dragging
if (m_dragLockedTan)
{
timeMap::iterator it = m_timeMap.find(returnedPos);
if (it != m_timeMap.end())
{
it.value().setInTangent(m_dragInTan);
it.value().setOutTangent(m_dragOutTan);
it.value().setLockedTangents(true);
}
}
return returnedPos;
}
/**
* @brief After the point is dragged, this function is called to apply the change.
*/
void AutomationClip::applyDragValue()
{
QMutexLocker m(&m_clipMutex);
m_dragging = false;
}
float AutomationClip::valueAt( const TimePos & _time ) const
{
QMutexLocker m(&m_clipMutex);
if( m_timeMap.isEmpty() )
{
return 0;
}
// If we have a node at that time, just return its value
if (m_timeMap.contains(_time))
{
// When the time is exactly the node's time, we want the inValue
return m_timeMap[_time].getInValue();
}
// lowerBound returns next value with equal or greater key. Since we already
// checked if the key contains a node, we know the returned node has a greater
// key than _time. Therefore we take the previous element to calculate the current value
timeMap::const_iterator v = m_timeMap.lowerBound(_time);
if( v == m_timeMap.begin() )
{
return 0;
}
if( v == m_timeMap.end() )
{
// When the time is after the last node, we want the outValue of it
return OUTVAL(v - 1);
}
return valueAt(v - 1, _time - POS(v - 1));
}
// This method will get the value at an offset from a node, so we use the outValue of
// that node and the inValue of the next node for the calculations.
float AutomationClip::valueAt( timeMap::const_iterator v, int offset ) const
{
QMutexLocker m(&m_clipMutex);
// We never use it with offset 0, but doesn't hurt to return a correct
// value if we do
if (offset == 0) { return INVAL(v); }
if (m_progressionType == ProgressionType::Discrete)
{
return OUTVAL(v);
}
else if( m_progressionType == ProgressionType::Linear )
{
float slope =
(INVAL(v + 1) - OUTVAL(v))
/ (POS(v + 1) - POS(v));
return OUTVAL(v) + offset * slope;
}
else /* ProgressionType::CubicHermite */
{
// Implements a Cubic Hermite spline as explained at:
// http://en.wikipedia.org/wiki/Cubic_Hermite_spline#Unit_interval_.280.2C_1.29
//
// Note that we are not interpolating a 2 dimensional point over
// time as the article describes. We are interpolating a single
// value: y. To make this work we map the values of x that this
// segment spans to values of t for t = 0.0 -> 1.0 and scale the
// tangents _m1 and _m2
int numValues = (POS(v + 1) - POS(v));
float t = (float) offset / (float) numValues;
float m1 = OUTTAN(v) * numValues * m_tension;
float m2 = INTAN(v + 1) * numValues * m_tension;
auto t2 = pow(t, 2);
auto t3 = pow(t, 3);
return (2 * t3 - 3 * t2 + 1) * OUTVAL(v)
+ (t3 - 2 * t2 + t) * m1
+ (-2 * t3 + 3 * t2) * INVAL(v + 1)
+ (t3 - t2) * m2;
}
}
float *AutomationClip::valuesAfter( const TimePos & _time ) const
{
QMutexLocker m(&m_clipMutex);
timeMap::const_iterator v = m_timeMap.lowerBound(_time);
if( v == m_timeMap.end() || (v+1) == m_timeMap.end() )
{
return nullptr;
}
int numValues = POS(v + 1) - POS(v);
auto ret = new float[numValues];
for( int i = 0; i < numValues; i++ )
{
ret[i] = valueAt( v, i );
}
return ret;
}
void AutomationClip::flipY(int min, int max)
{
QMutexLocker m(&m_clipMutex);
bool changedTimeMap = false;
for (auto it = m_timeMap.begin(); it != m_timeMap.end(); ++it)
{
// Get distance from IN/OUT values to max value
float inValDist = max - INVAL(it);
float outValDist = max - OUTVAL(it);
// To flip, that will be the new distance between
// the IN/OUT values and the min value
it.value().setInValue(min + inValDist);
it.value().setOutValue(min + outValDist);
changedTimeMap = true;
}
if (changedTimeMap)
{
generateTangents();
emit dataChanged();
}
}
void AutomationClip::flipY()
{
flipY(getMin(), getMax());
}
void AutomationClip::flipX(int length)
{
QMutexLocker m(&m_clipMutex);
timeMap::const_iterator it = m_timeMap.lowerBound(0);
if (it == m_timeMap.end()) { return; }
// Temporary map where we will store the flipped version
// of our clip
timeMap tempMap;
float tempValue = 0;
float tempOutValue = 0;
// We know the QMap isn't empty, making this safe:
float realLength = m_timeMap.lastKey();
// If we have a positive length, we want to flip the area covered by that
// length, even if it goes beyond the clip. A negative length means that
// we just want to flip the nodes we have
if (length >= 0 && length != realLength)
{
// If length to be flipped is bigger than the real length
if (realLength < length)
{
// We are flipping an area that goes beyond the last node. So we add a node to the
// beginning of the flipped timeMap representing the value of the end of the area
tempValue = valueAt(length);
tempMap[0] = AutomationNode(this, tempValue, 0);
// Now flip the nodes we have in relation to the length
do
{
// We swap the inValue and outValue when flipping horizontally
tempValue = OUTVAL(it);
tempOutValue = INVAL(it);
auto newTime = TimePos(length - POS(it));
tempMap[newTime] = AutomationNode(this, tempValue, tempOutValue, newTime);
++it;
} while (it != m_timeMap.end());
}
else // If the length to be flipped is smaller than the real length
{
do
{
TimePos newTime;
// Only flips the length to be flipped and keep the remaining values in place
// We also only swap the inValue and outValue if we are flipping the node
if (POS(it) <= length)
{
newTime = length - POS(it);
tempValue = OUTVAL(it);
tempOutValue = INVAL(it);
}
else
{
newTime = POS(it);
tempValue = INVAL(it);
tempOutValue = OUTVAL(it);
}
tempMap[newTime] = AutomationNode(this, tempValue, tempOutValue, newTime);
++it;
} while (it != m_timeMap.end());
}
}
else // Length to be flipped is the same as the real length
{
do
{
// Swap the inValue and outValue
tempValue = OUTVAL(it);
tempOutValue = INVAL(it);
auto newTime = TimePos(realLength - POS(it));
tempMap[newTime] = AutomationNode(this, tempValue, tempOutValue, newTime);
++it;
} while (it != m_timeMap.end());
}
m_timeMap.clear();
m_timeMap = tempMap;
cleanObjects();
generateTangents();
emit dataChanged();
}
void AutomationClip::saveSettings( QDomDocument & _doc, QDomElement & _this )
{
QMutexLocker m(&m_clipMutex);
_this.setAttribute( "pos", startPosition() );
_this.setAttribute( "len", length() );
_this.setAttribute( "name", name() );
_this.setAttribute( "prog", QString::number( static_cast<int>(progressionType()) ) );
_this.setAttribute( "tens", QString::number( getTension() ) );
_this.setAttribute( "mute", QString::number( isMuted() ) );
if (const auto& c = color())
{
_this.setAttribute("color", c->name());
}
for( timeMap::const_iterator it = m_timeMap.begin();
it != m_timeMap.end(); ++it )
{
QDomElement element = _doc.createElement( "time" );
element.setAttribute("pos", POS(it));
element.setAttribute("value", INVAL(it));
element.setAttribute("outValue", OUTVAL(it));
element.setAttribute("inTan", INTAN(it));
element.setAttribute("outTan", OUTTAN(it));
element.setAttribute("lockedTan", static_cast<int>(LOCKEDTAN(it)));
_this.appendChild( element );
}
for (const auto& object : m_objects)
{
if (object)
{
QDomElement element = _doc.createElement( "object" );
element.setAttribute("id", ProjectJournal::idToSave(object->id()));
_this.appendChild(element);
}
}
}
void AutomationClip::loadSettings( const QDomElement & _this )
{
QMutexLocker m(&m_clipMutex);
// Legacy compatibility: Previously tangents were not stored in
// the project file. So if any node doesn't have tangent information
// we will generate the tangents
bool shouldGenerateTangents = false;
clear();
movePosition( _this.attribute( "pos" ).toInt() );
setName( _this.attribute( "name" ) );
setProgressionType( static_cast<ProgressionType>( _this.attribute(
"prog" ).toInt() ) );
setTension( _this.attribute( "tens" ) );
setMuted(_this.attribute( "mute", QString::number( false ) ).toInt() );
for( QDomNode node = _this.firstChild(); !node.isNull();
node = node.nextSibling() )
{
QDomElement element = node.toElement();
if( element.isNull() )
{
continue;
}
if( element.tagName() == "time" )
{
int timeMapPos = element.attribute("pos").toInt();
float timeMapInValue = LocaleHelper::toFloat(element.attribute("value"));
float timeMapOutValue = LocaleHelper::toFloat(element.attribute("outValue"));
m_timeMap[timeMapPos] = AutomationNode(this, timeMapInValue, timeMapOutValue, timeMapPos);
// Load tangents if there is information about it (it's enough to check for either inTan or outTan)
if (element.hasAttribute("inTan"))
{
float inTan = LocaleHelper::toFloat(element.attribute("inTan"));
float outTan = LocaleHelper::toFloat(element.attribute("outTan"));
bool lockedTan = static_cast<bool>(element.attribute("lockedTan", "0").toInt());
m_timeMap[timeMapPos].setInTangent(inTan);
m_timeMap[timeMapPos].setOutTangent(outTan);
m_timeMap[timeMapPos].setLockedTangents(lockedTan);
}
else
{
shouldGenerateTangents = true;
}
}
else if( element.tagName() == "object" )
{
m_idsToResolve.push_back(element.attribute("id").toInt());
}
}
if (_this.hasAttribute("color"))
{
setColor(QColor{_this.attribute("color")});
}
int len = _this.attribute( "len" ).toInt();
if( len <= 0 )
{
// TODO: Handle with an upgrade method
updateLength();
}
else
{
changeLength( len );
}
if (shouldGenerateTangents) { generateTangents(); }
}
QString AutomationClip::name() const
{
QMutexLocker m(&m_clipMutex);
if( !Clip::name().isEmpty() )
{
return Clip::name();
}
if (!m_objects.empty() && m_objects.front() != nullptr)
{
return m_objects.front()->fullDisplayName();
}
return tr( "Drag a control while pressing <%1>" ).arg(UI_CTRL_KEY);
}
gui::ClipView * AutomationClip::createView( gui::TrackView * _tv )
{
QMutexLocker m(&m_clipMutex);
return new gui::AutomationClipView( this, _tv );
}
bool AutomationClip::isAutomated( const AutomatableModel * _m )
{
auto l = combineAllTracks();
for (const auto track : l)
{
if (track->type() == Track::Type::Automation || track->type() == Track::Type::HiddenAutomation)
{
for (const auto& clip : track->getClips())
{
const auto a = dynamic_cast<const AutomationClip*>(clip);
if( a && a->hasAutomation() )
{
for (const auto& object : a->m_objects)
{
if (object == _m)
{
return true;
}
}
}
}
}
}
return false;
}
/**