| 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 <engine/worker_singlefile.h> |
| 14 | |
| 15 | #include <engine/decoder.h> |
| 16 | #include <engine/encoder.h> |
| 17 | #include <engine/processor.h> |
| 18 | #include <engine/verifier.h> |
| 19 | |
| 20 | #include <config.h> |
| 21 | |
| 22 | using namespace BoCA; |
| 23 | using namespace BoCA::AS; |
| 24 | |
| 25 | freac::ConvertWorkerSingleFile::ConvertWorkerSingleFile(const BoCA::Config *iConfiguration, Int iConversionID, const BoCA::Format &iFormat, Processor *iProcessor, Encoder *iEncoder) : ConvertWorker(iConfiguration, iConversionID) |
| 26 | { |
| 27 | format = iFormat; |
| 28 | |
| 29 | processor = iProcessor; |
| 30 | encoder = iEncoder; |
| 31 | |
| 32 | encodedSamples = 0; |
| 33 | } |
| 34 | |
| 35 | freac::ConvertWorkerSingleFile::~ConvertWorkerSingleFile() |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | Int freac::ConvertWorkerSingleFile::Convert() |
| 40 | { |
| 41 | Registry &boca = Registry::Get(); |
| 42 | |
| 43 | /* Get config values. |
| 44 | */ |
| 45 | Int singleFileMode = configuration->GetIntValue(Config::CategoryProcessingID, Config::ProcessingSingleFileModeID, Config::ProcessingSingleFileModeDefault); |
| 46 | |
| 47 | Bool verifyInput = configuration->GetIntValue(Config::CategoryVerificationID, Config::VerificationVerifyInputID, Config::VerificationVerifyInputDefault); |
| 48 | |
| 49 | /* Set conversion step. |
| 50 | */ |
| 51 | if (conversionStep == ConversionStepNone) conversionStep = ConversionStepOnTheFly; |
| 52 | |
| 53 | /* Check format of output file in verification step. |
| 54 | */ |
| 55 | if (conversionStep == ConversionStepVerify) |
| 56 | { |
| 57 | DecoderComponent *decoder = boca.CreateDecoderForStream(trackToConvert.fileName, configuration); |
| 58 | |
| 59 | if (decoder != NIL) |
| 60 | { |
| 61 | Track outTrack; |
| 62 | |
| 63 | decoder->GetStreamInfo(trackToConvert.fileName, outTrack); |
| 64 | |
| 65 | boca.DeleteComponent(decoder); |
| 66 | |
| 67 | Format format = trackToConvert.GetFormat(); |
| 68 | const Format &outFormat = outTrack.GetFormat(); |
| 69 | |
| 70 | if (format != outFormat) |
| 71 | { |
| 72 | BoCA::I18n *i18n = BoCA::I18n::Get(); |
| 73 | BoCA::Protocol *log = BoCA::Protocol::Get(logName); |
| 74 | |
| 75 | onReportWarning.Emit(i18n->TranslateString(String("Skipped verification due to format mismatch: %1\n\n") |
| 76 | .Append("Original format: %2 Hz, %3 bit, %4 channels\n") |
| 77 | .Append("Output format: %5 Hz, %6 bit, %7 channels"), "Messages").Replace("%1", File(trackToConvert.fileName).GetFileName()).Replace("%2", String::FromInt(format.rate)).Replace("%3", String::FromInt(format.bits)).Replace("%4", String::FromInt(format.channels)) |
| 78 | .Replace("%5", String::FromInt(outFormat.rate)).Replace("%6", String::FromInt(outFormat.bits)).Replace("%7", String::FromInt(outFormat.channels))); |
| 79 | |
| 80 | log->Lock(); |
| 81 | log->Write(String(" Skipping verification due to format mismatch: ").Append(trackToConvert.fileName), MessageTypeWarning); |
| 82 | log->Write(NIL); |
| 83 | log->Release(); |
| 84 | |
| 85 | trackPosition = trackToConvert.length; |
| 86 | |
| 87 | onFinishTrack.Emit(trackToConvert, False); |
| 88 | |
| 89 | return Success(); |
| 90 | } |
| 91 | |
| 92 | /* Update track format with decoder format. |
| 93 | */ |
| 94 | trackToConvert.SetFormat(outFormat); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /* Create decoder. |
| 99 | */ |
| 100 | Decoder *decoder = new Decoder(configuration); |
| 101 | |
| 102 | if (!decoder->Create(trackToConvert.fileName, trackToConvert)) |
| 103 | { |
| 104 | onReportError.Emit(decoder->GetErrorString()); |
| 105 | |
| 106 | delete decoder; |
| 107 | |
| 108 | return Error(); |
| 109 | } |
| 110 | |
| 111 | decoderName = decoder->GetDecoderName(); |
| 112 | encoderName = encoder->GetEncoderName(); |
| 113 | |
| 114 | /* Create verifier. |
| 115 | */ |
| 116 | Verifier *verifier = new Verifier(configuration); |
| 117 | Bool verify = False; |
| 118 | |
| 119 | if ((conversionStep == ConversionStepOnTheFly || |
| 120 | conversionStep == ConversionStepDecode) && verifyInput) verify = verifier->Create(trackToConvert); |
| 121 | |
| 122 | /* Create format converter. |
| 123 | */ |
| 124 | FormatConverter *converter = new FormatConverter(trackToConvert.GetFormat(), format); |
| 125 | |
| 126 | /* Create processor. |
| 127 | */ |
| 128 | if (singleFileMode == 0) |
| 129 | { |
| 130 | Track trackToProcess = trackToConvert; |
| 131 | |
| 132 | trackToProcess.SetFormat(format); |
| 133 | |
| 134 | processor = new Processor(configuration); |
| 135 | |
| 136 | if ((conversionStep == ConversionStepOnTheFly || |
| 137 | conversionStep == ConversionStepDecode) && !processor->Create(trackToProcess)) |
| 138 | { |
| 139 | onReportError.Emit(processor->GetErrorString()); |
| 140 | |
| 141 | delete decoder; |
| 142 | delete verifier; |
| 143 | delete converter; |
| 144 | delete processor; |
| 145 | |
| 146 | return Error(); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /* Enable CRC calculation when ripping. |
| 151 | */ |
| 152 | if (trackToConvert.fileName.StartsWith("device://") && (conversionStep == ConversionStepOnTheFly || |
| 153 | conversionStep == ConversionStepDecode)) decoder->SetCalculateCRC(True); |
| 154 | |
| 155 | /* Enable MD5 if we are to verify the output. |
| 156 | */ |
| 157 | if (conversionStep == ConversionStepVerify) decoder->SetCalculateMD5(True); |
| 158 | |
| 159 | /* Output log messages. |
| 160 | */ |
| 161 | LogConversionStart(decoder, trackToConvert.fileName); |
| 162 | |
| 163 | /* Run main conversion loop. |
| 164 | */ |
| 165 | Int64 trackLength = Loop(decoder, verifier, converter, processor, encoder); |
| 166 | |
| 167 | /* Verify input. |
| 168 | */ |
| 169 | if (!Threads::Access::Value(cancel) && verify) VerifyInput(trackToConvert.fileName, verifier); |
| 170 | |
| 171 | /* Get CRC checksums when ripping. |
| 172 | */ |
| 173 | UnsignedInt32 rippingCRC = 0; |
| 174 | |
| 175 | if (trackToConvert.fileName.StartsWith("device://") && (conversionStep == ConversionStepOnTheFly || |
| 176 | conversionStep == ConversionStepDecode)) rippingCRC = decoder->GetCRCChecksum(); |
| 177 | |
| 178 | /* Get MD5 checksum if we are to verify the output. |
| 179 | */ |
| 180 | String verifyChecksum; |
| 181 | |
| 182 | if (conversionStep == ConversionStepVerify) verifyChecksum = decoder->GetMD5Checksum(); |
| 183 | |
| 184 | /* Get output format info. |
| 185 | */ |
| 186 | Format format = trackToConvert.GetFormat(); |
| 187 | |
| 188 | if (processor != NIL && processor->GetFormatInfo() != Format()) format = processor->GetFormatInfo(); |
| 189 | |
| 190 | /* Free decoder and verifier. |
| 191 | */ |
| 192 | decoder->Destroy(); |
| 193 | verifier->Destroy(); |
| 194 | |
| 195 | if (decoder->GetErrorState()) onReportError.Emit(decoder->GetErrorString()); |
| 196 | |
| 197 | delete decoder; |
| 198 | delete verifier; |
| 199 | delete converter; |
| 200 | |
| 201 | if (singleFileMode == 0) delete processor; |
| 202 | |
| 203 | /* Signal next chapter. |
| 204 | */ |
| 205 | if (!Threads::Access::Value(cancel)) |
| 206 | { |
| 207 | encoder->SignalChapterChange(); |
| 208 | encodedSamples += trackLength; |
| 209 | } |
| 210 | |
| 211 | /* Output log messages. |
| 212 | */ |
| 213 | LogConversionEnd(trackToConvert.fileName, trackLength, rippingCRC, encodeChecksum, verifyChecksum); |
| 214 | |
| 215 | /* Report finished conversion. |
| 216 | */ |
| 217 | onFinishTrack.Emit(trackToConvert, False); |
| 218 | |
| 219 | /* Update track length and offset. |
| 220 | */ |
| 221 | Track track = trackToConvert; |
| 222 | |
| 223 | trackToConvert.SetFormat(format); |
| 224 | |
| 225 | trackToConvert.sampleOffset = encodedSamples - trackLength; |
| 226 | trackToConvert.length = trackLength; |
| 227 | |
| 228 | /* Fix total samples value in case we had |
| 229 | * a wrong or uncertain track length before. |
| 230 | */ |
| 231 | onFixTotalSamples.Emit(track, trackToConvert); |
| 232 | |
| 233 | return Success(); |
| 234 | } |