自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

用Hi3861聯(lián)網(wǎng)科大訊飛實現(xiàn)TTS功能

系統(tǒng) OpenHarmony
因為業(yè)務(wù)需要,需要實現(xiàn)TTS功能?,F(xiàn)講開發(fā)過程和實現(xiàn)方式整理成文檔,供有需要的人參考和討論。

??想了解更多內(nèi)容,請訪問:??

??51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)??

??https://ost.51cto.com??

因為業(yè)務(wù)需要,需要實現(xiàn)TTS功能?,F(xiàn)講開發(fā)過程和實現(xiàn)方式整理成文檔,供有需要的人參考和討論。

1、科大訊飛訊飛開放平臺可以聯(lián)網(wǎng)實現(xiàn)TTS功能,注冊之后每天可以免費500次訪問。

2、訪問科大訊飛平臺

目前訪問需要Websocket API,??幫助文檔鏈接??,具體的使用流程可以參看文檔說明。

3、在Openharmony下移植websocket

訪問websocket使用的是nopoll開源方案。將nopoll工程復(fù)制到third_party\nopoll下,在該文件下,添加BUILD.gn文件。

import("http://build/lite/config/component/lite_component.gni")
import("http://build/lite/ndk/ndk.gni")

config("nopoll_config") {
include_dirs = [
"nopoll",
"http://device/hisilicon/hispark_pegasus/sdk_liteos/third_party/lwip_sack/include",
"http://kernel/liteos_m/components/cmsis/2.0",
"http://third_party/lwip/src/include",
"http://third_party/tinycrypt/include",
]
}
cflags = [ "-Wno-unused-variable" ]
cflags += [ "-Wno-unused-but-set-variable" ]
cflags += [ "-Wno-unused-parameter" ]
cflags += [ "-Wno-sign-compare" ]
cflags += [ "-Wno-unused-function" ]
cflags += [ "-Wno-return-type" ]
nopoll_sources = [
"nopoll/nopoll.c",
"nopoll/nopoll_decl.c",
"nopoll/nopoll_win32.c",
"nopoll/nopoll_ctx.c",
"nopoll/nopoll_conn.c",
"nopoll/nopoll_log.c",
"nopoll/nopoll_listener.c",
"nopoll/nopoll_loop.c",
"nopoll/nopoll_io.c",
"nopoll/nopoll_msg.c",
"nopoll/nopoll_conn_opts.c",
"nopoll/nopoll_rtthread.c",
]
lite_library("nopoll_static") {
target_type = "static_library"
sources = nopoll_sources
public_configs = [ ":nopoll_config" ]
}
lite_library("nopoll_shared") {
target_type = "shared_library"
sources = nopoll_sources
public_configs = [ ":nopoll_config" ]
}
ndk_lib("nopoll_ndk") {
if (board_name != "hi3861v100") {
lib_extension = ".so"
deps = [
":nopoll_shared"
]
} else {
deps = [
":nopoll_static"
]
}
head_files = [
"http://third_party/nopoll"
]
}

然后在工程的gn文件下,

4、實現(xiàn)websocket功能(關(guān)鍵代碼)

nopoll還是很吃內(nèi)存的,需要動態(tài)開辟很大的空間。因為考慮到空間,所以,轉(zhuǎn)換的tts格式是mp3格式。

(1)websocket規(guī)則的日期獲取

char *week[] = {"Mon, ", "Tues, ", "Wed, ", "Thur, ","Fri, ", "Sat, ","Sun, "};
char *month[] = {"", " Jan ", " Feb ", " Mar ", " Apr "," May ", " June "," July ", " Aug ", " Sept ", " Oct "," Nov ", " Dec "};
static void get_date(char *date)
{
int tv_sec = hi_get_real_time();
DEBUG_printf("hi_get_real_time=%d\r\n",tv_sec);
//timeutils_struct_time_t tm;
//timeutils_seconds_since_2000_to_struct_time(tv_sec, &tm);
time_t t = tv_sec;
struct tm *tm = localtime(&t);
// date: Tue, 15 Oct 2019 07:00:50 GMT
sprintf(date, "%s%02d%s%d%s%02d%s%02d%s%02d%s", week[tm->tm_wday], tm->tm_mday, month[tm->tm_mon], tm->tm_year+1900, " ",tm->tm_hour,":", tm->tm_min, ":", tm->tm_sec, " GMT");
}

因為需要校驗時間,所以,設(shè)備需要聯(lián)網(wǎng),然后從網(wǎng)絡(luò)拉取時間,進行時間更新。

(2)上傳的json打包

