| 1 | /*************************************************************************** |
| 2 | * fheroes2: https://github.com/ihhub/fheroes2 * |
| 3 | * Copyright (C) 2022 - 2024 * |
| 4 | * * |
| 5 | * Free Heroes2 Engine: http://sourceforge.net/projects/fheroes2 * |
| 6 | * Copyright (C) 2009 by Andrey Afletdinov <[email protected]> * |
| 7 | * * |
| 8 | * This program is free software; you can redistribute it and/or modify * |
| 9 | * it under the terms of the GNU General Public License as published by * |
| 10 | * the Free Software Foundation; either version 2 of the License, or * |
| 11 | * (at your option) any later version. * |
| 12 | * * |
| 13 | * This program is distributed in the hope that it will be useful, * |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 16 | * GNU General Public License for more details. * |
| 17 | * * |
| 18 | * You should have received a copy of the GNU General Public License * |
| 19 | * along with this program; if not, write to the * |
| 20 | * Free Software Foundation, Inc., * |
| 21 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 22 | ***************************************************************************/ |
| 23 | |
| 24 | #include <algorithm> |
| 25 | #include <cstdint> |
| 26 | #include <cstdlib> |
| 27 | #include <filesystem> |
| 28 | #include <fstream> |
| 29 | #include <iostream> |
| 30 | #include <limits> |
| 31 | #include <memory> |
| 32 | #include <optional> |
| 33 | #include <string> |
| 34 | #include <system_error> |
| 35 | #include <type_traits> |
| 36 | #include <vector> |
| 37 | |
| 38 | #include "serialize.h" |
| 39 | #include "system.h" |
| 40 | #include "tools.h" |
| 41 | |
| 42 | namespace |
| 43 | { |
| 44 | constexpr size_t wavHeaderLen = 44; |
| 45 | } |
| 46 | |
| 47 | int main( int argc, char ** argv ) |
| 48 | { |
| 49 | if ( argc < 3 ) { |
| 50 | const std::string toolName = System::GetFileName( argv[0] ); |
| 51 | |
| 52 | std::cerr << toolName << " converts the specified 82M file(s) to WAV format." << std::endl |
| 53 | << "Syntax: " << toolName << " dst_dir input_file.82m ..." << std::endl; |
| 54 | return EXIT_FAILURE; |
| 55 | } |
| 56 | |
| 57 | const char * dstDir = argv[1]; |
| 58 | |
| 59 | std::vector<std::string> inputFileNames; |
| 60 | for ( int i = 2; i < argc; ++i ) { |
| 61 | if ( System::isShellLevelGlobbingSupported() ) { |
| 62 | inputFileNames.emplace_back( argv[i] ); |
| 63 | } |
| 64 | else { |
| 65 | System::globFiles( argv[i], inputFileNames ); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | std::error_code ec; |
| 70 | |
| 71 | // Using the non-throwing overloads |
| 72 | if ( !std::filesystem::exists( dstDir, ec ) && !std::filesystem::create_directories( dstDir, ec ) ) { |
| 73 | std::cerr << "Cannot create directory " << dstDir << std::endl; |
| 74 | return EXIT_FAILURE; |
| 75 | } |
| 76 | |
| 77 | uint32_t tracksConverted = 0; |
| 78 | |
| 79 | for ( const std::string & inputFileName : inputFileNames ) { |
| 80 | std::cout << "Processing " << inputFileName << "..." << std::endl; |
| 81 | |
| 82 | std::ifstream inputStream( inputFileName, std::ios_base::binary | std::ios_base::ate ); |
| 83 | if ( !inputStream ) { |
| 84 | std::cerr << "Cannot open file " << inputFileName << std::endl; |
| 85 | // A non-existent or inaccessible file is not considered a fatal error |
| 86 | continue; |
| 87 | } |
| 88 | |
| 89 | const auto size = fheroes2::checkedCast<size_t>( static_cast<std::streamoff>( inputStream.tellg() ) ); |
| 90 | if ( !size ) { |
| 91 | std::cerr << "File " << inputFileName << " is too large" << std::endl; |
| 92 | return EXIT_FAILURE; |
| 93 | } |
| 94 | |
| 95 | if ( size == 0U ) { |
| 96 | std::cerr << "File " << inputFileName << " is empty" << std::endl; |
| 97 | return EXIT_FAILURE; |
| 98 | } |
| 99 | |
| 100 | if ( size.value() > std::numeric_limits<uint32_t>::max() - wavHeaderLen ) { |
| 101 | std::cerr << inputFileName << ": resulting WAV is too large" << std::endl; |
| 102 | return EXIT_FAILURE; |
| 103 | } |
| 104 | |
| 105 | const auto buf = std::make_unique<char[]>( size.value() ); |
| 106 | |
| 107 | inputStream.seekg( 0, std::ios_base::beg ); |
| 108 | |
| 109 | { |
| 110 | const auto streamSize = fheroes2::checkedCast<std::streamsize>( size.value() ); |
| 111 | if ( !streamSize ) { |
| 112 | std::cerr << "File " << inputFileName << " is too large" << std::endl; |
| 113 | return EXIT_FAILURE; |
| 114 | } |
| 115 | |
| 116 | inputStream.read( buf.get(), streamSize.value() ); |
| 117 | } |
| 118 | |
| 119 | if ( !inputStream ) { |
| 120 | std::cerr << "Error reading from file " << inputFileName << std::endl; |
| 121 | return EXIT_FAILURE; |
| 122 | } |
| 123 | |
| 124 | const std::filesystem::path outputFilePath = std::filesystem::path( dstDir ) / std::filesystem::path( inputFileName ).filename().replace_extension( "wav" ); |
| 125 | |
| 126 | std::ofstream outputStream( outputFilePath, std::ios_base::binary | std::ios_base::trunc ); |
| 127 | if ( !outputStream ) { |
| 128 | std::cerr << "Cannot open file " << outputFilePath << std::endl; |
| 129 | return EXIT_FAILURE; |
| 130 | } |
| 131 | |
| 132 | static_assert( std::is_same_v<uint8_t, unsigned char> ); |
| 133 | |
| 134 | RWStreamBuf wavHeader( wavHeaderLen ); |
| 135 | wavHeader.putLE32( 0x46464952 ); // RIFF marker ("RIFF") |
| 136 | wavHeader.putLE32( static_cast<uint32_t>( size.value() ) + ( wavHeaderLen - 8 ) ); // Total size minus the size of this and previous fields |
| 137 | wavHeader.putLE32( 0x45564157 ); // File type header ("WAVE") |
| 138 | wavHeader.putLE32( 0x20746D66 ); // Format sub-chunk marker ("fmt ") |
| 139 | wavHeader.putLE32( 0x10 ); // Size of the format sub-chunk |
| 140 | wavHeader.putLE16( 0x01 ); // Audio format (1 for PCM) |
| 141 | wavHeader.putLE16( 0x01 ); // Number of channels |
| 142 | wavHeader.putLE32( 22050 ); // Sample rate |
| 143 | wavHeader.putLE32( 22050 ); // Byte rate (SampleRate * BitsPerSample * NumberOfChannels) / 8 |
| 144 | wavHeader.putLE16( 0x01 ); // Block align (BitsPerSample * NumberOfChannels) / 8 |
| 145 | wavHeader.putLE16( 0x08 ); // Bits per sample |
| 146 | wavHeader.putLE32( 0x61746164 ); // Data sub-chunk marker ("data") |
| 147 | wavHeader.putLE32( static_cast<uint32_t>( size.value() ) ); // Size of the data sub-chunk |
| 148 | |
| 149 | { |
| 150 | const auto streamSize = fheroes2::checkedCast<std::streamsize>( wavHeader.size() ); |
| 151 | if ( !streamSize ) { |
| 152 | std::cerr << inputFileName << ": resulting WAV is too large" << std::endl; |
| 153 | return EXIT_FAILURE; |
| 154 | } |
| 155 | |
| 156 | outputStream.write( reinterpret_cast<const char *>( wavHeader.data() ), streamSize.value() ); |
| 157 | } |
| 158 | |
| 159 | { |
| 160 | const auto streamSize = fheroes2::checkedCast<std::streamsize>( size.value() ); |
| 161 | if ( !streamSize ) { |
| 162 | std::cerr << inputFileName << ": resulting WAV is too large" << std::endl; |
| 163 | return EXIT_FAILURE; |
| 164 | } |
| 165 | |
| 166 | outputStream.write( buf.get(), streamSize.value() ); |
| 167 | } |
| 168 | |
| 169 | if ( !outputStream ) { |
| 170 | std::cerr << "Error writing to file " << outputFilePath << std::endl; |
| 171 | return EXIT_FAILURE; |
| 172 | } |
| 173 | |
| 174 | ++tracksConverted; |
| 175 | } |
| 176 | |
| 177 | std::cout << "Total converted tracks: " << tracksConverted << std::endl; |
| 178 | |
| 179 | return EXIT_SUCCESS; |
| 180 | } |