| 1 | /* BoCA - BonkEnc Component Architecture |
| 2 | * Copyright (C) 2007-2024 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 <boca/application/external/encodercomponentfile.h> |
| 14 | #include <boca/common/utilities.h> |
| 15 | |
| 16 | #include <windows.h> |
| 17 | #include <mmreg.h> |
| 18 | |
| 19 | using namespace smooth::IO; |
| 20 | |
| 21 | BoCA::AS::EncoderComponentExternalFile::EncoderComponentExternalFile(ComponentSpecs *specs) : EncoderComponentExternal(specs) |
| 22 | { |
| 23 | out = NIL; |
| 24 | |
| 25 | nOfSamples = 0; |
| 26 | } |
| 27 | |
| 28 | BoCA::AS::EncoderComponentExternalFile::~EncoderComponentExternalFile() |
| 29 | { |
| 30 | } |
| 31 | |
| 32 | Bool BoCA::AS::EncoderComponentExternalFile::Activate() |
| 33 | { |
| 34 | if (!EncoderComponentExternal::Activate()) return False; |
| 35 | |
| 36 | /* Create temporary WAVE file. |
| 37 | */ |
| 38 | nOfSamples = 0; |
| 39 | |
| 40 | wavFileName = Utilities::GetNonUnicodeTempFileName(track.outputFile).Append(".wav"); |
| 41 | encFileName = Utilities::GetNonUnicodeTempFileName(track.outputFile).Append(".").Append(GetOutputFileExtension()); |
| 42 | |
| 43 | out = new OutStream(STREAM_FILE, wavFileName, OS_REPLACE); |
| 44 | |
| 45 | /* Write WAVE header. |
| 46 | */ |
| 47 | Int64 dataSize = track.length * format.channels * (format.bits / 8); |
| 48 | |
| 49 | if (dataSize > 0xFFFFFFFF) dataSize = 0; |
| 50 | |
| 51 | out->OutputString("RIFF"); |
| 52 | out->OutputNumber(dataSize + 36, 4); |
| 53 | out->OutputString("WAVE"); |
| 54 | out->OutputString("fmt "); |
| 55 | |
| 56 | out->OutputNumber(16, 4); |
| 57 | out->OutputNumber(format.fp ? WAVE_FORMAT_IEEE_FLOAT : WAVE_FORMAT_PCM, 2); |
| 58 | out->OutputNumber(format.channels, 2); |
| 59 | out->OutputNumber(format.rate, 4); |
| 60 | out->OutputNumber(format.rate * format.channels * (format.bits / 8), 4); |
| 61 | out->OutputNumber(format.channels * (format.bits / 8), 2); |
| 62 | |
| 63 | out->OutputNumber(format.bits, 2); |
| 64 | out->OutputString("data"); |
| 65 | out->OutputNumber(dataSize, 4); |
| 66 | |
| 67 | return True; |
| 68 | } |
| 69 | |
| 70 | Bool BoCA::AS::EncoderComponentExternalFile::Deactivate() |
| 71 | { |
| 72 | EncoderComponentExternal::Deactivate(); |
| 73 | |
| 74 | /* Finalize and close the uncompressed temporary file. |
| 75 | */ |
| 76 | Int64 dataSize = nOfSamples * (format.bits / 8); |
| 77 | |
| 78 | if (dataSize > 0xFFFFFFFF) dataSize = 0; |
| 79 | |
| 80 | out->Seek(4); |
| 81 | out->OutputNumber(dataSize + 36, 4); |
| 82 | |
| 83 | out->Seek(40); |
| 84 | out->OutputNumber(dataSize, 4); |
| 85 | |
| 86 | delete out; |
| 87 | |
| 88 | /* Start 3rd party command line encoder. |
| 89 | */ |
| 90 | const Info &info = track.GetInfo(); |
| 91 | |
| 92 | String command = String("\"").Append(specs->external_command).Append("\"").Replace("/", Directory::GetDirectoryDelimiter()); |
| 93 | String arguments = String(specs->external_arguments).Replace("%OPTIONS", specs->GetExternalArgumentsString(GetConfiguration())) |
| 94 | .Replace("%INFILE", String("\"").Append(wavFileName).Append("\"")) |
| 95 | .Replace("%OUTFILE", String("\"").Append(encFileName).Append("\"")) |
| 96 | .Replace("%ARTIST", String("\"").Append((char *) info.artist).Append("\"")) |
| 97 | .Replace("%ALBUM", String("\"").Append((char *) info.album).Append("\"")) |
| 98 | .Replace("%TITLE", String("\"").Append((char *) info.title).Append("\"")) |
| 99 | .Replace("%TRACK", String("\"").Append(String::FromInt(info.track)).Append("\"")) |
| 100 | .Replace("%YEAR", String("\"").Append(String::FromInt(info.year)).Append("\"")) |
| 101 | .Replace("%GENRE", String("\"").Append((char *) info.genre).Append("\"")); |
| 102 | |
| 103 | if (specs->debug) AllocConsole(); |
| 104 | |
| 105 | STARTUPINFOA startupInfo; |
| 106 | |
| 107 | ZeroMemory(&startupInfo, sizeof(startupInfo)); |
| 108 | |
| 109 | startupInfo.cb = sizeof(startupInfo); |
| 110 | startupInfo.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES; |
| 111 | startupInfo.wShowWindow = specs->debug ? SW_SHOW : SW_HIDE; |
| 112 | startupInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE); |
| 113 | startupInfo.hStdOutput = specs->debug ? GetStdHandle(STD_OUTPUT_HANDLE) : NIL; |
| 114 | startupInfo.hStdError = specs->debug ? GetStdHandle(STD_ERROR_HANDLE) : NIL; |
| 115 | |
| 116 | PROCESS_INFORMATION processInfo; |
| 117 | |
| 118 | ZeroMemory(&processInfo, sizeof(processInfo)); |
| 119 | |
| 120 | CreateProcessA(NIL, String(command).Append(" ").Append(arguments), NIL, NIL, True, 0, NIL, NIL, &startupInfo, &processInfo); |
| 121 | |
| 122 | /* Check process handle. |
| 123 | */ |
| 124 | if (processInfo.hProcess == NIL) |
| 125 | { |
| 126 | errorState = True; |
| 127 | errorString = String("Unable to run encoder ").Append(command).Append("."); |
| 128 | |
| 129 | return False; |
| 130 | } |
| 131 | |
| 132 | /* Wait until the encoder exits. |
| 133 | */ |
| 134 | while (WaitForSingleObject(processInfo.hProcess, 0) == WAIT_TIMEOUT) S::System::System::Sleep(10); |
| 135 | |
| 136 | if (specs->debug) |
| 137 | { |
| 138 | BoCA::Utilities::InfoMessage("Click OK to close console window."); |
| 139 | |
| 140 | FreeConsole(); |
| 141 | } |
| 142 | |
| 143 | File(wavFileName).Delete(); |
| 144 | |
| 145 | /* Check if anything went wrong. |
| 146 | */ |
| 147 | unsigned long exitCode = 0; |
| 148 | |
| 149 | GetExitCodeProcess(processInfo.hProcess, &exitCode); |
| 150 | |
| 151 | if (!specs->external_ignoreExitCode && exitCode != 0) |
| 152 | { |
| 153 | /* Remove output file. |
| 154 | */ |
| 155 | File(encFileName).Delete(); |
| 156 | |
| 157 | errorState = True; |
| 158 | errorString = String("Encoder returned exit code ").Append(String::FromInt((signed) exitCode)).Append("."); |
| 159 | |
| 160 | return False; |
| 161 | } |
| 162 | |
| 163 | /* Create tag buffers. |
| 164 | */ |
| 165 | Buffer<UnsignedByte> tagBufferPrepend; |
| 166 | Buffer<UnsignedByte> tagBufferAppend; |
| 167 | |
| 168 | RenderTags(encFileName, track, tagBufferPrepend, tagBufferAppend); |
| 169 | |
| 170 | /* Prepend tags. |
| 171 | */ |
| 172 | driver->WriteData(tagBufferPrepend, tagBufferPrepend.Size()); |
| 173 | |
| 174 | /* Stream contents of created file to output driver. |
| 175 | */ |
| 176 | InStream in(STREAM_FILE, encFileName, IS_READ); |
| 177 | Buffer<UnsignedByte> buffer(128 * 1024); |
| 178 | Int64 bytesLeft = in.Size(); |
| 179 | |
| 180 | while (bytesLeft) |
| 181 | { |
| 182 | Int bytes = Math::Min(buffer.Size(), bytesLeft); |
| 183 | |
| 184 | in.InputData(buffer, bytes); |
| 185 | |
| 186 | driver->WriteData(buffer, bytes); |
| 187 | |
| 188 | bytesLeft -= bytes; |
| 189 | } |
| 190 | |
| 191 | /* Append tags. |
| 192 | */ |
| 193 | driver->WriteData(tagBufferAppend, tagBufferAppend.Size()); |
| 194 | |
| 195 | in.Close(); |
| 196 | |
| 197 | File(encFileName).Delete(); |
| 198 | |
| 199 | /* Look for a companion file. |
| 200 | */ |
| 201 | File companionFile = GetCompanionFile(encFileName); |
| 202 | |
| 203 | if (companionFile.Exists()) companionFile.Move(GetCompanionFile(track.outputFile)); |
| 204 | |
| 205 | return True; |
| 206 | } |
| 207 | |
| 208 | Int BoCA::AS::EncoderComponentExternalFile::WriteData(const Buffer<UnsignedByte> &data) |
| 209 | { |
| 210 | Buffer<UnsignedByte> &buffer = const_cast<Buffer<UnsignedByte> &>(data); |
| 211 | |
| 212 | ProcessData(buffer); |
| 213 | |
| 214 | /* Convert to little-endian byte order. |
| 215 | */ |
| 216 | static Endianness endianness = CPU().GetEndianness(); |
| 217 | |
| 218 | if (endianness != EndianLittle) BoCA::Utilities::SwitchBufferByteOrder(buffer, format.bits / 8); |
| 219 | |
| 220 | /* Convert 8 bit samples to unsigned. |
| 221 | */ |
| 222 | if (format.bits == 8 && format.sign == True) |
| 223 | { |
| 224 | for (Int i = 0; i < buffer.Size(); i++) buffer[i] = SignedByte(buffer[i]) + 128; |
| 225 | } |
| 226 | |
| 227 | /* Hand data over to the output file. |
| 228 | */ |
| 229 | nOfSamples += (buffer.Size() / (format.bits / 8)); |
| 230 | |
| 231 | out->OutputData(buffer, buffer.Size()); |
| 232 | |
| 233 | return buffer.Size(); |
| 234 | } |