/* * Copyright (C) 2020 Linux Studio Plugins Project * (C) 2020 Vladimir Sadovnikov * * This file is part of lsp-runtime-lib * Created on: 20 апр. 2020 г. * * lsp-runtime-lib is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * any later version. * * lsp-runtime-lib is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with lsp-runtime-lib. If not, see . */ #include #include #include #include #include #include #define BUF_SAMPLES 127 #define FRAMES 2048 UTEST_BEGIN("runtime.mm", outaudiofilestream) void validate_file(const io::Path *path, const float *src, size_t srate, float tol) { mm::InAudioFileStream is; mm::audio_stream_t info; printf("Reading PCM audio file %s as floating-point samples\n", path->as_native()); UTEST_ASSERT(is.open(path) == STATUS_OK); UTEST_ASSERT(is.info(&info) == STATUS_OK); UTEST_ASSERT(info.srate == srate); UTEST_ASSERT(info.channels == 2); UTEST_ASSERT(info.frames == FRAMES); FloatBuffer buf(BUF_SAMPLES * 2); ssize_t off = 0; while (true) { // Check position UTEST_ASSERT(is.position() == off); // Read frames ssize_t read = is.read(buf.data(), BUF_SAMPLES); if (read < 0) { UTEST_ASSERT(read == -STATUS_EOF); break; } UTEST_ASSERT(buf.valid()); // Check contents float *ptr = buf.data(); for (ssize_t i=0; i(), to_write); UTEST_ASSERT(written >= 0); UTEST_ASSERT(buf.valid()); off += to_write; } UTEST_ASSERT(os.close() == STATUS_OK); validate_file(&path, src, srate, tol); } void test_write_s16(const char *file, const float *src, size_t codec, size_t srate, float tol) { io::Path path; UTEST_ASSERT(path.fmt("%s/%s-%s", tempdir(), full_name(), file)); printf("Writing PCM audio file %s as s16 samples\n", path.as_native()); mm::OutAudioFileStream os; mm::audio_stream_t info; info.srate = srate; info.channels = 2; info.frames = FRAMES; info.format = mm::SFMT_S16; UTEST_ASSERT(os.open(&path, &info, codec) == STATUS_OK); for (ssize_t off=0; off(); for (size_t i=0, j=off*2; i= 0); UTEST_ASSERT(buf.valid()); off += to_write; } UTEST_ASSERT(os.close() == STATUS_OK); validate_file(&path, src, srate, tol); } void test_write_u16(const char *file, const float *src, size_t codec, size_t srate, float tol) { io::Path path; UTEST_ASSERT(path.fmt("%s/%s-%s", tempdir(), full_name(), file)); printf("Writing PCM audio file %s as s16 samples\n", path.as_native()); mm::OutAudioFileStream os; mm::audio_stream_t info; info.srate = srate; info.channels = 2; info.frames = FRAMES; info.format = mm::SFMT_S16; UTEST_ASSERT(os.open(&path, &info, codec) == STATUS_OK); for (ssize_t off=0; off(); for (size_t i=0, j=off*2; i= 0); UTEST_ASSERT(buf.valid()); off += to_write; } UTEST_ASSERT(os.close() == STATUS_OK); validate_file(&path, src, srate, tol); } UTEST_MAIN { // Generate buffer float delta = (8 * M_PI) / FRAMES; float buf[2 * FRAMES]; float *dst = buf; for (size_t i=0; i