ai_bypass Demo*
- ai_bypass 是一个基于 vpa/algorithm_package/bypass 的一个录音Demo。
- 通过 uac 来录音。
- 开发者可以基于该 demo 和 vpa子系统 进行语音算法相关的开发。
重要
vpa子系统的音频算法开发指南请阅读:vpa算法移植指南
1. ai_bypass 目录结构介绍*
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
2.几个核心文件介绍*
2.1.app_system_clk.c*
-
app_system_clk.c: 包含了各个模块的时钟配置,其中系统时钟默认频率为 144M。
修改CPU主频的方法:
修改 app_system_clk.c 中的宏 SYS_CLK_CFG 的值:
1
#define SYS_CLK_CFG 3 //0:32M; 1:96M; 2:128M; 3:144M; 4:160M
- 配 0 为 32M
- 配 1 为 96M
- 配 2 为 144M
- 配 3 为 160M
2.2.ai_audio_in_config.c*
- ai_audio_in_config.c:主要是完成了 context、audio in 的配置。其配置参数主要是在 sdk_config.h 文件中。
2.3.sdk_config.h[重点关注]*
- sdk_config.h:包含 uac、麦克风、参考声道、context 的配置
- UAC 配置介绍:根据项目需求来配置UAC。
sdk_config.h 1 2 3 4 5 6 7 8 9 10 11 12
...... //省略一亿行 // UAC 配置 #define CONFIT_UAC_OPEN //使能 UAC 功能 // #define CONFIG_UAC_TASK_DEBUG //打开 UAC 后台监控 log #define UAC_UPSTREAM_ENABLE //使能上行功能 #define UAC_UPSTREAM_SAMPLERATE 16000 //上行的采样率 #define UAC_UPSTREAM_CHANNEL_NUM 2 //上行的通道数 #define UAC_DOWNSTREAM_ENABLE //使能上行功能 #define UAC_DOWNSTREAM_SAMPLERATE 16000 //下行的采样率 #define UAC_DOWNSTREAM_CHANNEL_NUM 2 //下行的通道数 ...... //省略一亿行
- 麦克风 和 参考声道配置:需要根据项目需求来配置麦克风数目和源、参考声道数目源以及output通道数目
sdk_config.h 1 2 3 4 5 6 7 8 9 10 11 12 13
...... //省略一亿行 // 麦克风数目 和 参考声道数目 [根据项目需求来配置] #define CONFIG_MIC_NUM (1) #define CONFIG_REF_NUM (0) #define CONFIG_OUT_NUM (2) // MIC SOURCE 根据项目需求来配置麦克风源,下面几项只能有一个为 1 #define CONFIG_MIC_SOURCE_AMIC (0) #define CONFIG_MIC_SOURCE_DMIC (1) #define CONFIG_MIC_SOURCE_IIS (0) // REF SOURCE 根据项目需求来配置参考声道源,下面几项只能有一个为 1 #define CONFIG_REF_SOURCE_ADC (1) #define CONFIG_REF_SOURCE_ECHO (0) ...... //省略一亿行
- context 的 audio_io_config 配置:需要根据算法需求来配置帧长、采样率、位宽,context帧数和通道的context数目
sdk_config.h 1 2 3 4 5 6 7 8 9
...... //省略一亿行 // audio_io_config 根据算法需求来配置 #define CONFIG_ADUIO_IO_FRAME_LENGTH_MS (2) // At least 32 sampling points, or a multiple of 32 sampling points. #define CONFIG_AUDIO_IO_SAMPLE_RATE (16000) // support 16000, 32000, 48000, 96000 #define CONFIG_AUDIO_IO_SAMPLE_RATE_BITZ_CFG (0) // 0: 16bit; 1: 24L-> 32bit; 2: 24H->32bit; 3: 32bit #define CONFIG_ADUIO_IO_PCM_FRAME_NUM_PER_CONTEXT (1) // #define CONFIG_ADUIO_IO_IN_PCM_CONTEXT_NUM_PER_CHANNEL (8) // the channel size(ctx_number) for for context->mic_buffer and context->ref_buffer #define CONFIG_ADUIO_IO_OUT_PCM_CONTEXT_NUM_PER_CHANNEL (CONFIG_ADUIO_IO_IN_PCM_CONTEXT_NUM_PER_CHANNEL) //the channel size(ctx_number) for for context->out_buffer ...... //省略一亿行
- UAC 配置介绍:根据项目需求来配置UAC。
2.4.main.c*
- 主要的核心代码就是 main 函数,main 函数先后完成了 audio in 初始化、uac 初始化、vpa 初始化、最后启动 audio in 开始录制。主要的代码如下:
main.c 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// Initialize Audio Input Module. audio_input_config_t *config = ai_audio_in_config_init(_audio_in_record_cb); audio_input_init(config); // Initialize UAC (USB Audio Class) sound card. VPA_CONTEXT_HEADER *ctx_header = vpa_get_context_header(); if (ctx_header == NULL) { printf (LOG_TAG"ctx_header is NULL\n"); return -1; } #ifdef CONFIT_UAC_OPEN uint32_t buffer_num = ctx_header->audio_io_config.context_num_per_channel_for_out; uint32_t buffer_size = vpa_get_context_length(ctx_header) * ctx_header->audio_io_config.out_interlaced_num; uac_init(buffer_num, buffer_size, buffer_num/2); #endif // Initialize VPA (Voice Processing Algorithm). vpa_init(ctx_header, NULL); // Start recording audio_input_start();
- audio in 每录制一个 context 时长的音频数据,就会发送消息队列给 main 所在的线程进行处理。
- 首先调用 vpa 子系统的 vpa_process_active 接口进行算法处理。
- 其次调用 uac_upstream_fifo_put 将 context 中的 output 通道的数据通过 USB 传到主机上。
- 最后必须调用 uac_task 接口,这个接口用于 uac 时钟同步用的。
main.c 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
while(1) { chip_os_error_t ret = chip_os_queue_get(s_audio_in_isr_queue, &queue_data, CHIP_OS_TIME_FOREVER); assert(ret == CHIP_OS_OK); // Get context VPA_CONTEXT *context; vpa_get_context(queue_data, &context); // Run voice process algorithm vpa_process_active(context); // Drive UAC to upload audio data #ifdef CONFIT_UAC_OPEN int16_t *out_buffer = vpa_get_output_buffer(context, 0, 0); uac_upstream_fifo_put(out_buffer); uac_task(); #endif }
2.5.其它*
- usb 目录是 uac 相关,基本不用修改,直接拿来用即可。
3.编译方法*
$make app=ai_bypass board=gx8302b_dev
4.录音测试方法*
下面以在windows上演示
首先 windows PC 安装好开源工具 Audacity
开发板按照如下图片所示,跳帽联通数字麦克风和USB
USB在设备管理器里识别为:RTT Composite Device
录音按照如下方式选择: