| 1 |
/* |
| 2 |
* Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2025 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 25 апр. 2020 г. |
| 7 |
* |
| 8 |
* lsp-runtime-lib is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU Lesser General Public License as published by |
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
| 11 |
* any later version. |
| 12 |
* |
| 13 |
* lsp-runtime-lib 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 Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License |
| 19 |
* along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <lsp-plug.in/common/types.h> |
| 23 |
|
| 24 |
#ifdef PLATFORM_WINDOWS |
| 25 |
|
| 26 |
#include <lsp-plug.in/common/endian.h> |
| 27 |
#include <private/mm/MMIOWriter.h> |
| 28 |
|
| 29 |
namespace lsp |
| 30 |
{ |
| 31 |
namespace mm |
| 32 |
{ |
| 33 |
|
| 34 |
MMIOWriter::MMIOWriter() |
| 35 |
{ |
| 36 |
hMMIO = NULL; |
| 37 |
nWritePos = 0; |
| 38 |
nDataSize = 0; |
| 39 |
nFrames = -1; |
| 40 |
bSeekable = false; |
| 41 |
|
| 42 |
::ZeroMemory(&ckRiff, sizeof(ckRiff)); |
| 43 |
::ZeroMemory(&ckData, sizeof(ckData)); |
| 44 |
::ZeroMemory(&ckFact, sizeof(ckFact)); |
| 45 |
} |
| 46 |
|
| 47 |
MMIOWriter::~MMIOWriter() |
| 48 |
{ |
| 49 |
close(); |
| 50 |
} |
| 51 |
|
| 52 |
status_t MMIOWriter::open(const LSPString *path, WAVEFORMATEX *fmt, wssize_t frames) |
| 53 |
{ |
| 54 |
MMRESULT error; |
| 55 |
status_t res; |
| 56 |
MMCKINFO ckOut; |
| 57 |
|
| 58 |
nFrames = frames; |
| 59 |
|
| 60 |
// Open MMIO file |
| 61 |
hMMIO = ::mmioOpenW(const_cast<lsp_utf16_t *>(path->get_utf16()), NULL, MMIO_ALLOCBUF | MMIO_READWRITE | MMIO_CREATE); |
| 62 |
if (hMMIO == NULL) |
| 63 |
return close(STATUS_IO_ERROR); |
| 64 |
|
| 65 |
// Create the output file RIFF chunk of form type 'WAVE' |
| 66 |
ckRiff.ckid = FOURCC_RIFF; |
| 67 |
ckRiff.fccType = mmioFOURCC('W', 'A', 'V', 'E'); |
| 68 |
ckRiff.cksize = 0; |
| 69 |
if ((error = ::mmioCreateChunk(hMMIO, &ckRiff, MMIO_CREATERIFF)) != 0) |
| 70 |
return close(STATUS_IO_ERROR); |
| 71 |
|
| 72 |
// Now create the 'fmt ' chunk. Since we know the size of this chunk, |
| 73 |
ckOut.ckid = mmioFOURCC('f', 'm', 't', ' '); |
| 74 |
ckOut.cksize = |
| 75 |
((fmt->wFormatTag == WAVE_FORMAT_PCM) || (fmt->wFormatTag == WAVE_FORMAT_IEEE_FLOAT)) ? |
| 76 |
sizeof(PCMWAVEFORMAT) : sizeof(WAVEFORMATEX) + fmt->cbSize; |
| 77 |
if ((error = ::mmioCreateChunk(hMMIO, &ckOut, 0)) != 0) |
| 78 |
return close(STATUS_IO_ERROR); |
| 79 |
|
| 80 |
// Write the format structure to the 'fmt ' chunk |
| 81 |
if ((res = write_padded(fmt, ckOut.cksize)) != STATUS_OK) |
| 82 |
return close(res); |
| 83 |
|
| 84 |
// Ascend out of the 'fmt ' chunk, back into the 'RIFF' chunk. |
| 85 |
if ((error = ::mmioAscend(hMMIO, &ckOut, 0)) != 0) |
| 86 |
return close(STATUS_IO_ERROR); |
| 87 |
|
| 88 |
// Now create the fact chunk, not required by PCM but nice to have. |
| 89 |
ckFact.ckid = mmioFOURCC('f', 'a', 'c', 't'); |
| 90 |
ckFact.cksize = 0; |
| 91 |
if ((error = ::mmioCreateChunk(hMMIO, &ckFact, 0)) != 0) |
| 92 |
return close(STATUS_IO_ERROR); |
| 93 |
|
| 94 |
DWORD factSize = 0; |
| 95 |
if ((res = write_padded(&factSize, sizeof(DWORD))) != STATUS_OK) |
| 96 |
return close(res); |
| 97 |
|
| 98 |
// Ascend out of the 'fact' chunk, back into the 'RIFF' chunk. |
| 99 |
if ((error = ::mmioAscend(hMMIO, &ckFact, 0)) != 0) |
| 100 |
return close(STATUS_IO_ERROR); |
| 101 |
|
| 102 |
// Create the 'data' chunk that holds the waveform samples. |
| 103 |
ckData.ckid = mmioFOURCC('d', 'a', 't', 'a'); |
| 104 |
ckData.cksize = 0; |
| 105 |
if ((error = ::mmioCreateChunk(hMMIO, &ckData, 0)) != 0) |
| 106 |
return close(STATUS_IO_ERROR); |
| 107 |
|
| 108 |
nWritePos = 0; |
| 109 |
nDataSize = 0; |
| 110 |
bSeekable = ((fmt->wFormatTag == WAVE_FORMAT_PCM) || (fmt->wFormatTag == WAVE_FORMAT_IEEE_FLOAT)); |
| 111 |
return STATUS_OK; |
| 112 |
} |
| 113 |
|
| 114 |
status_t MMIOWriter::finalize_riff_file() |
| 115 |
{ |
| 116 |
MMRESULT error; |
| 117 |
|
| 118 |
// Seek to the end of chunk if position is wrong |
| 119 |
if (::mmioSeek(hMMIO, ckData.dwDataOffset + nDataSize, SEEK_SET) < 0) |
| 120 |
return STATUS_IO_ERROR; |
| 121 |
|
| 122 |
// Pad the ckData chunk |
| 123 |
if (nDataSize & 1) |
| 124 |
{ |
| 125 |
// Write padding data |
| 126 |
BYTE data = 0; |
| 127 |
ssize_t written = ::mmioWrite(hMMIO, reinterpret_cast<HPSTR>(&data), sizeof(BYTE)); |
| 128 |
if (written != 1) |
| 129 |
return STATUS_IO_ERROR; |
| 130 |
nWritePos = ++nDataSize; |
| 131 |
} |
| 132 |
|
| 133 |
// Ascend the output file out of the 'data' chunk |
| 134 |
// this will cause the chunk size of the 'data' chunk to be written. |
| 135 |
if ((error = ::mmioAscend(hMMIO, &ckData, 0)) != 0) |
| 136 |
return STATUS_IO_ERROR; |
| 137 |
|
| 138 |
// Ascend the RIFF chunk |
| 139 |
if ((error = ::mmioAscend(hMMIO, &ckRiff, 0)) != 0) |
| 140 |
return STATUS_IO_ERROR; |
| 141 |
|
| 142 |
// Seek to the begin of 'fact' chunk |
| 143 |
if (::mmioSeek(hMMIO, ckFact.dwDataOffset, SEEK_SET) < 0) |
| 144 |
return STATUS_IO_ERROR; |
| 145 |
|
| 146 |
// Write actual size of the 'data' chunk in samples |
| 147 |
DWORD factSize = nFrames; |
| 148 |
factSize = CPU_TO_LE(factSize); |
| 149 |
if ((mmioWrite(hMMIO, reinterpret_cast<HPSTR>(&factSize), sizeof(DWORD))) != sizeof(DWORD)) |
| 150 |
return STATUS_IO_ERROR; |
| 151 |
|
| 152 |
return STATUS_OK; |
| 153 |
} |
| 154 |
|
| 155 |
wssize_t MMIOWriter::seek(wsize_t offset) |
| 156 |
{ |
| 157 |
if (!bSeekable) |
| 158 |
return -STATUS_NOT_SUPPORTED; |
| 159 |
|
| 160 |
if (offset > nDataSize) |
| 161 |
offset = nDataSize; |
| 162 |
|
| 163 |
if (::mmioSeek(hMMIO, ckData.dwDataOffset + offset, SEEK_SET) < 0) |
| 164 |
return -STATUS_IO_ERROR; |
| 165 |
|
| 166 |
return nWritePos = offset; |
| 167 |
} |
| 168 |
|
| 169 |
ssize_t MMIOWriter::write(const void *buf, size_t count) |
| 170 |
{ |
| 171 |
ssize_t written = ::mmioWrite(hMMIO, reinterpret_cast<HPSTR>(const_cast<void *>(buf)), count); |
| 172 |
if (written < 0) |
| 173 |
return -STATUS_IO_ERROR; |
| 174 |
nWritePos += written; |
| 175 |
if (nDataSize < nWritePos) |
| 176 |
nDataSize = nWritePos; |
| 177 |
return written; |
| 178 |
} |
| 179 |
|
| 180 |
status_t MMIOWriter::write_padded(const void *buf, size_t count) |
| 181 |
{ |
| 182 |
// Write data |
| 183 |
ssize_t written = ::mmioWrite(hMMIO, reinterpret_cast<HPSTR>(const_cast<void *>(buf)), count); |
| 184 |
if ((written < 0) || (size_t(written) != count)) |
| 185 |
return STATUS_IO_ERROR; |
| 186 |
|
| 187 |
// Need to pad? |
| 188 |
if (count & 1) |
| 189 |
{ |
| 190 |
BYTE data = 0; |
| 191 |
ssize_t written = ::mmioWrite(hMMIO, reinterpret_cast<HPSTR>(&data), sizeof(BYTE)); |
| 192 |
if (written != 1) |
| 193 |
return STATUS_IO_ERROR; |
| 194 |
} |
| 195 |
|
| 196 |
return STATUS_OK; |
| 197 |
} |
| 198 |
|
| 199 |
status_t MMIOWriter::flush() |
| 200 |
{ |
| 201 |
return (::mmioFlush(hMMIO, MMIO_EMPTYBUF) == 0) ? STATUS_OK : STATUS_IO_ERROR; |
| 202 |
} |
| 203 |
|
| 204 |
status_t MMIOWriter::close(status_t code) |
| 205 |
{ |
| 206 |
if (hMMIO != NULL) |
| 207 |
{ |
| 208 |
// If all is OK, then finalize the RIFF file |
| 209 |
if (code == STATUS_OK) |
| 210 |
code = finalize_riff_file(); |
| 211 |
|
| 212 |
::mmioClose(hMMIO, 0); |
| 213 |
hMMIO = NULL; |
| 214 |
} |
| 215 |
|
| 216 |
nWritePos = 0; |
| 217 |
nDataSize = 0; |
| 218 |
nFrames = -1; |
| 219 |
bSeekable = false; |
| 220 |
|
| 221 |
::ZeroMemory(&ckRiff, sizeof(ckRiff)); |
| 222 |
::ZeroMemory(&ckData, sizeof(ckData)); |
| 223 |
::ZeroMemory(&ckFact, sizeof(ckFact)); |
| 224 |
|
| 225 |
return code; |
| 226 |
} |
| 227 |
|
| 228 |
} /* namespace mm */ |
| 229 |
} /* namespace lsp */ |
| 230 |
|
| 231 |
#endif /* PLATFORM_WINDOWS */ |