Audio added to firmware, Website File handling
This commit is contained in:
5
firmware/libs/audio/CMakeLists.txt
Normal file
5
firmware/libs/audio/CMakeLists.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
if(CONFIG_AUDIO)
|
||||
zephyr_library()
|
||||
zephyr_library_sources(src/audio.c)
|
||||
zephyr_include_directories(include)
|
||||
endif()
|
||||
60
firmware/libs/audio/Kconfig
Normal file
60
firmware/libs/audio/Kconfig
Normal file
@@ -0,0 +1,60 @@
|
||||
menuconfig AUDIO
|
||||
bool "Audio handling"
|
||||
default y
|
||||
select I2S
|
||||
select POLL
|
||||
|
||||
if AUDIO
|
||||
config AUDIO_NO_SAMPLES_SAMPLE
|
||||
string "Audio no samples sample"
|
||||
default "404"
|
||||
help
|
||||
Sound do play when no audio files are available. Must be in the sys directory of the filesystem.
|
||||
config AUDIO_CACHE_SLAB_SIZE
|
||||
int "Audio slab size"
|
||||
default 4096
|
||||
help
|
||||
Audio cache slab size
|
||||
config AUDIO_CACHE_SLAB_COUNT
|
||||
int "Audio slab count"
|
||||
default 4
|
||||
help
|
||||
Number of audio slabs in cache
|
||||
|
||||
config AUDIO_THREAD_STACK_SIZE
|
||||
int "Audio thread stack size"
|
||||
default 4096
|
||||
help
|
||||
Stack size for audio processing thread
|
||||
config AUDIO_THREAD_PRIORITY
|
||||
int "Audio thread priority"
|
||||
default 5
|
||||
help
|
||||
Priority for audio processing thread (lower number = higher priority)
|
||||
|
||||
config AUDIO_PUMP_THREAD_STACK_SIZE
|
||||
int "Audio pump thread stack size"
|
||||
default 8192
|
||||
help
|
||||
Stack size for audio pump thread
|
||||
config AUDIO_PUMP_THREAD_PRIORITY
|
||||
int "Audio pump thread priority"
|
||||
default 4
|
||||
help
|
||||
Priority for audio pump thread (lower number = higher priority)
|
||||
|
||||
config AUDIO_WORKQUEUE_STACK_SIZE
|
||||
int "Audio workqueue stack size"
|
||||
default 2048
|
||||
help
|
||||
Stack size for audio workqueue
|
||||
config AUDIO_WORKQUEUE_PRIORITY
|
||||
int "Audio workqueue priority"
|
||||
default 10
|
||||
help
|
||||
Priority for audio workqueue (lower number = higher priority)
|
||||
|
||||
module = AUDIO
|
||||
module-str = audio
|
||||
source "subsys/logging/Kconfig.template.log_config"
|
||||
endif # AUDIO
|
||||
33
firmware/libs/audio/include/audio.h
Normal file
33
firmware/libs/audio/include/audio.h
Normal file
@@ -0,0 +1,33 @@
|
||||
#ifndef AUDIO_H
|
||||
#define AUDIO_H
|
||||
#include <zephyr/kernel.h>
|
||||
|
||||
#include "fs_mgmt.h"
|
||||
|
||||
/** Audio command message structure */
|
||||
struct audio_cmd_msg
|
||||
{
|
||||
char filename[CONFIG_FS_MGMT_MAX_PATH_LENGTH]; // Wenn leer, nutze Fallback-Sound
|
||||
bool is_interrupt; // True = sofort abbrechen, False = Enqueue
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Queues an audio playback command to the audio thread
|
||||
* @param filename Name of the audio file to play. If empty, a random sound will be played
|
||||
* @param is_interrupt If true, the command will interrupt any currently playing audio. If
|
||||
* false, it will be enqueued and played after the current audio finishes
|
||||
* @return 0 on success, negative error code on failure
|
||||
*/
|
||||
int audio_queue_play(const char *filename, bool is_interrupt);
|
||||
|
||||
/**
|
||||
* @brief Starts playback of a random audio file from the audio directory. This is a non-blocking call that signals the audio thread to select and play a random sound.
|
||||
*/
|
||||
void audio_start_random_playback(void);
|
||||
|
||||
/**
|
||||
* @brief Refreshes the list of available audio files
|
||||
*/
|
||||
void audio_refresh_files(void);
|
||||
|
||||
#endif /* AUDIO_H */
|
||||
622
firmware/libs/audio/src/audio.c
Normal file
622
firmware/libs/audio/src/audio.c
Normal file
@@ -0,0 +1,622 @@
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
#include <zephyr/fs/fs.h>
|
||||
#include <zephyr/drivers/i2s.h>
|
||||
|
||||
#include "audio.h"
|
||||
#include "fs_mgmt.h"
|
||||
#include "event_mgmt.h"
|
||||
|
||||
LOG_MODULE_REGISTER(audio, CONFIG_AUDIO_LOG_LEVEL);
|
||||
|
||||
const struct device *i2s_dev = DEVICE_DT_GET(DT_ALIAS(i2s_audio));
|
||||
|
||||
K_MSGQ_DEFINE(audio_cmd_q, sizeof(struct audio_cmd_msg), 10, 4);
|
||||
K_SEM_DEFINE(audio_files_count_sem, 0, 1);
|
||||
K_SEM_DEFINE(audio_file_select_sem, 0, 1);
|
||||
|
||||
K_MEM_SLAB_DEFINE(audio_cache_slab, CONFIG_AUDIO_CACHE_SLAB_SIZE, CONFIG_AUDIO_CACHE_SLAB_COUNT, 4);
|
||||
|
||||
struct k_work_q audio_work_q;
|
||||
K_THREAD_STACK_DEFINE(audio_work_q_stack, CONFIG_AUDIO_WORKQUEUE_STACK_SIZE);
|
||||
struct k_work select_next_file_work;
|
||||
|
||||
enum audio_thread_state_t
|
||||
{
|
||||
AUDIO_ARMED,
|
||||
AUDIO_PRECACHING,
|
||||
AUDIO_WAIT_FOR_CACHE,
|
||||
AUDIO_PLAYING,
|
||||
AUDIO_DRAINING,
|
||||
};
|
||||
|
||||
#define EV_PLAY_RANDOM BIT(0)
|
||||
#define EV_MSGQ_NOT_EMPTY BIT(1)
|
||||
#define EV_CACHE_READY BIT(2)
|
||||
#define EV_CACHE_DONE BIT(3)
|
||||
#define EV_STATE_STEP BIT(4)
|
||||
#define EV_AUTOSTART BIT(5)
|
||||
#define EV_ALL (EV_PLAY_RANDOM | EV_MSGQ_NOT_EMPTY | EV_CACHE_READY | EV_CACHE_DONE | EV_STATE_STEP | EV_AUTOSTART)
|
||||
|
||||
K_EVENT_DEFINE(audio_events);
|
||||
|
||||
#define AUDIO_CACHE_EVT_START BIT(0)
|
||||
#define AUDIO_CACHE_EVT_STOP BIT(1)
|
||||
|
||||
K_EVENT_DEFINE(audio_cache_event);
|
||||
|
||||
struct audio_ctx_t
|
||||
{
|
||||
char next_file_name[CONFIG_FS_MGMT_MAX_PATH_LENGTH];
|
||||
struct fs_file_t file;
|
||||
bool is_file_open;
|
||||
ssize_t audio_size;
|
||||
ssize_t cached_bytes;
|
||||
} audio_ctx;
|
||||
|
||||
static struct i2s_config i2s_cfg = {
|
||||
.word_size = 16,
|
||||
.channels = 2,
|
||||
.format = I2S_FMT_DATA_FORMAT_I2S,
|
||||
.options = I2S_OPT_BIT_CLK_MASTER | I2S_OPT_FRAME_CLK_MASTER,
|
||||
.frame_clk_freq = 16000,
|
||||
.mem_slab = &audio_cache_slab,
|
||||
.block_size = CONFIG_AUDIO_CACHE_SLAB_SIZE,
|
||||
.timeout = SYS_FOREVER_MS,
|
||||
};
|
||||
|
||||
K_MUTEX_DEFINE(audio_ctx_mutex);
|
||||
|
||||
atomic_t thread_state = ATOMIC_INIT(0);
|
||||
atomic_t num_files = ATOMIC_INIT(0);
|
||||
|
||||
static uint8_t audio_mono_stage[CONFIG_AUDIO_CACHE_SLAB_SIZE / 2];
|
||||
|
||||
int audio_queue_play(const char *filename, bool is_interrupt)
|
||||
{
|
||||
if (is_interrupt)
|
||||
{
|
||||
/* Keep hardware state changes inside audio_thread to avoid cross-thread races. */
|
||||
k_msgq_purge(&audio_cmd_q);
|
||||
}
|
||||
|
||||
if (strlen(filename) == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (k_msgq_num_free_get(&audio_cmd_q) == 0)
|
||||
{
|
||||
LOG_ERR("Audio command queue is full, cannot enqueue new command");
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
struct audio_cmd_msg cmd;
|
||||
strncpy(cmd.filename, filename, sizeof(cmd.filename) - 1);
|
||||
cmd.filename[sizeof(cmd.filename) - 1] = '\0';
|
||||
cmd.is_interrupt = is_interrupt;
|
||||
|
||||
if (k_msgq_put(&audio_cmd_q, &cmd, K_FOREVER) != 0)
|
||||
{
|
||||
LOG_ERR("Failed to enqueue audio command");
|
||||
return -EFAULT;
|
||||
}
|
||||
|
||||
/*
|
||||
* Wake immediately for interrupts or when not currently playing.
|
||||
* Non-interrupt commands during playback are picked up after drain.
|
||||
*/
|
||||
if (is_interrupt || (atomic_get(&thread_state) != AUDIO_PLAYING))
|
||||
{
|
||||
k_event_set(&audio_events, EV_MSGQ_NOT_EMPTY);
|
||||
}
|
||||
|
||||
LOG_DBG("Enqueued audio command: filename='%s', is_interrupt=%d", cmd.filename, cmd.is_interrupt);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int audio_select_random_sound(void)
|
||||
{
|
||||
k_sem_reset(&audio_file_select_sem);
|
||||
if (k_sem_take(&audio_files_count_sem, K_FOREVER) != 0)
|
||||
{
|
||||
LOG_ERR("Failed to take audio files count semaphore");
|
||||
k_sem_give(&audio_files_count_sem);
|
||||
k_mutex_unlock(&audio_ctx_mutex);
|
||||
return -EFAULT;
|
||||
}
|
||||
|
||||
int count = atomic_get(&num_files);
|
||||
|
||||
k_mutex_lock(&audio_ctx_mutex, K_FOREVER);
|
||||
if (count == 0)
|
||||
{
|
||||
LOG_WRN("No audio files available to select, returning no files sound");
|
||||
FS_MGMT_ASSEMBLE_PATH(audio_ctx.next_file_name, FS_SYSTEM_PATH, CONFIG_AUDIO_NO_SAMPLES_SAMPLE);
|
||||
|
||||
k_sem_give(&audio_file_select_sem);
|
||||
k_sem_give(&audio_files_count_sem);
|
||||
k_mutex_unlock(&audio_ctx_mutex);
|
||||
return -ENOENT;
|
||||
}
|
||||
|
||||
int random_index = k_cycle_get_32() % count;
|
||||
struct fs_dir_t dir;
|
||||
struct fs_dirent entry;
|
||||
int rc;
|
||||
|
||||
fs_dir_t_init(&dir);
|
||||
rc = fs_mgmt_pm_opendir(&dir, FS_AUDIO_PATH);
|
||||
if (rc < 0)
|
||||
{
|
||||
LOG_ERR("Failed to open audio directory '%s': %d", FS_AUDIO_PATH, rc);
|
||||
k_sem_give(&audio_file_select_sem);
|
||||
k_sem_give(&audio_files_count_sem);
|
||||
k_mutex_unlock(&audio_ctx_mutex);
|
||||
return rc;
|
||||
}
|
||||
|
||||
int current_index = 0;
|
||||
bool found = false;
|
||||
while (1)
|
||||
{
|
||||
rc = fs_readdir(&dir, &entry);
|
||||
if (rc < 0)
|
||||
{
|
||||
LOG_ERR("Directory read error: %d", rc);
|
||||
break;
|
||||
}
|
||||
if (entry.name[0] == '\0')
|
||||
{
|
||||
break;
|
||||
}
|
||||
if (entry.type == FS_DIR_ENTRY_FILE)
|
||||
{
|
||||
if (current_index == random_index)
|
||||
{
|
||||
FS_MGMT_ASSEMBLE_PATH(audio_ctx.next_file_name, FS_AUDIO_PATH, entry.name);
|
||||
LOG_DBG("Selected random audio file: %s", audio_ctx.next_file_name);
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
current_index++;
|
||||
}
|
||||
}
|
||||
|
||||
fs_mgmt_pm_closedir(&dir);
|
||||
k_sem_give(&audio_file_select_sem);
|
||||
k_sem_give(&audio_files_count_sem);
|
||||
k_mutex_unlock(&audio_ctx_mutex);
|
||||
return found ? 0 : -ENOENT;
|
||||
}
|
||||
|
||||
void audio_refresh_files(void)
|
||||
{
|
||||
// Lokale Strukturen verwenden, um Reentrancy-Probleme zu vermeiden
|
||||
struct fs_dir_t dir;
|
||||
struct fs_dirent entry;
|
||||
int count = 0;
|
||||
int rc;
|
||||
|
||||
k_sem_reset(&audio_files_count_sem);
|
||||
|
||||
fs_dir_t_init(&dir);
|
||||
// Nutze deinen PM-Wrapper für den Flash-Zugriff
|
||||
rc = fs_mgmt_pm_opendir(&dir, FS_AUDIO_PATH);
|
||||
if (rc < 0)
|
||||
{
|
||||
LOG_ERR("Failed to open audio directory '%s': %d", FS_AUDIO_PATH, rc);
|
||||
atomic_set(&num_files, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
while (1)
|
||||
{
|
||||
rc = fs_readdir(&dir, &entry);
|
||||
if (rc < 0)
|
||||
{
|
||||
LOG_ERR("Directory read error: %d", rc);
|
||||
break;
|
||||
}
|
||||
if (entry.name[0] == '\0')
|
||||
{
|
||||
break;
|
||||
}
|
||||
if (entry.type == FS_DIR_ENTRY_FILE)
|
||||
{
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
fs_mgmt_pm_closedir(&dir);
|
||||
k_sem_give(&audio_files_count_sem);
|
||||
atomic_set(&num_files, count);
|
||||
audio_select_random_sound();
|
||||
}
|
||||
|
||||
static void select_next_file_work_handler(struct k_work *work)
|
||||
{
|
||||
ARG_UNUSED(work);
|
||||
LOG_DBG("Select next file work handler");
|
||||
audio_select_random_sound();
|
||||
}
|
||||
|
||||
static int audio_init(void)
|
||||
{
|
||||
struct k_work_queue_config audio_work_q_config = {
|
||||
.name = "audio_work_q",
|
||||
.no_yield = false,
|
||||
.essential = true,
|
||||
.work_timeout_ms = 0};
|
||||
|
||||
k_work_queue_start(&audio_work_q,
|
||||
audio_work_q_stack,
|
||||
K_THREAD_STACK_SIZEOF(audio_work_q_stack),
|
||||
CONFIG_AUDIO_WORKQUEUE_PRIORITY, &audio_work_q_config);
|
||||
|
||||
k_work_init(&select_next_file_work, select_next_file_work_handler);
|
||||
|
||||
if (!device_is_ready(i2s_dev))
|
||||
{
|
||||
LOG_ERR("I2S device not ready");
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
int rc = i2s_configure(i2s_dev, I2S_DIR_TX, &i2s_cfg);
|
||||
if (rc < 0)
|
||||
{
|
||||
LOG_ERR("Failed to configure I2S: %d", rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
LOG_DBG("Audio module initialized");
|
||||
return 0;
|
||||
}
|
||||
|
||||
SYS_INIT(audio_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY); // Stelle sicher, dass dies nach der FS-Initialisierung erfolgt
|
||||
|
||||
static void audio_trigger_next_file_selection(void)
|
||||
{
|
||||
k_sem_reset(&audio_file_select_sem);
|
||||
LOG_DBG("Triggering workq file selection");
|
||||
k_work_submit_to_queue(&audio_work_q, &select_next_file_work);
|
||||
}
|
||||
|
||||
void audio_start_random_playback(void)
|
||||
{
|
||||
LOG_DBG("audio_start_random_playback called");
|
||||
k_event_set(&audio_events, EV_PLAY_RANDOM);
|
||||
}
|
||||
|
||||
void audio_thread(void *arg1, void *arg2, void *arg3)
|
||||
{
|
||||
ARG_UNUSED(arg1);
|
||||
ARG_UNUSED(arg2);
|
||||
ARG_UNUSED(arg3);
|
||||
|
||||
k_event_wait(&event_mgmt_events, EVENT_MGMT_FS_READY, false, K_FOREVER);
|
||||
|
||||
audio_refresh_files();
|
||||
atomic_set(&thread_state, AUDIO_PRECACHING);
|
||||
k_event_set(&audio_events, EV_STATE_STEP);
|
||||
|
||||
while (1)
|
||||
{
|
||||
enum audio_thread_state_t state = atomic_get(&thread_state);
|
||||
k_timeout_t timeout = (state == AUDIO_DRAINING) ? K_MSEC(10) : K_FOREVER;
|
||||
|
||||
uint32_t active_events = k_event_wait(&audio_events, EV_ALL, false, timeout);
|
||||
|
||||
if (active_events & EV_STATE_STEP)
|
||||
{
|
||||
k_event_clear(&audio_events, EV_STATE_STEP);
|
||||
}
|
||||
|
||||
if (active_events & EV_PLAY_RANDOM)
|
||||
{
|
||||
LOG_DBG("Play random event received");
|
||||
k_event_clear(&audio_events, EV_PLAY_RANDOM);
|
||||
|
||||
if (state == AUDIO_ARMED)
|
||||
{
|
||||
i2s_trigger(i2s_dev, I2S_DIR_TX, I2S_TRIGGER_START);
|
||||
atomic_set(&thread_state, AUDIO_PLAYING);
|
||||
}
|
||||
else
|
||||
{
|
||||
k_event_set(&audio_cache_event, AUDIO_CACHE_EVT_STOP);
|
||||
i2s_trigger(i2s_dev, I2S_DIR_TX, I2S_TRIGGER_DROP);
|
||||
|
||||
audio_select_random_sound();
|
||||
atomic_set(&thread_state, AUDIO_PRECACHING);
|
||||
k_event_set(&audio_events, EV_STATE_STEP);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
switch (state)
|
||||
{
|
||||
case AUDIO_PRECACHING:
|
||||
LOG_DBG("Audio thread starting precache task");
|
||||
k_event_set(&audio_cache_event, AUDIO_CACHE_EVT_START);
|
||||
atomic_set(&thread_state, AUDIO_WAIT_FOR_CACHE);
|
||||
break;
|
||||
|
||||
case AUDIO_WAIT_FOR_CACHE:
|
||||
if (active_events & EV_CACHE_READY)
|
||||
{
|
||||
k_event_clear(&audio_events, EV_CACHE_READY);
|
||||
atomic_set(&thread_state, AUDIO_ARMED);
|
||||
|
||||
if (k_event_wait(&audio_events, EV_AUTOSTART, false, K_NO_WAIT) & EV_AUTOSTART)
|
||||
{
|
||||
k_event_clear(&audio_events, EV_AUTOSTART);
|
||||
i2s_trigger(i2s_dev, I2S_DIR_TX, I2S_TRIGGER_START);
|
||||
atomic_set(&thread_state, AUDIO_PLAYING);
|
||||
LOG_DBG("Autostarting queued audio playback");
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_DBG("System Armed. Waiting for Buzzer...");
|
||||
audio_trigger_next_file_selection();
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case AUDIO_ARMED:
|
||||
if (active_events & EV_MSGQ_NOT_EMPTY)
|
||||
{
|
||||
k_event_clear(&audio_events, EV_MSGQ_NOT_EMPTY);
|
||||
i2s_trigger(i2s_dev, I2S_DIR_TX, I2S_TRIGGER_DROP);
|
||||
k_event_set(&audio_cache_event, AUDIO_CACHE_EVT_STOP);
|
||||
|
||||
struct audio_cmd_msg cmd;
|
||||
k_msgq_get(&audio_cmd_q, &cmd, K_NO_WAIT);
|
||||
|
||||
k_mutex_lock(&audio_ctx_mutex, K_FOREVER);
|
||||
strncpy(audio_ctx.next_file_name, cmd.filename, sizeof(audio_ctx.next_file_name) - 1);
|
||||
audio_ctx.next_file_name[sizeof(audio_ctx.next_file_name) - 1] = '\0';
|
||||
k_mutex_unlock(&audio_ctx_mutex);
|
||||
|
||||
k_event_set(&audio_events, EV_AUTOSTART);
|
||||
atomic_set(&thread_state, AUDIO_PRECACHING);
|
||||
k_event_set(&audio_events, EV_STATE_STEP);
|
||||
}
|
||||
break;
|
||||
|
||||
case AUDIO_PLAYING:
|
||||
if (active_events & EV_MSGQ_NOT_EMPTY)
|
||||
{
|
||||
k_event_clear(&audio_events, EV_MSGQ_NOT_EMPTY);
|
||||
|
||||
struct audio_cmd_msg cmd;
|
||||
if (k_msgq_peek(&audio_cmd_q, &cmd) == 0)
|
||||
{
|
||||
if (cmd.is_interrupt)
|
||||
{
|
||||
i2s_trigger(i2s_dev, I2S_DIR_TX, I2S_TRIGGER_DROP);
|
||||
k_event_set(&audio_cache_event, AUDIO_CACHE_EVT_STOP);
|
||||
|
||||
if (k_msgq_get(&audio_cmd_q, &cmd, K_NO_WAIT) == 0)
|
||||
{
|
||||
k_mutex_lock(&audio_ctx_mutex, K_FOREVER);
|
||||
strncpy(audio_ctx.next_file_name, cmd.filename, sizeof(audio_ctx.next_file_name) - 1);
|
||||
audio_ctx.next_file_name[sizeof(audio_ctx.next_file_name) - 1] = '\0';
|
||||
k_mutex_unlock(&audio_ctx_mutex);
|
||||
|
||||
k_event_set(&audio_events, EV_AUTOSTART);
|
||||
atomic_set(&thread_state, AUDIO_PRECACHING);
|
||||
k_event_set(&audio_events, EV_STATE_STEP);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_DBG("Non-interrupt command queued during playback; will process after drain");
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (active_events & EV_CACHE_DONE)
|
||||
{
|
||||
k_event_clear(&audio_events, EV_CACHE_DONE);
|
||||
i2s_trigger(i2s_dev, I2S_DIR_TX, I2S_TRIGGER_DRAIN);
|
||||
atomic_set(&thread_state, AUDIO_DRAINING);
|
||||
}
|
||||
break;
|
||||
|
||||
case AUDIO_DRAINING:
|
||||
if (k_mem_slab_num_free_get(&audio_cache_slab) == CONFIG_AUDIO_CACHE_SLAB_COUNT)
|
||||
{
|
||||
LOG_DBG("Audio for file drained, ready for next file");
|
||||
if (k_msgq_num_used_get(&audio_cmd_q) > 0)
|
||||
{
|
||||
k_event_set(&audio_events, EV_MSGQ_NOT_EMPTY);
|
||||
}
|
||||
else
|
||||
{
|
||||
atomic_set(&thread_state, AUDIO_PRECACHING);
|
||||
k_event_set(&audio_events, EV_STATE_STEP);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
K_THREAD_DEFINE(audio_thread_id, CONFIG_AUDIO_THREAD_STACK_SIZE, audio_thread, NULL, NULL, NULL,
|
||||
CONFIG_AUDIO_THREAD_PRIORITY, 0, 0);
|
||||
|
||||
void audio_pump_thread(void *arg1, void *arg2, void *arg3)
|
||||
{
|
||||
ARG_UNUSED(arg1);
|
||||
ARG_UNUSED(arg2);
|
||||
ARG_UNUSED(arg3);
|
||||
|
||||
uint8_t num_channels = 1;
|
||||
void *mem_slab;
|
||||
while (1)
|
||||
{
|
||||
uint32_t events = k_event_wait(&audio_cache_event, AUDIO_CACHE_EVT_START | AUDIO_CACHE_EVT_STOP, false, K_FOREVER);
|
||||
|
||||
if (events & AUDIO_CACHE_EVT_STOP)
|
||||
{
|
||||
k_mutex_lock(&audio_ctx_mutex, K_FOREVER);
|
||||
if (audio_ctx.is_file_open)
|
||||
{
|
||||
fs_close(&audio_ctx.file);
|
||||
audio_ctx.is_file_open = false;
|
||||
}
|
||||
k_mutex_unlock(&audio_ctx_mutex);
|
||||
|
||||
k_event_clear(&audio_cache_event, AUDIO_CACHE_EVT_START | AUDIO_CACHE_EVT_STOP);
|
||||
continue;
|
||||
}
|
||||
|
||||
int rc = k_mem_slab_alloc(&audio_cache_slab, &mem_slab, K_NO_WAIT);
|
||||
if (rc == -ENOMEM)
|
||||
{
|
||||
k_event_set(&audio_events, EV_CACHE_READY);
|
||||
rc = k_mem_slab_alloc(&audio_cache_slab, &mem_slab, K_FOREVER);
|
||||
if (k_event_wait(&audio_cache_event, AUDIO_CACHE_EVT_STOP, false, K_NO_WAIT))
|
||||
{
|
||||
k_mem_slab_free(&audio_cache_slab, &mem_slab);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (rc == 0)
|
||||
{
|
||||
k_mutex_lock(&audio_ctx_mutex, K_FOREVER);
|
||||
|
||||
if (!audio_ctx.is_file_open)
|
||||
{
|
||||
rc = fs_mgmt_pm_open(&audio_ctx.file, audio_ctx.next_file_name, FS_O_READ);
|
||||
if (rc < 0)
|
||||
{
|
||||
LOG_ERR("Failed to open audio file '%s': %d", audio_ctx.next_file_name, rc);
|
||||
k_mem_slab_free(&audio_cache_slab, &mem_slab);
|
||||
k_event_clear(&audio_cache_event, AUDIO_CACHE_EVT_START);
|
||||
k_mutex_unlock(&audio_ctx_mutex);
|
||||
continue;
|
||||
}
|
||||
|
||||
audio_ctx.is_file_open = true;
|
||||
|
||||
// NEU: Größen- und Zähler-Initialisierung exklusiv hier!
|
||||
audio_ctx.audio_size = fs_mgmt_get_audio_data_len(&audio_ctx.file);
|
||||
audio_ctx.cached_bytes = 0;
|
||||
|
||||
LOG_DBG("Audio file '%s' opened for caching, size: %d", audio_ctx.next_file_name, (int)audio_ctx.audio_size);
|
||||
}
|
||||
|
||||
if (audio_ctx.audio_size <= 0)
|
||||
{
|
||||
LOG_ERR("Invalid audio size for '%s': %d", audio_ctx.next_file_name, (int)audio_ctx.audio_size);
|
||||
k_mem_slab_free(&audio_cache_slab, &mem_slab);
|
||||
fs_close(&audio_ctx.file);
|
||||
audio_ctx.is_file_open = false;
|
||||
k_event_clear(&audio_cache_event, AUDIO_CACHE_EVT_START);
|
||||
k_mutex_unlock(&audio_ctx_mutex);
|
||||
continue;
|
||||
}
|
||||
|
||||
ssize_t remaining_bytes = audio_ctx.audio_size - audio_ctx.cached_bytes;
|
||||
if (remaining_bytes <= 0)
|
||||
{
|
||||
k_mem_slab_free(&audio_cache_slab, &mem_slab);
|
||||
fs_close(&audio_ctx.file);
|
||||
audio_ctx.is_file_open = false;
|
||||
k_event_clear(&audio_cache_event, AUDIO_CACHE_EVT_START);
|
||||
k_mutex_unlock(&audio_ctx_mutex);
|
||||
k_event_set(&audio_events, EV_CACHE_DONE);
|
||||
continue;
|
||||
}
|
||||
|
||||
ssize_t bytes_to_read = MIN(CONFIG_AUDIO_CACHE_SLAB_SIZE / 2, remaining_bytes);
|
||||
ssize_t bytes_read = fs_read(&audio_ctx.file, audio_mono_stage, bytes_to_read);
|
||||
|
||||
if (bytes_read <= 0) // <= 0, um EOF (0) und Fehler (< 0) abzufangen
|
||||
{
|
||||
if (bytes_read < 0)
|
||||
{
|
||||
LOG_ERR("Failed to read audio data: %d", (int)bytes_read);
|
||||
}
|
||||
k_mem_slab_free(&audio_cache_slab, &mem_slab);
|
||||
|
||||
// EOF erreicht -> Datei schließen und START-Bit löschen
|
||||
fs_close(&audio_ctx.file);
|
||||
audio_ctx.is_file_open = false;
|
||||
k_event_clear(&audio_cache_event, AUDIO_CACHE_EVT_START);
|
||||
k_mutex_unlock(&audio_ctx_mutex);
|
||||
k_event_set(&audio_events, EV_CACHE_DONE);
|
||||
continue;
|
||||
}
|
||||
|
||||
audio_ctx.cached_bytes += bytes_read;
|
||||
// LOG_DBG("Cached %u%% ", (int)((audio_ctx.cached_bytes * 100) / audio_ctx.audio_size));
|
||||
|
||||
if (bytes_read > CONFIG_AUDIO_CACHE_SLAB_SIZE / 2)
|
||||
{
|
||||
LOG_ERR("Read size %d exceeds half slab size %d", (int)bytes_read, CONFIG_AUDIO_CACHE_SLAB_SIZE / 2);
|
||||
k_mem_slab_free(&audio_cache_slab, &mem_slab);
|
||||
fs_close(&audio_ctx.file);
|
||||
audio_ctx.is_file_open = false;
|
||||
k_event_clear(&audio_cache_event, AUDIO_CACHE_EVT_START);
|
||||
k_mutex_unlock(&audio_ctx_mutex);
|
||||
continue;
|
||||
}
|
||||
|
||||
if ((bytes_read & 0x1) != 0)
|
||||
{
|
||||
if (bytes_read >= (CONFIG_AUDIO_CACHE_SLAB_SIZE / 2))
|
||||
{
|
||||
LOG_ERR("Odd mono byte count at half-slab boundary: %d", (int)bytes_read);
|
||||
k_mem_slab_free(&audio_cache_slab, &mem_slab);
|
||||
fs_close(&audio_ctx.file);
|
||||
audio_ctx.is_file_open = false;
|
||||
k_event_clear(&audio_cache_event, AUDIO_CACHE_EVT_START);
|
||||
k_mutex_unlock(&audio_ctx_mutex);
|
||||
continue;
|
||||
}
|
||||
|
||||
audio_mono_stage[bytes_read] = 0;
|
||||
bytes_read++;
|
||||
}
|
||||
|
||||
if (bytes_read < CONFIG_AUDIO_CACHE_SLAB_SIZE / 2)
|
||||
{
|
||||
memset(audio_mono_stage + bytes_read, 0, (CONFIG_AUDIO_CACHE_SLAB_SIZE / 2) - bytes_read);
|
||||
bytes_read = CONFIG_AUDIO_CACHE_SLAB_SIZE / 2;
|
||||
}
|
||||
|
||||
if (num_channels == 1)
|
||||
{
|
||||
uint8_t *dst = (uint8_t *)mem_slab;
|
||||
|
||||
for (size_t i = 0; i < (size_t)bytes_read; i += 2)
|
||||
{
|
||||
uint8_t lo = audio_mono_stage[i];
|
||||
uint8_t hi = audio_mono_stage[i + 1];
|
||||
size_t out = i * 2;
|
||||
|
||||
dst[out] = lo;
|
||||
dst[out + 1] = hi;
|
||||
dst[out + 2] = lo;
|
||||
dst[out + 3] = hi;
|
||||
}
|
||||
}
|
||||
|
||||
k_mutex_unlock(&audio_ctx_mutex);
|
||||
if (i2s_write(i2s_dev, mem_slab, CONFIG_AUDIO_CACHE_SLAB_SIZE) < 0)
|
||||
{
|
||||
LOG_ERR("Failed to write audio data to I2S");
|
||||
k_mem_slab_free(&audio_cache_slab, &mem_slab);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
K_THREAD_DEFINE(audio_cache_thread_id, CONFIG_AUDIO_PUMP_THREAD_STACK_SIZE, audio_pump_thread, NULL, NULL, NULL,
|
||||
CONFIG_AUDIO_PUMP_THREAD_PRIORITY, 0, 0);
|
||||
Reference in New Issue
Block a user