| 1 |
/* SPDX-License-Identifier: BSD-3-Clause */ |
| 2 |
/* SPDX-FileCopyrightText: OpenMPT Project Developers and Contributors */ |
| 3 |
|
| 4 |
|
| 5 |
#include "openmpt/all/BuildSettings.hpp" |
| 6 |
#include "openmpt/all/PlatformFixes.hpp" |
| 7 |
|
| 8 |
#include "wav_write.hpp" |
| 9 |
|
| 10 |
#include "mpt/base/memory.hpp" |
| 11 |
#include "mpt/base/saturate_cast.hpp" |
| 12 |
#include "mpt/base/span.hpp" |
| 13 |
#include "mpt/base/utility.hpp" |
| 14 |
#include "mpt/io/base.hpp" |
| 15 |
#include "mpt/io/io.hpp" |
| 16 |
#include "mpt/io/io_virtual_wrapper.hpp" |
| 17 |
#include "mpt/string/types.hpp" |
| 18 |
#include "mpt/string_transcode/transcode.hpp" |
| 19 |
|
| 20 |
#include "openmpt/base/Types.hpp" |
| 21 |
#include "openmpt/soundfile_data/tags.hpp" |
| 22 |
#include "openmpt/soundfile_data/wav.hpp" |
| 23 |
|
| 24 |
#include <limits> |
| 25 |
#include <string> |
| 26 |
|
| 27 |
#include <cassert> |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
OPENMPT_NAMESPACE_BEGIN |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
WAVWriter::WAVWriter(mpt::IO::OFileBase &stream) |
| 36 |
: s(stream) |
| 37 |
{ |
| 38 |
// Skip file header for now |
| 39 |
mpt::IO::SeekRelative(s, sizeof(RIFFHeader)); |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
void WAVWriter::StartChunk(RIFFChunk::ChunkIdentifiers id) |
| 44 |
{ |
| 45 |
FinalizeChunk(); |
| 46 |
|
| 47 |
chunkHeaderPos = mpt::IO::TellWrite(s); |
| 48 |
chunkHeader.id = id; |
| 49 |
mpt::IO::SeekRelative(s, sizeof(chunkHeader)); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
void WAVWriter::FinalizeChunk() |
| 54 |
{ |
| 55 |
if(chunkHeaderPos != 0) |
| 56 |
{ |
| 57 |
const mpt::IO::Offset position = mpt::IO::TellWrite(s); |
| 58 |
|
| 59 |
const mpt::IO::Offset chunkSize = position - (chunkHeaderPos + sizeof(RIFFChunk)); |
| 60 |
chunkHeader.length = mpt::saturate_cast<uint32>(chunkSize); |
| 61 |
|
| 62 |
mpt::IO::SeekAbsolute(s, chunkHeaderPos); |
| 63 |
mpt::IO::Write(s, chunkHeader); |
| 64 |
|
| 65 |
mpt::IO::SeekAbsolute(s, position); |
| 66 |
|
| 67 |
if((chunkSize % 2u) != 0) |
| 68 |
{ |
| 69 |
// Write padding |
| 70 |
uint8 padding = 0; |
| 71 |
mpt::IO::Write(s, padding); |
| 72 |
} |
| 73 |
|
| 74 |
chunkHeaderPos = 0; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
void WAVWriter::WriteFormat(uint32 sampleRate, uint16 bitDepth, uint16 numChannels, WAVFormatChunk::SampleFormats encoding) |
| 80 |
{ |
| 81 |
StartChunk(RIFFChunk::idfmt_); |
| 82 |
WAVFormatChunk wavFormat; |
| 83 |
mpt::reset(wavFormat); |
| 84 |
|
| 85 |
bool extensible = (numChannels > 2); |
| 86 |
|
| 87 |
wavFormat.format = static_cast<uint16>(extensible ? WAVFormatChunk::fmtExtensible : encoding); |
| 88 |
wavFormat.numChannels = numChannels; |
| 89 |
wavFormat.sampleRate = sampleRate; |
| 90 |
wavFormat.blockAlign = static_cast<uint16>((bitDepth * numChannels + 7u) / 8u); |
| 91 |
wavFormat.byteRate = wavFormat.sampleRate * wavFormat.blockAlign; |
| 92 |
wavFormat.bitsPerSample = bitDepth; |
| 93 |
|
| 94 |
mpt::IO::Write(s, wavFormat); |
| 95 |
|
| 96 |
if(extensible) |
| 97 |
{ |
| 98 |
WAVFormatChunkExtension extFormat; |
| 99 |
mpt::reset(extFormat); |
| 100 |
extFormat.size = sizeof(WAVFormatChunkExtension) - sizeof(uint16); |
| 101 |
extFormat.validBitsPerSample = bitDepth; |
| 102 |
switch(numChannels) |
| 103 |
{ |
| 104 |
case 1: |
| 105 |
extFormat.channelMask = 0x0004; // FRONT_CENTER |
| 106 |
break; |
| 107 |
case 2: |
| 108 |
extFormat.channelMask = 0x0003; // FRONT_LEFT | FRONT_RIGHT |
| 109 |
break; |
| 110 |
case 3: |
| 111 |
extFormat.channelMask = 0x0103; // FRONT_LEFT | FRONT_RIGHT | BACK_CENTER |
| 112 |
break; |
| 113 |
case 4: |
| 114 |
extFormat.channelMask = 0x0033; // FRONT_LEFT | FRONT_RIGHT | BACK_LEFT | BACK_RIGHT |
| 115 |
break; |
| 116 |
default: |
| 117 |
extFormat.channelMask = 0; |
| 118 |
break; |
| 119 |
} |
| 120 |
extFormat.subFormat = mpt::UUID(static_cast<uint16>(encoding), 0x0000, 0x0010, 0x800000AA00389B71ull); |
| 121 |
mpt::IO::Write(s, extFormat); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
void WAVWriter::WriteMetatags(const FileTags &tags) |
| 127 |
{ |
| 128 |
StartChunk(RIFFChunk::idCSET); |
| 129 |
mpt::IO::Write(s, mpt::as_le(uint16(65001))); // code page (UTF-8) |
| 130 |
mpt::IO::Write(s, mpt::as_le(uint16(0))); // country code (unset) |
| 131 |
mpt::IO::Write(s, mpt::as_le(uint16(0))); // language (unset) |
| 132 |
mpt::IO::Write(s, mpt::as_le(uint16(0))); // dialect (unset) |
| 133 |
|
| 134 |
StartChunk(RIFFChunk::idLIST); |
| 135 |
const char info[] = {'I', 'N', 'F', 'O'}; |
| 136 |
mpt::IO::Write(s, info); |
| 137 |
|
| 138 |
WriteTag(RIFFChunk::idINAM, tags.title); |
| 139 |
WriteTag(RIFFChunk::idIART, tags.artist); |
| 140 |
WriteTag(RIFFChunk::idIPRD, tags.album); |
| 141 |
WriteTag(RIFFChunk::idICRD, tags.year); |
| 142 |
WriteTag(RIFFChunk::idICMT, tags.comments); |
| 143 |
WriteTag(RIFFChunk::idIGNR, tags.genre); |
| 144 |
WriteTag(RIFFChunk::idTURL, tags.url); |
| 145 |
WriteTag(RIFFChunk::idISFT, tags.encoder); |
| 146 |
WriteTag(RIFFChunk::idTRCK, tags.trackno); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
void WAVWriter::WriteTag(RIFFChunk::ChunkIdentifiers id, const mpt::ustring &utext) |
| 151 |
{ |
| 152 |
std::string text = mpt::transcode<std::string>(mpt::common_encoding::utf8, utext); |
| 153 |
text = text.substr(0, std::numeric_limits<uint32>::max() - 1u); |
| 154 |
if(!text.empty()) |
| 155 |
{ |
| 156 |
const uint32 length = mpt::saturate_cast<uint32>(text.length() + 1); |
| 157 |
|
| 158 |
RIFFChunk chunk; |
| 159 |
mpt::reset(chunk); |
| 160 |
chunk.id = static_cast<uint32>(id); |
| 161 |
chunk.length = length; |
| 162 |
mpt::IO::Write(s, chunk); |
| 163 |
mpt::IO::Write(s, mpt::byte_cast<mpt::const_byte_span>(mpt::span(text.c_str(), length))); |
| 164 |
|
| 165 |
if((length % 2u) != 0) |
| 166 |
{ |
| 167 |
uint8 padding = 0; |
| 168 |
mpt::IO::Write(s, padding); |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
mpt::IO::Offset WAVWriter::Finalize() |
| 175 |
{ |
| 176 |
FinalizeChunk(); |
| 177 |
|
| 178 |
mpt::IO::Offset totalSize = mpt::IO::TellWrite(s); |
| 179 |
|
| 180 |
RIFFHeader fileHeader; |
| 181 |
mpt::reset(fileHeader); |
| 182 |
fileHeader.magic = RIFFHeader::idRIFF; |
| 183 |
fileHeader.length = mpt::saturate_cast<uint32>(totalSize - 8); |
| 184 |
fileHeader.type = RIFFHeader::idWAVE; |
| 185 |
|
| 186 |
mpt::IO::SeekBegin(s); |
| 187 |
mpt::IO::Write(s, fileHeader); |
| 188 |
mpt::IO::SeekAbsolute(s, totalSize); |
| 189 |
finalized = true; |
| 190 |
|
| 191 |
return totalSize; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
WAVWriter::~WAVWriter() |
| 196 |
{ |
| 197 |
assert(finalized); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
OPENMPT_NAMESPACE_END |