Debian Codegraph @

modules/lsp-runtime-lib/src/test/utest/mm/inaudiofilestream.cpp

raw
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: 19 апр. 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/stdlib/math.h>
27
28 #define BUF_SAMPLES 127
29 #define FRAMES 2048
30
31 /* Code to generate audio file
32 #include <stdio.h>
33 #include <sndfile.h>
34 #include <math.h>
35
36 #define FRAMES 2048
37
38 int main()
39 {
40 SF_INFO sf_info;
41 SNDFILE *sf_obj;
42
43 sf_info.frames = FRAMES;
44 sf_info.samplerate = 48000;
45 sf_info.channels = 2;
46 sf_info.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16 | SF_ENDIAN_LITTLE;
47 sf_info.sections = 0;
48 sf_info.seekable = 0;
49
50 // Open sound file
51 if ((sf_obj = sf_open("pcm.wav", SFM_WRITE, &sf_info)) == NULL)
52 return -1;
53
54 float delta = (8 * M_PI) / FRAMES;
55 float buf[2 * FRAMES];
56 float *dst = buf;
57 for (size_t i=0; i<FRAMES; ++i, dst += 2)
58 {
59 dst[0] = sinf(i * delta);
60 dst[1] = cosf(i * delta);
61 }
62
63 sf_writef_float(sf_obj, buf, FRAMES);
64 sf_close(sf_obj);
65 }
66 */
67
68 UTEST_BEGIN("runtime.mm", inaudiofilestream)
69
70 void test_read_f32(const char *file)
71 {
72 io::Path path;
73
74 UTEST_ASSERT(path.fmt("%s/%s", resources(), file));
75 printf("Reading PCM audio file %s as floating-point samples\n", path.as_native());
76
77 mm::InAudioFileStream is;
78 mm::audio_stream_t info;
79
80 UTEST_ASSERT(is.open(&path) == STATUS_OK);
81 UTEST_ASSERT(is.info(&info) == STATUS_OK);
82 UTEST_ASSERT(info.srate == 48000);
83 UTEST_ASSERT(info.channels == 2);
84 UTEST_ASSERT(info.frames == FRAMES);
85
86 FloatBuffer buf(BUF_SAMPLES * 2);
87 float delta = (8 * M_PI) / FRAMES;
88 ssize_t off = 0;
89
90 while (true)
91 {
92 // Check position
93 UTEST_ASSERT(is.position() == off);
94
95 // Read frames
96 ssize_t read = is.read(buf.data(), BUF_SAMPLES);
97 if (read < 0)
98 {
99 UTEST_ASSERT(read == -STATUS_EOF);
100 break;
101 }
102 UTEST_ASSERT(buf.valid());
103
104 // Check contents
105 float *ptr = buf.data();
106 for (ssize_t i=0; i<read; ++i, ++off, ptr += 2)
107 {
108 float s = sinf(delta * off);
109 float c = cosf(delta * off);
110 UTEST_ASSERT_MSG(float_equals_absolute(s, ptr[0], 8e-5), "Samples for channel 0[%d] differ: exp=%e, act=%e", int(off), s, ptr[0]);
111 UTEST_ASSERT_MSG(float_equals_absolute(c, ptr[1], 8e-5), "Samples for channel 1[%d] differ: exp=%e, act=%e", int(off), c, ptr[1]);
112 }
113 }
114
115 UTEST_ASSERT(is.close() == STATUS_OK);
116 }
117
118 void test_read_s16(const char *file)
119 {
120 io::Path path;
121
122 UTEST_ASSERT(path.fmt("%s/%s", resources(), file));
123 printf("Reading PCM audio file %s as s16 samples\n", path.as_native());
124
125 mm::InAudioFileStream is;
126 mm::audio_stream_t info;
127
128 UTEST_ASSERT(is.open(&path) == STATUS_OK);
129 UTEST_ASSERT(is.info(&info) == STATUS_OK);
130 UTEST_ASSERT(info.srate == 48000);
131 UTEST_ASSERT(info.channels == 2);
132 UTEST_ASSERT(info.frames == FRAMES);
133
134 ByteBuffer buf(BUF_SAMPLES * sizeof(int16_t) * 2);
135 float delta = (8 * M_PI) / FRAMES;
136 ssize_t off = 0;
137
138 while (true)
139 {
140 // Check position
141 UTEST_ASSERT(is.position() == off);
142
143 // Read frames
144 ssize_t read = is.read(buf.data<int16_t>(), BUF_SAMPLES);
145 if (read < 0)
146 {
147 UTEST_ASSERT(read == -STATUS_EOF);
148 break;
149 }
150 UTEST_ASSERT(buf.valid());
151
152 // Check contents
153 int16_t *ptr = buf.data<int16_t>();
154 for (ssize_t i=0; i<read; ++i, ++off, ptr += 2)
155 {
156 int16_t s = int16_t(int32_t(sinf(delta * off)*32767 + 32767.5) - 32767);
157 int16_t c = int16_t(int32_t(cosf(delta * off)*32767 + 32767.5) - 32767);
158 UTEST_ASSERT_MSG(float_equals_absolute(s, ptr[0], 2.0f), "Samples for channel 0[%d] differ: exp=%d, act=%d", int(off), int(s), int(ptr[0]));
159 UTEST_ASSERT_MSG(float_equals_absolute(c, ptr[1], 2.0f), "Samples for channel 1[%d] differ: exp=%d, act=%d", int(off), int(c), int(ptr[1]));
160 }
161 }
162
163 UTEST_ASSERT(is.close() == STATUS_OK);
164 }
165
166 void test_read_u16(const char *file)
167 {
168 io::Path path;
169
170 UTEST_ASSERT(path.fmt("%s/%s", resources(), file));
171 printf("Reading PCM audio file %s as u16 samples\n", path.as_native());
172
173 mm::InAudioFileStream is;
174 mm::audio_stream_t info;
175
176 UTEST_ASSERT(is.open(&path) == STATUS_OK);
177 UTEST_ASSERT(is.info(&info) == STATUS_OK);
178 UTEST_ASSERT(info.srate == 48000);
179 UTEST_ASSERT(info.channels == 2);
180 UTEST_ASSERT(info.frames == FRAMES);
181
182 ByteBuffer buf(BUF_SAMPLES * sizeof(uint16_t) * 2);
183 float delta = (8 * M_PI) / FRAMES;
184 ssize_t off = 0;
185
186 while (true)
187 {
188 // Check position
189 UTEST_ASSERT(is.position() == off);
190
191 // Read frames
192 ssize_t read = is.read(buf.data<uint16_t>(), BUF_SAMPLES);
193 if (read < 0)
194 {
195 UTEST_ASSERT(read == -STATUS_EOF);
196 break;
197 }
198 UTEST_ASSERT(buf.valid());
199
200 // Check contents
201 uint16_t *ptr = buf.data<uint16_t>();
202 for (ssize_t i=0; i<read; ++i, ++off, ptr += 2)
203 {
204 uint16_t s = uint16_t(sinf(delta * off)*32767 + 32767.5);
205 uint16_t c = uint16_t(cosf(delta * off)*32767 + 32767.5);
206 UTEST_ASSERT_MSG(float_equals_absolute(s, ptr[0], 2.0f), "Samples for channel 0[%d] differ: exp=%d, act=%d", int(off), int(s), int(ptr[0]));
207 UTEST_ASSERT_MSG(float_equals_absolute(c, ptr[1], 2.0f), "Samples for channel 1[%d] differ: exp=%d, act=%d", int(off), int(c), int(ptr[1]));
208 }
209 }
210
211 UTEST_ASSERT(is.close() == STATUS_OK);
212 }
213
214 void test_read_alaw(const char *file)
215 {
216 io::Path path;
217
218 UTEST_ASSERT(path.fmt("%s/%s", resources(), file));
219 printf("Reading ALAW audio file %s as f32 samples\n", path.as_native());
220
221 mm::InAudioFileStream is;
222 mm::audio_stream_t info;
223
224 UTEST_ASSERT(is.open(&path) == STATUS_OK);
225 UTEST_ASSERT(is.info(&info) == STATUS_OK);
226 UTEST_ASSERT(info.srate == 8000);
227 UTEST_ASSERT(info.channels == 2);
228 UTEST_ASSERT(info.frames == FRAMES);
229
230 ByteBuffer buf(BUF_SAMPLES * sizeof(float) * 2);
231 float delta = (8 * M_PI) / FRAMES;
232 ssize_t off = 0;
233
234 while (true)
235 {
236 // Check position
237 UTEST_ASSERT(is.position() == off);
238
239 // Read frames
240 ssize_t read = is.read(buf.data<float>(), BUF_SAMPLES);
241 if (read < 0)
242 {
243 UTEST_ASSERT(read == -STATUS_EOF);
244 break;
245 }
246 UTEST_ASSERT(buf.valid());
247
248 // Check contents
249 float *ptr = buf.data<float>();
250 for (ssize_t i=0; i<read; ++i, ++off, ptr += 2)
251 {
252 float s = sinf(delta * off);
253 float c = cosf(delta * off);
254 UTEST_ASSERT_MSG(float_equals_absolute(s, ptr[0], 3e-2), "Samples for channel 0[%d] differ: exp=%e, act=%e", int(off), s, ptr[0]);
255 UTEST_ASSERT_MSG(float_equals_absolute(c, ptr[1], 3e-2), "Samples for channel 1[%d] differ: exp=%e, act=%e", int(off), c, ptr[1]);
256 }
257 }
258
259 UTEST_ASSERT(is.close() == STATUS_OK);
260 }
261
262 void test_read_src_f32(const char *file)
263 {
264 io::Path path;
265
266 UTEST_ASSERT(path.fmt("%s/%s", resources(), file));
267 printf("Reading source f32 audio file %s as f32 samples\n", path.as_native());
268
269 mm::InAudioFileStream is;
270 mm::audio_stream_t info;
271
272 UTEST_ASSERT(is.open(&path) == STATUS_OK);
273 UTEST_ASSERT(is.info(&info) == STATUS_OK);
274 UTEST_ASSERT(info.srate == 48000);
275 UTEST_ASSERT(info.channels == 2);
276 UTEST_ASSERT(info.frames == 4096);
277
278 ByteBuffer buf(BUF_SAMPLES * sizeof(float) * 2);
279 ssize_t off = 0;
280
281 while (true)
282 {
283 // Check position
284 UTEST_ASSERT(is.position() == off);
285
286 // Read frames
287 ssize_t read = is.read(buf.data<float>(), BUF_SAMPLES);
288 if (read < 0)
289 {
290 UTEST_ASSERT(read == -STATUS_EOF);
291 break;
292 }
293 UTEST_ASSERT(buf.valid());
294
295 off += read;
296 }
297
298 UTEST_ASSERT(is.close() == STATUS_OK);
299 }
300
301 UTEST_MAIN
302 {
303 test_read_src_f32("mm/f32.wav");
304 test_read_f32("mm/pcm.wav");
305 test_read_s16("mm/pcm.wav");
306 test_read_u16("mm/pcm.wav");
307 test_read_alaw("mm/alaw.wav");
308 }
309 UTEST_END;
310
311