| 1 | /* fre:ac - free audio converter |
| 2 | * Copyright (C) 2001-2021 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 <progress.h> |
| 14 | #include <config.h> |
| 15 | |
| 16 | #include <support/autorelease.h> |
| 17 | |
| 18 | using namespace BoCA; |
| 19 | using namespace BoCA::AS; |
| 20 | |
| 21 | Array<freac::Progress *> freac::Progress::progressIndicators; |
| 22 | |
| 23 | freac::Progress::Progress(const BoCA::Config *iConfiguration) |
| 24 | { |
| 25 | configuration = iConfiguration; |
| 26 | consoleMode = configuration->GetIntValue(Config::CategorySettingsID, Config::SettingsEnableConsoleID, Config::SettingsEnableConsoleDefault); |
| 27 | |
| 28 | lastInvoked = 0; |
| 29 | |
| 30 | totalSamplesLeft = 0; |
| 31 | totalSamplesMultiplier = 1; |
| 32 | totalProgressDone = 0; |
| 33 | |
| 34 | startTicks = 0; |
| 35 | pauseTicks = 0; |
| 36 | |
| 37 | progressIndicators.EnableLocking(); |
| 38 | progressIndicators.Add(this); |
| 39 | |
| 40 | /* Get main window handle for taskbar/dock progress. |
| 41 | */ |
| 42 | window = Window::GetNthWindow(0); |
| 43 | } |
| 44 | |
| 45 | freac::Progress::~Progress() |
| 46 | { |
| 47 | /* Remove progress indicator from taskbar/dock. |
| 48 | */ |
| 49 | if (window != NIL && progressIndicators.GetLast() == this) window->SetProgressIndicator(Window::ProgressIndicatorNone); |
| 50 | |
| 51 | /* Remove ourselves from progress indicator array. |
| 52 | */ |
| 53 | progressIndicators.LockForWrite(); |
| 54 | |
| 55 | foreach (Progress *progress, progressIndicators) |
| 56 | { |
| 57 | if (progress != this) continue; |
| 58 | |
| 59 | progressIndicators.RemoveNth(foreachindex); |
| 60 | |
| 61 | break; |
| 62 | } |
| 63 | |
| 64 | progressIndicators.Unlock(); |
| 65 | } |
| 66 | |
| 67 | Void freac::Progress::ComputeTotalSamples(const Array<Track> &tracks) |
| 68 | { |
| 69 | if (consoleMode) return; |
| 70 | |
| 71 | Registry &boca = Registry::Get(); |
| 72 | |
| 73 | totalSamplesLeft = 0; |
| 74 | totalSamplesMultiplier = 1; |
| 75 | totalProgressDone = 0; |
| 76 | |
| 77 | foreach (const Track &track, tracks) |
| 78 | { |
| 79 | if (track.length >= 0) totalSamplesLeft += track.length; |
| 80 | else if (track.approxLength >= 0) totalSamplesLeft += track.approxLength; |
| 81 | else totalSamplesLeft += (240 * track.GetFormat().rate); |
| 82 | } |
| 83 | |
| 84 | /* Find out if we are encoding lossless. |
| 85 | */ |
| 86 | String encoderID = configuration->GetStringValue(Config::CategorySettingsID, Config::SettingsEncoderID, Config::SettingsEncoderDefault); |
| 87 | EncoderComponent *encoder = (EncoderComponent *) boca.CreateComponentByID(encoderID); |
| 88 | Bool encodeLossless = False; |
| 89 | |
| 90 | if (encoder != NIL) |
| 91 | { |
| 92 | encoder->SetConfiguration(configuration); |
| 93 | |
| 94 | encodeLossless = encoder->IsLossless(); |
| 95 | |
| 96 | boca.DeleteComponent(encoder); |
| 97 | } |
| 98 | |
| 99 | /* Check if we are encoding on-the-fly. |
| 100 | */ |
| 101 | Bool encodeOnTheFly = configuration->GetIntValue(Config::CategorySettingsID, Config::SettingsEncodeOnTheFlyID, Config::SettingsEncodeOnTheFlyDefault); |
| 102 | Bool encodeToSingleFile = configuration->GetIntValue(Config::CategorySettingsID, Config::SettingsEncodeToSingleFileID, Config::SettingsEncodeToSingleFileDefault); |
| 103 | |
| 104 | String selectedEncoderID = configuration->GetStringValue(Config::CategorySettingsID, Config::SettingsEncoderID, Config::SettingsEncoderDefault); |
| 105 | |
| 106 | if (!encodeOnTheFly && !encodeToSingleFile && selectedEncoderID != "wave-enc" && |
| 107 | selectedEncoderID != "sndfile-enc") totalSamplesMultiplier += 1; |
| 108 | |
| 109 | /* Check if we are performing verification. |
| 110 | */ |
| 111 | Bool verifyOutput = configuration->GetIntValue(Config::CategoryVerificationID, Config::VerificationVerifyOutputID, Config::VerificationVerifyOutputDefault); |
| 112 | |
| 113 | if (encodeLossless && verifyOutput && selectedEncoderID != "meh-enc") totalSamplesMultiplier += 1; |
| 114 | |
| 115 | totalSamplesLeft *= totalSamplesMultiplier; |
| 116 | } |
| 117 | |
| 118 | Void freac::Progress::FixTotalSamples(const Track &track, const Track &nTrack) |
| 119 | { |
| 120 | if (consoleMode) return; |
| 121 | |
| 122 | Threads::Lock lock(mutex); |
| 123 | |
| 124 | /* Update internal track list entry. |
| 125 | */ |
| 126 | Track &trackListTrack = trackList.GetReference(track.GetTrackID()); |
| 127 | |
| 128 | trackListTrack.length = nTrack.length; |
| 129 | |
| 130 | /* Fix total number of samples. |
| 131 | */ |
| 132 | if (track.length >= 0) totalSamplesLeft -= (totalSamplesMultiplier - 1) * track.length; |
| 133 | else if (track.approxLength >= 0) totalSamplesLeft -= (totalSamplesMultiplier - 1) * track.approxLength; |
| 134 | else totalSamplesLeft -= (totalSamplesMultiplier - 1) * (240 * track.GetFormat().rate); |
| 135 | |
| 136 | totalSamplesLeft += (totalSamplesMultiplier - 1) * nTrack.length; |
| 137 | } |
| 138 | |
| 139 | Int64 freac::Progress::GetTotalSamples() const |
| 140 | { |
| 141 | return totalSamplesLeft / totalSamplesMultiplier; |
| 142 | } |
| 143 | |
| 144 | Void freac::Progress::Start() |
| 145 | { |
| 146 | startTicks = S::System::System::Clock(); |
| 147 | |
| 148 | /* Show progress in taskbar. |
| 149 | */ |
| 150 | if (window != NIL && progressIndicators.GetLast() == this) window->SetProgressIndicator(Window::ProgressIndicatorNormal, 0.0); |
| 151 | } |
| 152 | |
| 153 | Void freac::Progress::Pause() |
| 154 | { |
| 155 | if (pauseTicks) return; |
| 156 | |
| 157 | pauseTicks = S::System::System::Clock(); |
| 158 | |
| 159 | /* Set taskbar progress state to paused. |
| 160 | */ |
| 161 | if (window != NIL && progressIndicators.GetLast() == this) window->SetProgressIndicator(Window::ProgressIndicatorPaused); |
| 162 | } |
| 163 | |
| 164 | Void freac::Progress::Resume() |
| 165 | { |
| 166 | if (!pauseTicks) return; |
| 167 | |
| 168 | for (Int i = 0; i < trackList.Length(); i++) trackStartTicks.SetNth(i, trackStartTicks.GetNth(i) + S::System::System::Clock() - pauseTicks); |
| 169 | |
| 170 | startTicks += S::System::System::Clock() - pauseTicks; |
| 171 | pauseTicks = 0; |
| 172 | |
| 173 | /* Set taskbar/dock progress state to normal. |
| 174 | */ |
| 175 | if (window != NIL && progressIndicators.GetLast() == this) window->SetProgressIndicator(Window::ProgressIndicatorNormal); |
| 176 | } |
| 177 | |
| 178 | Void freac::Progress::StartTrack(const Track &track) |
| 179 | { |
| 180 | if (consoleMode) return; |
| 181 | |
| 182 | /* Add to internal track list. |
| 183 | */ |
| 184 | Threads::Lock lock(mutex); |
| 185 | |
| 186 | trackList.Add(track, track.GetTrackID()); |
| 187 | |
| 188 | trackStartTicks.Add(S::System::System::Clock(), track.GetTrackID()); |
| 189 | trackPositions.Add(0, track.GetTrackID()); |
| 190 | |
| 191 | lastInvoked = 0; |
| 192 | } |
| 193 | |
| 194 | Void freac::Progress::UpdateTrack(const Track &track, Int64 position) |
| 195 | { |
| 196 | if (consoleMode) return; |
| 197 | |
| 198 | /* Update internal track list. |
| 199 | */ |
| 200 | mutex.Lock(); |
| 201 | |
| 202 | Int nonZeroTracks = 0; |
| 203 | |
| 204 | foreach (const Track &trackListTrack, trackList) |
| 205 | { |
| 206 | if (trackPositions.GetNth(foreachindex) == 0) trackStartTicks.SetNth(foreachindex, S::System::System::Clock()); |
| 207 | else nonZeroTracks++; |
| 208 | |
| 209 | if (trackListTrack.GetTrackID() != track.GetTrackID()) continue; |
| 210 | |
| 211 | trackPositions.SetNth(foreachindex, position); |
| 212 | |
| 213 | if (foreachindex == 0 || nonZeroTracks <= 1) break; |
| 214 | |
| 215 | mutex.Release(); |
| 216 | |
| 217 | return; |
| 218 | } |
| 219 | |
| 220 | /* Perform updates every 25ms only. |
| 221 | */ |
| 222 | UnsignedInt64 clockValue = S::System::System::Clock(); |
| 223 | |
| 224 | if (clockValue - lastInvoked < 25) |
| 225 | { |
| 226 | mutex.Release(); |
| 227 | |
| 228 | return; |
| 229 | } |
| 230 | |
| 231 | lastInvoked = clockValue; |
| 232 | |
| 233 | /* Calculate progress values. |
| 234 | */ |
| 235 | Float trackProgress = 0; |
| 236 | Float totalProgress = totalProgressDone; |
| 237 | |
| 238 | if (track.length > 0) trackProgress = Float(position) / track.length; |
| 239 | else trackProgress = Float(position) / track.fileSize; |
| 240 | |
| 241 | for (Int i = 0; i < trackList.Length(); i++) |
| 242 | { |
| 243 | if (totalSamplesLeft == 0) break; |
| 244 | |
| 245 | const Track &track = trackList.GetNth(i); |
| 246 | |
| 247 | if (track.length > 0) totalProgress += (1.0 - totalProgressDone) * trackPositions.GetNth(i) / track.length * (Float(track.length) / totalSamplesLeft); |
| 248 | else if (track.approxLength > 0) totalProgress += (1.0 - totalProgressDone) * trackPositions.GetNth(i) / track.fileSize * (Float(track.approxLength) / totalSamplesLeft); |
| 249 | else totalProgress += (1.0 - totalProgressDone) * trackPositions.GetNth(i) / track.fileSize * (Float(240 * track.GetFormat().rate) / totalSamplesLeft); |
| 250 | } |
| 251 | |
| 252 | Float trackTicks = clockValue - trackStartTicks.Get(track.GetTrackID()); |
| 253 | Float totalTicks = clockValue - startTicks; |
| 254 | |
| 255 | trackTicks = trackProgress > 0 ? Math::Round((trackTicks / trackProgress - trackTicks) / 1000.0) : 0; |
| 256 | totalTicks = totalProgress > 0 ? Math::Round((totalTicks / totalProgress - totalTicks) / 1000.0) : 0; |
| 257 | |
| 258 | mutex.Release(); |
| 259 | |
| 260 | /* Notify listeners of updated values. |
| 261 | */ |
| 262 | AutoRelease autoRelease; |
| 263 | |
| 264 | onReportProgress.Emit(Math::Min(1000.0, Math::Round(trackProgress * 1000.0)), Math::Max(0.0, Math::Round(trackTicks)), |
| 265 | Math::Min(1000.0, Math::Round(totalProgress * 1000.0)), Math::Max(0.0, Math::Round(totalTicks))); |
| 266 | |
| 267 | /* Show progress in taskbar/dock. |
| 268 | */ |
| 269 | if (window != NIL && progressIndicators.GetLast() == this) window->SetProgressIndicator(Window::ProgressIndicatorNormal, Math::Min(100.0, totalProgress * 100.0)); |
| 270 | } |
| 271 | |
| 272 | Void freac::Progress::FinishTrack(const Track &track, Bool stepsLeft) |
| 273 | { |
| 274 | if (consoleMode) return; |
| 275 | |
| 276 | /* Remove from internal track list. |
| 277 | */ |
| 278 | Threads::Lock lock(mutex); |
| 279 | |
| 280 | for (Int i = 0; i < trackList.Length(); i++) |
| 281 | { |
| 282 | if (trackList.GetNth(i).GetTrackID() != track.GetTrackID()) continue; |
| 283 | |
| 284 | if (stepsLeft) |
| 285 | { |
| 286 | trackStartTicks.SetNth(i, S::System::System::Clock()); |
| 287 | trackPositions.SetNth(i, 0); |
| 288 | } |
| 289 | else |
| 290 | { |
| 291 | trackList.RemoveNth(i); |
| 292 | trackStartTicks.RemoveNth(i); |
| 293 | trackPositions.RemoveNth(i); |
| 294 | } |
| 295 | |
| 296 | break; |
| 297 | } |
| 298 | |
| 299 | /* Update total number of samples done. |
| 300 | */ |
| 301 | if (totalSamplesLeft) |
| 302 | { |
| 303 | if (track.length >= 0) totalProgressDone += (1.0 - totalProgressDone) * track.length / totalSamplesLeft; |
| 304 | else if (track.approxLength >= 0) totalProgressDone += (1.0 - totalProgressDone) * track.approxLength / totalSamplesLeft; |
| 305 | else totalProgressDone += (1.0 - totalProgressDone) * (240 * track.GetFormat().rate) / totalSamplesLeft; |
| 306 | |
| 307 | if (track.length >= 0) totalSamplesLeft -= track.length; |
| 308 | else if (track.approxLength >= 0) totalSamplesLeft -= track.approxLength; |
| 309 | else totalSamplesLeft -= (240 * track.GetFormat().rate); |
| 310 | } |
| 311 | |
| 312 | lastInvoked = 0; |
| 313 | } |