Debian Codegraph @

modules/lsp-runtime-lib/src/main/fmt/lspc/AudioWriter.cpp

raw
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: 6 нояб. 2018 г.
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/endian.h>
23 #include <lsp-plug.in/fmt/lspc/AudioWriter.h>
24 #include <stdlib.h>
25
26 #define BUFFER_FRAMES 0x400
27
28 namespace lsp
29 {
30 namespace lspc
31 {
32 void AudioWriter::encode_u8(void *vp, const float *src, size_t ns)
33 {
34 uint8_t *p = reinterpret_cast<uint8_t *>(vp);
35 while (ns--)
36 *(p++) = int8_t((*src++) * 0x7f) + 0x80;
37 }
38
39 void AudioWriter::encode_s8(void *vp, const float *src, size_t ns)
40 {
41 int8_t *p = reinterpret_cast<int8_t *>(vp);
42 while (ns--)
43 *(p++) = int8_t((*src++) * 0x7f);
44 }
45
46 void AudioWriter::encode_u16(void *vp, const float *src, size_t ns)
47 {
48 uint16_t *p = reinterpret_cast<uint16_t *>(vp);
49 while (ns--)
50 *(p++) = int16_t((*src++) * 0x7fff) + 0x8000;
51 }
52
53 void AudioWriter::encode_s16(void *vp, const float *src, size_t ns)
54 {
55 int16_t *p = reinterpret_cast<int16_t *>(vp);
56 while (ns--)
57 *(p++) = int16_t((*src++) * 0x7fff);
58 }
59
60 void AudioWriter::encode_u24le(void *vp, const float *src, size_t ns)
61 {
62 uint8_t *p = reinterpret_cast<uint8_t *>(vp);
63 while (ns--)
64 {
65 uint32_t s = int32_t(*(src++) * 0x7fffff) + 0x800000;
66 __IF_LE(p[0] = uint8_t(s); p[1] = uint8_t(s >> 8); p[2] = uint8_t(s >> 16));
67 __IF_BE(p[0] = uint8_t(s >> 16); p[1] = uint8_t(s >> 8); p[2] = uint8_t(s));
68 p += 3;
69 }
70 }
71
72 void AudioWriter::encode_u24be(void *vp, const float *src, size_t ns)
73 {
74 uint8_t *p = reinterpret_cast<uint8_t *>(vp);
75 while (ns--)
76 {
77 uint32_t s = int32_t(*(src++) * 0x7fffff) + 0x800000;
78 __IF_BE(p[0] = uint8_t(s); p[1] = uint8_t(s >> 8); p[2] = uint8_t(s >> 16));
79 __IF_LE(p[0] = uint8_t(s >> 16); p[1] = uint8_t(s >> 8); p[2] = uint8_t(s));
80 p += 3;
81 }
82 }
83
84 void AudioWriter::encode_s24le(void *vp, const float *src, size_t ns)
85 {
86 uint8_t *p = reinterpret_cast<uint8_t *>(vp);
87 while (ns--)
88 {
89 int32_t s = int32_t(*(src++) * 0x7fffff);
90 __IF_LE(p[0] = uint8_t(s); p[1] = uint8_t(s >> 8); p[2] = uint8_t(s >> 16));
91 __IF_BE(p[0] = uint8_t(s >> 16); p[1] = uint8_t(s >> 8); p[2] = uint8_t(s));
92 p += 3;
93 }
94 }
95
96 void AudioWriter::encode_s24be(void *vp, const float *src, size_t ns)
97 {
98 uint8_t *p = reinterpret_cast<uint8_t *>(vp);
99 while (ns--)
100 {
101 int32_t s = int32_t(*(src++) * 0x7fffff);
102 __IF_BE(p[0] = uint8_t(s); p[1] = uint8_t(s >> 8); p[2] = uint8_t(s >> 16));
103 __IF_LE(p[0] = uint8_t(s >> 16); p[1] = uint8_t(s >> 8); p[2] = uint8_t(s));
104 p += 3;
105 }
106 }
107
108 void AudioWriter::encode_u32(void *vp, const float *src, size_t ns)
109 {
110 uint32_t *p = reinterpret_cast<uint32_t *>(vp);
111 while (ns--)
112 {
113 int32_t v = double(*(src++)) * 0x7fffffff;
114 *(p++) = v + 0x80000000;
115 }
116 }
117
118 void AudioWriter::encode_s32(void *vp, const float *src, size_t ns)
119 {
120 int32_t *p = reinterpret_cast<int32_t *>(vp);
121 while (ns--)
122 *(p++) = double(*(src++)) * 0x7fffffff;
123 }
124
125 void AudioWriter::encode_f32(void *vp, const float *src, size_t ns)
126 {
127 float *p = reinterpret_cast<float *>(vp);
128 ::memcpy(p, src, ns * sizeof(float));
129 }
130
131 void AudioWriter::encode_f64(void *vp, const float *src, size_t ns)
132 {
133 double *p = reinterpret_cast<double *>(vp);
134 while (ns--)
135 *(p++) = *(src++);
136 }
137
138 AudioWriter::AudioWriter()
139 {
140 sParams.channels = 0;
141 sParams.sample_format = 0;
142 sParams.sample_rate = 0;
143 sParams.codec = 0;
144 sParams.frames = 0;
145
146 pFD = NULL;
147 pWD = NULL;
148 nFlags = 0;
149 nBPS = 0;
150 nFrameChannels = 0;
151 pEncode = NULL;
152 pBuffer = NULL;
153 pFBuffer = NULL;
154 }
155
156 AudioWriter::~AudioWriter()
157 {
158 free_resources();
159 }
160
161
162 status_t AudioWriter::free_resources()
163 {
164 status_t res = STATUS_OK;
165 if (pWD != NULL)
166 {
167 if (nFlags & F_CLOSE_WRITER)
168 res = update_status(res, pWD->close());
169 if (nFlags & F_DROP_WRITER)
170 delete pWD;
171 pWD = NULL;
172 }
173
174 if (pFD != NULL)
175 {
176 if (nFlags & F_CLOSE_FILE)
177 res = update_status(res, pFD->close());
178 if (nFlags & F_DROP_FILE)
179 delete pFD;
180 pFD = NULL;
181 }
182
183 if (pFBuffer != NULL)
184 {
185 delete [] pFBuffer;
186 pFBuffer = NULL;
187 }
188
189 if (pBuffer != NULL)
190 {
191 delete [] pBuffer;
192 pBuffer = NULL;
193 }
194
195 nFlags = 0;
196 nBPS = 0;
197 nFrameChannels = 0;
198 pEncode = NULL;
199
200 return res;
201 }
202
203 status_t AudioWriter::close()
204 {
205 // Check open status first
206 if (!(nFlags & F_OPENED))
207 return STATUS_CLOSED;
208 return free_resources();
209 }
210
211 status_t AudioWriter::parse_parameters(const audio_parameters_t *p)
212 {
213 if (p == NULL)
214 return STATUS_BAD_ARGUMENTS;
215 else if (p->channels > 0xff)
216 return STATUS_BAD_FORMAT;
217 else if (p->sample_rate == 0)
218 return STATUS_BAD_FORMAT;
219 else if (p->codec != lspc::CODEC_PCM)
220 return STATUS_BAD_FORMAT;
221
222 size_t sb = 0;
223 encode_func_t ef = NULL;
224 bool le = false;
225 bool int_sample = true;
226 bool arch_le = __IF_LEBE(true, false);
227
228 switch (p->sample_format)
229 {
230 case lspc::SAMPLE_FMT_U8LE:
231 case lspc::SAMPLE_FMT_U8BE:
232 ef = encode_u8;
233 sb = 1;
234 le = p->sample_format == lspc::SAMPLE_FMT_U8LE;
235 break;
236
237 case lspc::SAMPLE_FMT_S8LE:
238 case lspc::SAMPLE_FMT_S8BE:
239 ef = encode_s8;
240 sb = 1;
241 le = p->sample_format == lspc::SAMPLE_FMT_S8LE;
242 break;
243
244 case lspc::SAMPLE_FMT_U16LE:
245 case lspc::SAMPLE_FMT_U16BE:
246 ef = encode_u16;
247 sb = 2;
248 le = p->sample_format == lspc::SAMPLE_FMT_U16LE;
249 break;
250
251 case lspc::SAMPLE_FMT_S16LE:
252 case lspc::SAMPLE_FMT_S16BE:
253 ef = encode_s16;
254 sb = 2;
255 le = p->sample_format == lspc::SAMPLE_FMT_S16LE;
256 break;
257
258 case lspc::SAMPLE_FMT_U24LE:
259 ef = encode_u24le;
260 sb = 3;
261 le = true;
262 break;
263 case lspc::SAMPLE_FMT_U24BE:
264 ef = encode_u24be;
265 sb = 3;
266 le = false;
267 break;
268 case lspc::SAMPLE_FMT_S24LE:
269 ef = encode_s24le;
270 sb = 3;
271 le = true;
272 break;
273 case lspc::SAMPLE_FMT_S24BE:
274 ef = encode_s24be;
275 sb = 3;
276 le = false;
277 break;
278
279 case lspc::SAMPLE_FMT_U32LE:
280 case lspc::SAMPLE_FMT_U32BE:
281 ef = encode_u32;
282 sb = 4;
283 le = p->sample_format == lspc::SAMPLE_FMT_U32LE;
284 break;
285
286 case lspc::SAMPLE_FMT_S32LE:
287 case lspc::SAMPLE_FMT_S32BE:
288 ef = encode_s32;
289 sb = 4;
290 le = p->sample_format == lspc::SAMPLE_FMT_S32LE;
291 break;
292
293 case lspc::SAMPLE_FMT_F32LE:
294 case lspc::SAMPLE_FMT_F32BE:
295 ef = encode_f32;
296 int_sample = false;
297 sb = 4;
298 le = p->sample_format == lspc::SAMPLE_FMT_F32LE;
299 break;
300
301 case lspc::SAMPLE_FMT_F64LE:
302 case lspc::SAMPLE_FMT_F64BE:
303 ef = encode_f64;
304 int_sample = false;
305 sb = 8;
306 le = p->sample_format == lspc::SAMPLE_FMT_F64LE;
307 break;
308
309 default:
310 return STATUS_UNSUPPORTED_FORMAT;
311 }
312
313 // Estimate number of bytes to read
314 size_t fz = sb * p->channels;
315
316 // Allocate buffers
317 pFBuffer = new uint8_t[fz * BUFFER_FRAMES];
318 if (pFBuffer == NULL)
319 return STATUS_NO_MEM;
320
321 pBuffer = new float[p->channels * BUFFER_FRAMES];
322 if (pBuffer == NULL)
323 {
324 delete [] pFBuffer;
325 pFBuffer = NULL;
326 return STATUS_NO_MEM;
327 }
328
329 if (le != arch_le)
330 nFlags |= F_REV_BYTES; // Set-up byte-reversal flag
331 if (int_sample)
332 nFlags |= F_INTEGER_SAMPLE;
333
334 sParams = *p;
335 nBPS = sb;
336 nFrameChannels = p->channels;
337 pEncode = ef;
338
339 return STATUS_OK;
340 }
341
342 status_t AudioWriter::write_header(ChunkWriter *wr)
343 {
344 chunk_audio_header_t hdr;
345
346 ::memset(&hdr, 0, sizeof(hdr));
347 hdr.common.size = sizeof(chunk_audio_header_t);
348 hdr.common.version = 1;
349 hdr.channels = sParams.channels;
350 hdr.sample_format = sParams.sample_format;
351 hdr.sample_rate = uint32_t(sParams.sample_rate);
352 hdr.codec = uint32_t(sParams.codec);
353 hdr.frames = sParams.frames;
354 hdr.offset = 0;
355
356 hdr.channels = CPU_TO_BE(hdr.channels);
357 hdr.sample_format = CPU_TO_BE(hdr.sample_format);
358 hdr.sample_rate = CPU_TO_BE(hdr.sample_rate);
359 hdr.codec = CPU_TO_BE(hdr.codec);
360 hdr.frames = CPU_TO_BE(hdr.frames);
361 hdr.offset = CPU_TO_BE(hdr.offset);
362
363 return wr->write_header(&hdr);
364 }
365
366 status_t AudioWriter::create(const char *path, const audio_parameters_t *params)
367 {
368 LSPString tmp;
369 if (!tmp.set_utf8(path))
370 return STATUS_NO_MEM;
371 return create(&tmp, params);
372 }
373
374 status_t AudioWriter::create(const LSPString *path, const audio_parameters_t *params)
375 {
376 File *fd = new File();
377 if (fd == NULL)
378 return STATUS_NO_MEM;
379
380 status_t res = fd->create(path);
381 if (res == STATUS_OK)
382 {
383 res = open(fd, params, true);
384 if (res == STATUS_OK)
385 nFlags |= F_CLOSE_FILE | F_DROP_FILE;
386 }
387
388 if (res != STATUS_OK)
389 {
390 fd->close();
391 delete fd;
392 return res;
393 }
394
395 return STATUS_OK;
396 }
397
398 status_t AudioWriter::create(const io::Path *path, const audio_parameters_t *params)
399 {
400 return create(path->as_string(), params);
401 }
402
403 status_t AudioWriter::open(File *lspc, uint32_t magic, const audio_parameters_t *params, bool auto_close)
404 {
405 if (nFlags & F_OPENED)
406 return STATUS_OPENED;
407
408 nFlags = 0;
409 status_t res = parse_parameters(params);
410 if (res != STATUS_OK)
411 return res;
412
413 ChunkWriter *wr = lspc->write_chunk(magic);
414 if (wr == NULL)
415 return STATUS_NO_MEM;
416
417 res = write_header(wr);
418 if (res != STATUS_OK)
419 {
420 free_resources();
421 wr->close();
422 delete wr;
423 return res;
424 }
425
426 pFD = lspc;
427 pWD = wr;
428 nFlags |= F_OPENED | F_CLOSE_WRITER | F_DROP_WRITER;
429 if (auto_close)
430 nFlags |= F_CLOSE_FILE;
431 return STATUS_OK;
432 }
433
434 status_t AudioWriter::open(File *lspc, const audio_parameters_t *params, bool auto_close)
435 {
436 return open(lspc, LSPC_CHUNK_AUDIO, params, auto_close);
437 }
438
439 status_t AudioWriter::open(ChunkWriter *wr, const audio_parameters_t *params, bool auto_close)
440 {
441 if (nFlags & F_OPENED)
442 return STATUS_OPENED;
443
444 nFlags = 0;
445 status_t res = parse_parameters(params);
446 if (res != STATUS_OK)
447 return res;
448
449 res = write_header(wr);
450 if (res != STATUS_OK)
451 {
452 free_resources();
453 return res;
454 }
455
456 nFlags |= F_OPENED;
457 if (auto_close)
458 nFlags |= F_CLOSE_WRITER;
459 return STATUS_OK;
460 }
461
462 status_t AudioWriter::open_raw(File *lspc, const audio_parameters_t *params, bool auto_close)
463 {
464 if (nFlags & F_OPENED)
465 return STATUS_OPENED;
466
467 nFlags = 0;
468 status_t res = parse_parameters(params);
469 if (res != STATUS_OK)
470 return res;
471
472 ChunkWriter *wr = lspc->write_chunk(LSPC_CHUNK_AUDIO);
473 if (wr == NULL)
474 return STATUS_NO_MEM;
475
476 pFD = lspc;
477 pWD = wr;
478 nFlags |= F_OPENED | F_CLOSE_WRITER | F_DROP_WRITER;
479 if (auto_close)
480 nFlags |= F_CLOSE_FILE;
481 return STATUS_OK;
482 }
483
484 status_t AudioWriter::open_raw(File *lspc, uint32_t magic, const audio_parameters_t *params, bool auto_close)
485 {
486 if (nFlags & F_OPENED)
487 return STATUS_OPENED;
488
489 nFlags = 0;
490 status_t res = parse_parameters(params);
491 if (res != STATUS_OK)
492 return res;
493
494 ChunkWriter *wr = lspc->write_chunk(magic);
495 if (wr == NULL)
496 return STATUS_NO_MEM;
497
498 pFD = lspc;
499 pWD = wr;
500 nFlags |= F_OPENED | F_CLOSE_WRITER | F_DROP_WRITER;
501 if (auto_close)
502 nFlags |= F_CLOSE_FILE;
503 return STATUS_OK;
504 }
505
506 status_t AudioWriter::open_raw(ChunkWriter *wr, const audio_parameters_t *params, bool auto_close)
507 {
508 if (nFlags & F_OPENED)
509 return STATUS_OPENED;
510
511 nFlags = 0;
512 status_t res = parse_parameters(params);
513 if (res != STATUS_OK)
514 return res;
515
516 nFlags |= F_OPENED;
517 if (auto_close)
518 nFlags |= F_CLOSE_WRITER;
519 return STATUS_OK;
520 }
521
522 status_t AudioWriter::write_samples(const float **data, size_t frames)
523 {
524 if (!(nFlags & F_OPENED))
525 return STATUS_CLOSED;
526
527 size_t nc = sParams.channels;
528 const float **vp = reinterpret_cast<const float **>(alloca(nc * sizeof(float *)));
529 for (size_t i=0; i<nc; ++i)
530 vp[i] = data[i];
531
532 for (size_t n_written = 0; n_written < frames; )
533 {
534 // Estimate number of frames to write
535 size_t to_write = frames - n_written;
536 if (to_write > BUFFER_FRAMES)
537 to_write = BUFFER_FRAMES;
538
539 // Pack frames
540 float *p = pBuffer;
541 for (size_t i=0; i<to_write; ++i)
542 for (size_t j=0; j<nc; ++j, ++p)
543 *p = (vp[j]) ? *(vp[j]++) : 0.0f;
544
545 // Write frames
546 status_t res = write_frames(pBuffer, to_write);
547 if (res != STATUS_OK)
548 return res;
549 n_written += to_write;
550 }
551
552 return STATUS_OK;
553 }
554
555 status_t AudioWriter::write_frames(const float *data, size_t frames)
556 {
557 if (!(nFlags & F_OPENED))
558 return STATUS_CLOSED;
559
560 for (size_t n_written = 0; n_written < frames; )
561 {
562 // Estimate number of frames to write
563 size_t to_write = frames - n_written;
564 if (to_write > BUFFER_FRAMES)
565 to_write = BUFFER_FRAMES;
566
567 // Copy frames to buffer
568 size_t floats = to_write * nFrameChannels;
569 pEncode(pFBuffer, data, floats);
570
571 // Reverse bytes (if required)
572 if (nFlags & F_REV_BYTES)
573 {
574 switch (nBPS)
575 {
576 case 1:
577 case 3:
578 break;
579 case 2:
580 byte_swap(reinterpret_cast<uint16_t *>(pFBuffer), floats);
581 break;
582 case 4:
583 byte_swap(reinterpret_cast<uint32_t *>(pFBuffer), floats);
584 break;
585 case 8:
586 byte_swap(reinterpret_cast<uint64_t *>(pFBuffer), floats);
587 break;
588 default:
589 return STATUS_BAD_STATE;
590 }
591 }
592
593 // Write data to LSPC
594 status_t res = pWD->write(pFBuffer, floats * nBPS);
595 if (res != STATUS_OK)
596 return res;
597
598 data += floats;
599 n_written += to_write;
600 }
601
602 return STATUS_OK;
603 }
604
605 status_t AudioWriter::get_parameters(audio_parameters_t *dst) const
606 {
607 if (!(nFlags & F_OPENED))
608 return STATUS_CLOSED;
609 else if (dst == NULL)
610 return STATUS_BAD_ARGUMENTS;
611 *dst = sParams;
612
613 return STATUS_OK;
614 }
615
616 uint32_t AudioWriter::unique_id() const
617 {
618 if (!(nFlags & F_OPENED))
619 return 0;
620 else if (pWD == NULL)
621 return 0;
622
623 return pWD->unique_id();
624 }
625
626 uint32_t AudioWriter::magic() const
627 {
628 if (!(nFlags & F_OPENED))
629 return 0;
630 else if (pWD == NULL)
631 return 0;
632
633 return pWD->magic();
634 }
635
636 } /* namespace lspc */
637 } /* namespace lsp */