| 1 | /* fre:ac - free audio converter |
| 2 | * Copyright (C) 2001-2023 Robert Kausch <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License as |
| 6 | * published by the Free Software Foundation, either version 2 of |
| 7 | * the License, or (at your option) any later version. |
| 8 | * |
| 9 | * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR |
| 10 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
| 11 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ |
| 12 | |
| 13 | #include <dialogs/splitter.h> |
| 14 | |
| 15 | #include <engine/decoder.h> |
| 16 | |
| 17 | #include <jobs/engine/convert.h> |
| 18 | |
| 19 | #include <config.h> |
| 20 | #include <utilities.h> |
| 21 | |
| 22 | using namespace BoCA; |
| 23 | using namespace BoCA::AS; |
| 24 | |
| 25 | namespace freac |
| 26 | { |
| 27 | class RepeatPreviousPartComponent : public Layer |
| 28 | { |
| 29 | private: |
| 30 | CheckBox *check_repeat; |
| 31 | Text *text_repeat; |
| 32 | EditBox *edit_repeat; |
| 33 | Arrows *arrows_repeat; |
| 34 | Text *text_repeat_sec; |
| 35 | |
| 36 | Bool repeatEnd; |
| 37 | Int repeatSeconds; |
| 38 | public: |
| 39 | RepeatPreviousPartComponent(const Point &, Bool, Int); |
| 40 | ~RepeatPreviousPartComponent(); |
| 41 | |
| 42 | Void SetRepeat(Bool); |
| 43 | Void SetDuration(Int); |
| 44 | signals: |
| 45 | Signal1<Void, Bool> onToggleRepeat; |
| 46 | Signal1<Void, Int> onChangeDuration; |
| 47 | slots: |
| 48 | Void OnToggleRepeat(); |
| 49 | Void OnEditDuration(); |
| 50 | Void OnChangeDuration(); |
| 51 | }; |
| 52 | }; |
| 53 | |
| 54 | freac::RepeatPreviousPartComponent::RepeatPreviousPartComponent(const Point &pos, Bool repeat, Int seconds) |
| 55 | { |
| 56 | repeatEnd = repeat; |
| 57 | repeatSeconds = seconds; |
| 58 | |
| 59 | BoCA::I18n *i18n = BoCA::I18n::Get(); |
| 60 | |
| 61 | i18n->SetContext("Dialogs::Splitter"); |
| 62 | |
| 63 | check_repeat = new CheckBox(i18n->TranslateString("Repeat end of previous part at beginning of each part"), Point(0, 1), Size(140, 0), &repeatEnd); |
| 64 | check_repeat->SetWidth(check_repeat->GetUnscaledTextWidth() + 21); |
| 65 | check_repeat->onAction.Connect(&RepeatPreviousPartComponent::OnToggleRepeat, this); |
| 66 | |
| 67 | text_repeat = new Text(i18n->AddColon(i18n->TranslateString("Repeat duration")), Point(check_repeat->GetWidth() + 8, 3)); |
| 68 | |
| 69 | edit_repeat = new EditBox(String::FromInt(repeatSeconds), Point(text_repeat->GetX() + text_repeat->GetUnscaledTextWidth() + 9, 0), Size(24, 0), 3); |
| 70 | edit_repeat->SetFlags(EDB_NUMERIC); |
| 71 | edit_repeat->onInput.Connect(&RepeatPreviousPartComponent::OnEditDuration, this); |
| 72 | |
| 73 | arrows_repeat = new Arrows(Point(edit_repeat->GetX() + edit_repeat->GetWidth() + 1, 1), Size(15, 18), OR_VERT, &repeatSeconds, 1, 120); |
| 74 | arrows_repeat->onValueChange.Connect(&RepeatPreviousPartComponent::OnChangeDuration, this); |
| 75 | |
| 76 | text_repeat_sec = new Text(i18n->TranslateString("second(s)"), Point(arrows_repeat->GetX() + 23, 3)); |
| 77 | |
| 78 | Add(check_repeat); |
| 79 | Add(text_repeat); |
| 80 | Add(edit_repeat); |
| 81 | Add(arrows_repeat); |
| 82 | Add(text_repeat_sec); |
| 83 | |
| 84 | SetMetrics(pos, Size(text_repeat_sec->GetX() + text_repeat_sec->GetUnscaledTextWidth(), 25)); |
| 85 | } |
| 86 | |
| 87 | freac::RepeatPreviousPartComponent::~RepeatPreviousPartComponent() |
| 88 | { |
| 89 | DeleteObject(check_repeat); |
| 90 | DeleteObject(text_repeat); |
| 91 | DeleteObject(edit_repeat); |
| 92 | DeleteObject(arrows_repeat); |
| 93 | DeleteObject(text_repeat_sec); |
| 94 | } |
| 95 | |
| 96 | Void freac::RepeatPreviousPartComponent::SetRepeat(Bool repeat) |
| 97 | { |
| 98 | repeatEnd = repeat; |
| 99 | |
| 100 | check_repeat->SetChecked(repeatEnd); |
| 101 | |
| 102 | if (repeatEnd) |
| 103 | { |
| 104 | text_repeat->Activate(); |
| 105 | edit_repeat->Activate(); |
| 106 | arrows_repeat->Activate(); |
| 107 | text_repeat_sec->Activate(); |
| 108 | } |
| 109 | else |
| 110 | { |
| 111 | text_repeat->Deactivate(); |
| 112 | edit_repeat->Deactivate(); |
| 113 | arrows_repeat->Deactivate(); |
| 114 | text_repeat_sec->Deactivate(); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | Void freac::RepeatPreviousPartComponent::SetDuration(Int seconds) |
| 119 | { |
| 120 | repeatSeconds = Math::Max(1, Math::Min(seconds, 120)); |
| 121 | |
| 122 | edit_repeat->SetText(String::FromInt(repeatSeconds)); |
| 123 | arrows_repeat->SetValue(repeatSeconds); |
| 124 | } |
| 125 | |
| 126 | Void freac::RepeatPreviousPartComponent::OnToggleRepeat() |
| 127 | { |
| 128 | SetRepeat(repeatEnd); |
| 129 | |
| 130 | onToggleRepeat.Emit(repeatEnd); |
| 131 | } |
| 132 | |
| 133 | Void freac::RepeatPreviousPartComponent::OnEditDuration() |
| 134 | { |
| 135 | SetDuration(edit_repeat->GetText().ToInt()); |
| 136 | |
| 137 | onChangeDuration.Emit(repeatSeconds); |
| 138 | } |
| 139 | |
| 140 | Void freac::RepeatPreviousPartComponent::OnChangeDuration() |
| 141 | { |
| 142 | SetDuration(repeatSeconds); |
| 143 | |
| 144 | onChangeDuration.Emit(repeatSeconds); |
| 145 | } |
| 146 | |
| 147 | freac::DialogSplitter::DialogSplitter(const Array<Track> &tracks) |
| 148 | { |
| 149 | disableUpdates = True; |
| 150 | |
| 151 | cancelUpdateTrackLengths = False; |
| 152 | updateTrackLengthsRunning = False; |
| 153 | |
| 154 | /* Get configuration. |
| 155 | */ |
| 156 | BoCA::Config *config = BoCA::Config::Get(); |
| 157 | |
| 158 | splitMode = config->GetIntValue(Config::CategorySplitterID, Config::SplitterSplitModeID, Config::SplitterSplitModeDefault); |
| 159 | splitAtTracks = config->GetIntValue(Config::CategorySplitterID, Config::SplitterSplitAtTracksID, Config::SplitterSplitAtTracksDefault); |
| 160 | |
| 161 | partDuration = config->GetIntValue(Config::CategorySplitterID, Config::SplitterPartDurationID, Config::SplitterPartDurationDefault); |
| 162 | numberOfParts = config->GetIntValue(Config::CategorySplitterID, Config::SplitterNumberOfPartsID, Config::SplitterNumberOfPartsDefault); |
| 163 | |
| 164 | repeatEnd = config->GetIntValue(Config::CategorySplitterID, Config::SplitterRepeatEndID, Config::SplitterRepeatEndDefault); |
| 165 | repeatSeconds = config->GetIntValue(Config::CategorySplitterID, Config::SplitterRepeatSecondsID, Config::SplitterRepeatSecondsDefault); |
| 166 | |
| 167 | /* Setup window metrics. |
| 168 | */ |
| 169 | Rect workArea = S::System::Screen::GetActiveScreenWorkArea(); |
| 170 | Rect virtualScreen = Screen::GetVirtualScreenMetrics(); |
| 171 | Float scaleFactor = Surface().GetSurfaceDPI() / 96.0; |
| 172 | |
| 173 | Point defaultPos = workArea.GetPosition() + Point((workArea.GetSize().cx - Config::SplitterWindowSizeXDefault) / 2, (workArea.GetSize().cy - Config::SplitterWindowSizeYDefault - 50) / 2); |
| 174 | |
| 175 | Point wndPos = Point(config->GetIntValue(Config::CategorySplitterID, Config::SplitterWindowPosXID, defaultPos.x), config->GetIntValue(Config::CategorySplitterID, Config::SplitterWindowPosYID, defaultPos.y)); |
| 176 | Size wndSize = Size(config->GetIntValue(Config::CategorySplitterID, Config::SplitterWindowSizeXID, Config::SplitterWindowSizeXDefault), config->GetIntValue(Config::CategorySplitterID, Config::SplitterWindowSizeYID, Config::SplitterWindowSizeYDefault)); |
| 177 | |
| 178 | if (wndPos.x + wndSize.cx * scaleFactor > virtualScreen.right + 2 || |
| 179 | wndPos.y + wndSize.cy * scaleFactor > virtualScreen.bottom + 2) |
| 180 | { |
| 181 | wndPos.x = (Int) Math::Min(virtualScreen.right - 10 - Math::Round(wndSize.cx * scaleFactor), (Int64) wndPos.x); |
| 182 | wndPos.y = (Int) Math::Min(virtualScreen.bottom - 10 - Math::Round(wndSize.cy * scaleFactor), (Int64) wndPos.y); |
| 183 | } |
| 184 | |
| 185 | if (wndPos.x < virtualScreen.left - 2 || |
| 186 | wndPos.y < virtualScreen.top - 2) |
| 187 | { |
| 188 | wndPos.x = (Int) Math::Max(virtualScreen.left + 10, wndPos.x); |
| 189 | wndPos.y = (Int) Math::Max(virtualScreen.top + 10, wndPos.y); |
| 190 | |
| 191 | wndSize.cx = (Int) Math::Min(Math::Round((virtualScreen.right - 20) / scaleFactor), (Int64) wndSize.cx); |
| 192 | wndSize.cy = (Int) Math::Min(Math::Round((virtualScreen.bottom - 20) / scaleFactor), (Int64) wndSize.cy); |
| 193 | } |
| 194 | |
| 195 | config->SetIntValue(Config::CategorySplitterID, Config::SplitterWindowPosXID, wndPos.x); |
| 196 | config->SetIntValue(Config::CategorySplitterID, Config::SplitterWindowPosYID, wndPos.y); |
| 197 | |
| 198 | config->SetIntValue(Config::CategorySplitterID, Config::SplitterWindowSizeXID, wndSize.cx); |
| 199 | config->SetIntValue(Config::CategorySplitterID, Config::SplitterWindowSizeYID, wndSize.cy); |
| 200 | |
| 201 | /* Create UI elements. |
| 202 | */ |
| 203 | BoCA::I18n *i18n = BoCA::I18n::Get(); |
| 204 | |
| 205 | i18n->SetContext("Dialogs::Splitter"); |
| 206 | |
| 207 | mainWnd = new Window(i18n->TranslateString("Output splitting tool"), wndPos, wndSize); |
| 208 | mainWnd->SetMinimumSize(Size(580, 410)); |
| 209 | mainWnd->SetRightToLeft(i18n->IsActiveLanguageRightToLeft()); |
| 210 | mainWnd->GetMainLayer()->onChangeSize.Connect(&DialogSplitter::OnChangeSize, this); |
| 211 | |
| 212 | mainWnd_titlebar = new Titlebar(TB_CLOSEBUTTON); |
| 213 | divbar = new Divider(39, OR_HORZ | OR_BOTTOM); |
| 214 | |
| 215 | button_convert = new Button(i18n->TranslateString("Convert"), Point(87, 29), Size()); |
| 216 | button_convert->onAction.Connect(&DialogSplitter::OnConvert, this); |
| 217 | button_convert->SetOrientation(OR_LOWERRIGHT); |
| 218 | button_convert->SetWidth(Math::Max(80, button_convert->GetUnscaledTextWidth() + 14)); |
| 219 | button_convert->SetX(7 + button_convert->GetWidth()); |
| 220 | |
| 221 | button_save = new Button(i18n->TranslateString("Save"), Point(175, 29), Size()); |
| 222 | button_save->onAction.Connect(&DialogSplitter::OnSave, this); |
| 223 | button_save->SetOrientation(OR_LOWERRIGHT); |
| 224 | button_save->SetX(15 + button_convert->GetWidth() + button_save->GetWidth()); |
| 225 | |
| 226 | button_cancel = new Button(i18n->TranslateString("Cancel"), Point(175, 29), Size()); |
| 227 | button_cancel->onAction.Connect(&DialogSplitter::OnCancel, this); |
| 228 | button_cancel->SetOrientation(OR_LOWERRIGHT); |
| 229 | button_cancel->SetX(23 + button_convert->GetWidth() + button_save->GetWidth() + button_cancel->GetWidth()); |
| 230 | |
| 231 | /* Track list. |
| 232 | */ |
| 233 | Config *freacConfig = Config::Get(); |
| 234 | |
| 235 | text_tracks = new Text(i18n->AddColon(i18n->TranslateString("%1 track(s) in joblist").Replace("%1", String::FromInt(tracks.Length()))), Point(7, 5)); |
| 236 | |
| 237 | i18n->SetContext("Joblist"); |
| 238 | |
| 239 | button_select_all = new Button(ImageLoader::Load(String(freacConfig->resourcesPath).Append("icons/select/select-all.png")), Point(-2, 20), Size(21, 21)); |
| 240 | button_select_all->onAction.Connect(&DialogSplitter::SelectAll, this); |
| 241 | button_select_all->SetFlags(BF_NOFRAME); |
| 242 | button_select_all->SetTooltipText(i18n->TranslateString("Select all")); |
| 243 | |
| 244 | button_select_none = new Button(ImageLoader::Load(String(freacConfig->resourcesPath).Append("icons/select/select-none.png")), Point(-2, 34), Size(21, 21)); |
| 245 | button_select_none->onAction.Connect(&DialogSplitter::SelectNone, this); |
| 246 | button_select_none->SetFlags(BF_NOFRAME); |
| 247 | button_select_none->SetTooltipText(i18n->TranslateString("Select none")); |
| 248 | |
| 249 | button_select_toggle = new Button(ImageLoader::Load(String(freacConfig->resourcesPath).Append("icons/select/select-toggle.png")), Point(-2, 48), Size(21, 21)); |
| 250 | button_select_toggle->onAction.Connect(&DialogSplitter::ToggleSelection, this); |
| 251 | button_select_toggle->SetFlags(BF_NOFRAME); |
| 252 | button_select_toggle->SetTooltipText(i18n->TranslateString("Toggle selection")); |
| 253 | |
| 254 | Int numAlign = i18n->IsActiveLanguageRightToLeft() ? OR_LEFT : OR_RIGHT; |
| 255 | |
| 256 | list_tracks = new ListBox(Point(17, 24), Size(378, 90)); |
| 257 | list_tracks->SetFlags(LF_ALLOWREORDER | LF_MULTICHECKBOX); |
| 258 | list_tracks->onMarkEntry.Connect(&DialogSplitter::OnMarkEntry, this); |
| 259 | list_tracks->onClickTab.Connect(&DialogSplitter::OnClickTab, this); |
| 260 | list_tracks->onChangeEntryOrder.Connect(&DialogSplitter::OnChangeConfiguration, this); |
| 261 | list_tracks->AddTab(i18n->TranslateString("Artist"), 100); |
| 262 | list_tracks->AddTab(i18n->TranslateString("Title")); |
| 263 | list_tracks->AddTab(i18n->TranslateString("Track"), 50, numAlign); |
| 264 | list_tracks->AddTab(i18n->TranslateString("Length"), 80, numAlign); |
| 265 | |
| 266 | foreach (const Track &track, tracks) |
| 267 | { |
| 268 | ListEntry *entry = list_tracks->AddEntry(GetJoblistEntryString(track), True); |
| 269 | |
| 270 | joblistTracks.Add(track, entry->GetHandle()); |
| 271 | } |
| 272 | |
| 273 | /* Result list. |
| 274 | */ |
| 275 | list_result = new ListBox(Point(17, 24), Size(378, 90)); |
| 276 | list_result->AddTab(i18n->TranslateString("Artist"), 100); |
| 277 | list_result->AddTab(i18n->TranslateString("Title")); |
| 278 | list_result->AddTab(i18n->TranslateString("Length"), 80, numAlign); |
| 279 | |
| 280 | progress_length = new Progressbar(Point(17, 96), Size(378, 10), OR_HORZ, PB_NOTEXT, 0, 1000); |
| 281 | progress_length->Hide(); |
| 282 | |
| 283 | /* Configuration. |
| 284 | */ |
| 285 | i18n->SetContext("Dialogs::Splitter"); |
| 286 | |
| 287 | group_mode = new GroupBox(i18n->TranslateString("Split mode"), Point(7, 244), Size(300, 40)); |
| 288 | group_mode->SetOrientation(OR_LOWERLEFT); |
| 289 | |
| 290 | option_mode_time = new OptionBox(i18n->TranslateString("Split by duration"), Point(10, 13), Size(140, 0), &splitMode, SplitModeTime); |
| 291 | option_mode_time->onAction.Connect(&DialogSplitter::OnSelectMode, this); |
| 292 | |
| 293 | option_mode_parts = new OptionBox(i18n->TranslateString("Fixed number of parts"), Point(10, 13), Size(140, 0), &splitMode, SplitModeParts); |
| 294 | option_mode_parts->onAction.Connect(&DialogSplitter::OnSelectMode, this); |
| 295 | |
| 296 | option_mode_metadata = new OptionBox(i18n->TranslateString("Split upon metadata change"), Point(10, 13), Size(140, 0), &splitMode, SplitModeMetadata); |
| 297 | option_mode_metadata->onAction.Connect(&DialogSplitter::OnSelectMode, this); |
| 298 | |
| 299 | group_mode->Add(option_mode_time); |
| 300 | group_mode->Add(option_mode_parts); |
| 301 | group_mode->Add(option_mode_metadata); |
| 302 | |
| 303 | /* Split by duration. |
| 304 | */ |
| 305 | group_time = new GroupBox(i18n->TranslateString("Split by duration"), Point(7, 193), Size(300, 93)); |
| 306 | group_time->SetOrientation(OR_LOWERLEFT); |
| 307 | |
| 308 | text_time = new Text(i18n->AddColon(i18n->TranslateString("Duration per part")), Point(10, 16)); |
| 309 | |
| 310 | combo_time_mode = new ComboBox(Point(text_time->GetUnscaledTextWidth() + 17, 13), Size(100, 0)); |
| 311 | combo_time_mode->AddEntry(i18n->TranslateString("at most")); |
| 312 | combo_time_mode->AddEntry(i18n->TranslateString("approximately")); |
| 313 | combo_time_mode->AddEntry(i18n->TranslateString("at least")); |
| 314 | combo_time_mode->SelectNthEntry(config->GetIntValue(Config::CategorySplitterID, Config::SplitterPartDurationModeID, Config::SplitterPartDurationModeDefault)); |
| 315 | combo_time_mode->onSelectEntry.Connect(&DialogSplitter::OnChangeConfiguration, this); |
| 316 | |
| 317 | combo_time_exact = new ComboBox(Point(text_time->GetUnscaledTextWidth() + 17, 13), Size(100, 0)); |
| 318 | combo_time_exact->AddEntry(i18n->TranslateString("exactly")); |
| 319 | combo_time_exact->Deactivate(); |
| 320 | |
| 321 | edit_time_value = new EditBox(String::FromInt(partDuration), Point(combo_time_mode->GetX() + combo_time_mode->GetWidth() + 8, 13), Size(24, 0), 3); |
| 322 | edit_time_value->SetFlags(EDB_NUMERIC); |
| 323 | edit_time_value->onInput.Connect(&DialogSplitter::OnEditDuration, this); |
| 324 | |
| 325 | arrows_time_value = new Arrows(Point(edit_time_value->GetX() + edit_time_value->GetWidth() + 1, 14), Size(15, 18), OR_VERT, &partDuration, 1, 999); |
| 326 | arrows_time_value->onValueChange.Connect(&DialogSplitter::OnChangeDuration, this); |
| 327 | |
| 328 | combo_time_unit = new ComboBox(Point(arrows_time_value->GetX() + 23, 13), Size(80, 0)); |
| 329 | combo_time_unit->AddEntry(i18n->TranslateString("second(s)")); |
| 330 | combo_time_unit->AddEntry(i18n->TranslateString("minute(s)")); |
| 331 | combo_time_unit->AddEntry(i18n->TranslateString("hour(s)")); |
| 332 | combo_time_unit->SelectNthEntry(config->GetIntValue(Config::CategorySplitterID, Config::SplitterPartDurationUnitID, Config::SplitterPartDurationUnitDefault)); |
| 333 | combo_time_unit->onSelectEntry.Connect(&DialogSplitter::OnChangeConfiguration, this); |
| 334 | |
| 335 | check_time_tracks = new CheckBox(i18n->TranslateString("Split at track boundaries"), Point(10, 40), Size(140, 0), &splitAtTracks); |
| 336 | check_time_tracks->onAction.Connect(&DialogSplitter::OnToggleSplitAtTracks, this); |
| 337 | |
| 338 | repeat_time = new RepeatPreviousPartComponent(Point(10, 64), repeatEnd, repeatSeconds); |
| 339 | repeat_time->onToggleRepeat.Connect(&DialogSplitter::OnToggleRepeatEnd, this); |
| 340 | repeat_time->onChangeDuration.Connect(&DialogSplitter::OnChangeRepeatDuration, this); |
| 341 | |
| 342 | group_time->Add(text_time); |
| 343 | group_time->Add(combo_time_mode); |
| 344 | group_time->Add(combo_time_exact); |
| 345 | group_time->Add(edit_time_value); |
| 346 | group_time->Add(arrows_time_value); |
| 347 | group_time->Add(combo_time_unit); |
| 348 | group_time->Add(check_time_tracks); |
| 349 | group_time->Add(repeat_time); |
| 350 | |
| 351 | /* Split by number of parts. |
| 352 | */ |
| 353 | group_parts = new GroupBox(i18n->TranslateString("Fixed number of parts"), Point(7, 193), Size(300, 93)); |
| 354 | group_parts->SetOrientation(OR_LOWERLEFT); |
| 355 | |
| 356 | text_parts = new Text(i18n->AddColon(i18n->TranslateString("Number of parts")), Point(10, 16)); |
| 357 | |
| 358 | edit_parts = new EditBox(Point(text_parts->GetUnscaledTextWidth() + 17, 13), Size(24, 0), 3); |
| 359 | edit_parts->SetFlags(EDB_NUMERIC); |
| 360 | edit_parts->onInput.Connect(&DialogSplitter::OnEditParts, this); |
| 361 | |
| 362 | arrows_parts = new Arrows(Point(edit_parts->GetX() + edit_parts->GetWidth() + 1, 14), Size(15, 18), OR_VERT, &numberOfParts, 2, 100); |
| 363 | arrows_parts->onValueChange.Connect(&DialogSplitter::OnChangeParts, this); |
| 364 | |
| 365 | check_parts_tracks = new CheckBox(i18n->TranslateString("Split at track boundaries"), Point(10, 40), Size(140, 0), &splitAtTracks); |
| 366 | check_parts_tracks->onAction.Connect(&DialogSplitter::OnToggleSplitAtTracks, this); |
| 367 | |
| 368 | repeat_parts = new RepeatPreviousPartComponent(Point(10, 64), repeatEnd, repeatSeconds); |
| 369 | repeat_parts->onToggleRepeat.Connect(&DialogSplitter::OnToggleRepeatEnd, this); |
| 370 | repeat_parts->onChangeDuration.Connect(&DialogSplitter::OnChangeRepeatDuration, this); |
| 371 | |
| 372 | group_parts->Add(text_parts); |
| 373 | group_parts->Add(edit_parts); |
| 374 | group_parts->Add(arrows_parts); |
| 375 | group_parts->Add(check_parts_tracks); |
| 376 | group_parts->Add(repeat_parts); |
| 377 | |
| 378 | /* Split upon metadata change. |
| 379 | */ |
| 380 | group_metadata = new GroupBox(i18n->TranslateString("Split upon metadata change"), Point(7, 193), Size(300, 93)); |
| 381 | group_metadata->SetOrientation(OR_LOWERLEFT); |
| 382 | |
| 383 | text_metadata = new Text(i18n->AddColon(i18n->TranslateString("Metadata pattern to compare")), Point(10, 16)); |
| 384 | |
| 385 | edit_metadata = new EditBox(config->GetStringValue(Config::CategorySplitterID, Config::SplitterMetadataStringID, Config::SplitterMetadataStringDefault), Point(text_metadata->GetUnscaledTextWidth() + 17, 13), Size(240, 0)); |
| 386 | edit_metadata->onInput.Connect(&DialogSplitter::OnEditMetadataString, this); |
| 387 | |
| 388 | list_metadata_defaults = new List(); |
| 389 | list_metadata_defaults->AddEntry("<albumartist>,<album>"); |
| 390 | list_metadata_defaults->AddEntry("<albumartist>,<album>,<title>"); |
| 391 | list_metadata_defaults->AddEntry("<directory>"); |
| 392 | list_metadata_defaults->AddEntry("<filename>"); |
| 393 | |
| 394 | edit_metadata->SetDropDownList(list_metadata_defaults); |
| 395 | |
| 396 | repeat_metadata = new RepeatPreviousPartComponent(Point(10, 64), repeatEnd, repeatSeconds); |
| 397 | repeat_metadata->onToggleRepeat.Connect(&DialogSplitter::OnToggleRepeatEnd, this); |
| 398 | repeat_metadata->onChangeDuration.Connect(&DialogSplitter::OnChangeRepeatDuration, this); |
| 399 | |
| 400 | group_metadata->Add(text_metadata); |
| 401 | group_metadata->Add(edit_metadata); |
| 402 | group_metadata->Add(repeat_metadata); |
| 403 | |
| 404 | /* Output file name configuration. |
| 405 | */ |
| 406 | group_filename = new GroupBox(i18n->TranslateString("Output filenames"), Point(7, 89), Size(300, 42)); |
| 407 | group_filename->SetOrientation(OR_LOWERLEFT); |
| 408 | |
| 409 | text_filename = new Text(i18n->AddColon(i18n->TranslateString("Filename pattern")), Point(10, 16)); |
| 410 | |
| 411 | edit_filename = new EditBox(config->GetStringValue(Config::CategorySplitterID, Config::SplitterFileNamePatternID, Config::SplitterFileNamePatternDefault), Point(text_filename->GetUnscaledTextWidth() + 17, 13), Size(240, 0)); |
| 412 | |
| 413 | list_filename = new List(); |
| 414 | list_filename->AddEntry(String("<albumartist> - <album>")); |
| 415 | list_filename->AddEntry(String("<albumartist> - <album> - <title>")); |
| 416 | list_filename->AddEntry(String("<albumartist>").Append(Directory::GetDirectoryDelimiter()).Append("<album>")); |
| 417 | list_filename->AddEntry(String("<albumartist>").Append(Directory::GetDirectoryDelimiter()).Append("<album> - <title>")); |
| 418 | list_filename->AddEntry(String("<filename>")); |
| 419 | |
| 420 | edit_filename->SetDropDownList(list_filename); |
| 421 | |
| 422 | group_filename->Add(text_filename); |
| 423 | group_filename->Add(edit_filename); |
| 424 | |
| 425 | /* Configure widgets. |
| 426 | */ |
| 427 | OnSelectMode(); |
| 428 | OnToggleSplitAtTracks(); |
| 429 | |
| 430 | OnToggleRepeatEnd(repeatEnd); |
| 431 | |
| 432 | OnChangeParts(); |
| 433 | |
| 434 | disableUpdates = False; |
| 435 | |
| 436 | OnChangeConfiguration(); |
| 437 | |
| 438 | /* Add widgets to dialog. |
| 439 | */ |
| 440 | Add(mainWnd); |
| 441 | |
| 442 | mainWnd->Add(button_convert); |
| 443 | mainWnd->Add(button_save); |
| 444 | mainWnd->Add(button_cancel); |
| 445 | |
| 446 | mainWnd->Add(text_tracks); |
| 447 | |
| 448 | mainWnd->Add(button_select_all); |
| 449 | mainWnd->Add(button_select_none); |
| 450 | mainWnd->Add(button_select_toggle); |
| 451 | |
| 452 | mainWnd->Add(list_tracks); |
| 453 | mainWnd->Add(list_result); |
| 454 | |
| 455 | mainWnd->Add(progress_length); |
| 456 | |
| 457 | mainWnd->Add(group_mode); |
| 458 | mainWnd->Add(group_time); |
| 459 | mainWnd->Add(group_parts); |
| 460 | mainWnd->Add(group_metadata); |
| 461 | mainWnd->Add(group_filename); |
| 462 | |
| 463 | mainWnd->Add(mainWnd_titlebar); |
| 464 | mainWnd->Add(divbar); |
| 465 | |
| 466 | mainWnd->SetFlags(WF_MODAL); |
| 467 | mainWnd->SetIcon(ImageLoader::Load(String(Config::Get()->resourcesPath).Append("icons/freac.png"))); |
| 468 | |
| 469 | /* Update inexact track lengths in joblist. |
| 470 | */ |
| 471 | updateTrackLengthsThread = NonBlocking0<>(&DialogSplitter::UpdateTrackLengths, this).Call(); |
| 472 | } |
| 473 | |
| 474 | freac::DialogSplitter::~DialogSplitter() |
| 475 | { |
| 476 | DeleteObject(mainWnd_titlebar); |
| 477 | DeleteObject(mainWnd); |
| 478 | DeleteObject(divbar); |
| 479 | |
| 480 | DeleteObject(text_tracks); |
| 481 | |
| 482 | DeleteObject(button_select_all); |
| 483 | DeleteObject(button_select_none); |
| 484 | DeleteObject(button_select_toggle); |
| 485 | |
| 486 | DeleteObject(list_tracks); |
| 487 | DeleteObject(list_result); |
| 488 | |
| 489 | DeleteObject(progress_length); |
| 490 | |
| 491 | DeleteObject(group_mode); |
| 492 | DeleteObject(option_mode_time); |
| 493 | DeleteObject(option_mode_parts); |
| 494 | DeleteObject(option_mode_metadata); |
| 495 | |
| 496 | DeleteObject(group_time); |
| 497 | DeleteObject(text_time); |
| 498 | DeleteObject(combo_time_mode); |
| 499 | DeleteObject(combo_time_exact); |
| 500 | DeleteObject(edit_time_value); |
| 501 | DeleteObject(arrows_time_value); |
| 502 | DeleteObject(combo_time_unit); |
| 503 | DeleteObject(check_time_tracks); |
| 504 | DeleteObject(repeat_time); |
| 505 | |
| 506 | DeleteObject(group_parts); |
| 507 | DeleteObject(text_parts); |
| 508 | DeleteObject(edit_parts); |
| 509 | DeleteObject(arrows_parts); |
| 510 | DeleteObject(check_parts_tracks); |
| 511 | DeleteObject(repeat_parts); |
| 512 | |
| 513 | DeleteObject(group_metadata); |
| 514 | DeleteObject(text_metadata); |
| 515 | DeleteObject(edit_metadata); |
| 516 | DeleteObject(list_metadata_defaults); |
| 517 | DeleteObject(repeat_metadata); |
| 518 | |
| 519 | DeleteObject(group_filename); |
| 520 | DeleteObject(text_filename); |
| 521 | DeleteObject(edit_filename); |
| 522 | DeleteObject(list_filename); |
| 523 | |
| 524 | DeleteObject(button_convert); |
| 525 | DeleteObject(button_save); |
| 526 | DeleteObject(button_cancel); |
| 527 | } |
| 528 | |
| 529 | const Error &freac::DialogSplitter::ShowDialog() |
| 530 | { |
| 531 | mainWnd->WaitUntilClosed(); |
| 532 | |
| 533 | return error; |
| 534 | } |
| 535 | |
| 536 | Void freac::DialogSplitter::SaveSettings() |
| 537 | { |
| 538 | /* Save configuration. |
| 539 | */ |
| 540 | BoCA::Config *config = BoCA::Config::Get(); |
| 541 | |
| 542 | config->SetIntValue(Config::CategorySplitterID, Config::SplitterSplitModeID, splitMode); |
| 543 | config->SetIntValue(Config::CategorySplitterID, Config::SplitterSplitAtTracksID, splitAtTracks); |
| 544 | |
| 545 | config->SetIntValue(Config::CategorySplitterID, Config::SplitterPartDurationModeID, combo_time_mode->GetSelectedEntryNumber()); |
| 546 | config->SetIntValue(Config::CategorySplitterID, Config::SplitterPartDurationID, partDuration); |
| 547 | config->SetIntValue(Config::CategorySplitterID, Config::SplitterPartDurationUnitID, combo_time_unit->GetSelectedEntryNumber()); |
| 548 | |
| 549 | config->SetIntValue(Config::CategorySplitterID, Config::SplitterNumberOfPartsID, numberOfParts); |
| 550 | |
| 551 | config->SetIntValue(Config::CategorySplitterID, Config::SplitterRepeatEndID, repeatEnd); |
| 552 | config->SetIntValue(Config::CategorySplitterID, Config::SplitterRepeatSecondsID, repeatSeconds); |
| 553 | |
| 554 | config->SetStringValue(Config::CategorySplitterID, Config::SplitterMetadataStringID, edit_metadata->GetText()); |
| 555 | |
| 556 | config->SetStringValue(Config::CategorySplitterID, Config::SplitterFileNamePatternID, edit_filename->GetText()); |
| 557 | |
| 558 | /* Save window position. |
| 559 | */ |
| 560 | Rect wndRect = mainWnd->GetRestoredWindowRect(); |
| 561 | |
| 562 | config->SetIntValue(Config::CategorySplitterID, Config::SplitterWindowPosXID, wndRect.left); |
| 563 | config->SetIntValue(Config::CategorySplitterID, Config::SplitterWindowPosYID, wndRect.top); |
| 564 | |
| 565 | config->SetIntValue(Config::CategorySplitterID, Config::SplitterWindowSizeXID, wndRect.right - wndRect.left); |
| 566 | config->SetIntValue(Config::CategorySplitterID, Config::SplitterWindowSizeYID, wndRect.bottom - wndRect.top); |
| 567 | } |
| 568 | |
| 569 | String freac::DialogSplitter::GetJoblistEntryString(const BoCA::Track &track) const |
| 570 | { |
| 571 | const Info &info = track.GetInfo(); |
| 572 | |
| 573 | return String(info.artist).Append(ListEntry::tabDelimiter) |
| 574 | .Append(info.title).Append(ListEntry::tabDelimiter) |
| 575 | .Append(info.track > 0 ? (info.track < 10 ? String("0").Append(String::FromInt(info.track)) : String::FromInt(info.track)) : String()).Append(ListEntry::tabDelimiter) |
| 576 | .Append(track.GetLengthString()); |
| 577 | } |
| 578 | |
| 579 | String freac::DialogSplitter::GetResultEntryString(const BoCA::Track &track) const |
| 580 | { |
| 581 | const Info &info = track.GetInfo(); |
| 582 | |
| 583 | return String(info.artist).Append(ListEntry::tabDelimiter) |
| 584 | .Append(info.title).Append(ListEntry::tabDelimiter) |
| 585 | .Append(track.GetLengthString()); |
| 586 | } |
| 587 | |
| 588 | Bool freac::DialogSplitter::SortsAfter(const String &str1, const String &str2) const |
| 589 | { |
| 590 | Int length = Math::Min(str1.Length(), str2.Length()); |
| 591 | |
| 592 | for (Int i = 0; i < length; i++) |
| 593 | { |
| 594 | if (str1[i] > str2[i]) return True; |
| 595 | else if (str1[i] < str2[i]) return False; |
| 596 | } |
| 597 | |
| 598 | if (str1.Length() > str2.Length()) return True; |
| 599 | else return False; |
| 600 | } |
| 601 | |
| 602 | Void freac::DialogSplitter::UpdateTrackLengths() |
| 603 | { |
| 604 | BoCA::Config *config = BoCA::Config::Get(); |
| 605 | |
| 606 | Int trackCount = 0; |
| 607 | |
| 608 | foreach (Track &track, joblistTracks) |
| 609 | { |
| 610 | if (track.length >= 0) continue; |
| 611 | |
| 612 | trackCount++; |
| 613 | } |
| 614 | |
| 615 | if (trackCount == 0) return; |
| 616 | |
| 617 | /* Show progress bar. |
| 618 | */ |
| 619 | GUI::Application::Lock::Acquire(); |
| 620 | |
| 621 | updateTrackLengthsRunning = True; |
| 622 | |
| 623 | button_convert->Deactivate(); |
| 624 | list_tracks->SetHeight(list_result->GetHeight() - progress_length->GetHeight() - 8); |
| 625 | progress_length->Show(); |
| 626 | |
| 627 | GUI::Application::Lock::Release(); |
| 628 | |
| 629 | /* Process tracks. |
| 630 | */ |
| 631 | Int tracksProcessed = 0; |
| 632 | |
| 633 | foreach (Track &track, joblistTracks) |
| 634 | { |
| 635 | if (track.length >= 0) continue; |
| 636 | |
| 637 | /* Create decoder for track. |
| 638 | */ |
| 639 | Decoder *decoder = new Decoder(config); |
| 640 | |
| 641 | if (!decoder->Create(track.fileName, track)) |
| 642 | { |
| 643 | delete decoder; |
| 644 | |
| 645 | continue; |
| 646 | } |
| 647 | |
| 648 | /* Decode whole track. |
| 649 | */ |
| 650 | Bool failed = False; |
| 651 | |
| 652 | const Format &format = track.GetFormat(); |
| 653 | UnsignedLong samplesSize = format.rate / 8; |
| 654 | Int bytesPerSample = format.bits / 8 * format.channels; |
| 655 | |
| 656 | Buffer<UnsignedByte> buffer(samplesSize * bytesPerSample); |
| 657 | |
| 658 | while (!Threads::Access::Value(cancelUpdateTrackLengths)) |
| 659 | { |
| 660 | buffer.Resize(samplesSize * bytesPerSample); |
| 661 | |
| 662 | /* Read samples from decoder. |
| 663 | */ |
| 664 | Int bytes = decoder->Read(buffer); |
| 665 | |
| 666 | if (bytes == -1) { failed = True; break; } |
| 667 | else if (bytes == 0) break; |
| 668 | } |
| 669 | |
| 670 | /* Update track length if successful. |
| 671 | */ |
| 672 | GUI::Application::Lock::Acquire(); |
| 673 | |
| 674 | if (!failed) |
| 675 | { |
| 676 | track.length = decoder->GetDecodedSamples(); |
| 677 | |
| 678 | /* Update list entry. |
| 679 | */ |
| 680 | ListEntry *entry = list_tracks->GetNthEntry(foreachindex); |
| 681 | |
| 682 | entry->SetText(GetJoblistEntryString(track)); |
| 683 | |
| 684 | /* Update split result. |
| 685 | */ |
| 686 | OnChangeConfiguration(); |
| 687 | |
| 688 | /* Update track in main joblist. |
| 689 | */ |
| 690 | BoCA::JobList::Get()->onComponentModifyTrack.Emit(track); |
| 691 | } |
| 692 | |
| 693 | progress_length->SetValue(Math::Round(1000.0 / trackCount * ++tracksProcessed)); |
| 694 | |
| 695 | GUI::Application::Lock::Release(); |
| 696 | |
| 697 | decoder->Destroy(); |
| 698 | |
| 699 | delete decoder; |
| 700 | |
| 701 | if (Threads::Access::Value(cancelUpdateTrackLengths)) break; |
| 702 | } |
| 703 | |
| 704 | /* Hide progress bar. |
| 705 | */ |
| 706 | GUI::Application::Lock::Acquire(); |
| 707 | |
| 708 | progress_length->Hide(); |
| 709 | list_tracks->SetHeight(list_result->GetHeight()); |
| 710 | button_convert->Activate(); |
| 711 | |
| 712 | updateTrackLengthsRunning = False; |
| 713 | |
| 714 | GUI::Application::Lock::Release(); |
| 715 | } |
| 716 | |
| 717 | Void freac::DialogSplitter::OnChangeSize(const Size &nSize) |
| 718 | { |
| 719 | Rect clientRect = Rect(mainWnd->GetMainLayer()->GetPosition(), mainWnd->GetMainLayer()->GetSize()); |
| 720 | Size clientSize = Size(clientRect.right - clientRect.left, clientRect.bottom - clientRect.top); |
| 721 | |
| 722 | list_tracks->SetSize(Size(clientSize.cx / 2 - 16, clientSize.cy - 279)); |
| 723 | |
| 724 | list_result->SetX(clientSize.cx / 2 + 9); |
| 725 | list_result->SetSize(list_tracks->GetSize() + Size(clientSize.cx & 1, 0)); |
| 726 | |
| 727 | if (updateTrackLengthsRunning) list_tracks->SetHeight(list_tracks->GetHeight() - progress_length->GetHeight() - 8); |
| 728 | |
| 729 | progress_length->SetPosition(list_tracks->GetPosition() + Point(0, list_result->GetHeight() - progress_length->GetHeight())); |
| 730 | progress_length->SetWidth(list_tracks->GetWidth()); |
| 731 | |
| 732 | group_mode->SetWidth(clientSize.cx - 14); |
| 733 | |
| 734 | option_mode_time->SetWidth(group_mode->GetWidth() / 3 - 12); |
| 735 | |
| 736 | option_mode_parts->SetX(group_mode->GetWidth() / 3 + 6); |
| 737 | option_mode_parts->SetWidth(group_mode->GetWidth() / 3 - 12 + (group_mode->GetWidth() % 3)); |
| 738 | |
| 739 | option_mode_metadata->SetX(group_mode->GetWidth() / 3 * 2 + 2 + (group_mode->GetWidth() % 3)); |
| 740 | option_mode_metadata->SetWidth(group_mode->GetWidth() / 3 - 12); |
| 741 | |
| 742 | group_time->SetWidth(clientSize.cx - 14); |
| 743 | check_time_tracks->SetWidth(group_time->GetWidth() - 20); |
| 744 | |
| 745 | group_parts->SetWidth(clientSize.cx - 14); |
| 746 | check_parts_tracks->SetWidth(group_parts->GetWidth() - 20); |
| 747 | |
| 748 | group_metadata->SetWidth(clientSize.cx - 14); |
| 749 | edit_metadata->SetWidth(group_metadata->GetWidth() - edit_metadata->GetX() - 10); |
| 750 | |
| 751 | group_filename->SetWidth(clientSize.cx - 14); |
| 752 | edit_filename->SetWidth(group_filename->GetWidth() - edit_filename->GetX() - 10); |
| 753 | } |
| 754 | |
| 755 | Void freac::DialogSplitter::SelectAll() |
| 756 | { |
| 757 | disableUpdates = True; |
| 758 | |
| 759 | for (Int i = 0; i < list_tracks->Length(); i++) |
| 760 | { |
| 761 | if (!list_tracks->GetNthEntry(i)->IsMarked()) list_tracks->GetNthEntry(i)->SetMark(True); |
| 762 | } |
| 763 | |
| 764 | disableUpdates = False; |
| 765 | |
| 766 | OnChangeConfiguration(); |
| 767 | } |
| 768 | |
| 769 | Void freac::DialogSplitter::SelectNone() |
| 770 | { |
| 771 | disableUpdates = True; |
| 772 | |
| 773 | for (Int i = 0; i < list_tracks->Length(); i++) |
| 774 | { |
| 775 | if (list_tracks->GetNthEntry(i)->IsMarked()) list_tracks->GetNthEntry(i)->SetMark(False); |
| 776 | } |
| 777 | |
| 778 | disableUpdates = False; |
| 779 | |
| 780 | OnChangeConfiguration(); |
| 781 | } |
| 782 | |
| 783 | Void freac::DialogSplitter::ToggleSelection() |
| 784 | { |
| 785 | disableUpdates = True; |
| 786 | |
| 787 | for (Int i = 0; i < list_tracks->Length(); i++) |
| 788 | { |
| 789 | if (list_tracks->GetNthEntry(i)->IsMarked()) list_tracks->GetNthEntry(i)->SetMark(False); |
| 790 | else list_tracks->GetNthEntry(i)->SetMark(True); |
| 791 | } |
| 792 | |
| 793 | disableUpdates = False; |
| 794 | |
| 795 | OnChangeConfiguration(); |
| 796 | } |
| 797 | |
| 798 | Void freac::DialogSplitter::OnMarkEntry(ListEntry *entry) |
| 799 | { |
| 800 | OnChangeConfiguration(); |
| 801 | } |
| 802 | |
| 803 | Void freac::DialogSplitter::OnClickTab(Int n) |
| 804 | { |
| 805 | Surface *surface = mainWnd->GetDrawSurface(); |
| 806 | |
| 807 | surface->StartPaint(Rect(list_result->GetRealPosition(), list_result->GetRealSize())); |
| 808 | |
| 809 | /* Operate on a copy of the actual track list. |
| 810 | */ |
| 811 | Array<Track> tracks = joblistTracks; |
| 812 | |
| 813 | /* Find sorting criteria. |
| 814 | */ |
| 815 | Bool sortByArtist = (n == 0); |
| 816 | Bool sortByTitle = (n == 1); |
| 817 | Bool sortByTrack = (n == 2); |
| 818 | Bool sortByTime = (n == 3); |
| 819 | |
| 820 | /* Check if reverse sorting is needed. |
| 821 | */ |
| 822 | static Int previousCriteria; |
| 823 | static Bool reverse = False; |
| 824 | |
| 825 | if (n == previousCriteria) |
| 826 | { |
| 827 | /* Revert sort order previously reversed. |
| 828 | */ |
| 829 | for (Int i = 0; reverse && i < tracks.Length(); i++) list_tracks->MoveEntry(0, tracks.Length() - i - 1); |
| 830 | |
| 831 | reverse = !reverse; |
| 832 | } |
| 833 | else |
| 834 | { |
| 835 | previousCriteria = n; |
| 836 | |
| 837 | reverse = False; |
| 838 | } |
| 839 | |
| 840 | /* Sort the list using insertion sort. |
| 841 | */ |
| 842 | for (Int i = 0; i < tracks.Length(); i++) |
| 843 | { |
| 844 | Track &thisTrack = tracks.GetReference(list_tracks->GetNthEntry(i)->GetHandle()); |
| 845 | Info thisInfo = thisTrack.GetInfo(); |
| 846 | const Format &thisFormat = thisTrack.GetFormat(); |
| 847 | |
| 848 | if (sortByArtist) thisInfo.artist = thisInfo.artist.ToLower(); |
| 849 | else if (sortByTitle) thisInfo.title = thisInfo.title.ToLower(); |
| 850 | |
| 851 | thisTrack.SetInfo(thisInfo); |
| 852 | |
| 853 | /* Find position using optimized binary search. |
| 854 | */ |
| 855 | Int bottom = 0; |
| 856 | Int top = i - 1; |
| 857 | |
| 858 | while (top >= bottom) |
| 859 | { |
| 860 | /* Select element to compare optimizing for pre-sorted lists by |
| 861 | * selecting i - 1 initially before sticking to binary search. |
| 862 | */ |
| 863 | Int m = (top == i - 1 ? top : (top + bottom) / 2); |
| 864 | |
| 865 | /* Perform comparison. |
| 866 | */ |
| 867 | const Track &compTrack = tracks.Get(list_tracks->GetNthEntry(m)->GetHandle()); |
| 868 | const Info &compInfo = compTrack.GetInfo(); |
| 869 | const Format &compFormat = compTrack.GetFormat(); |
| 870 | |
| 871 | if ((sortByArtist && SortsAfter(compInfo.artist, thisInfo.artist) ) || |
| 872 | (sortByTitle && SortsAfter(compInfo.title, thisInfo.title) ) || |
| 873 | (sortByTrack && compInfo.track > thisInfo.track ) || |
| 874 | (sortByTime && (compTrack.length > 0 ? compTrack.length : compTrack.approxLength) / compFormat.rate > |
| 875 | (thisTrack.length > 0 ? thisTrack.length : thisTrack.approxLength) / thisFormat.rate)) top = m - 1; |
| 876 | else bottom = m + 1; |
| 877 | } |
| 878 | |
| 879 | /* Move element to target position. |
| 880 | */ |
| 881 | if (Math::Max(top, bottom) != i) list_tracks->MoveEntry(i, Math::Max(top, bottom)); |
| 882 | } |
| 883 | |
| 884 | /* Revert sort order if requested. |
| 885 | */ |
| 886 | for (Int i = 0; reverse && i < tracks.Length(); i++) list_tracks->MoveEntry(0, tracks.Length() - i - 1); |
| 887 | |
| 888 | surface->EndPaint(); |
| 889 | |
| 890 | /* Redraw joblist. |
| 891 | */ |
| 892 | list_tracks->Paint(SP_UPDATE); |
| 893 | |
| 894 | } |
| 895 | |
| 896 | Void freac::DialogSplitter::OnSelectMode() |
| 897 | { |
| 898 | Surface *surface = mainWnd->GetDrawSurface(); |
| 899 | Float scaleFactor = surface->GetSurfaceDPI() / 96.0; |
| 900 | |
| 901 | surface->StartPaint(Rect(group_time->GetRealPosition() - Point(0, 8 * scaleFactor), group_time->GetRealSize() + Size(0, 8 * scaleFactor))); |
| 902 | |
| 903 | group_time->Hide(); |
| 904 | group_parts->Hide(); |
| 905 | group_metadata->Hide(); |
| 906 | |
| 907 | if (splitMode == SplitModeTime) group_time->Show(); |
| 908 | else if (splitMode == SplitModeParts) group_parts->Show(); |
| 909 | else if (splitMode == SplitModeMetadata) group_metadata->Show(); |
| 910 | |
| 911 | surface->EndPaint(); |
| 912 | |
| 913 | OnChangeConfiguration(); |
| 914 | } |
| 915 | |
| 916 | Void freac::DialogSplitter::OnToggleSplitAtTracks() |
| 917 | { |
| 918 | if (splitAtTracks) |
| 919 | { |
| 920 | combo_time_exact->Hide(); |
| 921 | combo_time_mode->Show(); |
| 922 | } |
| 923 | else |
| 924 | { |
| 925 | combo_time_mode->Hide(); |
| 926 | combo_time_exact->Show(); |
| 927 | } |
| 928 | |
| 929 | OnChangeConfiguration(); |
| 930 | } |
| 931 | |
| 932 | Void freac::DialogSplitter::OnToggleRepeatEnd(Bool repeat) |
| 933 | { |
| 934 | repeatEnd = repeat; |
| 935 | |
| 936 | repeat_time->SetRepeat(repeatEnd); |
| 937 | repeat_parts->SetRepeat(repeatEnd); |
| 938 | repeat_metadata->SetRepeat(repeatEnd); |
| 939 | |
| 940 | OnChangeConfiguration(); |
| 941 | } |
| 942 | |
| 943 | Void freac::DialogSplitter::OnEditDuration() |
| 944 | { |
| 945 | partDuration = Math::Max(1, Math::Min(edit_time_value->GetText().ToInt(), 999)); |
| 946 | |
| 947 | edit_time_value->SetText(String::FromInt(partDuration)); |
| 948 | |
| 949 | OnChangeConfiguration(); |
| 950 | } |
| 951 | |
| 952 | Void freac::DialogSplitter::OnChangeDuration() |
| 953 | { |
| 954 | edit_time_value->SetText(String::FromInt(partDuration)); |
| 955 | |
| 956 | OnChangeConfiguration(); |
| 957 | } |
| 958 | |
| 959 | Void freac::DialogSplitter::OnEditParts() |
| 960 | { |
| 961 | numberOfParts = Math::Max(2, Math::Min(edit_parts->GetText().ToInt(), 100)); |
| 962 | |
| 963 | edit_parts->SetText(String::FromInt(numberOfParts)); |
| 964 | |
| 965 | OnChangeConfiguration(); |
| 966 | } |
| 967 | |
| 968 | Void freac::DialogSplitter::OnChangeParts() |
| 969 | { |
| 970 | edit_parts->SetText(String::FromInt(numberOfParts)); |
| 971 | |
| 972 | OnChangeConfiguration(); |
| 973 | } |
| 974 | |
| 975 | Void freac::DialogSplitter::OnChangeRepeatDuration(Int seconds) |
| 976 | { |
| 977 | repeatSeconds = seconds; |
| 978 | |
| 979 | repeat_time->SetDuration(repeatSeconds); |
| 980 | repeat_parts->SetDuration(repeatSeconds); |
| 981 | repeat_metadata->SetDuration(repeatSeconds); |
| 982 | |
| 983 | OnChangeConfiguration(); |
| 984 | } |
| 985 | |
| 986 | Void freac::DialogSplitter::OnEditMetadataString() |
| 987 | { |
| 988 | OnChangeConfiguration(); |
| 989 | } |
| 990 | |
| 991 | Void freac::DialogSplitter::OnChangeConfiguration() |
| 992 | { |
| 993 | if (disableUpdates) return; |
| 994 | |
| 995 | /* Get configuration. |
| 996 | */ |
| 997 | BoCA::Config *config = BoCA::Config::Get(); |
| 998 | |
| 999 | String encoderID = config->GetStringValue(Config::CategorySettingsID, Config::SettingsEncoderID, Config::SettingsEncoderDefault); |
| 1000 | Int splitMode = this->splitMode; |
| 1001 | |
| 1002 | /* Protect this code by a mutex. |
| 1003 | */ |
| 1004 | static Threads::Mutex mutex; |
| 1005 | |
| 1006 | Int suspendCount = GUI::Application::Lock::SuspendLock(); |
| 1007 | |
| 1008 | Threads::Lock lock(mutex); |
| 1009 | |
| 1010 | GUI::Application::Lock::ResumeLock(suspendCount); |
| 1011 | |
| 1012 | /* Find target duration for output tracks. |
| 1013 | */ |
| 1014 | Int64 targetDuration = -1; |
| 1015 | |
| 1016 | Int durationMode = 1; |
| 1017 | Int durationRate = 48000; |
| 1018 | |
| 1019 | if (joblistTracks.Length() > 0) durationRate = joblistTracks.GetFirst().GetFormat().rate; |
| 1020 | |
| 1021 | if (splitMode == SplitModeTime) |
| 1022 | { |
| 1023 | /* Split by duration. |
| 1024 | */ |
| 1025 | Int durationUnit = combo_time_unit->GetSelectedEntryNumber(); |
| 1026 | |
| 1027 | if (durationUnit == 0) targetDuration = durationRate * edit_time_value->GetText().ToInt() ; |
| 1028 | else if (durationUnit == 1) targetDuration = durationRate * edit_time_value->GetText().ToInt() * 60; |
| 1029 | else targetDuration = durationRate * edit_time_value->GetText().ToInt() * 3600; |
| 1030 | |
| 1031 | durationMode = combo_time_mode->GetSelectedEntryNumber(); |
| 1032 | } |
| 1033 | else if (splitMode == SplitModeParts) |
| 1034 | { |
| 1035 | /* Split by number of parts. |
| 1036 | */ |
| 1037 | Float totalDuration = 0; |
| 1038 | |
| 1039 | foreach (const Track &track, joblistTracks) |
| 1040 | { |
| 1041 | if (!list_tracks->GetNthEntry(foreachindex)->IsMarked()) continue; |
| 1042 | |
| 1043 | totalDuration += GetTrackDuration(track, durationRate); |
| 1044 | } |
| 1045 | |
| 1046 | if (repeatEnd) totalDuration += repeatSeconds * durationRate * (numberOfParts - 1); |
| 1047 | |
| 1048 | targetDuration = Math::Ceil(totalDuration / numberOfParts); |
| 1049 | } |
| 1050 | |
| 1051 | /* Generate output tracks. |
| 1052 | */ |
| 1053 | Array<Track> currentTrackList; |
| 1054 | Int64 currentDuration = 0; |
| 1055 | String currentMetadataString; |
| 1056 | |
| 1057 | resultTrackLists.RemoveAll(); |
| 1058 | |
| 1059 | for (Int i = 0; i < list_tracks->Length(); i++) |
| 1060 | { |
| 1061 | ListEntry *entry = list_tracks->GetNthEntry(i); |
| 1062 | |
| 1063 | if (!entry->IsMarked()) continue; |
| 1064 | |
| 1065 | const Track &track = joblistTracks.Get(entry->GetHandle()); |
| 1066 | |
| 1067 | if (splitMode == SplitModeTime || splitMode == SplitModeParts) |
| 1068 | { |
| 1069 | Int64 trackDuration = GetTrackDuration(track, durationRate); |
| 1070 | |
| 1071 | if (currentTrackList.Length() > 0 && splitAtTracks) |
| 1072 | { |
| 1073 | /* For duration modes 0 (at most) and 1 (approximate), check |
| 1074 | * the duration we would reach when including the next track. |
| 1075 | */ |
| 1076 | Int64 newDuration = currentDuration + trackDuration; |
| 1077 | |
| 1078 | if ((durationMode == 0 && newDuration > targetDuration ) || |
| 1079 | (durationMode == 1 && (newDuration - targetDuration) > (targetDuration - currentDuration))) |
| 1080 | { |
| 1081 | resultTrackLists.Add(currentTrackList); |
| 1082 | currentTrackList.RemoveAll(); |
| 1083 | |
| 1084 | currentDuration = 0; |
| 1085 | |
| 1086 | if (repeatEnd && i < list_tracks->Length() - 1) AddPreviousPartRepeat(currentTrackList, currentDuration, durationRate); |
| 1087 | } |
| 1088 | } |
| 1089 | |
| 1090 | /* Add next track or segment. |
| 1091 | */ |
| 1092 | if (splitAtTracks) |
| 1093 | { |
| 1094 | currentTrackList.Add(track); |
| 1095 | |
| 1096 | currentDuration += trackDuration; |
| 1097 | |
| 1098 | /* Check if target duration is reached. |
| 1099 | */ |
| 1100 | if (currentDuration >= targetDuration) |
| 1101 | { |
| 1102 | resultTrackLists.Add(currentTrackList); |
| 1103 | currentTrackList.RemoveAll(); |
| 1104 | |
| 1105 | currentDuration = 0; |
| 1106 | |
| 1107 | if (repeatEnd && i < list_tracks->Length() - 1) AddPreviousPartRepeat(currentTrackList, currentDuration, durationRate); |
| 1108 | } |
| 1109 | } |
| 1110 | else |
| 1111 | { |
| 1112 | Track trackToAdd = track; |
| 1113 | Int64 trackToAddLength = 0; |
| 1114 | |
| 1115 | if (trackToAdd.length > 0) trackToAddLength = trackToAdd.length; |
| 1116 | else if (trackToAdd.approxLength > 0) trackToAddLength = trackToAdd.approxLength; |
| 1117 | |
| 1118 | while (currentDuration + trackDuration > targetDuration) |
| 1119 | { |
| 1120 | /* Add next segment of track to current track list. |
| 1121 | */ |
| 1122 | Track cutTrack = trackToAdd; |
| 1123 | Int64 cutTrackDuration = targetDuration - currentDuration; |
| 1124 | |
| 1125 | cutTrack.length = cutTrackDuration * cutTrack.GetFormat().rate / durationRate; |
| 1126 | |
| 1127 | if (trackToAddLength - cutTrack.length < cutTrack.GetFormat().rate / 8) cutTrack.length = trackToAddLength; |
| 1128 | |
| 1129 | currentTrackList.Add(cutTrack); |
| 1130 | resultTrackLists.Add(currentTrackList); |
| 1131 | currentTrackList.RemoveAll(); |
| 1132 | |
| 1133 | currentDuration = 0; |
| 1134 | trackDuration -= cutTrackDuration; |
| 1135 | |
| 1136 | trackToAdd.sampleOffset += cutTrack.length; |
| 1137 | |
| 1138 | if (trackToAdd.length > 0) trackToAdd.length -= cutTrack.length; |
| 1139 | else if (trackToAdd.approxLength > 0) trackToAdd.approxLength -= cutTrack.length; |
| 1140 | |
| 1141 | if (repeatEnd) AddPreviousPartRepeat(currentTrackList, currentDuration, durationRate); |
| 1142 | } |
| 1143 | |
| 1144 | if (trackToAddLength > 0) currentTrackList.Add(trackToAdd); |
| 1145 | |
| 1146 | currentDuration += trackDuration; |
| 1147 | } |
| 1148 | } |
| 1149 | else if (splitMode == SplitModeMetadata) |
| 1150 | { |
| 1151 | String metadataString = Utilities::GetOutputFileName(track, edit_metadata->GetText(), NIL, False, encoderID, False, True, False, False); |
| 1152 | |
| 1153 | if (metadataString != currentMetadataString) |
| 1154 | { |
| 1155 | if (currentTrackList.Length() > 0) |
| 1156 | { |
| 1157 | resultTrackLists.Add(currentTrackList); |
| 1158 | currentTrackList.RemoveAll(); |
| 1159 | |
| 1160 | if (repeatEnd) AddPreviousPartRepeat(currentTrackList, currentDuration, durationRate); |
| 1161 | } |
| 1162 | |
| 1163 | currentMetadataString = metadataString; |
| 1164 | } |
| 1165 | |
| 1166 | currentTrackList.Add(track); |
| 1167 | } |
| 1168 | } |
| 1169 | |
| 1170 | if (currentTrackList.Length() > 0) resultTrackLists.Add(currentTrackList); |
| 1171 | |
| 1172 | /* Add track lists to result list. |
| 1173 | */ |
| 1174 | Surface *surface = mainWnd->GetDrawSurface(); |
| 1175 | |
| 1176 | surface->StartPaint(Rect(list_result->GetRealPosition(), list_result->GetRealSize())); |
| 1177 | |
| 1178 | Int selectedEntry = list_result->GetSelectedEntryNumber(); |
| 1179 | Scrollbar *scrollbar = NIL; |
| 1180 | Int scrollbarPos = 0; |
| 1181 | |
| 1182 | for (Int i = 0; i < list_result->GetNOfObjects(); i++) |
| 1183 | { |
| 1184 | Object *object = list_result->GetNthObject(i); |
| 1185 | |
| 1186 | if (object->GetObjectType() != Scrollbar::classID) continue; |
| 1187 | |
| 1188 | scrollbar = (Scrollbar *) object; |
| 1189 | scrollbarPos = scrollbar->GetValue(); |
| 1190 | |
| 1191 | break; |
| 1192 | } |
| 1193 | |
| 1194 | list_result->SetVisibleDirect(False); |
| 1195 | list_result->RemoveAllEntries(); |
| 1196 | |
| 1197 | foreach (const Array<Track> &trackList, resultTrackLists) |
| 1198 | { |
| 1199 | /* Compute total length of track list. |
| 1200 | */ |
| 1201 | Track resultTrack = GetIdentifyingTrack(trackList, foreachindex == 0); |
| 1202 | |
| 1203 | resultTrack.length = 0; |
| 1204 | resultTrack.approxLength = 0; |
| 1205 | |
| 1206 | foreach (const Track &track, trackList) |
| 1207 | { |
| 1208 | if (track.length > 0) resultTrack.length += track.length * resultTrack.GetFormat().rate / track.GetFormat().rate; |
| 1209 | else if (track.approxLength > 0) resultTrack.approxLength += track.approxLength * resultTrack.GetFormat().rate / track.GetFormat().rate; |
| 1210 | } |
| 1211 | |
| 1212 | if (resultTrack.approxLength > 0) |
| 1213 | { |
| 1214 | resultTrack.approxLength += resultTrack.length; |
| 1215 | resultTrack.length = -1; |
| 1216 | } |
| 1217 | |
| 1218 | /* Add consolidated track to result list. |
| 1219 | */ |
| 1220 | list_result->AddEntry(GetResultEntryString(resultTrack)); |
| 1221 | } |
| 1222 | |
| 1223 | if (selectedEntry >= 0) list_result->SelectNthEntry(selectedEntry); |
| 1224 | if (scrollbar != NIL) scrollbar->SetValue(scrollbarPos); |
| 1225 | |
| 1226 | list_result->Show(); |
| 1227 | list_result->Process(SM_MOUSEMOVE, 0, 0); |
| 1228 | |
| 1229 | surface->EndPaint(); |
| 1230 | } |
| 1231 | |
| 1232 | Void freac::DialogSplitter::AddPreviousPartRepeat(Array<Track> ¤tTrackList, Int64 ¤tDuration, Int durationRate) const |
| 1233 | { |
| 1234 | /* Add requested number of seconds from previous part. |
| 1235 | */ |
| 1236 | Int64 durationLeft = repeatSeconds * durationRate; |
| 1237 | |
| 1238 | foreachreverse (const Array<Track> &trackList, resultTrackLists) |
| 1239 | { |
| 1240 | foreachreverse (const Track &track, trackList) |
| 1241 | { |
| 1242 | Track trackToAdd = track; |
| 1243 | Int64 trackDuration = GetTrackDuration(track, durationRate); |
| 1244 | Int64 durationToAdd = Math::Min(trackDuration, durationLeft); |
| 1245 | Int64 segmentLength = durationToAdd * trackToAdd.GetFormat().rate / durationRate; |
| 1246 | |
| 1247 | if (trackToAdd.length > 0) { trackToAdd.sampleOffset = trackToAdd.length - segmentLength; trackToAdd.length = segmentLength; } |
| 1248 | else if (trackToAdd.approxLength > 0) { trackToAdd.sampleOffset = trackToAdd.approxLength - segmentLength; trackToAdd.approxLength = segmentLength; } |
| 1249 | |
| 1250 | currentTrackList.Add(trackToAdd); |
| 1251 | |
| 1252 | durationLeft -= durationToAdd; |
| 1253 | |
| 1254 | if (durationLeft == 0) break; |
| 1255 | } |
| 1256 | |
| 1257 | if (durationLeft == 0) break; |
| 1258 | } |
| 1259 | |
| 1260 | currentDuration = repeatSeconds * durationRate - durationLeft; |
| 1261 | } |
| 1262 | |
| 1263 | Int64 freac::DialogSplitter::GetTrackDuration(const Track &track, Int durationRate) const |
| 1264 | { |
| 1265 | if (track.length > 0) return durationRate * track.length / track.GetFormat().rate; |
| 1266 | else if (track.approxLength > 0) return durationRate * track.approxLength / track.GetFormat().rate; |
| 1267 | |
| 1268 | return Int64(durationRate) * 240; |
| 1269 | } |
| 1270 | |
| 1271 | const Track &freac::DialogSplitter::GetIdentifyingTrack(const Array<Track> &trackList, Bool first) const |
| 1272 | { |
| 1273 | if (!repeatEnd) return trackList.GetFirst(); |
| 1274 | |
| 1275 | Int durationRate = trackList.GetFirst().GetFormat().rate; |
| 1276 | Int64 durationLeft = repeatSeconds * durationRate; |
| 1277 | |
| 1278 | foreach (const Track &track, trackList) |
| 1279 | { |
| 1280 | Int64 trackDuration = GetTrackDuration(track, durationRate); |
| 1281 | |
| 1282 | if (trackDuration > durationLeft) return track; |
| 1283 | |
| 1284 | durationLeft -= trackDuration; |
| 1285 | } |
| 1286 | |
| 1287 | return trackList.GetFirst(); |
| 1288 | } |
| 1289 | |
| 1290 | Void freac::DialogSplitter::OnConvert() |
| 1291 | { |
| 1292 | /* Save configuration. |
| 1293 | */ |
| 1294 | SaveSettings(); |
| 1295 | |
| 1296 | /* Wait for track length updates to finish. |
| 1297 | */ |
| 1298 | updateTrackLengthsThread->Wait(); |
| 1299 | |
| 1300 | /* Get and adjust configuration values. |
| 1301 | */ |
| 1302 | BoCA::Config *config = BoCA::Config::Get(); |
| 1303 | |
| 1304 | Bool warnLossyToLossless = config->GetIntValue(Config::CategorySettingsID, Config::SettingsWarnLossyToLosslessID, Config::SettingsWarnLossyToLosslessDefault); |
| 1305 | Bool encodeToSingleFile = config->GetIntValue(Config::CategorySettingsID, Config::SettingsEncodeToSingleFileID, Config::SettingsEncodeToSingleFileDefault); |
| 1306 | Bool writeToInputDir = config->GetIntValue(Config::CategorySettingsID, Config::SettingsWriteToInputDirectoryID, Config::SettingsWriteToInputDirectoryDefault); |
| 1307 | String singleFileName = config->GetStringValue(Config::CategorySettingsID, Config::SettingsSingleFilenameID, Config::SettingsSingleFilenameDefault); |
| 1308 | String fileNamePattern = config->GetStringValue(Config::CategorySettingsID, Config::SettingsEncoderFilenamePatternID, Config::SettingsEncoderFilenamePatternDefault); |
| 1309 | |
| 1310 | config->SetIntValue(Config::CategorySettingsID, Config::SettingsEncodeToSingleFileID, True); |
| 1311 | config->SetIntValue(Config::CategorySettingsID, Config::SettingsWriteToInputDirectoryID, False); |
| 1312 | config->SetStringValue(Config::CategorySettingsID, Config::SettingsEncoderFilenamePatternID, edit_filename->GetText()); |
| 1313 | |
| 1314 | /* Schedule conversion jobs. |
| 1315 | */ |
| 1316 | Bool aborted = False; |
| 1317 | String fileNames; |
| 1318 | |
| 1319 | foreach (const Array<Track> &trackList, resultTrackLists) |
| 1320 | { |
| 1321 | /* Set output filename. |
| 1322 | */ |
| 1323 | const Track &track = GetIdentifyingTrack(trackList, foreachindex == 0); |
| 1324 | String defaultFileName = Utilities::GetOutputFileName(config, track); |
| 1325 | String baseFileName = String(defaultFileName).Head(defaultFileName.FindLast(".")); |
| 1326 | String fileExtension = String(defaultFileName).Tail(defaultFileName.Length() - defaultFileName.FindLast(".")); |
| 1327 | String outputFile = defaultFileName; |
| 1328 | Int count = 1; |
| 1329 | |
| 1330 | while (File(outputFile).Exists() || fileNames.Contains(outputFile)) outputFile = String(baseFileName).Append(" (").Append(String::FromInt(++count)).Append(")").Append(fileExtension); |
| 1331 | |
| 1332 | fileNames.Append(outputFile).Append("\n"); |
| 1333 | |
| 1334 | config->SetStringValue(Config::CategorySettingsID, Config::SettingsSingleFilenameID, outputFile); |
| 1335 | |
| 1336 | /* Schedule job. |
| 1337 | */ |
| 1338 | Job *job = new JobConvert(trackList); |
| 1339 | |
| 1340 | if (job->RunPrecheck() != Success()) |
| 1341 | { |
| 1342 | DeleteObject(job); |
| 1343 | |
| 1344 | aborted = True; |
| 1345 | |
| 1346 | break; |
| 1347 | } |
| 1348 | |
| 1349 | config->SetIntValue(Config::CategorySettingsID, Config::SettingsWarnLossyToLosslessID, False); |
| 1350 | } |
| 1351 | |
| 1352 | /* Restore configuration values. |
| 1353 | */ |
| 1354 | config->SetIntValue(Config::CategorySettingsID, Config::SettingsWarnLossyToLosslessID, warnLossyToLossless); |
| 1355 | config->SetIntValue(Config::CategorySettingsID, Config::SettingsEncodeToSingleFileID, encodeToSingleFile); |
| 1356 | config->SetIntValue(Config::CategorySettingsID, Config::SettingsWriteToInputDirectoryID, writeToInputDir); |
| 1357 | config->SetStringValue(Config::CategorySettingsID, Config::SettingsSingleFilenameID, singleFileName); |
| 1358 | config->SetStringValue(Config::CategorySettingsID, Config::SettingsEncoderFilenamePatternID, fileNamePattern); |
| 1359 | |
| 1360 | /* Close dialog window. |
| 1361 | */ |
| 1362 | if (!aborted) mainWnd->Close(); |
| 1363 | } |
| 1364 | |
| 1365 | Void freac::DialogSplitter::OnSave() |
| 1366 | { |
| 1367 | SaveSettings(); |
| 1368 | |
| 1369 | OnCancel(); |
| 1370 | } |
| 1371 | |
| 1372 | Void freac::DialogSplitter::OnCancel() |
| 1373 | { |
| 1374 | Threads::Access::Set(cancelUpdateTrackLengths, True); |
| 1375 | |
| 1376 | Int suspendCount = GUI::Application::Lock::SuspendLock(); |
| 1377 | |
| 1378 | updateTrackLengthsThread->Wait(); |
| 1379 | |
| 1380 | GUI::Application::Lock::ResumeLock(suspendCount); |
| 1381 | |
| 1382 | mainWnd->Close(); |
| 1383 | } |