| 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: 8 нояб. 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/fmt/lspc/AudioWriter.h> |
| 23 |
#include <lsp-plug.in/fmt/lspc/AudioReader.h> |
| 24 |
#include <lsp-plug.in/fmt/lspc/lspc.h> |
| 25 |
#include <lsp-plug.in/lltl/parray.h> |
| 26 |
#include <lsp-plug.in/runtime/LSPString.h> |
| 27 |
#include <lsp-plug.in/stdlib/stdlib.h> |
| 28 |
#include <lsp-plug.in/test-fw/utest.h> |
| 29 |
#include <lsp-plug.in/test-fw/FloatBuffer.h> |
| 30 |
|
| 31 |
#define TOTAL_FRAMES 0xfffff |
| 32 |
#define CHANNELS 5 |
| 33 |
#define INVALID_VALUE 12.34f |
| 34 |
#define BLK_SIZE 0x10000 |
| 35 |
|
| 36 |
using namespace lsp; |
| 37 |
|
| 38 |
static const float cvalues[CHANNELS] = { -1.0f, -0.5f, 0.0f, 0.5f, 1.0f }; |
| 39 |
|
| 40 |
static const size_t formats[] = |
| 41 |
{ |
| 42 |
lspc::SAMPLE_FMT_U8LE, |
| 43 |
lspc::SAMPLE_FMT_U8BE, |
| 44 |
lspc::SAMPLE_FMT_S8LE, |
| 45 |
lspc::SAMPLE_FMT_S8BE, |
| 46 |
lspc::SAMPLE_FMT_U16LE, |
| 47 |
lspc::SAMPLE_FMT_U16BE, |
| 48 |
lspc::SAMPLE_FMT_S16LE, |
| 49 |
lspc::SAMPLE_FMT_S16BE, |
| 50 |
lspc::SAMPLE_FMT_U24LE, |
| 51 |
lspc::SAMPLE_FMT_U24BE, |
| 52 |
lspc::SAMPLE_FMT_S24LE, |
| 53 |
lspc::SAMPLE_FMT_S24BE, |
| 54 |
lspc::SAMPLE_FMT_U32LE, |
| 55 |
lspc::SAMPLE_FMT_U32BE, |
| 56 |
lspc::SAMPLE_FMT_S32LE, |
| 57 |
lspc::SAMPLE_FMT_S32BE, |
| 58 |
lspc::SAMPLE_FMT_F32LE, |
| 59 |
lspc::SAMPLE_FMT_F32BE, |
| 60 |
lspc::SAMPLE_FMT_F64LE, |
| 61 |
lspc::SAMPLE_FMT_F64BE |
| 62 |
}; |
| 63 |
|
| 64 |
UTEST_BEGIN("runtime.fmt.lspc", audio) |
| 65 |
|
| 66 |
UTEST_TIMELIMIT(300) |
| 67 |
|
| 68 |
void create_lspc_file(lltl::parray<FloatBuffer> &v, size_t fmt) |
| 69 |
{ |
| 70 |
LSPString file_name; |
| 71 |
lspc::File fd; |
| 72 |
lspc::AudioWriter aw; |
| 73 |
lspc::audio_parameters_t p; |
| 74 |
|
| 75 |
// Create LSPC file |
| 76 |
io::Path path; |
| 77 |
UTEST_ASSERT(path.fmt("%s/utest-%s.lspc", tempdir(), full_name())); |
| 78 |
status_t res = fd.create(&path); |
| 79 |
UTEST_ASSERT(res == STATUS_OK); |
| 80 |
|
| 81 |
// Write audio chunk |
| 82 |
p.channels = v.size(); |
| 83 |
p.sample_format = fmt; |
| 84 |
p.sample_rate = 48000; |
| 85 |
p.codec = lspc::CODEC_PCM; |
| 86 |
p.frames = TOTAL_FRAMES; |
| 87 |
|
| 88 |
res = aw.open(&fd, &p); |
| 89 |
UTEST_ASSERT(res == STATUS_OK); |
| 90 |
|
| 91 |
// Initialize channel pointers |
| 92 |
const float **vp = reinterpret_cast<const float **>(alloca(sizeof(float *) * v.size())); |
| 93 |
for (size_t i=0, n=v.size(); i<n; ++i) |
| 94 |
{ |
| 95 |
FloatBuffer *fb = v.get(i); |
| 96 |
UTEST_ASSERT(fb != NULL); |
| 97 |
vp[i] = fb->data(); |
| 98 |
} |
| 99 |
|
| 100 |
// Write channel data |
| 101 |
size_t written = 0; |
| 102 |
while (written < TOTAL_FRAMES) |
| 103 |
{ |
| 104 |
size_t to_write = TOTAL_FRAMES - written; |
| 105 |
if (to_write > TOTAL_FRAMES) |
| 106 |
to_write = TOTAL_FRAMES; |
| 107 |
res = aw.write_samples(vp, to_write); |
| 108 |
|
| 109 |
UTEST_ASSERT(res == STATUS_OK); |
| 110 |
|
| 111 |
written += to_write; |
| 112 |
for (size_t i=0, n=v.size(); i<n; ++i) |
| 113 |
vp[i] += to_write; |
| 114 |
} |
| 115 |
|
| 116 |
// Check final status |
| 117 |
res = aw.close(); |
| 118 |
UTEST_ASSERT(res == STATUS_OK); |
| 119 |
res = fd.close(); |
| 120 |
UTEST_ASSERT(res == STATUS_OK); |
| 121 |
} |
| 122 |
|
| 123 |
void parse_lspc_file(lltl::parray<FloatBuffer> &v, size_t fmt) |
| 124 |
{ |
| 125 |
LSPString file_name; |
| 126 |
lspc::File fd; |
| 127 |
lspc::AudioReader ar; |
| 128 |
lspc::audio_parameters_t p; |
| 129 |
|
| 130 |
// Create LSPC file |
| 131 |
io::Path path; |
| 132 |
UTEST_ASSERT(path.fmt("%s/utest-%s.lspc", tempdir(), full_name())); |
| 133 |
status_t res = fd.open(&path); |
| 134 |
UTEST_ASSERT(res == STATUS_OK); |
| 135 |
|
| 136 |
// Read audio chunk |
| 137 |
res = ar.open(&fd); |
| 138 |
UTEST_ASSERT(res == STATUS_OK); |
| 139 |
|
| 140 |
res = ar.get_parameters(&p); |
| 141 |
UTEST_ASSERT(res == STATUS_OK); |
| 142 |
|
| 143 |
UTEST_ASSERT(p.channels == v.size()); |
| 144 |
UTEST_ASSERT(p.sample_format == fmt); |
| 145 |
UTEST_ASSERT(p.sample_rate == 48000); |
| 146 |
UTEST_ASSERT(p.codec == lspc::CODEC_PCM); |
| 147 |
UTEST_ASSERT(p.frames == TOTAL_FRAMES); |
| 148 |
|
| 149 |
// Initialize channel pointers |
| 150 |
float **vp = reinterpret_cast<float **>(alloca(sizeof(float *) * v.size())); |
| 151 |
for (size_t i=0, n=v.size(); i<n; ++i) |
| 152 |
{ |
| 153 |
FloatBuffer *fb = v.get(i); |
| 154 |
UTEST_ASSERT(fb != NULL); |
| 155 |
|
| 156 |
float *dst = fb->data(); |
| 157 |
for (size_t j=0; j<fb->size(); ++j) |
| 158 |
dst[j] = INVALID_VALUE; |
| 159 |
UTEST_ASSERT(fb->valid()); |
| 160 |
|
| 161 |
vp[i] = fb->data(); |
| 162 |
} |
| 163 |
|
| 164 |
// Write channel data |
| 165 |
size_t read = 0; |
| 166 |
while (read < TOTAL_FRAMES) |
| 167 |
{ |
| 168 |
size_t to_read = TOTAL_FRAMES - read; |
| 169 |
if (to_read > BLK_SIZE) |
| 170 |
to_read = BLK_SIZE; |
| 171 |
|
| 172 |
ssize_t n_read = ar.read_samples(vp, to_read); |
| 173 |
UTEST_ASSERT_MSG(n_read > 0, "Returned invalid value on read: %d, requested=%d", int(n_read), int(to_read)); |
| 174 |
|
| 175 |
read += n_read; |
| 176 |
for (size_t i=0, n=v.size(); i<n; ++i) |
| 177 |
vp[i] += n_read; |
| 178 |
} |
| 179 |
|
| 180 |
// Check final status |
| 181 |
res = ar.close(); |
| 182 |
UTEST_ASSERT(res == STATUS_OK); |
| 183 |
res = fd.close(); |
| 184 |
UTEST_ASSERT(res == STATUS_OK); |
| 185 |
} |
| 186 |
|
| 187 |
void validate_contents(lltl::parray<FloatBuffer> &src, lltl::parray<FloatBuffer> &dst) |
| 188 |
{ |
| 189 |
UTEST_ASSERT(src.size() == dst.size()); |
| 190 |
|
| 191 |
for (size_t i=0, n=src.size(); i<n; ++i) |
| 192 |
{ |
| 193 |
FloatBuffer *s = src.get(i), *d = dst.get(i); |
| 194 |
UTEST_ASSERT(s != NULL); |
| 195 |
UTEST_ASSERT(d != NULL); |
| 196 |
UTEST_ASSERT(s->valid()); |
| 197 |
UTEST_ASSERT(d->valid()); |
| 198 |
if (!s->equals_relative(*d, 1e-2)) |
| 199 |
{ |
| 200 |
const ssize_t diff = s->last_diff(); |
| 201 |
UTEST_FAIL_MSG("Buffer data for channel %d differs at sample %d: %.6f vs %.6f", |
| 202 |
int(i), int(diff), s->get(diff), d->get(diff)); |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
void add_buffer(lltl::parray<FloatBuffer> &v, float value) |
| 208 |
{ |
| 209 |
FloatBuffer *fb = new FloatBuffer(TOTAL_FRAMES); |
| 210 |
UTEST_ASSERT(fb != NULL); |
| 211 |
float *dst = fb->data(); |
| 212 |
for (size_t i=0; i<TOTAL_FRAMES; ++i) |
| 213 |
dst[i] = value; |
| 214 |
UTEST_ASSERT(fb->valid()); |
| 215 |
UTEST_ASSERT(v.add(fb)); |
| 216 |
} |
| 217 |
|
| 218 |
void drop_buffers(lltl::parray<FloatBuffer> &v) |
| 219 |
{ |
| 220 |
for (size_t i=0, n=v.size(); i<n; ++i) |
| 221 |
{ |
| 222 |
FloatBuffer *fb = v.get(i); |
| 223 |
UTEST_ASSERT(fb != NULL); |
| 224 |
delete fb; |
| 225 |
} |
| 226 |
v.clear(); |
| 227 |
UTEST_ASSERT(v.size() == 0); |
| 228 |
} |
| 229 |
|
| 230 |
UTEST_MAIN |
| 231 |
{ |
| 232 |
// Initialize buffers |
| 233 |
lltl::parray<FloatBuffer> src, dst; |
| 234 |
for (size_t i=0; i<CHANNELS; ++i) |
| 235 |
{ |
| 236 |
add_buffer(src, cvalues[i]); |
| 237 |
add_buffer(dst, INVALID_VALUE); |
| 238 |
} |
| 239 |
|
| 240 |
for (size_t i=0, n = sizeof(formats) / sizeof(size_t); i<n; ++i) |
| 241 |
{ |
| 242 |
printf("Testing LSPC audio creation sample_format=%d\n", int(formats[i])); |
| 243 |
create_lspc_file(src, formats[i]); |
| 244 |
parse_lspc_file(dst, formats[i]); |
| 245 |
validate_contents(src, dst); |
| 246 |
} |
| 247 |
|
| 248 |
// Drop buffers |
| 249 |
drop_buffers(src); |
| 250 |
drop_buffers(dst); |
| 251 |
} |
| 252 |
|
| 253 |
UTEST_END |
| 254 |
|
| 255 |
|