| 1 |
/* |
| 2 |
* Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2020 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 20 апр. 2020 г. |
| 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/test-fw/utest.h> |
| 23 |
#include <lsp-plug.in/test-fw/ByteBuffer.h> |
| 24 |
#include <lsp-plug.in/test-fw/FloatBuffer.h> |
| 25 |
#include <lsp-plug.in/mm/InAudioFileStream.h> |
| 26 |
#include <lsp-plug.in/mm/OutAudioFileStream.h> |
| 27 |
#include <lsp-plug.in/stdlib/math.h> |
| 28 |
|
| 29 |
#define BUF_SAMPLES 127 |
| 30 |
#define FRAMES 2048 |
| 31 |
|
| 32 |
UTEST_BEGIN("runtime.mm", outaudiofilestream) |
| 33 |
|
| 34 |
void validate_file(const io::Path *path, const float *src, size_t srate, float tol) |
| 35 |
{ |
| 36 |
mm::InAudioFileStream is; |
| 37 |
mm::audio_stream_t info; |
| 38 |
|
| 39 |
printf("Reading PCM audio file %s as floating-point samples\n", path->as_native()); |
| 40 |
|
| 41 |
UTEST_ASSERT(is.open(path) == STATUS_OK); |
| 42 |
UTEST_ASSERT(is.info(&info) == STATUS_OK); |
| 43 |
UTEST_ASSERT(info.srate == srate); |
| 44 |
UTEST_ASSERT(info.channels == 2); |
| 45 |
UTEST_ASSERT(info.frames == FRAMES); |
| 46 |
|
| 47 |
FloatBuffer buf(BUF_SAMPLES * 2); |
| 48 |
ssize_t off = 0; |
| 49 |
|
| 50 |
while (true) |
| 51 |
{ |
| 52 |
// Check position |
| 53 |
UTEST_ASSERT(is.position() == off); |
| 54 |
|
| 55 |
// Read frames |
| 56 |
ssize_t read = is.read(buf.data(), BUF_SAMPLES); |
| 57 |
if (read < 0) |
| 58 |
{ |
| 59 |
UTEST_ASSERT(read == -STATUS_EOF); |
| 60 |
break; |
| 61 |
} |
| 62 |
UTEST_ASSERT(buf.valid()); |
| 63 |
|
| 64 |
// Check contents |
| 65 |
float *ptr = buf.data(); |
| 66 |
for (ssize_t i=0; i<read; ++i, ++off, ptr += 2, src += 2) |
| 67 |
{ |
| 68 |
UTEST_ASSERT_MSG(float_equals_absolute(src[0], ptr[0], tol), "Samples for channel 0[%d] differ: exp=%e, act=%e", int(off), src[0], ptr[0]); |
| 69 |
UTEST_ASSERT_MSG(float_equals_absolute(src[1], ptr[1], tol), "Samples for channel 1[%d] differ: exp=%e, act=%e", int(off), src[1], ptr[1]); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
UTEST_ASSERT(is.close() == STATUS_OK); |
| 74 |
} |
| 75 |
|
| 76 |
void test_write_f32(const char *file, const float *src, size_t codec, size_t srate, float tol) |
| 77 |
{ |
| 78 |
io::Path path; |
| 79 |
UTEST_ASSERT(path.fmt("%s/%s-%s", tempdir(), full_name(), file)); |
| 80 |
printf("Writing PCM audio file %s as floating-point samples\n", path.as_native()); |
| 81 |
|
| 82 |
mm::OutAudioFileStream os; |
| 83 |
mm::audio_stream_t info; |
| 84 |
info.srate = srate; |
| 85 |
info.channels = 2; |
| 86 |
info.frames = FRAMES; |
| 87 |
info.format = mm::SFMT_F32; |
| 88 |
|
| 89 |
UTEST_ASSERT(os.open(&path, &info, codec) == STATUS_OK); |
| 90 |
|
| 91 |
for (ssize_t off=0; off<FRAMES; ) |
| 92 |
{ |
| 93 |
size_t to_write = lsp_min(FRAMES - off, BUF_SAMPLES); |
| 94 |
|
| 95 |
// Check position |
| 96 |
UTEST_ASSERT(os.position() == off); |
| 97 |
ByteBuffer buf(&src[off * 2], to_write * 2 * sizeof(float)); |
| 98 |
|
| 99 |
// Write frames |
| 100 |
ssize_t written = os.write(buf.data<mm::f32_t>(), to_write); |
| 101 |
UTEST_ASSERT(written >= 0); |
| 102 |
UTEST_ASSERT(buf.valid()); |
| 103 |
|
| 104 |
off += to_write; |
| 105 |
} |
| 106 |
|
| 107 |
UTEST_ASSERT(os.close() == STATUS_OK); |
| 108 |
|
| 109 |
validate_file(&path, src, srate, tol); |
| 110 |
} |
| 111 |
|
| 112 |
void test_write_s16(const char *file, const float *src, size_t codec, size_t srate, float tol) |
| 113 |
{ |
| 114 |
io::Path path; |
| 115 |
UTEST_ASSERT(path.fmt("%s/%s-%s", tempdir(), full_name(), file)); |
| 116 |
printf("Writing PCM audio file %s as s16 samples\n", path.as_native()); |
| 117 |
|
| 118 |
mm::OutAudioFileStream os; |
| 119 |
mm::audio_stream_t info; |
| 120 |
info.srate = srate; |
| 121 |
info.channels = 2; |
| 122 |
info.frames = FRAMES; |
| 123 |
info.format = mm::SFMT_S16; |
| 124 |
|
| 125 |
UTEST_ASSERT(os.open(&path, &info, codec) == STATUS_OK); |
| 126 |
|
| 127 |
for (ssize_t off=0; off<FRAMES; ) |
| 128 |
{ |
| 129 |
size_t to_write = lsp_min(FRAMES - off, BUF_SAMPLES); |
| 130 |
|
| 131 |
// Check position |
| 132 |
UTEST_ASSERT(os.position() == off); |
| 133 |
ByteBuffer buf(to_write * 2 * sizeof(int16_t)); |
| 134 |
|
| 135 |
int16_t *dst = buf.data<int16_t>(); |
| 136 |
for (size_t i=0, j=off*2; i<to_write*2; ++i, ++j) |
| 137 |
{ |
| 138 |
float k = src[j] * 32767.5 + 32768.0; |
| 139 |
dst[i] = int16_t(int32_t(k) - 32768); |
| 140 |
} |
| 141 |
|
| 142 |
// Write frames |
| 143 |
ssize_t written = os.write(dst, to_write); |
| 144 |
UTEST_ASSERT(written >= 0); |
| 145 |
UTEST_ASSERT(buf.valid()); |
| 146 |
|
| 147 |
off += to_write; |
| 148 |
} |
| 149 |
|
| 150 |
UTEST_ASSERT(os.close() == STATUS_OK); |
| 151 |
|
| 152 |
validate_file(&path, src, srate, tol); |
| 153 |
} |
| 154 |
|
| 155 |
void test_write_u16(const char *file, const float *src, size_t codec, size_t srate, float tol) |
| 156 |
{ |
| 157 |
io::Path path; |
| 158 |
UTEST_ASSERT(path.fmt("%s/%s-%s", tempdir(), full_name(), file)); |
| 159 |
printf("Writing PCM audio file %s as s16 samples\n", path.as_native()); |
| 160 |
|
| 161 |
mm::OutAudioFileStream os; |
| 162 |
mm::audio_stream_t info; |
| 163 |
info.srate = srate; |
| 164 |
info.channels = 2; |
| 165 |
info.frames = FRAMES; |
| 166 |
info.format = mm::SFMT_S16; |
| 167 |
|
| 168 |
UTEST_ASSERT(os.open(&path, &info, codec) == STATUS_OK); |
| 169 |
|
| 170 |
for (ssize_t off=0; off<FRAMES; ) |
| 171 |
{ |
| 172 |
size_t to_write = lsp_min(FRAMES - off, BUF_SAMPLES); |
| 173 |
|
| 174 |
// Check position |
| 175 |
UTEST_ASSERT(os.position() == off); |
| 176 |
ByteBuffer buf(to_write * 2 * sizeof(uint16_t)); |
| 177 |
|
| 178 |
uint16_t *dst = buf.data<uint16_t>(); |
| 179 |
for (size_t i=0, j=off*2; i<to_write*2; ++i, ++j) |
| 180 |
{ |
| 181 |
float k = src[j] * 32767.5 + 32768.0; |
| 182 |
dst[i] = uint16_t(int32_t(k)); |
| 183 |
} |
| 184 |
|
| 185 |
// Write frames |
| 186 |
ssize_t written = os.write(dst, to_write); |
| 187 |
UTEST_ASSERT(written >= 0); |
| 188 |
UTEST_ASSERT(buf.valid()); |
| 189 |
|
| 190 |
off += to_write; |
| 191 |
} |
| 192 |
|
| 193 |
UTEST_ASSERT(os.close() == STATUS_OK); |
| 194 |
|
| 195 |
validate_file(&path, src, srate, tol); |
| 196 |
} |
| 197 |
|
| 198 |
UTEST_MAIN |
| 199 |
{ |
| 200 |
// Generate buffer |
| 201 |
float delta = (8 * M_PI) / FRAMES; |
| 202 |
float buf[2 * FRAMES]; |
| 203 |
float *dst = buf; |
| 204 |
for (size_t i=0; i<FRAMES; ++i, dst += 2) |
| 205 |
{ |
| 206 |
dst[0] = sinf(i * delta); |
| 207 |
dst[1] = cosf(i * delta); |
| 208 |
} |
| 209 |
|
| 210 |
// Call tests |
| 211 |
test_write_f32("pcm-f32.wav", buf, mm::AFMT_WAV | mm::CFMT_PCM, 48000, 1e-5f); |
| 212 |
test_write_s16("pcm-s16.wav", buf, mm::AFMT_WAV | mm::CFMT_PCM, 48000, 5e-5); |
| 213 |
test_write_u16("pcm-u16.wav", buf, mm::AFMT_WAV | mm::CFMT_PCM, 48000, 5e-5); |
| 214 |
|
| 215 |
// Call tests |
| 216 |
test_write_f32("alaw-f32.wav", buf, mm::AFMT_WAV | mm::CFMT_ALAW, 48000, 3e-2); |
| 217 |
test_write_s16("alaw-s16.wav", buf, mm::AFMT_WAV | mm::CFMT_ALAW, 48000, 3e-2); |
| 218 |
test_write_u16("alaw-u16.wav", buf, mm::AFMT_WAV | mm::CFMT_ALAW, 48000, 3e-2); |
| 219 |
} |
| 220 |
UTEST_END; |