| 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 "drive.h" |
| 14 | #include "driveoffsets.h" |
| 15 | #include "algorithm.h" |
| 16 | #include "database.h" |
| 17 | #include "config.h" |
| 18 | |
| 19 | using namespace BoCA::AS; |
| 20 | |
| 21 | using namespace smooth::GUI::Dialogs; |
| 22 | |
| 23 | BoCA::Drive::Drive(const Config *iConfig, Int driveNumber) |
| 24 | { |
| 25 | config = iConfig; |
| 26 | |
| 27 | Registry &boca = Registry::Get(); |
| 28 | DeviceInfoComponent *info = boca.CreateDeviceInfoComponent(); |
| 29 | |
| 30 | if (info != NIL) |
| 31 | { |
| 32 | device = info->GetNthDeviceInfo(driveNumber); |
| 33 | |
| 34 | boca.DeleteComponent(info); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | BoCA::Drive::~Drive() |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | Int BoCA::Drive::FindCurrentDriveNumber() const |
| 43 | { |
| 44 | Registry &boca = Registry::Get(); |
| 45 | DeviceInfoComponent *info = boca.CreateDeviceInfoComponent(); |
| 46 | |
| 47 | if (info != NIL) |
| 48 | { |
| 49 | for (Int i = 0; i < info->GetNumberOfDevices(); i++) |
| 50 | { |
| 51 | if (info->GetNthDeviceInfo(i).path != device.path) continue; |
| 52 | |
| 53 | boca.DeleteComponent(info); |
| 54 | |
| 55 | return i; |
| 56 | } |
| 57 | |
| 58 | boca.DeleteComponent(info); |
| 59 | } |
| 60 | |
| 61 | return -1; |
| 62 | } |
| 63 | |
| 64 | BoCA::ConfigurationLevel BoCA::Drive::GetConfigurationLevel(Bool ignoreManual) const |
| 65 | { |
| 66 | String driveID = device.GetID(); |
| 67 | ConfigurationLevel configurationLevel = (ConfigurationLevel) config->GetIntValue(ConfigureAccurateRip::ConfigID, String("DriveLevel-").Append(driveID), ConfigurationLevelUnconfigured); |
| 68 | |
| 69 | if (ignoreManual) return configurationLevel; |
| 70 | |
| 71 | DriveOffsets *offsets = DriveOffsets::Get(); |
| 72 | |
| 73 | Int detectedOffset = config->GetIntValue(ConfigureAccurateRip::ConfigID, String("DriveOffset-").Append(driveID), DriveOffset::Invalid); |
| 74 | Int drivedbOffset = offsets->FindDriveOffset(device).offset; |
| 75 | |
| 76 | Bool ripperOffsetUsed = config->GetIntValue(ConfigureAccurateRip::ConfigCategoryRipperID, String("UseOffsetDrive-").Append(driveID), False); |
| 77 | Int ripperOffset = config->GetIntValue(ConfigureAccurateRip::ConfigCategoryRipperID, String("ReadOffsetDrive-").Append(driveID), 0); |
| 78 | |
| 79 | if (configurationLevel == ConfigurationLevelVerified) |
| 80 | { |
| 81 | if ((detectedOffset != 0 && !ripperOffsetUsed) || (ripperOffsetUsed && ripperOffset != detectedOffset)) return ConfigurationLevelManualOverride; |
| 82 | } |
| 83 | else if (configurationLevel == ConfigurationLevelAutomatic) |
| 84 | { |
| 85 | if ((drivedbOffset != 0 && !ripperOffsetUsed) || (ripperOffsetUsed && ripperOffset != drivedbOffset )) return ConfigurationLevelManual; |
| 86 | } |
| 87 | else if (configurationLevel == ConfigurationLevelUnconfigured) |
| 88 | { |
| 89 | if (ripperOffsetUsed) return ConfigurationLevelManual; |
| 90 | } |
| 91 | |
| 92 | return configurationLevel; |
| 93 | } |
| 94 | |
| 95 | Int BoCA::Drive::GetDetectedOffset() const |
| 96 | { |
| 97 | String driveID = device.GetID(); |
| 98 | |
| 99 | return config->GetIntValue(ConfigureAccurateRip::ConfigID, String("DriveOffset-").Append(driveID), DriveOffset::Invalid); |
| 100 | } |
| 101 | |
| 102 | BoCA::ConfigurationLevel BoCA::Drive::ConfigureOffset(Bool automatic, Bool askToDetect) const |
| 103 | { |
| 104 | ConfigurationLevel configurationLevel = GetConfigurationLevel(True); |
| 105 | |
| 106 | if (configurationLevel > ConfigurationLevelUnconfigured && automatic) return configurationLevel; |
| 107 | |
| 108 | AS::Registry &boca = AS::Registry::Get(); |
| 109 | I18n *i18n = I18n::Get(); |
| 110 | |
| 111 | i18n->SetContext("Verifiers::AccurateRip::Messages"); |
| 112 | |
| 113 | /* Automatic configuration using drive database. |
| 114 | */ |
| 115 | DriveOffsets *offsets = DriveOffsets::Get(); |
| 116 | Int drivedbOffset = offsets->FindDriveOffset(device).offset; |
| 117 | |
| 118 | if (configurationLevel < ConfigurationLevelAutomatic) |
| 119 | { |
| 120 | if (drivedbOffset != DriveOffset::Invalid) configurationLevel = ConfigurationLevelAutomatic; |
| 121 | } |
| 122 | |
| 123 | /* Ask to detect drive offset if not listed in drive database. |
| 124 | */ |
| 125 | Bool detectOffset = False; |
| 126 | |
| 127 | if (configurationLevel < ConfigurationLevelAutomatic && automatic && askToDetect) |
| 128 | { |
| 129 | Config *config = Config::Get(); |
| 130 | Bool doNotAskAgain = !config->GetIntValue(ConfigureAccurateRip::ConfigID, "AskToConfigure", True); |
| 131 | |
| 132 | if (doNotAskAgain) return configurationLevel; |
| 133 | |
| 134 | MessageDlg messageBox(i18n->TranslateString("Your CD drive could not be found in the AccurateRip database.\n\nDrive name: %1\n\nWould you like to run an automatic drive offset detection now?").Replace("%1", device.GetName()), i18n->TranslateString("AccurateRip configuration"), Message::Buttons::YesNo, Message::Icon::Question, i18n->TranslateString("Do not ask again"), &doNotAskAgain); |
| 135 | |
| 136 | messageBox.ShowDialog(); |
| 137 | |
| 138 | config->SetIntValue(ConfigureAccurateRip::ConfigID, "AskToConfigure", !doNotAskAgain); |
| 139 | |
| 140 | if (messageBox.GetButtonCode() != Message::Button::Yes) return configurationLevel; |
| 141 | |
| 142 | detectOffset = True; |
| 143 | } |
| 144 | |
| 145 | /* Perform offset detection. |
| 146 | */ |
| 147 | Int detectedOffset = DriveOffset::Invalid; |
| 148 | |
| 149 | if (!automatic || detectOffset) |
| 150 | { |
| 151 | DatabaseEntry entry = GetDatabaseEntry(); |
| 152 | |
| 153 | if (!entry.IsValid()) return configurationLevel; |
| 154 | |
| 155 | /* Disable automatic handling of inserted CDs. |
| 156 | */ |
| 157 | Bool autoReadContents = ToggleAutoRead(False); |
| 158 | |
| 159 | /* Read samples for offset detection. |
| 160 | */ |
| 161 | Config *config = Config::Copy(); |
| 162 | |
| 163 | String driveID = device.GetID(); |
| 164 | Int driveNumber = FindCurrentDriveNumber(); |
| 165 | |
| 166 | config->SetIntValue(ConfigureAccurateRip::ConfigCategoryRipperID, String("UseOffsetDrive-").Append(driveID), False); |
| 167 | config->SetIntValue(ConfigureAccurateRip::ConfigCategoryRipperID, String("SpinUpTimeDrive-").Append(driveID), -1); |
| 168 | |
| 169 | config->SetIntValue(ConfigureAccurateRip::ConfigCategoryRipperID, "ReadCDText", False); |
| 170 | config->SetIntValue(ConfigureAccurateRip::ConfigCategoryRipperID, "ReadISRC", False); |
| 171 | |
| 172 | config->SetIntValue(ConfigureAccurateRip::ConfigCategoryRipperID, "JitterCorrection", False); |
| 173 | config->SetIntValue(ConfigureAccurateRip::ConfigCategoryRipperID, "CDParanoia", False); |
| 174 | |
| 175 | Array<Int> candidates; |
| 176 | |
| 177 | while (True) |
| 178 | { |
| 179 | Array<Int> offsets; |
| 180 | |
| 181 | Int trackNumber = 1; |
| 182 | String trackURI = String("device://cdda:").Append(String::FromInt(driveNumber).Append("/").Append(String::FromInt(trackNumber))); |
| 183 | Buffer<UnsignedByte> samples; |
| 184 | |
| 185 | AS::DecoderComponent *decoder = boca.CreateDecoderForStream(trackURI, config); |
| 186 | |
| 187 | if (decoder == NIL) break; |
| 188 | |
| 189 | Track track; |
| 190 | |
| 191 | decoder->GetStreamInfo(trackURI, track); |
| 192 | decoder->SetAudioTrackInfo(track); |
| 193 | |
| 194 | decoder->Activate(); |
| 195 | decoder->Seek(445 * samplesPerSector); |
| 196 | |
| 197 | Int bytes = decoder->ReadData(samples); |
| 198 | |
| 199 | if (bytes >= 11 * bytesPerSector) |
| 200 | { |
| 201 | Algorithm ar; |
| 202 | |
| 203 | for (Int i = 0; i < 10 * bytesPerSector; i += 4) |
| 204 | { |
| 205 | Buffer<UnsignedByte> sector(bytesPerSector); |
| 206 | |
| 207 | memcpy(sector, samples + i, sector.Size()); |
| 208 | |
| 209 | if (track.cdTrack == 1) track.cdTrack = 2; // To avoid skipping first 5 sectors in CRC calculation. |
| 210 | |
| 211 | ar.Init(track); |
| 212 | ar.ProcessData(sector); |
| 213 | |
| 214 | if (entry.VerifyOffsetDetection(trackNumber, ar.GetCRCv1())) offsets.Add(i / 4 - 5 * samplesPerSector); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | decoder->Deactivate(); |
| 219 | |
| 220 | boca.DeleteComponent(decoder); |
| 221 | |
| 222 | /* Check candidates against newly detected offsets. |
| 223 | */ |
| 224 | if (candidates.Length() == 0) |
| 225 | { |
| 226 | foreach (Int offset, offsets) |
| 227 | { |
| 228 | /* Accept offset if it matches the one found in the drive database. |
| 229 | */ |
| 230 | if (offset == drivedbOffset) |
| 231 | { |
| 232 | candidates.RemoveAll(); |
| 233 | candidates.Add(drivedbOffset); |
| 234 | |
| 235 | break; |
| 236 | } |
| 237 | |
| 238 | candidates.Add(offset); |
| 239 | } |
| 240 | } |
| 241 | else |
| 242 | { |
| 243 | /* Check each previous candidate and remove it if |
| 244 | * it is not among the newly detected offsets. |
| 245 | */ |
| 246 | foreachreverse (Int candidate, candidates) |
| 247 | { |
| 248 | Bool found = False; |
| 249 | |
| 250 | foreach (Int offset, offsets) |
| 251 | { |
| 252 | if (offset != candidate) continue; |
| 253 | |
| 254 | found = True; |
| 255 | break; |
| 256 | } |
| 257 | |
| 258 | if (!found) candidates.RemoveNth(foreachindex); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | if (candidates.Length() == 1) break; |
| 263 | |
| 264 | /* Open drive tray. |
| 265 | */ |
| 266 | AS::DeviceInfoComponent *deviceInfo = boca.CreateDeviceInfoComponent(); |
| 267 | |
| 268 | if (deviceInfo == NIL) break; |
| 269 | |
| 270 | deviceInfo->OpenNthDeviceTray(driveNumber); |
| 271 | |
| 272 | boca.DeleteComponent(deviceInfo); |
| 273 | |
| 274 | /* Ask for another disc. |
| 275 | */ |
| 276 | String message = i18n->TranslateString("Offset detection needs to check another disc.").Append("\n\n").Append(i18n->TranslateString("Please insert another disc into the CD drive and click OK.\n\nThe disc must be present in the AccurateRip database. The chance for this is\nhighest for popular CDs that have likely been ripped by other users before.")); |
| 277 | |
| 278 | if (QuickMessage(message, i18n->TranslateString("AccurateRip offset detection"), Message::Buttons::OkCancel, Message::Icon::Information) != Message::Button::Ok) break; |
| 279 | |
| 280 | entry = GetDatabaseEntry(); |
| 281 | driveNumber = FindCurrentDriveNumber(); |
| 282 | |
| 283 | if (!entry.IsValid()) break; |
| 284 | } |
| 285 | |
| 286 | Config::Free(config); |
| 287 | |
| 288 | /* Restore automatic handling of inserted CDs. |
| 289 | */ |
| 290 | ToggleAutoRead(autoReadContents); |
| 291 | |
| 292 | /* Check configuration result. |
| 293 | */ |
| 294 | if (candidates.Length() != 1) return configurationLevel; |
| 295 | |
| 296 | configurationLevel = ConfigurationLevelVerified; |
| 297 | detectedOffset = candidates.GetFirst(); |
| 298 | |
| 299 | QuickMessage(i18n->TranslateString("The drive offset has been successfully detected and configured to %1 samples.").Replace("%1", String::FromInt(detectedOffset)), i18n->TranslateString("AccurateRip offset detection"), Message::Buttons::Ok, Message::Icon::Information); |
| 300 | } |
| 301 | |
| 302 | /* Save configuration level. |
| 303 | */ |
| 304 | Config *config = Config::Get(); |
| 305 | String driveID = device.GetID(); |
| 306 | |
| 307 | Bool ripperOffsetUsed = config->GetIntValue(ConfigureAccurateRip::ConfigCategoryRipperID, String("UseOffsetDrive-").Append(driveID), False); |
| 308 | |
| 309 | config->SetIntValue(ConfigureAccurateRip::ConfigID, String("DriveLevel-").Append(driveID), configurationLevel); |
| 310 | |
| 311 | if (configurationLevel == ConfigurationLevelAutomatic && !ripperOffsetUsed) |
| 312 | { |
| 313 | config->SetIntValue(ConfigureAccurateRip::ConfigCategoryRipperID, String("UseOffsetDrive-").Append(driveID), True); |
| 314 | config->SetIntValue(ConfigureAccurateRip::ConfigCategoryRipperID, String("ReadOffsetDrive-").Append(driveID), drivedbOffset); |
| 315 | } |
| 316 | else if (configurationLevel == ConfigurationLevelVerified) |
| 317 | { |
| 318 | config->SetIntValue(ConfigureAccurateRip::ConfigID, String("DriveOffset-").Append(driveID), detectedOffset); |
| 319 | |
| 320 | config->SetIntValue(ConfigureAccurateRip::ConfigCategoryRipperID, String("UseOffsetDrive-").Append(driveID), True); |
| 321 | config->SetIntValue(ConfigureAccurateRip::ConfigCategoryRipperID, String("ReadOffsetDrive-").Append(driveID), detectedOffset); |
| 322 | } |
| 323 | |
| 324 | /* Notify of ripper configuration change. |
| 325 | */ |
| 326 | if (boca.ComponentExists("cdio-dec")) Settings::onChangeComponentSettings.Emit("cdio-dec"); |
| 327 | else if (boca.ComponentExists("cdparanoia-dec")) Settings::onChangeComponentSettings.Emit("cdparanoia-dec"); |
| 328 | else if (boca.ComponentExists("cdrip-dec")) Settings::onChangeComponentSettings.Emit("cdrip-dec"); |
| 329 | else if (boca.ComponentExists("akrip-dec")) Settings::onChangeComponentSettings.Emit("akrip-dec"); |
| 330 | |
| 331 | return configurationLevel; |
| 332 | } |
| 333 | |
| 334 | BoCA::DatabaseEntry BoCA::Drive::GetDatabaseEntry() const |
| 335 | { |
| 336 | AS::Registry &boca = AS::Registry::Get(); |
| 337 | AS::DeviceInfoComponent *deviceInfo = boca.CreateDeviceInfoComponent(); |
| 338 | |
| 339 | if (deviceInfo == NIL) return DatabaseEntry(); |
| 340 | |
| 341 | /* Disable automatic handling of inserted CDs. |
| 342 | */ |
| 343 | Bool autoReadContents = ToggleAutoRead(False); |
| 344 | |
| 345 | /* Get inserted CD database entry. |
| 346 | */ |
| 347 | DatabaseEntry entry; |
| 348 | I18n *i18n = I18n::Get(); |
| 349 | |
| 350 | while (True) |
| 351 | { |
| 352 | /* Wait up to 10 seconds for a disc to be inserted. |
| 353 | */ |
| 354 | Int driveNumber = -1; |
| 355 | MCDI mcdi = MCDI(Buffer<UnsignedByte>()); |
| 356 | |
| 357 | for (Int i = 0; i < 20; i++) |
| 358 | { |
| 359 | driveNumber = FindCurrentDriveNumber(); |
| 360 | |
| 361 | if (driveNumber >= 0) |
| 362 | { |
| 363 | mcdi = deviceInfo->GetNthDeviceMCDI(driveNumber); |
| 364 | |
| 365 | if (mcdi.IsValid()) break; |
| 366 | } |
| 367 | |
| 368 | S::System::System::Sleep(500); |
| 369 | } |
| 370 | |
| 371 | /* Query entry if a disc was found. |
| 372 | */ |
| 373 | if (mcdi.IsValid()) |
| 374 | { |
| 375 | /* Query AccurateRip database. |
| 376 | */ |
| 377 | String discID = Algorithm::CalculateDiscID(mcdi); |
| 378 | |
| 379 | entry = Database::Query(config, discID); |
| 380 | |
| 381 | if (entry.IsValid()) break; |
| 382 | |
| 383 | /* Open drive tray. |
| 384 | */ |
| 385 | deviceInfo->OpenNthDeviceTray(driveNumber); |
| 386 | } |
| 387 | |
| 388 | /* Ask for another disc. |
| 389 | */ |
| 390 | String message = i18n->TranslateString("The inserted disc could not be found in the AccurateRip database.").Append("\n\n").Append(i18n->TranslateString("Please insert another disc into the CD drive and click OK.\n\nThe disc must be present in the AccurateRip database. The chance for this is\nhighest for popular CDs that have likely been ripped by other users before.")); |
| 391 | |
| 392 | if (QuickMessage(message, i18n->TranslateString("AccurateRip offset detection"), Message::Buttons::OkCancel, Message::Icon::Information) != Message::Button::Ok) break; |
| 393 | } |
| 394 | |
| 395 | boca.DeleteComponent(deviceInfo); |
| 396 | |
| 397 | /* Restore automatic handling of inserted CDs. |
| 398 | */ |
| 399 | ToggleAutoRead(autoReadContents); |
| 400 | |
| 401 | return entry; |
| 402 | } |
| 403 | |
| 404 | Bool BoCA::Drive::ToggleAutoRead(Bool newValue) const |
| 405 | { |
| 406 | /* Configure automatic handling of inserted CDs. |
| 407 | */ |
| 408 | Config *config = Config::Get(); |
| 409 | |
| 410 | Bool previousValue = config->GetIntValue(ConfigureAccurateRip::ConfigCategoryRipperID, "AutoReadContents", True); |
| 411 | |
| 412 | config->SetIntValue(ConfigureAccurateRip::ConfigCategoryRipperID, "AutoReadContents", newValue); |
| 413 | |
| 414 | return previousValue; |
| 415 | } |