| 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: 24 окт. 2022 г. |
| 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/alloc.h> |
| 23 |
#include <lsp-plug.in/fmt/lspc/util/audio.h> |
| 24 |
#include <lsp-plug.in/fmt/lspc/AudioReader.h> |
| 25 |
#include <lsp-plug.in/fmt/lspc/AudioWriter.h> |
| 26 |
#include <lsp-plug.in/fmt/lspc/IAudioFormatSelector.h> |
| 27 |
#include <lsp-plug.in/fmt/lspc/InAudioStream.h> |
| 28 |
#include <lsp-plug.in/mm/InAudioFileStream.h> |
| 29 |
#include <lsp-plug.in/mm/OutAudioFileStream.h> |
| 30 |
#include <lsp-plug.in/stdlib/stdlib.h> |
| 31 |
|
| 32 |
namespace lsp |
| 33 |
{ |
| 34 |
namespace lspc |
| 35 |
{ |
| 36 |
static IAudioFormatSelector default_selector; |
| 37 |
|
| 38 |
LSP_RUNTIME_LIB_PUBLIC |
| 39 |
status_t write_audio(chunk_id_t *chunk_id, File *file, const char *path, IAudioFormatSelector *selector, size_t buf_size) |
| 40 |
{ |
| 41 |
mm::InAudioFileStream is; |
| 42 |
status_t res = is.open(path); |
| 43 |
if (res != STATUS_OK) |
| 44 |
return res; |
| 45 |
res = write_audio(chunk_id, file, &is, selector, buf_size); |
| 46 |
status_t res2 = is.close(); |
| 47 |
return (res != STATUS_OK) ? res2 : res; |
| 48 |
} |
| 49 |
|
| 50 |
LSP_RUNTIME_LIB_PUBLIC |
| 51 |
status_t write_audio(chunk_id_t *chunk_id, File *file, const io::Path *path, IAudioFormatSelector *selector, size_t buf_size) |
| 52 |
{ |
| 53 |
mm::InAudioFileStream is; |
| 54 |
status_t res = is.open(path); |
| 55 |
if (res != STATUS_OK) |
| 56 |
return res; |
| 57 |
res = write_audio(chunk_id, file, &is, selector, buf_size); |
| 58 |
status_t res2 = is.close(); |
| 59 |
return (res != STATUS_OK) ? res2 : res; |
| 60 |
} |
| 61 |
|
| 62 |
LSP_RUNTIME_LIB_PUBLIC |
| 63 |
status_t write_audio(chunk_id_t *chunk_id, File *file, const LSPString *path, IAudioFormatSelector *selector, size_t buf_size) |
| 64 |
{ |
| 65 |
mm::InAudioFileStream is; |
| 66 |
status_t res = is.open(path); |
| 67 |
if (res != STATUS_OK) |
| 68 |
return res; |
| 69 |
res = write_audio(chunk_id, file, &is, selector, buf_size); |
| 70 |
status_t res2 = is.close(); |
| 71 |
return (res != STATUS_OK) ? res2 : res; |
| 72 |
} |
| 73 |
|
| 74 |
LSP_RUNTIME_LIB_PUBLIC |
| 75 |
status_t write_audio(chunk_id_t *chunk_id, File *file, mm::IInAudioStream *is, IAudioFormatSelector *selector, size_t buf_size) |
| 76 |
{ |
| 77 |
status_t res; |
| 78 |
if (is == NULL) |
| 79 |
return STATUS_BAD_ARGUMENTS; |
| 80 |
|
| 81 |
// Select audio format |
| 82 |
audio_format_t ofmt; |
| 83 |
mm::audio_stream_t ifmt; |
| 84 |
if ((res = is->info(&ifmt)) != STATUS_OK) |
| 85 |
return res; |
| 86 |
if (selector == NULL) |
| 87 |
selector = &default_selector; |
| 88 |
if ((res = selector->decide(&ofmt, &ifmt)) != STATUS_OK) |
| 89 |
return res; |
| 90 |
|
| 91 |
// Create audio writer |
| 92 |
audio_parameters_t params; |
| 93 |
AudioWriter wr; |
| 94 |
params.channels = ifmt.channels; |
| 95 |
params.frames = ifmt.frames; |
| 96 |
params.sample_rate = ofmt.sample_rate; |
| 97 |
params.codec = ofmt.codec; |
| 98 |
params.sample_format= ofmt.sample_format; |
| 99 |
if ((res = wr.open(file, ¶ms, false)) != STATUS_OK) |
| 100 |
return res; |
| 101 |
lsp_finally { wr.close(); }; |
| 102 |
chunk_id_t res_chunk_id = wr.unique_id(); |
| 103 |
|
| 104 |
// Allocate buffer for I/O |
| 105 |
size_t min_buf_size = ifmt.channels * sizeof(float); |
| 106 |
buf_size = lsp_max(min_buf_size, buf_size - (buf_size % min_buf_size)); |
| 107 |
float *data = static_cast<float *>(malloc(buf_size)); |
| 108 |
if (data == NULL) |
| 109 |
return STATUS_NO_MEM; |
| 110 |
lsp_finally { free(data); }; |
| 111 |
ssize_t max_frames = buf_size / (ifmt.channels * sizeof(float)); |
| 112 |
|
| 113 |
// Perform data copy |
| 114 |
for (wssize_t frame = 0; frame < ifmt.frames; ) |
| 115 |
{ |
| 116 |
size_t to_do = lsp_min(max_frames, ifmt.frames - frame); |
| 117 |
ssize_t nread = is->read(data, to_do); |
| 118 |
if (nread < 0) |
| 119 |
return status_t(-nread); |
| 120 |
if ((res = wr.write_frames(data, nread)) != STATUS_OK) |
| 121 |
return res; |
| 122 |
frame += nread; |
| 123 |
} |
| 124 |
|
| 125 |
// Close the audio chunk writer |
| 126 |
if ((res = wr.close()) != STATUS_OK) |
| 127 |
return res; |
| 128 |
|
| 129 |
// Return success result |
| 130 |
if (chunk_id != NULL) |
| 131 |
*chunk_id = res_chunk_id; |
| 132 |
return STATUS_OK; |
| 133 |
} |
| 134 |
|
| 135 |
LSP_RUNTIME_LIB_PUBLIC |
| 136 |
status_t write_audio( |
| 137 |
chunk_id_t *chunk_id, File *file, |
| 138 |
const float *frames, const audio_parameters_t *params) |
| 139 |
{ |
| 140 |
status_t res; |
| 141 |
if ((frames == NULL) || (params == NULL)) |
| 142 |
return STATUS_BAD_ARGUMENTS; |
| 143 |
|
| 144 |
// Create audio writer |
| 145 |
AudioWriter wr; |
| 146 |
if ((res = wr.open(file, params, false)) != STATUS_OK) |
| 147 |
return res; |
| 148 |
lsp_finally { wr.close(); }; |
| 149 |
chunk_id_t res_chunk_id = wr.unique_id(); |
| 150 |
|
| 151 |
// Perform data write |
| 152 |
if ((res = wr.write_frames(frames, params->frames)) != STATUS_OK) |
| 153 |
return res; |
| 154 |
|
| 155 |
// Close the audio chunk writer |
| 156 |
if ((res = wr.close()) != STATUS_OK) |
| 157 |
return res; |
| 158 |
|
| 159 |
// Return success result |
| 160 |
if (chunk_id != NULL) |
| 161 |
*chunk_id = res_chunk_id; |
| 162 |
return STATUS_OK; |
| 163 |
} |
| 164 |
|
| 165 |
status_t copy_frames(mm::IOutAudioStream *os, lspc::AudioReader *is, const audio_parameters_t *params, size_t buf_size) |
| 166 |
{ |
| 167 |
// Allocate buffer for I/O |
| 168 |
size_t min_buf_size = params->channels * sizeof(float); |
| 169 |
buf_size = lsp_max(min_buf_size, buf_size - (buf_size % min_buf_size)); |
| 170 |
float *data = static_cast<float *>(malloc(buf_size)); |
| 171 |
if (data == NULL) |
| 172 |
return STATUS_NO_MEM; |
| 173 |
lsp_finally { free(data); }; |
| 174 |
size_t max_frames = buf_size / (params->channels * sizeof(float)); |
| 175 |
|
| 176 |
// Perform data copy |
| 177 |
for (wsize_t frame = 0; frame < params->frames; ) |
| 178 |
{ |
| 179 |
size_t to_do = lsp_min(max_frames, params->frames - frame); |
| 180 |
ssize_t nread = is->read_frames(data, to_do); |
| 181 |
if (nread < 0) |
| 182 |
return status_t(-nread); |
| 183 |
ssize_t nwritten = os->write(data, nread); |
| 184 |
if (nwritten < 0) |
| 185 |
return status_t(-nwritten); |
| 186 |
else if (nwritten < nread) |
| 187 |
return STATUS_IO_ERROR; |
| 188 |
frame += nread; |
| 189 |
} |
| 190 |
|
| 191 |
// Return success result |
| 192 |
return STATUS_OK; |
| 193 |
} |
| 194 |
|
| 195 |
LSP_RUNTIME_LIB_PUBLIC |
| 196 |
status_t read_audio( |
| 197 |
chunk_id_t chunk_id, File *file, |
| 198 |
const char *path, size_t format, size_t codec, size_t buf_size) |
| 199 |
{ |
| 200 |
io::Path tmp; |
| 201 |
status_t res = tmp.set(path); |
| 202 |
if (res != STATUS_OK) |
| 203 |
return res; |
| 204 |
return read_audio(chunk_id, file, &tmp, format, codec, buf_size); |
| 205 |
} |
| 206 |
|
| 207 |
LSP_RUNTIME_LIB_PUBLIC |
| 208 |
status_t read_audio( |
| 209 |
chunk_id_t chunk_id, File *file, |
| 210 |
const LSPString *path, size_t format, size_t codec, size_t buf_size) |
| 211 |
{ |
| 212 |
io::Path tmp; |
| 213 |
status_t res = tmp.set(path); |
| 214 |
if (res != STATUS_OK) |
| 215 |
return res; |
| 216 |
return read_audio(chunk_id, file, &tmp, format, codec, buf_size); |
| 217 |
} |
| 218 |
|
| 219 |
LSP_RUNTIME_LIB_PUBLIC |
| 220 |
status_t read_audio( |
| 221 |
chunk_id_t chunk_id, File *file, |
| 222 |
const io::Path *path, size_t format, size_t codec, size_t buf_size) |
| 223 |
{ |
| 224 |
if ((file == NULL) || (path == NULL)) |
| 225 |
return STATUS_BAD_ARGUMENTS; |
| 226 |
|
| 227 |
// Read the chunk |
| 228 |
lspc::AudioReader rd; |
| 229 |
status_t res = rd.open(file, chunk_id); |
| 230 |
if (res != STATUS_OK) |
| 231 |
return res; |
| 232 |
lsp_finally { rd.close(); }; |
| 233 |
|
| 234 |
// Get audio parameters and open output audio stream |
| 235 |
audio_parameters_t aparams; |
| 236 |
mm::audio_stream_t sparams; |
| 237 |
mm::OutAudioFileStream os; |
| 238 |
if ((res = rd.get_parameters(&aparams)) != STATUS_OK) |
| 239 |
return res; |
| 240 |
|
| 241 |
sparams.channels = aparams.channels; |
| 242 |
sparams.srate = aparams.sample_rate; |
| 243 |
sparams.frames = aparams.frames; |
| 244 |
sparams.format = format; |
| 245 |
|
| 246 |
if ((res = os.open(path, &sparams, codec)) != STATUS_OK) |
| 247 |
return res; |
| 248 |
lsp_finally { os.close(); }; |
| 249 |
|
| 250 |
// Copy data and close streams |
| 251 |
if ((res = copy_frames(&os, &rd, &aparams, buf_size)) != STATUS_OK) |
| 252 |
return res; |
| 253 |
if ((res = os.close()) != STATUS_OK) |
| 254 |
return res; |
| 255 |
if ((res = rd.close()) != STATUS_OK) |
| 256 |
return res; |
| 257 |
|
| 258 |
// Return result |
| 259 |
return STATUS_OK; |
| 260 |
} |
| 261 |
|
| 262 |
LSP_RUNTIME_LIB_PUBLIC |
| 263 |
status_t read_audio( |
| 264 |
chunk_id_t chunk_id, File *file, |
| 265 |
float **frames, audio_parameters_t *params) |
| 266 |
{ |
| 267 |
if ((file == NULL) || (frames == NULL)) |
| 268 |
return STATUS_BAD_ARGUMENTS; |
| 269 |
|
| 270 |
// Read the chunk |
| 271 |
lspc::AudioReader rd; |
| 272 |
status_t res = rd.open(file, chunk_id); |
| 273 |
if (res != STATUS_OK) |
| 274 |
return res; |
| 275 |
lsp_finally { rd.close(); }; |
| 276 |
|
| 277 |
// Get audio parameters |
| 278 |
audio_parameters_t aparams; |
| 279 |
if ((res = rd.get_parameters(&aparams)) != STATUS_OK) |
| 280 |
return res; |
| 281 |
|
| 282 |
// Allocate the buffer of necessary size |
| 283 |
size_t buf_size = align_size(sizeof(float) * aparams.channels * aparams.frames, DEFAULT_ALIGN); |
| 284 |
float *data = static_cast<float *>(malloc(buf_size)); |
| 285 |
if (data == NULL) |
| 286 |
return STATUS_NO_MEM; |
| 287 |
lsp_finally { |
| 288 |
if (data != NULL) |
| 289 |
free(data); |
| 290 |
}; |
| 291 |
|
| 292 |
// Read the contents and close stream |
| 293 |
wssize_t read = rd.read_frames(data, aparams.frames); |
| 294 |
if (read < 0) |
| 295 |
return status_t(-read); |
| 296 |
else if (read < wssize_t(aparams.frames)) |
| 297 |
return STATUS_CORRUPTED; |
| 298 |
if ((res = rd.close()) != STATUS_OK) |
| 299 |
return res; |
| 300 |
|
| 301 |
// Return the result |
| 302 |
*frames = data; |
| 303 |
data = NULL; |
| 304 |
if (params != NULL) |
| 305 |
*params = aparams; |
| 306 |
return STATUS_OK; |
| 307 |
} |
| 308 |
|
| 309 |
LSP_RUNTIME_LIB_PUBLIC |
| 310 |
status_t read_audio( |
| 311 |
chunk_id_t chunk_id, File *file, |
| 312 |
mm::IOutAudioStream *os, audio_parameters_t *params, size_t buf_size) |
| 313 |
{ |
| 314 |
if ((file == NULL) || (os == NULL)) |
| 315 |
return STATUS_BAD_ARGUMENTS; |
| 316 |
|
| 317 |
// Read the chunk |
| 318 |
lspc::AudioReader rd; |
| 319 |
status_t res = rd.open(file, chunk_id); |
| 320 |
if (res != STATUS_OK) |
| 321 |
return res; |
| 322 |
lsp_finally { rd.close(); }; |
| 323 |
|
| 324 |
// Get audio parameters |
| 325 |
audio_parameters_t aparams; |
| 326 |
if ((res = rd.get_parameters(&aparams)) != STATUS_OK) |
| 327 |
return res; |
| 328 |
if (os->sample_rate() != aparams.sample_rate) |
| 329 |
return STATUS_INCOMPATIBLE; |
| 330 |
|
| 331 |
// Copy data and close streams |
| 332 |
if ((res = copy_frames(os, &rd, &aparams, buf_size)) != STATUS_OK) |
| 333 |
return res; |
| 334 |
if ((res = rd.close()) != STATUS_OK) |
| 335 |
return res; |
| 336 |
|
| 337 |
// Return result |
| 338 |
if (params != NULL) |
| 339 |
*params = aparams; |
| 340 |
return STATUS_OK; |
| 341 |
} |
| 342 |
|
| 343 |
LSP_RUNTIME_LIB_PUBLIC |
| 344 |
status_t read_audio(chunk_id_t chunk_id, File *file, mm::IInAudioStream **is) |
| 345 |
{ |
| 346 |
if ((file == NULL) || (is == NULL)) |
| 347 |
return STATUS_BAD_ARGUMENTS; |
| 348 |
|
| 349 |
// Read the chunk |
| 350 |
lspc::AudioReader *rd = new lspc::AudioReader(); |
| 351 |
if (rd == NULL) |
| 352 |
return STATUS_NO_MEM; |
| 353 |
lsp_finally { |
| 354 |
if (rd != NULL) |
| 355 |
delete rd; |
| 356 |
}; |
| 357 |
|
| 358 |
status_t res = rd->open(file, chunk_id); |
| 359 |
if (res != STATUS_OK) |
| 360 |
return res; |
| 361 |
|
| 362 |
// Get audio parameters |
| 363 |
mm::audio_stream_t sparams; |
| 364 |
audio_parameters_t aparams; |
| 365 |
if ((res = rd->get_parameters(&aparams)) != STATUS_OK) |
| 366 |
return res; |
| 367 |
|
| 368 |
sparams.srate = aparams.sample_rate; |
| 369 |
sparams.channels = aparams.channels; |
| 370 |
sparams.frames = aparams.frames; |
| 371 |
sparams.format = mm::SFMT_F32_CPU; |
| 372 |
|
| 373 |
// Create audio stream |
| 374 |
InAudioStream *ias = new InAudioStream(rd, &sparams, true); |
| 375 |
if (ias == NULL) |
| 376 |
return STATUS_NO_MEM; |
| 377 |
|
| 378 |
// Return result |
| 379 |
*is = ias; |
| 380 |
ias = NULL; |
| 381 |
rd = NULL; |
| 382 |
return STATUS_OK; |
| 383 |
} |
| 384 |
} /* namespace lspc */ |
| 385 |
} /* namespace lsp */ |
| 386 |
|
| 387 |
|
| 388 |
|