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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
|
/**********************************************************************
Audacity: A Digital Audio Editor
LabelTrack.cpp
Dominic Mazzoni
James Crook
Jun Wan
*******************************************************************//**
\class LabelTrack
\brief A LabelTrack is a Track that holds labels (LabelStruct).
These are used to annotate a waveform.
Each label has a start time and an end time.
The text of the labels is editable and the
positions of the end points are draggable.
*//****************************************************************//**
\class LabelStruct
\brief A LabelStruct holds information for ONE label in a LabelTrack
LabelStruct also has label specific functions, mostly functions
for drawing different aspects of the label and its text box.
*//*******************************************************************/
#include "LabelTrack.h"
#include <algorithm>
#include <limits.h>
#include <float.h>
#include <wx/log.h>
#include <wx/tokenzr.h>
#include "Prefs.h"
#include "Project.h"
#include "prefs/ImportExportPrefs.h"
#include "TimeWarper.h"
#include "widgets/AudacityMessageBox.h"
wxDEFINE_EVENT(EVT_LABELTRACK_ADDITION, LabelTrackEvent);
wxDEFINE_EVENT(EVT_LABELTRACK_DELETION, LabelTrackEvent);
wxDEFINE_EVENT(EVT_LABELTRACK_PERMUTED, LabelTrackEvent);
wxDEFINE_EVENT(EVT_LABELTRACK_SELECTION, LabelTrackEvent);
static ProjectFileIORegistry::ObjectReaderEntry readerEntry{
"labeltrack",
LabelTrack::New
};
wxString LabelTrack::GetDefaultName()
{
return _("Labels");
}
LabelTrack *LabelTrack::New( AudacityProject &project )
{
auto &tracks = TrackList::Get( project );
auto result = tracks.Add(std::make_shared<LabelTrack>());
result->AttachedTrackObjects::BuildAll();
return result;
}
LabelTrack* LabelTrack::Create(TrackList& trackList, const wxString& name)
{
auto track = std::make_shared<LabelTrack>();
track->SetName(name);
trackList.Add(track);
return track.get();
}
LabelTrack* LabelTrack::Create(TrackList& trackList)
{
return Create(trackList, trackList.MakeUniqueTrackName(GetDefaultName()));
}
LabelTrack::LabelTrack():
Track(),
mClipLen(0.0),
miLastLabel(-1)
{
}
LabelTrack::LabelTrack(const LabelTrack &orig, ProtectedCreationArg &&a)
: Track(orig, std::move(a))
, mClipLen(0.0)
{
for (auto &original: orig.mLabels) {
LabelStruct l { original.selectedRegion, original.title };
mLabels.push_back(l);
}
}
static const Track::TypeInfo &typeInfo()
{
static Track::TypeInfo info{
{ "label", "label", XO("Label Track") }, true, &Track::ClassTypeInfo() };
return info;
}
auto LabelTrack::GetTypeInfo() const -> const TypeInfo &
{
return typeInfo();
}
auto LabelTrack::ClassTypeInfo() -> const TypeInfo &
{
return typeInfo();
}
Track::Holder LabelTrack::PasteInto( AudacityProject & ) const
{
auto pNewTrack = std::make_shared<LabelTrack>();
pNewTrack->Init(*this);
pNewTrack->Paste(0.0, this);
return pNewTrack;
}
template<typename IntervalType>
static IntervalType DoMakeInterval(const LabelStruct &label, size_t index)
{
return {
label.getT0(), label.getT1(),
std::make_unique<LabelTrack::IntervalData>( index ) };
}
auto LabelTrack::MakeInterval( size_t index ) const -> ConstInterval
{
return DoMakeInterval<ConstInterval>(mLabels[index], index);
}
auto LabelTrack::MakeInterval( size_t index ) -> Interval
{
return DoMakeInterval<Interval>(mLabels[index], index);
}
template< typename Container, typename LabelTrack >
static Container DoMakeIntervals(LabelTrack &track)
{
Container result;
for (size_t ii = 0, nn = track.GetNumLabels(); ii < nn; ++ii)
result.emplace_back( track.MakeInterval( ii ) );
return result;
}
auto LabelTrack::GetIntervals() const -> ConstIntervals
{
return DoMakeIntervals<ConstIntervals>(*this);
}
auto LabelTrack::GetIntervals() -> Intervals
{
return DoMakeIntervals<Intervals>(*this);
}
void LabelTrack::SetLabel( size_t iLabel, const LabelStruct &newLabel )
{
if( iLabel >= mLabels.size() ) {
wxASSERT( false );
mLabels.resize( iLabel + 1 );
}
mLabels[ iLabel ] = newLabel;
}
LabelTrack::~LabelTrack()
{
}
void LabelTrack::SetOffset(double dOffset)
{
for (auto &labelStruct: mLabels)
labelStruct.selectedRegion.move(dOffset);
}
void LabelTrack::Clear(double b, double e)
{
// May DELETE labels, so use subscripts to iterate
for (size_t i = 0; i < mLabels.size(); ++i) {
auto &labelStruct = mLabels[i];
LabelStruct::TimeRelations relation =
labelStruct.RegionRelation(b, e, this);
if (relation == LabelStruct::BEFORE_LABEL)
labelStruct.selectedRegion.move(- (e-b));
else if (relation == LabelStruct::SURROUNDS_LABEL) {
DeleteLabel( i );
--i;
}
else if (relation == LabelStruct::ENDS_IN_LABEL)
labelStruct.selectedRegion.setTimes(
b,
labelStruct.getT1() - (e - b));
else if (relation == LabelStruct::BEGINS_IN_LABEL)
labelStruct.selectedRegion.setT1(b);
else if (relation == LabelStruct::WITHIN_LABEL)
labelStruct.selectedRegion.moveT1( - (e-b));
}
}
#if 0
//used when we want to use clear only on the labels
bool LabelTrack::SplitDelete(double b, double e)
{
// May DELETE labels, so use subscripts to iterate
for (size_t i = 0, len = mLabels.size(); i < len; ++i) {
auto &labelStruct = mLabels[i];
LabelStruct::TimeRelations relation =
labelStruct.RegionRelation(b, e, this);
if (relation == LabelStruct::SURROUNDS_LABEL) {
DeleteLabel(i);
--i;
}
else if (relation == LabelStruct::WITHIN_LABEL)
labelStruct.selectedRegion.moveT1( - (e-b));
else if (relation == LabelStruct::ENDS_IN_LABEL)
labelStruct.selectedRegion.setT0(e);
else if (relation == LabelStruct::BEGINS_IN_LABEL)
labelStruct.selectedRegion.setT1(b);
}
return true;
}
#endif
void LabelTrack::ShiftLabelsOnInsert(double length, double pt)
{
for (auto &labelStruct: mLabels) {
LabelStruct::TimeRelations relation =
labelStruct.RegionRelation(pt, pt, this);
if (relation == LabelStruct::BEFORE_LABEL)
labelStruct.selectedRegion.move(length);
else if (relation == LabelStruct::WITHIN_LABEL)
labelStruct.selectedRegion.moveT1(length);
}
}
void LabelTrack::ChangeLabelsOnReverse(double b, double e)
{
for (auto &labelStruct: mLabels) {
if (labelStruct.RegionRelation(b, e, this) ==
LabelStruct::SURROUNDS_LABEL)
{
double aux = b + (e - labelStruct.getT1());
labelStruct.selectedRegion.setTimes(
aux,
e - (labelStruct.getT0() - b));
}
}
SortLabels();
}
void LabelTrack::ScaleLabels(double b, double e, double change)
{
for (auto &labelStruct: mLabels) {
labelStruct.selectedRegion.setTimes(
AdjustTimeStampOnScale(labelStruct.getT0(), b, e, change),
AdjustTimeStampOnScale(labelStruct.getT1(), b, e, change));
}
}
double LabelTrack::AdjustTimeStampOnScale(double t, double b, double e, double change)
{
//t is the time stamp we'll be changing
//b and e are the selection start and end
if (t < b){
return t;
}else if (t > e){
double shift = (e-b)*change - (e-b);
return t + shift;
}else{
double shift = (t-b)*change - (t-b);
return t + shift;
}
}
// Move the labels in the track according to the given TimeWarper.
// (If necessary this could be optimised by ignoring labels that occur before a
// specified time, as in most cases they don't need to move.)
void LabelTrack::WarpLabels(const TimeWarper &warper) {
for (auto &labelStruct: mLabels) {
labelStruct.selectedRegion.setTimes(
warper.Warp(labelStruct.getT0()),
warper.Warp(labelStruct.getT1()));
}
// This should not be needed, assuming the warper is nondecreasing, but
// let's not assume too much.
SortLabels();
}
LabelStruct::LabelStruct(const SelectedRegion ®ion,
const wxString& aTitle)
: selectedRegion(region)
, title(aTitle)
{
updated = false;
width = 0;
x = 0;
x1 = 0;
xText = 0;
y = 0;
}
LabelStruct::LabelStruct(const SelectedRegion ®ion,
double t0, double t1,
const wxString& aTitle)
: selectedRegion(region)
, title(aTitle)
{
// Overwrite the times
selectedRegion.setTimes(t0, t1);
updated = false;
width = 0;
x = 0;
x1 = 0;
xText = 0;
y = 0;
}
void LabelTrack::SetSelected( bool s )
{
bool selected = GetSelected();
Track::SetSelected( s );
if ( selected != GetSelected() ) {
LabelTrackEvent evt{
EVT_LABELTRACK_SELECTION, SharedPointer<LabelTrack>(), {}, -1, -1
};
ProcessEvent( evt );
}
}
double LabelTrack::GetOffset() const
{
return mOffset;
}
double LabelTrack::GetStartTime() const
{
if (mLabels.empty())
return 0.0;
else
return mLabels[0].getT0();
}
double LabelTrack::GetEndTime() const
{
//we need to scan through all the labels, because the last
//label might not have the right-most end (if there is overlap).
if (mLabels.empty())
return 0.0;
double end = 0.0;
for (auto &labelStruct: mLabels) {
const double t1 = labelStruct.getT1();
if(t1 > end)
end = t1;
}
return end;
}
Track::Holder LabelTrack::Clone() const
{
auto result = std::make_shared<LabelTrack>(*this, ProtectedCreationArg{});
result->Init(*this);
return result;
}
// Adjust label's left or right boundary, depending which is requested.
// Return true iff the label flipped.
bool LabelStruct::AdjustEdge( int iEdge, double fNewTime)
{
updated = true;
if( iEdge < 0 )
return selectedRegion.setT0(fNewTime);
else
return selectedRegion.setT1(fNewTime);
}
// We're moving the label. Adjust both left and right edge.
void LabelStruct::MoveLabel( int iEdge, double fNewTime)
{
double fTimeSpan = getDuration();
if( iEdge < 0 )
{
selectedRegion.setTimes(fNewTime, fNewTime+fTimeSpan);
}
else
{
selectedRegion.setTimes(fNewTime-fTimeSpan, fNewTime);
}
updated = true;
}
LabelStruct LabelStruct::Import(wxTextFile &file, int &index)
{
SelectedRegion sr;
wxString title;
static const wxString continuation{ wxT("\\") };
wxString firstLine = file.GetLine(index++);
{
// Assume tab is an impossible character within the exported text
// of the label, so can be only a delimiter. But other white space may
// be part of the label text.
wxStringTokenizer toker { firstLine, wxT("\t") };
//get the timepoint of the left edge of the label.
auto token = toker.GetNextToken();
double t0;
if (!Internat::CompatibleToDouble(token, &t0))
throw BadFormatException{};
token = toker.GetNextToken();
double t1;
if (!Internat::CompatibleToDouble(token, &t1))
//s1 is not a number.
t1 = t0; //This is a one-sided label; t1 == t0.
else
token = toker.GetNextToken();
sr.setTimes( t0, t1 );
title = token;
}
// Newer selection fields are written on additional lines beginning with
// '\' which is an impossible numerical character that older versions of
// audacity will ignore. Test for the presence of such a line and then
// parse it if we can.
// There may also be additional continuation lines from future formats that
// we ignore.
// Advance index over all continuation lines first, before we might throw
// any exceptions.
int index2 = index;
while (index < (int)file.GetLineCount() &&
file.GetLine(index).StartsWith(continuation))
++index;
if (index2 < index) {
wxStringTokenizer toker { file.GetLine(index2++), wxT("\t") };
auto token = toker.GetNextToken();
if (token != continuation)
throw BadFormatException{};
token = toker.GetNextToken();
double f0;
if (!Internat::CompatibleToDouble(token, &f0))
throw BadFormatException{};
token = toker.GetNextToken();
double f1;
if (!Internat::CompatibleToDouble(token, &f1))
throw BadFormatException{};
sr.setFrequencies(f0, f1);
}
return LabelStruct{ sr, title };
}
void LabelStruct::Export(wxTextFile &file) const
{
file.AddLine(wxString::Format(wxT("%s\t%s\t%s"),
Internat::ToString(getT0(), FLT_DIG),
Internat::ToString(getT1(), FLT_DIG),
title
));
// Do we need more lines?
auto f0 = selectedRegion.f0();
auto f1 = selectedRegion.f1();
if ((f0 == SelectedRegion::UndefinedFrequency &&
f1 == SelectedRegion::UndefinedFrequency) ||
ImportExportPrefs::LabelStyleSetting.ReadEnum())
return;
// Write a \ character at the start of a second line,
// so that earlier versions of Audacity ignore it.
file.AddLine(wxString::Format(wxT("\\\t%s\t%s"),
Internat::ToString(f0, FLT_DIG),
Internat::ToString(f1, FLT_DIG)
));
// Additional lines in future formats should also start with '\'.
}
auto LabelStruct::RegionRelation(
double reg_t0, double reg_t1, const LabelTrack * WXUNUSED(parent)) const
-> TimeRelations
{
bool retainLabels = false;
wxASSERT(reg_t0 <= reg_t1);
gPrefs->Read(wxT("/GUI/RetainLabels"), &retainLabels);
if(retainLabels) {
// Desired behavior for edge cases: The length of the selection is smaller
// than the length of the label if the selection is within the label or
// matching exactly a (region) label.
if (reg_t0 < getT0() && reg_t1 > getT1())
return SURROUNDS_LABEL;
else if (reg_t1 < getT0())
return BEFORE_LABEL;
else if (reg_t0 > getT1())
return AFTER_LABEL;
else if (reg_t0 >= getT0() && reg_t0 <= getT1() &&
reg_t1 >= getT0() && reg_t1 <= getT1())
return WITHIN_LABEL;
else if (reg_t0 >= getT0() && reg_t0 <= getT1())
return BEGINS_IN_LABEL;
else
return ENDS_IN_LABEL;
} else {
// AWD: Desired behavior for edge cases: point labels bordered by the
// selection are included within it. Region labels are included in the
// selection to the extent that the selection covers them; specifically,
// they're not included at all if the selection borders them, and they're
// fully included if the selection covers them fully, even if it just
// borders their endpoints. This is just one of many possible schemes.
// The first test catches bordered point-labels and selected-through
// region-labels; move it to third and selection edges become inclusive
// WRT point-labels.
if (reg_t0 <= getT0() && reg_t1 >= getT1())
return SURROUNDS_LABEL;
else if (reg_t1 <= getT0())
return BEFORE_LABEL;
else if (reg_t0 >= getT1())
return AFTER_LABEL;
// At this point, all point labels should have returned.
else if (reg_t0 > getT0() && reg_t0 < getT1() &&
reg_t1 > getT0() && reg_t1 < getT1())
return WITHIN_LABEL;
// Knowing that none of the other relations match simplifies remaining
// tests
else if (reg_t0 > getT0() && reg_t0 < getT1())
return BEGINS_IN_LABEL;
else
return ENDS_IN_LABEL;
}
}
/// Export labels including label start and end-times.
void LabelTrack::Export(wxTextFile & f) const
{
// PRL: to do: export other selection fields
for (auto &labelStruct: mLabels)
labelStruct.Export(f);
}
/// Import labels, handling files with or without end-times.
void LabelTrack::Import(wxTextFile & in)
{
int lines = in.GetLineCount();
mLabels.clear();
mLabels.reserve(lines);
//Currently, we expect a tag file to have two values and a label
//on each line. If the second token is not a number, we treat
//it as a single-value label.
bool error = false;
for (int index = 0; index < lines;) {
try {
// Let LabelStruct::Import advance index
LabelStruct l { LabelStruct::Import(in, index) };
mLabels.push_back(l);
}
catch(const LabelStruct::BadFormatException&) { error = true; }
}
if (error)
::AudacityMessageBox( XO("One or more saved labels could not be read.") );
SortLabels();
}
bool LabelTrack::HandleXMLTag(const std::string_view& tag, const AttributesList &attrs)
{
if (tag == "label") {
SelectedRegion selectedRegion;
wxString title;
// loop through attrs, which is a null-terminated list of
// attribute-value pairs
for (auto pair : attrs)
{
auto attr = pair.first;
auto value = pair.second;
if (selectedRegion.HandleXMLAttribute(attr, value, "t", "t1"))
;
// Bug 1905 no longer applies, as valueView has no limits anyway
else if (attr == "title")
title = value.ToWString();
} // while
// Handle files created by Audacity 1.1. Labels in Audacity 1.1
// did not have separate start- and end-times.
// PRL: this superfluous now, given class SelectedRegion's internal
// consistency guarantees
//if (selectedRegion.t1() < 0)
// selectedRegion.collapseToT0();
LabelStruct l { selectedRegion, title };
mLabels.push_back(l);
return true;
}
else if (tag == "labeltrack") {
long nValue = -1;
for (auto pair : attrs)
{
auto attr = pair.first;
auto value = pair.second;
if (this->Track::HandleCommonXMLAttribute(attr, value))
;
else if (attr == "numlabels" && value.TryGet(nValue))
{
if (nValue < 0)
{
wxLogWarning(wxT("Project shows negative number of labels: %d"), nValue);
return false;
}
mLabels.clear();
mLabels.reserve(nValue);
}
}
return true;
}
return false;
}
XMLTagHandler *LabelTrack::HandleXMLChild(const std::string_view& tag)
{
if (tag == "label")
return this;
else
return NULL;
}
void LabelTrack::WriteXML(XMLWriter &xmlFile) const
// may throw
{
int len = mLabels.size();
xmlFile.StartTag(wxT("labeltrack"));
this->Track::WriteCommonXMLAttributes( xmlFile );
xmlFile.WriteAttr(wxT("numlabels"), len);
for (auto &labelStruct: mLabels) {
xmlFile.StartTag(wxT("label"));
labelStruct.getSelectedRegion()
.WriteXMLAttributes(xmlFile, "t", "t1");
// PRL: to do: write other selection fields
xmlFile.WriteAttr(wxT("title"), labelStruct.title);
xmlFile.EndTag(wxT("label"));
}
xmlFile.EndTag(wxT("labeltrack"));
}
Track::Holder LabelTrack::Cut(double t0, double t1)
{
auto tmp = Copy(t0, t1);
Clear(t0, t1);
return tmp;
}
#if 0
Track::Holder LabelTrack::SplitCut(double t0, double t1)
{
// SplitCut() == Copy() + SplitDelete()
Track::Holder tmp = Copy(t0, t1);
if (!SplitDelete(t0, t1))
return {};
return tmp;
}
#endif
Track::Holder LabelTrack::Copy(double t0, double t1, bool) const
{
auto tmp = std::make_shared<LabelTrack>();
tmp->Init(*this);
const auto lt = static_cast<LabelTrack*>(tmp.get());
for (auto &labelStruct: mLabels) {
LabelStruct::TimeRelations relation =
labelStruct.RegionRelation(t0, t1, this);
if (relation == LabelStruct::SURROUNDS_LABEL) {
LabelStruct l {
labelStruct.selectedRegion,
labelStruct.getT0() - t0,
labelStruct.getT1() - t0,
labelStruct.title
};
lt->mLabels.push_back(l);
}
else if (relation == LabelStruct::WITHIN_LABEL) {
LabelStruct l {
labelStruct.selectedRegion,
0,
t1-t0,
labelStruct.title
};
lt->mLabels.push_back(l);
}
else if (relation == LabelStruct::BEGINS_IN_LABEL) {
LabelStruct l {
labelStruct.selectedRegion,
0,
labelStruct.getT1() - t0,
labelStruct.title
};
lt->mLabels.push_back(l);
}
else if (relation == LabelStruct::ENDS_IN_LABEL) {
LabelStruct l {
labelStruct.selectedRegion,
labelStruct.getT0() - t0,
t1 - t0,
labelStruct.title
};
lt->mLabels.push_back(l);
}
}
lt->mClipLen = (t1 - t0);
return tmp;
}
bool LabelTrack::PasteOver(double t, const Track * src)
{
auto result = src->TypeSwitch< bool >( [&](const LabelTrack *sl) {
int len = mLabels.size();
int pos = 0;
while (pos < len && mLabels[pos].getT0() < t)
pos++;
for (auto &labelStruct: sl->mLabels) {
LabelStruct l {
labelStruct.selectedRegion,
labelStruct.getT0() + t,
labelStruct.getT1() + t,
labelStruct.title
};
mLabels.insert(mLabels.begin() + pos++, l);
}
return true;
} );
if (! result )
// THROW_INCONSISTENCY_EXCEPTION; // ?
(void)0;// intentionally do nothing
return result;
}
void LabelTrack::Paste(double t, const Track *src)
{
bool bOk = src->TypeSwitch< bool >( [&](const LabelTrack *lt) {
double shiftAmt = lt->mClipLen > 0.0 ? lt->mClipLen : lt->GetEndTime();
ShiftLabelsOnInsert(shiftAmt, t);
PasteOver(t, src);
return true;
} );
if ( !bOk )
// THROW_INCONSISTENCY_EXCEPTION; // ?
(void)0;// intentionally do nothing
}
// This repeats the labels in a time interval a specified number of times.
bool LabelTrack::Repeat(double t0, double t1, int n)
{
// Sanity-check the arguments
if (n < 0 || t1 < t0)
return false;
double tLen = t1 - t0;
// Insert space for the repetitions
ShiftLabelsOnInsert(tLen * n, t1);
// mLabels may resize as we iterate, so use subscripting
for (unsigned int i = 0; i < mLabels.size(); ++i)
{
LabelStruct::TimeRelations relation =
mLabels[i].RegionRelation(t0, t1, this);
if (relation == LabelStruct::SURROUNDS_LABEL)
{
// Label is completely inside the selection; duplicate it in each
// repeat interval
unsigned int pos = i; // running label insertion position in mLabels
for (int j = 1; j <= n; j++)
{
const LabelStruct &label = mLabels[i];
LabelStruct l {
label.selectedRegion,
label.getT0() + j * tLen,
label.getT1() + j * tLen,
label.title
};
// Figure out where to insert
while (pos < mLabels.size() &&
mLabels[pos].getT0() < l.getT0())
pos++;
mLabels.insert(mLabels.begin() + pos, l);
}
}
else if (relation == LabelStruct::BEGINS_IN_LABEL)
{
// Label ends inside the selection; ShiftLabelsOnInsert() hasn't touched
// it, and we need to extend it through to the last repeat interval
mLabels[i].selectedRegion.moveT1(n * tLen);
}
// Other cases have already been handled by ShiftLabelsOnInsert()
}
return true;
}
void LabelTrack::SyncLockAdjust(double oldT1, double newT1)
{
if (newT1 > oldT1) {
// Insert space within the track
if (oldT1 > GetEndTime())
return;
//Clear(oldT1, newT1);
ShiftLabelsOnInsert(newT1 - oldT1, oldT1);
}
else if (newT1 < oldT1) {
// Remove from the track
Clear(newT1, oldT1);
}
}
void LabelTrack::Silence(double t0, double t1)
{
int len = mLabels.size();
// mLabels may resize as we iterate, so use subscripting
for (int i = 0; i < len; ++i) {
LabelStruct::TimeRelations relation =
mLabels[i].RegionRelation(t0, t1, this);
if (relation == LabelStruct::WITHIN_LABEL)
{
// Split label around the selection
const LabelStruct &label = mLabels[i];
LabelStruct l {
label.selectedRegion,
t1,
label.getT1(),
label.title
};
mLabels[i].selectedRegion.setT1(t0);
// This might not be the right place to insert, but we sort at the end
++i;
mLabels.insert(mLabels.begin() + i, l);
}
else if (relation == LabelStruct::ENDS_IN_LABEL)
{
// Beginning of label to selection end
mLabels[i].selectedRegion.setT0(t1);
}
else if (relation == LabelStruct::BEGINS_IN_LABEL)
{
// End of label to selection beginning
mLabels[i].selectedRegion.setT1(t0);
}
else if (relation == LabelStruct::SURROUNDS_LABEL)
{
DeleteLabel( i );
len--;
i--;
}
}
SortLabels();
}
void LabelTrack::InsertSilence(double t, double len)
{
for (auto &labelStruct: mLabels) {
double t0 = labelStruct.getT0();
double t1 = labelStruct.getT1();
if (t0 >= t)
t0 += len;
if (t1 >= t)
t1 += len;
labelStruct.selectedRegion.setTimes(t0, t1);
}
}
int LabelTrack::GetNumLabels() const
{
return mLabels.size();
}
const LabelStruct *LabelTrack::GetLabel(int index) const
{
return &mLabels[index];
}
int LabelTrack::AddLabel(const SelectedRegion &selectedRegion,
const wxString &title)
{
LabelStruct l { selectedRegion, title };
int len = mLabels.size();
int pos = 0;
while (pos < len && mLabels[pos].getT0() < selectedRegion.t0())
pos++;
mLabels.insert(mLabels.begin() + pos, l);
LabelTrackEvent evt{
EVT_LABELTRACK_ADDITION, SharedPointer<LabelTrack>(), title, -1, pos
};
ProcessEvent( evt );
return pos;
}
void LabelTrack::DeleteLabel(int index)
{
wxASSERT((index < (int)mLabels.size()));
auto iter = mLabels.begin() + index;
const auto title = iter->title;
mLabels.erase(iter);
LabelTrackEvent evt{
EVT_LABELTRACK_DELETION, SharedPointer<LabelTrack>(), title, index, -1
};
ProcessEvent( evt );
}
/// Sorts the labels in order of their starting times.
/// This function is called often (whilst dragging a label)
/// We expect them to be very nearly in order, so insertion
/// sort (with a linear search) is a reasonable choice.
void LabelTrack::SortLabels()
{
const auto begin = mLabels.begin();
const auto nn = (int)mLabels.size();
int i = 1;
while (true)
{
// Find the next disorder
while (i < nn && mLabels[i - 1].getT0() <= mLabels[i].getT0())
++i;
if (i >= nn)
break;
// Where must element i sink to? At most i - 1, maybe less
int j = i - 2;
while( (j >= 0) && (mLabels[j].getT0() > mLabels[i].getT0()) )
--j;
++j;
// Now fix the disorder
std::rotate(
begin + j,
begin + i,
begin + i + 1
);
// Let listeners update their stored indices
LabelTrackEvent evt{
EVT_LABELTRACK_PERMUTED, SharedPointer<LabelTrack>(),
mLabels[j].title, i, j
};
ProcessEvent( evt );
}
}
wxString LabelTrack::GetTextOfLabels(double t0, double t1) const
{
bool firstLabel = true;
wxString retVal;
for (auto &labelStruct: mLabels) {
if (labelStruct.getT0() >= t0 &&
labelStruct.getT1() <= t1)
{
if (!firstLabel)
retVal += '\t';
firstLabel = false;
retVal += labelStruct.title;
}
}
return retVal;
}
int LabelTrack::FindNextLabel(const SelectedRegion& currentRegion)
{
int i = -1;
if (!mLabels.empty()) {
int len = (int) mLabels.size();
if (miLastLabel >= 0 && miLastLabel + 1 < len
&& currentRegion.t0() == mLabels[miLastLabel].getT0()
&& currentRegion.t0() == mLabels[miLastLabel + 1].getT0() ) {
i = miLastLabel + 1;
}
else {
i = 0;
if (currentRegion.t0() < mLabels[len - 1].getT0()) {
while (i < len &&
mLabels[i].getT0() <= currentRegion.t0()) {
i++;
}
}
}
}
miLastLabel = i;
return i;
}
int LabelTrack::FindPrevLabel(const SelectedRegion& currentRegion)
{
int i = -1;
if (!mLabels.empty()) {
int len = (int) mLabels.size();
if (miLastLabel > 0 && miLastLabel < len
&& currentRegion.t0() == mLabels[miLastLabel].getT0()
&& currentRegion.t0() == mLabels[miLastLabel - 1].getT0() ) {
i = miLastLabel - 1;
}
else {
i = len - 1;
if (currentRegion.t0() > mLabels[0].getT0()) {
while (i >=0 &&
mLabels[i].getT0() >= currentRegion.t0()) {
i--;
}
}
}
}
miLastLabel = i;
return i;
}
|