From 76d9da07891056cba86ff40f1e4ae593b5f13f46 Mon Sep 17 00:00:00 2001 From: Zihao Yu Date: Wed, 30 Sep 2020 22:01:33 +0800 Subject: [PATCH] amdev,audio: make bufsize unconfigurable --- am/include/amdev.h | 4 ++-- am/src/native/ioe.c | 2 +- am/src/native/native-audio.c | 22 ++++++++++++---------- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/am/include/amdev.h b/am/include/amdev.h index fb4300f..cd4f61b 100644 --- a/am/include/amdev.h +++ b/am/include/amdev.h @@ -17,8 +17,8 @@ AM_DEVREG(10, GPU_STATUS, RD, bool ready); AM_DEVREG(11, GPU_FBDRAW, WR, int x, y; void *pixels; int w, h; bool sync); AM_DEVREG(12, GPU_MEMCPY, WR, uint32_t dest; void *src; int size); AM_DEVREG(13, GPU_RENDER, WR, uint32_t root); -AM_DEVREG(14, AUDIO_CONFIG, RD, bool present); -AM_DEVREG(15, AUDIO_CTRL, WR, int freq, channels, samples, bufsize); +AM_DEVREG(14, AUDIO_CONFIG, RD, bool present; int bufsize); +AM_DEVREG(15, AUDIO_CTRL, WR, int freq, channels, samples); AM_DEVREG(16, AUDIO_STATUS, RD, int count); AM_DEVREG(17, AUDIO_PLAY, WR, Area buf); AM_DEVREG(18, DISK_CONFIG, RD, bool present; int blksz, blkcnt); diff --git a/am/src/native/ioe.c b/am/src/native/ioe.c index 4e7b19d..4015c16 100644 --- a/am/src/native/ioe.c +++ b/am/src/native/ioe.c @@ -15,11 +15,11 @@ void __am_input_keybrd(AM_INPUT_KEYBRD_T *); void __am_gpu_config(AM_GPU_CONFIG_T *); void __am_gpu_status(AM_GPU_STATUS_T *); void __am_gpu_fbdraw(AM_GPU_FBDRAW_T *); +void __am_audio_config(AM_AUDIO_CONFIG_T *); void __am_audio_ctrl(AM_AUDIO_CTRL_T *); void __am_audio_status(AM_AUDIO_STATUS_T *); void __am_audio_play(AM_AUDIO_PLAY_T *); static void __am_uart_config(AM_UART_CONFIG_T *cfg) { cfg->present = false; } -static void __am_audio_config(AM_AUDIO_CONFIG_T *cfg) { cfg->present = true; } static void __am_disk_config(AM_DISK_CONFIG_T *cfg) { cfg->present = false; } static void __am_net_config (AM_NET_CONFIG_T *cfg) { cfg->present = false; } diff --git a/am/src/native/native-audio.c b/am/src/native/native-audio.c index f3e2854..59e6851 100644 --- a/am/src/native/native-audio.c +++ b/am/src/native/native-audio.c @@ -3,7 +3,6 @@ #define SBUF_SIZE_MAX 65536 static uint8_t sbuf [SBUF_SIZE_MAX] = {}; -static int sbuf_size = 0; static int head = 0, tail = 0; static volatile int count = 0; @@ -11,11 +10,11 @@ static void audio_play(void *userdata, uint8_t *stream, int len) { int nread = len; if (count < len) nread = count; - if (nread + tail < sbuf_size) { + if (nread + tail < SBUF_SIZE_MAX) { memcpy(stream, sbuf + tail, nread); tail += nread; } else { - int first_cpy_len = sbuf_size - tail; + int first_cpy_len = SBUF_SIZE_MAX - tail; memcpy(stream, sbuf + tail, first_cpy_len); memcpy(stream + first_cpy_len, sbuf, nread - first_cpy_len); tail = nread - first_cpy_len; @@ -25,15 +24,15 @@ static void audio_play(void *userdata, uint8_t *stream, int len) { } static int audio_write(uint8_t *buf, int len) { - int free = sbuf_size - count; + int free = SBUF_SIZE_MAX - count; int nwrite = len; if (free < len) nwrite = free; - if (nwrite + head < sbuf_size) { + if (nwrite + head < SBUF_SIZE_MAX) { memcpy(sbuf + head, buf, nwrite); head += nwrite; } else { - int first_cpy_len = sbuf_size - head; + int first_cpy_len = SBUF_SIZE_MAX - head; memcpy(sbuf + head, buf, first_cpy_len); memcpy(sbuf, buf + first_cpy_len, nwrite - first_cpy_len); head = nwrite - first_cpy_len; @@ -50,8 +49,6 @@ void __am_audio_ctrl(AM_AUDIO_CTRL_T *ctrl) { s.samples = ctrl->samples; s.callback = audio_play; s.userdata = NULL; - sbuf_size = ctrl->bufsize; - assert(sbuf_size <= SBUF_SIZE_MAX); head = tail = 0; count = 0; @@ -68,7 +65,12 @@ void __am_audio_status(AM_AUDIO_STATUS_T *stat) { void __am_audio_play(AM_AUDIO_PLAY_T *ctl) { int len = ctl->buf.end - ctl->buf.start; - assert(len <= sbuf_size); - while (sbuf_size - count < len); + assert(len <= SBUF_SIZE_MAX); + while (SBUF_SIZE_MAX - count < len); audio_write(ctl->buf.start, len); } + +void __am_audio_config(AM_AUDIO_CONFIG_T *cfg) { + cfg->present = true; + cfg->bufsize = SBUF_SIZE_MAX; +}