void ws_xfyun_tts_request_json(char *buff)
{
char *string = NULL;
cJSON *root = cJSON_CreateObject();
//common
cJSON *cj_common = cJSON_CreateObject();
cJSON_AddItemToObject(root, "common", cj_common);
cJSON_AddItemToObject(cj_common, "app_id", cJSON_CreateString("0ea5cd21"));
//business
cJSON *cj_business = cJSON_CreateObject();
cJSON_AddItemToObject(root, "business", cj_business);
cJSON_AddItemToObject(cj_business, "aue", cJSON_CreateString("lame"));
cJSON_AddItemToObject(cj_business, "sfl", cJSON_CreateNumber(1));
cJSON_AddItemToObject(cj_business, "vcn",cJSON_CreateString("xiaoyan"));
cJSON_AddItemToObject(cj_business, "tte",cJSON_CreateString("UTF8"));
cJSON_AddItemToObject(cj_business, "pitch",cJSON_CreateNumber(50));
cJSON_AddItemToObject(cj_business, "speed",cJSON_CreateNumber(50));
//data
cJSON *cj_data = cJSON_CreateObject();
cJSON_AddItemToObject(root, "data", cj_data);
cJSON_AddItemToObject(cj_data, "status", cJSON_CreateNumber(2));
char base64_text[64];
int base64_len = sizeof(base64_text);
tiny_base64_encode(base64_text,&base64_len,tts_text,strlen(tts_text));
cJSON_AddItemToObject(cj_data, "text", cJSON_CreateString(base64_text));//北京 5YyX5Lqs
string = cJSON_PrintUnformatted(root);
strcpy(buff, string);
cJSON_Delete(root);
free(string);
}

(3)MP3解碼

使用了helix庫。

void mp3_decode_array(char *data,int len)
{
HMP3Decoder Decoder;
MP3FrameInfo mp3FrameInfo;
int bytesleft = len;
int decode_step = 0;;
unsigned short int output[1024*2];
Decoder = MP3InitDecoder();

int offset = MP3FindSyncWord(data,bytesleft); //搜索緩存中第一個有效數(shù)據(jù)幀
DEBUG_printf("offset = %d\r\n",offset);
if (offset < 0)
{
DEBUG_printf("MP3FindSyncWord not find.\r\n");
bytesleft = 0; // All data not avalible, clear the buffer.
return;
}
else if (offset > 0)
{
//去除頭部無效數(shù)據(jù)
bytesleft -= offset;
decode_step += offset;
}
//以下解碼n幀,readPtr會遞增,bytesleft遞減
unsigned char *readPtr;
readPtr = data+decode_step;
while (bytesleft > 0)
{
int ret = MP3Decode(Decoder, &readPtr, &bytesleft, (short *)output, 0);
if (ret == ERR_MP3_NONE) //正常解碼
{
DEBUG_printf("decode ok:bytesleft=%d\r\n",bytesleft);
MP3GetLastFrameInfo(Decoder, &(mp3FrameInfo));
hi_i2s_write((unsigned char *)output, mp3FrameInfo.outputSamps * 2, 1000);
}
else//解碼異常
{
DEBUG_printf("decode err: %d %d\r\n", ret,bytesleft);
}
}
DEBUG_printf("decode end.\r\n");
}

??想了解更多內(nèi)容,請訪問:??

??51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)??

??https://ost.51cto.com??

責(zé)任編輯:jianghua 來源: 鴻蒙社區(qū)
相關(guān)推薦

2020-10-12 09:36:04

鴻蒙

2021-09-30 10:11:05

鴻蒙HarmonyOS應(yīng)用

2022-03-07 15:05:58

HTTPHi3861數(shù)據(jù)解析

2012-08-16 09:44:03

中國移動投資

2015-11-20 09:58:46

浪潮科大訊飛

2020-10-27 16:52:10

科大訊飛

2024-11-13 10:16:37

2020-10-16 09:50:37

Hi3861WiFi熱點

2019-11-22 15:50:56

數(shù)字安全

2013-09-25 14:29:06

科大訊飛消費市場

2015-10-23 13:41:20

android源碼科大訊飛語音識別

2016-08-15 17:21:02

聯(lián)想

2022-03-15 15:00:59

Hi3861Pin接口鴻蒙

2023-05-26 16:07:14

Hi3861Wifi模塊

2021-11-01 10:06:41

AI

2020-10-14 09:41:02

Hi3861GPIO點燈

2022-05-30 15:21:27

Hi3861TCP通信

2020-11-02 12:07:11

鴻蒙 GPIO

2011-08-25 18:15:11

Android應(yīng)用iflyiOS應(yīng)用
點贊
收藏

51CTO技術(shù)棧公眾號