| 1 |
/* |
| 2 |
* Copyright (C) 2022 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2022 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 22 окт. 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/fmt/lspc/File.h> |
| 23 |
#include <lsp-plug.in/fmt/lspc/util.h> |
| 24 |
#include <lsp-plug.in/io/Dir.h> |
| 25 |
#include <lsp-plug.in/io/Path.h> |
| 26 |
#include <lsp-plug.in/test-fw/utest.h> |
| 27 |
|
| 28 |
UTEST_BEGIN("runtime.fmt.lspc", drumkit) |
| 29 |
|
| 30 |
UTEST_TIMELIMIT(300) |
| 31 |
|
| 32 |
void write_dir_to_drumkit(lspc::File *lspc, const io::Path *base, const io::Path *relative) |
| 33 |
{ |
| 34 |
io::Dir dir; |
| 35 |
io::fattr_t attr; |
| 36 |
io::Path dir_path, child, abs_child; |
| 37 |
lspc::chunk_id_t chunk_id; |
| 38 |
LSPString item; |
| 39 |
status_t res; |
| 40 |
|
| 41 |
UTEST_ASSERT(dir_path.set(base, relative) == STATUS_OK); |
| 42 |
UTEST_ASSERT(dir.open(&dir_path) == STATUS_OK); |
| 43 |
while ((res = dir.reads(&item, &attr)) == STATUS_OK) |
| 44 |
{ |
| 45 |
UTEST_ASSERT(child.set(relative, &item) == STATUS_OK); |
| 46 |
UTEST_ASSERT(abs_child.set(base, &child) == STATUS_OK); |
| 47 |
|
| 48 |
if (attr.type == io::fattr_t::FT_REGULAR) |
| 49 |
{ |
| 50 |
// Read file and create audio chunk |
| 51 |
printf("Writing file data chunk for file '%s'...\n", abs_child.as_native()); |
| 52 |
UTEST_ASSERT(lspc::write_audio(&chunk_id, lspc, &abs_child) == STATUS_OK); |
| 53 |
printf("Written as chunk id=%d\n", int(chunk_id)); |
| 54 |
|
| 55 |
// Write the path entry that references the created audio chunk |
| 56 |
printf("Writing path entry chunk '%s' referencing chunk id=%d for file '%s'...\n", |
| 57 |
child.as_native(), int(chunk_id), abs_child.as_native()); |
| 58 |
UTEST_ASSERT(lspc::write_path(&chunk_id, lspc, &child, 0, chunk_id) == STATUS_OK); |
| 59 |
printf("Written as chunk id=%d\n", int(chunk_id)); |
| 60 |
} |
| 61 |
else if (attr.type == io::fattr_t::FT_DIRECTORY) |
| 62 |
{ |
| 63 |
if (io::Path::is_dots(&item)) |
| 64 |
continue; |
| 65 |
|
| 66 |
printf("Writing path entry chunk for directory '%s'...\n", abs_child.as_native()); |
| 67 |
UTEST_ASSERT(lspc::write_path(&chunk_id, lspc, &item, lspc::PATH_DIR, 0) == STATUS_OK); |
| 68 |
printf("Written as chunk id=%d\n", int(chunk_id)); |
| 69 |
|
| 70 |
// Perform recursive call |
| 71 |
write_dir_to_drumkit(lspc, base, &child); |
| 72 |
} |
| 73 |
} |
| 74 |
UTEST_ASSERT(res == STATUS_EOF); |
| 75 |
} |
| 76 |
|
| 77 |
void create_drumkit_file(const io::Path *drumkit, const io::Path *path, const io::Path *config) |
| 78 |
{ |
| 79 |
lspc::File lspc; |
| 80 |
lspc::chunk_id_t chunk_id; |
| 81 |
io::Path relative; |
| 82 |
|
| 83 |
printf("Creating drumkit file to '%s'...\n", drumkit->as_native()); |
| 84 |
UTEST_ASSERT(lspc.create(drumkit) == STATUS_OK); |
| 85 |
|
| 86 |
printf("Writing audio files...\n"); |
| 87 |
write_dir_to_drumkit(&lspc, path, &relative); |
| 88 |
|
| 89 |
printf("Writing configuration file '%s'...\n", config->as_native()); |
| 90 |
UTEST_ASSERT(lspc::write_config(&chunk_id, &lspc, config) == STATUS_OK); |
| 91 |
printf("Written as chunk id=%d\n", int(chunk_id)); |
| 92 |
|
| 93 |
UTEST_ASSERT(lspc.close() == STATUS_OK); |
| 94 |
} |
| 95 |
|
| 96 |
void enumerate_chunks(lspc::File *lspc, uint32_t chunk_magic, ssize_t required_count) |
| 97 |
{ |
| 98 |
lspc::chunk_id_t *list = NULL; |
| 99 |
ssize_t count = lspc->enumerate_chunks(chunk_magic, &list); |
| 100 |
UTEST_ASSERT(count == required_count); |
| 101 |
UTEST_ASSERT(list != NULL); |
| 102 |
|
| 103 |
for (ssize_t i=0; i<count; ++i) |
| 104 |
printf(" found chunk id=%d\n", int(list[i])); |
| 105 |
free(list); |
| 106 |
} |
| 107 |
|
| 108 |
void enumerate_all_chunks(lspc::File *lspc, ssize_t required_count) |
| 109 |
{ |
| 110 |
lspc::chunk_info_t *list = NULL; |
| 111 |
ssize_t count = lspc->enumerate_chunks(&list); |
| 112 |
UTEST_ASSERT(count == required_count); |
| 113 |
UTEST_ASSERT(list != NULL); |
| 114 |
|
| 115 |
for (ssize_t i=0; i<count; ++i) |
| 116 |
printf(" found chunk id=%2d magic='%c%c%c%c' position=%lld size=%lld\n", |
| 117 |
int(list[i].chunk_id), |
| 118 |
char((list[i].magic >> 24) & 0xff), |
| 119 |
char((list[i].magic >> 16) & 0xff), |
| 120 |
char((list[i].magic >> 8) & 0xff), |
| 121 |
char((list[i].magic) & 0xff), |
| 122 |
(long long)list[i].position, |
| 123 |
(long long)list[i].size); |
| 124 |
free(list); |
| 125 |
} |
| 126 |
|
| 127 |
void enumerate_drumkit_chunks(const io::Path *drumkit) |
| 128 |
{ |
| 129 |
lspc::File lspc; |
| 130 |
|
| 131 |
printf("Opening drumkit file...\n"); |
| 132 |
UTEST_ASSERT(lspc.open(drumkit) == STATUS_OK); |
| 133 |
|
| 134 |
printf("Enumerating text configuration chunks...\n"); |
| 135 |
enumerate_chunks(&lspc, LSPC_CHUNK_TEXT_CONFIG, 1); |
| 136 |
printf("Enumerating audio chunks...\n"); |
| 137 |
enumerate_chunks(&lspc, LSPC_CHUNK_AUDIO, 4); |
| 138 |
printf("Enumerating path chunks...\n"); |
| 139 |
enumerate_chunks(&lspc, LSPC_CHUNK_PATH, 5); |
| 140 |
|
| 141 |
printf("Enumerating all chunks...\n"); |
| 142 |
enumerate_all_chunks(&lspc, 10); |
| 143 |
|
| 144 |
UTEST_ASSERT(lspc.close() == STATUS_OK); |
| 145 |
} |
| 146 |
|
| 147 |
ssize_t read_audio_stream(lspc::chunk_id_t chunk_id, lspc::File *lspc, const io::Path *path) |
| 148 |
{ |
| 149 |
mm::IInAudioStream *is = NULL; |
| 150 |
status_t res = lspc::read_audio(chunk_id, lspc, &is); |
| 151 |
if (res < 0) |
| 152 |
return - res; |
| 153 |
else if (is == NULL) |
| 154 |
return - STATUS_NO_MEM; |
| 155 |
lsp_finally { |
| 156 |
if (is != NULL) |
| 157 |
delete is; |
| 158 |
}; |
| 159 |
|
| 160 |
float *buf = new float[is->channels() * 1024]; |
| 161 |
if (buf == NULL) |
| 162 |
return - STATUS_NO_MEM; |
| 163 |
lsp_finally { delete[] buf; }; |
| 164 |
|
| 165 |
ssize_t n; |
| 166 |
ssize_t count = 0; |
| 167 |
while ((n = is->read(buf, 1024)) >= 0) |
| 168 |
count += n; |
| 169 |
|
| 170 |
if ((n < 0) && (n != -STATUS_EOF)) |
| 171 |
return n; |
| 172 |
if ((res = is->close()) != STATUS_OK) |
| 173 |
return res; |
| 174 |
delete is; |
| 175 |
is = NULL; |
| 176 |
|
| 177 |
return count; |
| 178 |
}; |
| 179 |
|
| 180 |
void extract_drumkit_file(io::Path *dst_dir, io::Path *drumkit) |
| 181 |
{ |
| 182 |
lspc::File lspc; |
| 183 |
|
| 184 |
printf("Extracting drumkit file...\n"); |
| 185 |
UTEST_ASSERT(lspc.open(drumkit) == STATUS_OK); |
| 186 |
|
| 187 |
lspc::chunk_id_t *paths = NULL; |
| 188 |
ssize_t path_count = lspc.enumerate_chunks(LSPC_CHUNK_PATH, &paths); |
| 189 |
UTEST_ASSERT(paths != NULL); |
| 190 |
UTEST_ASSERT(path_count == 5); |
| 191 |
lsp_finally { free(paths); }; |
| 192 |
|
| 193 |
// Process audio files |
| 194 |
LSPString rel_path; |
| 195 |
io::Path path, dir; |
| 196 |
size_t flags; |
| 197 |
lspc::chunk_id_t ref_id; |
| 198 |
for (ssize_t i=0; i<path_count; ++i) |
| 199 |
{ |
| 200 |
UTEST_ASSERT(lspc::read_path(paths[i], &lspc, &rel_path, &flags, &ref_id) == STATUS_OK); |
| 201 |
printf(" read chunk %d: path='%s' flags=0x%x, referenced chunk_id=%d\n", |
| 202 |
int(paths[i]), rel_path.get_native(), int(flags), int(ref_id)); |
| 203 |
if (flags & lspc::PATH_DIR) |
| 204 |
{ |
| 205 |
printf(" chunk is a directory record, nothing to do, skipping\n"); |
| 206 |
continue; |
| 207 |
} |
| 208 |
|
| 209 |
// Extract file |
| 210 |
UTEST_ASSERT(path.set(dst_dir, &rel_path) == STATUS_OK); |
| 211 |
printf(" extracting audio chunk id=%d to '%s'...\n", int(ref_id), path.as_native()); |
| 212 |
UTEST_ASSERT(path.get_parent(&dir) == STATUS_OK); |
| 213 |
UTEST_ASSERT(dir.mkdir(true) == STATUS_OK); |
| 214 |
UTEST_ASSERT(lspc::read_audio(ref_id, &lspc, &path, mm::SFMT_S24_DFL, mm::AFMT_WAV | mm::CFMT_PCM) == STATUS_OK); |
| 215 |
|
| 216 |
// Test reading as audio stream |
| 217 |
printf(" testing reading audio stream from chunk id=%d...\n", int(ref_id)); |
| 218 |
ssize_t res = read_audio_stream(ref_id, &lspc, &path); |
| 219 |
UTEST_ASSERT(res >= 0); |
| 220 |
printf(" read %d frames\n", int(res)); |
| 221 |
} |
| 222 |
|
| 223 |
// Find the text configuration chunk |
| 224 |
lspc::chunk_id_t *config = NULL; |
| 225 |
ssize_t config_count = lspc.enumerate_chunks(LSPC_CHUNK_TEXT_CONFIG, &config); |
| 226 |
UTEST_ASSERT(config != NULL); |
| 227 |
UTEST_ASSERT(config_count == 1); |
| 228 |
lsp_finally { free(config); }; |
| 229 |
|
| 230 |
// Extract the text configuration |
| 231 |
UTEST_ASSERT(path.set(dst_dir, "drumkit.cfg") == STATUS_OK); |
| 232 |
printf(" extracting text configuration chunk id=%d to '%s'...\n", int(config[0]), path.as_native()); |
| 233 |
UTEST_ASSERT(path.get_parent(&dir) == STATUS_OK); |
| 234 |
UTEST_ASSERT(dir.mkdir(true) == STATUS_OK); |
| 235 |
UTEST_ASSERT(lspc::read_config(config[0], &lspc, &path) == STATUS_OK); |
| 236 |
|
| 237 |
// Close the LSPC file |
| 238 |
UTEST_ASSERT(lspc.close() == STATUS_OK); |
| 239 |
} |
| 240 |
|
| 241 |
UTEST_MAIN |
| 242 |
{ |
| 243 |
io::Path drumkit, src_dir, dst_dir, config; |
| 244 |
UTEST_ASSERT(drumkit.fmt("%s/utest-%s-drumkit.lspc", tempdir(), full_name())); |
| 245 |
UTEST_ASSERT(src_dir.fmt("%s/fmt/lspc/drumkit/data", resources())); |
| 246 |
UTEST_ASSERT(dst_dir.fmt("%s/utest-%s-drumkit", tempdir(), full_name())); |
| 247 |
UTEST_ASSERT(config.fmt("%s/fmt/lspc/drumkit/drumkit.cfg", resources())); |
| 248 |
|
| 249 |
create_drumkit_file(&drumkit, &src_dir, &config); |
| 250 |
enumerate_drumkit_chunks(&drumkit); |
| 251 |
extract_drumkit_file(&dst_dir, &drumkit); |
| 252 |
} |
| 253 |
|
| 254 |
UTEST_END |
| 255 |
|