【涂鸦T5】3. 录音
硬件
开发板上有两个mic,默认用的mic1。不需要外接mic
注意sd卡不要用大容量的卡,4g或者8g可以,否则会遇到mount失败的问题
编译
代码
TuyaOpen/examples/multimedia/audio/audio_recorder
可以拷贝example中的代码,但是会有问题,需要部分修改
见如下,可以直接拷贝下来,存成diff.txt,拷贝到TuyaOpen目录下,用git apply diff.txt命令自动复制改动
把模式切换成了sd卡存储
diff --git a/examples/multimedia/audio/audio_recorder/src/example_recorder.c b/examples/multimedia/audio/audio_recorder/src/example_recorder.c
index 397e57c..03e67c6 100644
--- a/examples/multimedia/audio/audio_recorder/src/example_recorder.c
+++ b/examples/multimedia/audio/audio_recorder/src/example_recorder.c
@@ -38,7 +38,7 @@#define USE_RING_BUFFER 0#define USE_INTERNAL_FLASH 1 // Store recording file in internal flash#define USE_SD_CARD 2
-#define RECORDER_FILE_SOURCE USE_RING_BUFFER
+#define RECORDER_FILE_SOURCE USE_SD_CARD#if (RECORDER_FILE_SOURCE == USE_INTERNAL_FLASH)#define RECORDER_FILE_DIR "/"
@@ -98,12 +98,14 @@ struct recorder_ctx sg_recorder_ctx = {.file_hdl = NULL,};+int flag_status = 0;/**********************************************************************************function define*********************************************************************************/static void app_audio_trigger_pin_init(void){OPERATE_RET rt = OPRT_OK;
+ PR_DEBUG("%s", __func__);TUYA_GPIO_BASE_CFG_T pin_cfg = {.mode = TUYA_GPIO_PULLUP,
@@ -119,7 +121,31 @@ static BOOL_T audio_trigger_pin_is_pressed(void){TUYA_GPIO_LEVEL_E level = TUYA_GPIO_LEVEL_HIGH;tkl_gpio_read(AUDIO_TRIGGER_PIN, &level);
- return (level == TUYA_GPIO_LEVEL_LOW) ? TRUE : FALSE;
+ if (level == TUYA_GPIO_LEVEL_LOW){
+ PR_DEBUG("TUYA_GPIO_LEVEL_LOW");
+ tal_system_sleep(1000);
+ flag_status += 1;
+ if(flag_status == 3)
+ flag_status = 0;
+ switch (flag_status)
+ {
+ case 0:
+ PR_DEBUG("Flag waitting");
+ break;
+ case 1:
+ PR_DEBUG("Flag start record");
+ break;
+ case 2:
+ PR_DEBUG("Flag end record");
+ break;
+ default:
+ PR_DEBUG("Flag waitting");
+ break;
+ }
+ return TRUE;
+ } else {
+ return FALSE;
+ }}static int _audio_frame_put(TKL_AUDIO_FRAME_INFO_T *pframe)
@@ -139,6 +165,7 @@ static int _audio_frame_put(TKL_AUDIO_FRAME_INFO_T *pframe)static void app_audio_init(void){TKL_AUDIO_CONFIG_T config;
+ PR_DEBUG("%s", __func__);memset(&config, 0, sizeof(TKL_AUDIO_CONFIG_T));@@ -343,10 +370,11 @@ static OPERATE_RET app_pcm_to_wav(char *pcm_file)return rt;}- TAL_PR_HEXDUMP_DEBUG("wav head", head, WAV_HEAD_LEN);
+ PR_HEXDUMP_DEBUG("wav head", head, WAV_HEAD_LEN);// Create wav fileTUYA_FILE wav_hdl = tkl_fopen(RECORDER_WAV_FILE_PATH, "w");
+ char *read_buf = tkl_system_psram_malloc(PCM_FRAME_SIZE);if (NULL == wav_hdl) {PR_ERR("open file: %s failed", RECORDER_WAV_FILE_PATH);rt = OPRT_FILE_OPEN_FAILED;
@@ -357,7 +385,7 @@ static OPERATE_RET app_pcm_to_wav(char *pcm_file)tkl_fwrite(head, WAV_HEAD_LEN, wav_hdl);// Read pcm file
- char *read_buf = tkl_system_psram_malloc(PCM_FRAME_SIZE);
+if (NULL == read_buf) {PR_ERR("tkl_system_psram_malloc failed");// return OPRT_COM_ERROR;
@@ -410,13 +438,16 @@ static void app_recorder_thread(void *arg){OPERATE_RET rt = OPRT_OK;+ PR_DEBUG("%s", __func__);app_audio_trigger_pin_init();app_audio_init();+ PR_DEBUG("%s start for", __func__);for (;;) {
+ if(flag_status == 1)app_mic_record();- if (FALSE == audio_trigger_pin_is_pressed()) {
+ if (FALSE == audio_trigger_pin_is_pressed() && flag_status == 2) {tal_system_sleep(100);// End recording
@@ -451,7 +482,7 @@ static void app_recorder_thread(void *arg)}// Start recording
- if (FALSE == sg_recorder_ctx.recording) {
+ if ((FALSE == sg_recorder_ctx.recording) && flag_status == 1) {#if (RECORDER_FILE_SOURCE == USE_INTERNAL_FLASH) || (RECORDER_FILE_SOURCE == USE_SD_CARD)// If recording file exists, delete itBOOL_T is_exist = FALSE;
测试
我增加了一个机制,状态转换:wait record -> start record -> record end
按一次开始录制,按第二次停止录制。
录制完成后可以把sd卡插在电脑上去听。
缺点:示例代码只能录制采样率16k,位深16bit的音频,我试过想要录制48k的,提示size过大,这个等后面有空再看。
就目前听到效果还行,人声基本能听清内容。
问题
rt的问题在USE_RING_BUFFER会遇到,但是切换到USE_SD_CARD就不会了
需要把代码挪下位置,因为直接go to __EXIT,会导致部分代码无法运行,所以挪到前面
char *read_buf = tkl_system_psram_malloc(PCM_FRAME_SIZE);