启明智显分享|乐鑫ESP32-S3语音--文字转语音(TTS)
今天,我们来研究乐鑫的语音助手框架ESP-Skainet其中的中文语音合成的例程。
编译原例程
- 首先需要clone例程
git clone --recursive https://github.com/espressif/esp-skainet.git
该工程内部自带一个idf,是运行的最佳版本,不过你任然可以使用自己的idf。
cd esp-skainet/examples/chinese_tts
cp sdkconfig.defaults.esp32s3 sdkconfig.defaults
idf.py set-target esp32s3
idf.py menuconfig
修改Audio Media Hal -> Audio Hardware board 改成ESP32-S3-Korvo-1
idf.py flash monitor -p /dev/ttyUSB0
欢迎使用乐鑫语音合成 I (266) tts_parser: unicode:0x6b22 -> huan1 I (266) tts_parser: unicode:0x8fce -> ying2 I (276) tts_parser: unicode:0x4f7f -> shi3 I (276) tts_parser: unicode:0x7528 -> yong4 I (286) tts_parser: unicode:0x4e50 -> le4 I (286) tts_parser: unicode:0x946b -> xin1 I (296) tts_parser: unicode:0x8bed -> yu3 I (296) tts_parser: unicode:0x97f3 -> yin1 I (306) tts_parser: unicode:0x5408 -> he2 I (306) tts_parser: unicode:0x6210 -> cheng2 请输入短语:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "esp_system.h" #include "esp_tts.h" #include "esp_tts_voice_xiaole.h" #include "esp_board_init.h" int app_main() { ESP_ERROR_CHECK(esp_board_init(AUDIO_HAL_16K_SAMPLES, 1, 16)); // 初始化codec芯片,配置好采样率、声道数、采样大小 esp_tts_voice_t *voice = (esp_tts_voice_t *)&esp_tts_voice_xiaole; // 配置tts的声音配置文件,来自libvoice_set_xiaole esp_tts_handle_t *tts_handle = esp_tts_create(voice); // 创建tts对象 char *prompt1 = "你好我是启明云端"; // 需要转换的文字 if (esp_tts_parse_chinese(tts_handle, prompt1)) // 文字解析成拼音 { int len[1] = {0}; do { short *pcm_data = esp_tts_stream_play(tts_handle, len, 3); // 拼音转换成pcm音频 esp_audio_play(pcm_data, len[0] * 2, portMAX_DELAY); //播放音频 } while (len[0] > 0); } esp_tts_stream_reset(tts_handle); // 重置 tts 流并清除 TTS 实例的所有缓存 return 0; }
这里音频的tts来自静态库libvoice_set_xiaole中,目前也只有这一个音色可供使用,其余的tts相关函数则是属于静态库libesp_tts_chinese。