OpenRTX/tests/platform/codec2_encode_test.c

108 lines
2.4 KiB
C
Raw Permalink Normal View History

/*
* SPDX-FileCopyrightText: Copyright 2020-2026 OpenRTX Contributors
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
2021-08-26 10:40:46 +02:00
#include "core/audio_stream.h"
#include "core/audio_path.h"
#include "interfaces/platform.h"
#include "interfaces/delays.h"
#include "core/memory_profiling.h"
#include "interfaces/audio.h"
#include <pthread.h>
#include "codec2.h"
2021-08-26 10:40:46 +02:00
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "core/dsp.h"
2021-08-26 10:40:46 +02:00
static const size_t audioBufSize = 320;
static const size_t dataBufSize = 2 * 1024;
2021-08-26 10:40:46 +02:00
2021-09-01 11:50:22 +02:00
void error()
{
while (1) {
2021-09-01 11:50:22 +02:00
platform_ledOn(RED);
sleepFor(0u, 500u);
platform_ledOff(RED);
sleepFor(0u, 500u);
}
}
2021-08-26 10:40:46 +02:00
void *mic_task(void *arg)
{
(void)arg;
2021-08-26 10:40:46 +02:00
struct CODEC2 *codec2 = codec2_create(CODEC2_MODE_3200);
int16_t *audioBuf = ((int16_t *)malloc(audioBufSize * sizeof(int16_t)));
if (audioBuf == NULL)
error();
uint8_t *dataBuf = ((uint8_t *)malloc(dataBufSize * sizeof(uint8_t)));
2021-08-26 10:40:46 +02:00
memset(dataBuf, 0x00, dataBufSize);
2021-09-01 11:50:22 +02:00
sleepFor(0u, 500u);
2021-08-26 10:40:46 +02:00
pathId path = audioPath_request(SOURCE_MIC, SINK_MCU, PRIO_TX);
streamId id = audioStream_start(path, audioBuf, audioBufSize, 8000,
BUF_CIRC_DOUBLE | STREAM_INPUT);
2021-08-26 10:40:46 +02:00
platform_ledOn(GREEN);
filter_state_t dcr;
2021-08-26 10:40:46 +02:00
size_t pos = 0;
dsp_resetFilterState(&dcr);
while (pos < dataBufSize) {
2021-08-26 10:40:46 +02:00
dataBlock_t data = inputStream_getData(id);
if (data.data == NULL)
error();
2021-08-26 10:40:46 +02:00
2021-09-01 11:50:22 +02:00
// Pre-amplification stage
for (size_t i = 0; i < data.len; i++)
data.data[i] <<= 3;
2021-09-01 11:50:22 +02:00
// DC removal
dsp_dcRemoval(&dcr, data.data, data.len);
2021-09-01 11:50:22 +02:00
// Post-amplification stage
for (size_t i = 0; i < data.len; i++)
data.data[i] *= 20;
2021-09-01 11:50:22 +02:00
2021-08-27 14:20:23 +02:00
codec2_encode(codec2, &dataBuf[pos], data.data);
2021-08-26 10:40:46 +02:00
pos += 8;
}
platform_ledOff(GREEN);
2021-09-01 11:50:22 +02:00
sleepFor(10u, 0u);
2021-08-26 10:40:46 +02:00
platform_ledOn(RED);
for (size_t i = 0; i < dataBufSize; i++) {
2021-08-26 10:40:46 +02:00
iprintf("%02x ", dataBuf[i]);
}
platform_ledOff(RED);
while (1)
;
2021-08-26 10:40:46 +02:00
return 0;
}
int main()
{
platform_init();
// Create mic input thread
pthread_t mic_thread;
2021-08-26 10:40:46 +02:00
pthread_attr_t mic_attr;
pthread_attr_init(&mic_attr);
pthread_attr_setstacksize(&mic_attr, 20 * 1024);
pthread_create(&mic_thread, &mic_attr, mic_task, NULL);
while (1)
;
2021-08-26 10:40:46 +02:00
return 0;
}