2025-10-16 09:59:12 -05:00
|
|
|
/*
|
|
|
|
|
* SPDX-FileCopyrightText: Copyright 2020-2026 OpenRTX Contributors
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
*/
|
2021-04-12 19:16:47 +02:00
|
|
|
|
2022-06-03 01:36:32 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
#include <chrono>
|
2022-06-03 01:36:32 +02:00
|
|
|
#include <cstdio>
|
|
|
|
|
#include <cstdlib>
|
2022-06-03 01:36:32 +02:00
|
|
|
#include <thread>
|
|
|
|
|
#include <vector>
|
2021-04-12 19:16:47 +02:00
|
|
|
|
2025-09-25 20:47:42 -05:00
|
|
|
#include "core/audio_stream.h"
|
2021-04-12 19:16:47 +02:00
|
|
|
|
2026-02-24 19:33:36 -06:00
|
|
|
static const char *files[] = { "MIC.raw", "RTX.raw", "MCU.raw" };
|
2022-06-03 01:36:32 +02:00
|
|
|
|
2022-06-03 01:36:32 +02:00
|
|
|
#define CHECK(x) \
|
2026-02-24 19:33:36 -06:00
|
|
|
do { \
|
|
|
|
|
if (!(x)) { \
|
2022-06-03 01:36:32 +02:00
|
|
|
puts("Failed assertion: " #x "\n"); \
|
|
|
|
|
abort(); \
|
|
|
|
|
} \
|
|
|
|
|
} while (0)
|
2021-04-12 19:16:47 +02:00
|
|
|
|
2022-06-03 01:36:32 +02:00
|
|
|
void test_linear()
|
2021-04-12 19:16:47 +02:00
|
|
|
{
|
2026-02-24 19:33:36 -06:00
|
|
|
for (int fn = 0; fn < 3; fn++) {
|
2022-06-03 01:36:32 +02:00
|
|
|
// Write a mock audio file
|
2026-02-24 19:33:36 -06:00
|
|
|
FILE *fp = fopen(files[fn], "wb");
|
2022-06-03 01:36:32 +02:00
|
|
|
CHECK(fp);
|
2026-02-24 19:33:36 -06:00
|
|
|
for (int i = 0; i < 13; i++) {
|
2022-06-03 01:36:32 +02:00
|
|
|
CHECK(fputc(i, fp) == i);
|
|
|
|
|
CHECK(fputc(0, fp) == 0);
|
|
|
|
|
}
|
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
|
|
// Start inputStream using that file as source
|
|
|
|
|
stream_sample_t tmp[128];
|
2026-02-24 19:33:36 -06:00
|
|
|
auto id = inputStream_start(static_cast<AudioSource>(fn),
|
|
|
|
|
AudioPriority::PRIO_PROMPT, tmp,
|
|
|
|
|
sizeof(tmp) / sizeof(tmp[0]),
|
|
|
|
|
BufMode::BUF_LINEAR, 44100);
|
2022-06-03 01:36:32 +02:00
|
|
|
CHECK(id != -1);
|
|
|
|
|
|
|
|
|
|
// Should fail
|
2026-02-24 19:33:36 -06:00
|
|
|
auto id_2 = inputStream_start(static_cast<AudioSource>(fn),
|
|
|
|
|
AudioPriority::PRIO_BEEP, tmp,
|
|
|
|
|
sizeof(tmp) / sizeof(tmp[0]),
|
|
|
|
|
BufMode::BUF_LINEAR, 44100);
|
2022-06-03 01:36:32 +02:00
|
|
|
CHECK(id_2 == -1);
|
|
|
|
|
|
2026-02-24 19:33:36 -06:00
|
|
|
auto id_3 = inputStream_start(static_cast<AudioSource>(fn),
|
|
|
|
|
AudioPriority::PRIO_RX, tmp,
|
|
|
|
|
sizeof(tmp) / sizeof(tmp[0]),
|
|
|
|
|
BufMode::BUF_LINEAR, 44100);
|
2022-06-03 01:36:32 +02:00
|
|
|
CHECK(id_3 == -1);
|
|
|
|
|
|
|
|
|
|
// This should work, as it has higher priority
|
2026-02-24 19:33:36 -06:00
|
|
|
auto id_4 = inputStream_start(static_cast<AudioSource>(fn),
|
|
|
|
|
AudioPriority::PRIO_TX, tmp,
|
|
|
|
|
sizeof(tmp) / sizeof(tmp[0]),
|
|
|
|
|
BufMode::BUF_LINEAR, 44100);
|
2022-06-03 01:36:32 +02:00
|
|
|
CHECK(id_4 != -1);
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
// id is now invalid (overridden by id_4), getData should fail
|
|
|
|
|
auto should_fail = inputStream_getData(id);
|
|
|
|
|
CHECK(should_fail.data == NULL);
|
|
|
|
|
CHECK(should_fail.len == 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// id_4 is now the only valid pointer, should work
|
|
|
|
|
id = id_4;
|
|
|
|
|
|
|
|
|
|
int c_ptr = 0;
|
|
|
|
|
// getData multiple times
|
2026-02-24 19:33:36 -06:00
|
|
|
for (int i = 0; i < 20; i++) {
|
2022-06-03 01:36:32 +02:00
|
|
|
using namespace std::chrono;
|
2026-02-24 19:33:36 -06:00
|
|
|
auto t1 = steady_clock::now();
|
|
|
|
|
auto db = inputStream_getData(id);
|
|
|
|
|
auto t2 = steady_clock::now();
|
2022-06-03 01:36:32 +02:00
|
|
|
const uint64_t delta = duration_cast<microseconds>(t2 - t1).count();
|
|
|
|
|
const uint64_t expected = (128 * 1000000 / 44100);
|
|
|
|
|
|
|
|
|
|
// Check that the getData() sleeps the right amount of time
|
|
|
|
|
CHECK((delta > expected) && delta < (expected + 10000));
|
|
|
|
|
|
|
|
|
|
// Check the contents
|
|
|
|
|
CHECK(db.len == 128);
|
2026-02-24 19:33:36 -06:00
|
|
|
for (int i = 0; i < 128; i++) {
|
2022-06-03 01:36:32 +02:00
|
|
|
CHECK(tmp[i] == (c_ptr % 13));
|
|
|
|
|
CHECK(db.data[i] == (c_ptr % 13));
|
|
|
|
|
c_ptr++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Close inputStream and remove the temporary file
|
|
|
|
|
inputStream_stop(id);
|
|
|
|
|
CHECK(remove(files[fn]) == 0);
|
2022-06-03 01:36:32 +02:00
|
|
|
}
|
2022-06-03 01:36:32 +02:00
|
|
|
}
|
|
|
|
|
|
2026-02-24 19:33:36 -06:00
|
|
|
void test_ring_buffer(uint64_t n_bytes, uint64_t n_iter,
|
2022-06-03 01:36:32 +02:00
|
|
|
const uint64_t buf_size)
|
|
|
|
|
{
|
2026-02-24 19:33:36 -06:00
|
|
|
for (int fn = 0; fn < 3; fn++) {
|
|
|
|
|
FILE *fp = fopen(files[fn], "wb");
|
2022-06-03 01:36:32 +02:00
|
|
|
CHECK(fp);
|
|
|
|
|
|
2026-02-24 19:33:36 -06:00
|
|
|
for (uint64_t i = 0; i < n_bytes; i++) {
|
|
|
|
|
uint16_t j = i;
|
|
|
|
|
uint8_t *buf = (uint8_t *)&j;
|
2022-06-03 01:36:32 +02:00
|
|
|
CHECK(fputc(buf[0], fp) == buf[0]);
|
|
|
|
|
CHECK(fputc(buf[1], fp) == buf[1]);
|
|
|
|
|
}
|
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
|
|
std::vector<stream_sample_t> tmp(buf_size);
|
2026-02-24 19:33:36 -06:00
|
|
|
auto id = inputStream_start(static_cast<AudioSource>(fn),
|
|
|
|
|
AudioPriority::PRIO_BEEP, tmp.data(),
|
|
|
|
|
tmp.size(), BufMode::BUF_CIRC_DOUBLE,
|
|
|
|
|
44100);
|
2022-06-03 01:36:32 +02:00
|
|
|
CHECK(id != -1);
|
|
|
|
|
|
|
|
|
|
using namespace std::chrono;
|
|
|
|
|
time_point<steady_clock> t0 = steady_clock::now();
|
|
|
|
|
|
|
|
|
|
uint64_t ctr = 0;
|
|
|
|
|
std::vector<stream_sample_t> tmp2(buf_size / 2);
|
2026-02-24 19:33:36 -06:00
|
|
|
for (uint64_t i = 0; i < n_iter; i++) {
|
2022-06-03 01:36:32 +02:00
|
|
|
{
|
|
|
|
|
auto db = inputStream_getData(id);
|
|
|
|
|
|
|
|
|
|
CHECK(db.len > 0);
|
|
|
|
|
CHECK(db.data == &tmp[0]);
|
|
|
|
|
memcpy(tmp2.data(), db.data, db.len * sizeof(stream_sample_t));
|
2026-02-24 19:33:36 -06:00
|
|
|
for (uint64_t i = 0; i < db.len; i++) {
|
2022-06-03 01:36:32 +02:00
|
|
|
CHECK(uint16_t(tmp2[i]) == uint16_t(ctr % n_bytes));
|
|
|
|
|
ctr++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
auto db = inputStream_getData(id);
|
|
|
|
|
|
|
|
|
|
CHECK(db.len > 0);
|
|
|
|
|
CHECK(db.data == &tmp[buf_size / 2]);
|
|
|
|
|
memcpy(tmp2.data(), db.data, db.len * sizeof(stream_sample_t));
|
2026-02-24 19:33:36 -06:00
|
|
|
for (uint64_t i = 0; i < db.len; i++) {
|
2022-06-03 01:36:32 +02:00
|
|
|
CHECK(uint16_t(tmp2[i]) == uint16_t(ctr % n_bytes));
|
|
|
|
|
ctr++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-24 19:33:36 -06:00
|
|
|
auto t2 = steady_clock::now();
|
|
|
|
|
const uint64_t delta = duration_cast<microseconds>(t2 - t0).count();
|
2022-06-03 01:36:32 +02:00
|
|
|
const uint64_t expected = (buf_size * n_iter * 1000000lu / 44100);
|
|
|
|
|
CHECK(delta > expected && delta < expected * 2);
|
|
|
|
|
|
|
|
|
|
inputStream_stop(id);
|
|
|
|
|
CHECK(remove(files[fn]) == 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
|
{
|
|
|
|
|
test_linear();
|
|
|
|
|
test_ring_buffer(13, 10, 256);
|
|
|
|
|
test_ring_buffer(128, 10, 256);
|
|
|
|
|
test_ring_buffer(256, 10, 128);
|
|
|
|
|
test_ring_buffer(1234, 10, 768);
|
2022-06-03 01:36:32 +02:00
|
|
|
return 0;
|
2021-04-12 19:16:47 +02:00
|
|
|
}
|