From 3d4213e9432f57f280140dccd7a305282c58be35 Mon Sep 17 00:00:00 2001 From: WangChang Date: Wed, 7 Jun 2023 17:36:53 +0800 Subject: [PATCH] [Fix] Fix the issue with playback speed. --- drivers/drivers-x2600/include/x2600_hal_aic.h | 2 +- drivers/drivers-x2600/src/x2600_hal_aic.c | 39 ++++++++++++++++--- projects/x2660-halley/Examples/icodec/main.c | 12 +++--- 3 files changed, 42 insertions(+), 11 deletions(-) diff --git a/drivers/drivers-x2600/include/x2600_hal_aic.h b/drivers/drivers-x2600/include/x2600_hal_aic.h index 8949dfd5..c231251d 100644 --- a/drivers/drivers-x2600/include/x2600_hal_aic.h +++ b/drivers/drivers-x2600/include/x2600_hal_aic.h @@ -349,7 +349,7 @@ typedef struct __AIC_HandleTypeDef { HAL_StatusTypeDef HAL_AIC_Init(AIC_HandleTypeDef *haic); HAL_StatusTypeDef HAL_AIC_DeInit(AIC_HandleTypeDef *haic); -void HAL_AIC_PlaybackWriteFrame(AIC_HandleTypeDef *haic, unsigned int *buf, unsigned int len); +void HAL_AIC_PlaybackWriteFrame(AIC_HandleTypeDef *haic, void *buf, unsigned int frame_num); void HAL_AIC_PlaybackEnable(AIC_HandleTypeDef *haic, HAL_AIC_PowerGpioTypeDef pin); void HAL_AIC_PlaybackDisable(AIC_HandleTypeDef *haic, HAL_AIC_PowerGpioTypeDef pin); void HAL_AIC_PlaybackStart(AIC_HandleTypeDef *haic); diff --git a/drivers/drivers-x2600/src/x2600_hal_aic.c b/drivers/drivers-x2600/src/x2600_hal_aic.c index 64092d71..b8af1924 100644 --- a/drivers/drivers-x2600/src/x2600_hal_aic.c +++ b/drivers/drivers-x2600/src/x2600_hal_aic.c @@ -407,13 +407,34 @@ HAL_StatusTypeDef HAL_AIC_DeInit(AIC_HandleTypeDef *haic) * @brief AIC 写数据函数 * * @param haic aic句柄 + * @param buf 采样数据 + * @param frame_num 帧个数 */ -void HAL_AIC_PlaybackWriteFrame(AIC_HandleTypeDef *haic, unsigned int *buf, unsigned int len) +void HAL_AIC_PlaybackWriteFrame(AIC_HandleTypeDef *haic, void *buf, unsigned int frame_num) { int level; unsigned int pos = 0, cnt = 0; unsigned int filled_num, i; int send_ok = 1; + int fmt_width = 0; + unsigned int sample_num = 0; + unsigned short *psbuf = NULL; + unsigned int *pibuf = NULL; + + fmt_width = AIC_Get_FmtWidth(haic->Playback.Format); + switch (fmt_width) { + case 16: + psbuf = (unsigned short *)buf; + break; + case 24: + pibuf = (unsigned int *)buf; + break; + default: + prom_printk("Currently, only 16bit sampling is supported.\r\n"); + return; + } + + sample_num = frame_num * haic->Playback.Channels; __HAL_Lock(&haic->Lock); while (send_ok) { @@ -422,13 +443,21 @@ void HAL_AIC_PlaybackWriteFrame(AIC_HandleTypeDef *haic, unsigned int *buf, unsi filled_num = AIC_TFIFO_DEPTH / 2; pos += filled_num; - if(pos >= len) { - filled_num -= (pos - len); + if(pos >= sample_num) { + filled_num -= (pos - sample_num); send_ok = 0; } - for (i= 0; i < filled_num; i++) - __HAL_AIC_WRITE_TX_FIFO(haic, buf[cnt++]); + if (16 == fmt_width) { + for (i= 0; i < filled_num; i++) { + __HAL_AIC_WRITE_TX_FIFO(haic, psbuf[cnt++]); + } + } else if (24 == fmt_width) { + for (i= 0; i < filled_num; i++) { + __HAL_AIC_WRITE_TX_FIFO(haic, pibuf[cnt++]); + } + } else { + } } } diff --git a/projects/x2660-halley/Examples/icodec/main.c b/projects/x2660-halley/Examples/icodec/main.c index 9e131cde..63a033cb 100644 --- a/projects/x2660-halley/Examples/icodec/main.c +++ b/projects/x2660-halley/Examples/icodec/main.c @@ -12,11 +12,13 @@ LL_CPM_CGU_I2STypeDef AicCguConfig = CGU_CONFIG_I2S_APLL_8192K; unsigned int pcm_playback[] = { - #include "pcm.txt" + #include "pcm.txt" /* 16kHz、16bit采样、单通道音频 */ }; +#if 0 #define BUF_LEN (200*1024) unsigned int CapBuff[BUF_LEN]; +#endif // #define TEST_AIC_DEBUG @@ -63,7 +65,7 @@ int main(void) /* 设置放音相关参数 */ AicHandle.Playback.SampleRates = HAL_AIC_SAMPLE_RATE_16000; AicHandle.Playback.Format = HAL_AIC_DATA_FMT_S16LE; - AicHandle.Playback.Channels = 2; + AicHandle.Playback.Channels = 1; AicHandle.Interface = HAL_AIC_OPERATION_MODE_I2S; AicHandle.LrFirst = HAL_AIC_FRAME_MODE_LR; @@ -97,7 +99,7 @@ int main(void) HAL_ICODEC_DAC_Enable(&IcodecHandle); HAL_ICODEC_ADC_Enable(&IcodecHandle); -#if 1 +#if 0 HAL_AIC_CaptureStart(&AicHandle); HAL_AIC_PlaybackStart(&AicHandle); while (times--) { @@ -112,11 +114,11 @@ int main(void) HAL_AIC_PlaybackStop(&AicHandle); #endif -#if 0 +#if 1 prom_printk(">>>>>>>>>>>>>>>>>>>>>> playback start >>>>>>>>>>>>>>>>>>>>>>>>> \n"); while (times--) { HAL_AIC_PlaybackStart(&AicHandle); - HAL_AIC_PlaybackWriteFrame(&AicHandle, pcm_playback, sizeof(pcm_playback)); + HAL_AIC_PlaybackWriteFrame(&AicHandle, pcm_playback, sizeof(pcm_playback) * 8 / 16); HAL_AIC_PlaybackStop(&AicHandle); } prom_printk(">>>>>>>>>>>>>>>>>>>>>> playback over >>>>>>>>>>>>>>>>>>>>>>>>> \n"); -- Gitee