1 /*
2 * Copyright (C) 2015-2020 Alibaba Group Holding Limited
3 *
4 */
5
6 #include <stdio.h>
7 #include <stdint.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <math.h>
11
12 #include "uvoice_types.h"
13 #include "uvoice_tts.h"
14
15 #include "../internal/uvoice_os.h"
16
17
18 #define APP_LOGI(fmt, ...) printf("%s: "fmt, __func__, ##__VA_ARGS__)
19 #define APP_LOGE(fmt, ...) printf("%s: "fmt, __func__, ##__VA_ARGS__)
20
21 static os_task_t voicelouder_task;
22
23 static char tts_text[128];
24 static char tts_filename[128];
25 static bool tts_configred = false;
26
alicloud_tts_config(void)27 static int alicloud_tts_config(void)
28 {
29 tts_config_t tts_config;
30 uvoice_tts_t *tts;
31 int ret;
32
33 /** 请开发者根据个人在智能语音交互平台的账号信息,更新下面的app_key和token
34 * https://nls-portal.console.aliyun.com/overview
35 **/
36 tts_config.app_key = "NULL";
37 tts_config.token = "NULL";
38 tts_config.format = MEDIA_FMT_MP3;
39 tts_config.sample_rate = 16000;
40 tts_config.voice = "siqi";
41 tts_config.volume = 80;
42 tts_config.speech_rate = 0;
43 tts_config.pitch_rate = 0;
44 tts_config.text_encode_type = TTS_ENCODE_UTF8;
45
46 if(0 == strcmp(tts_config.app_key, "NULL")) {
47 printf("aliyun tts need app key and token !!!\r\n");
48 printf("help document: https://help.aliyun.com/document_detail/72153.html\n");
49 return -1;
50 }
51
52 tts = uvoice_tts_create();
53 if (!tts) {
54 APP_LOGE("create tts failed !\n");
55 return -1;
56 }
57
58 if (tts->tts_init(TTS_AICLOUD_ALIYUN, &tts_config)) {
59 APP_LOGE("config tts failed !\n");
60 uvoice_tts_release(tts);
61 return -1;
62 }
63 APP_LOGI("config tts success\n");
64
65 return 0;
66 }
67
alicoud_tts_event(tts_event_e event,char * info)68 static int alicoud_tts_event(tts_event_e event, char *info)
69 {
70 return 0;
71 }
72
alicloud_tts_recv_data(uint8_t * buffer,int nbytes,int index)73 static int alicloud_tts_recv_data(uint8_t *buffer, int nbytes, int index)
74 {
75 FILE *fp;
76 if (index == 0) {
77 fp = fopen(tts_filename, "w");
78 if (!fp) {
79 printf("%s: open %s failed !\n", __func__, tts_filename);
80 return -1;
81 }
82 } else {
83 fp = fopen(tts_filename, "a+");
84 if (!fp) {
85 printf("%s: open %s failed !\n", __func__, tts_filename);
86 return -1;
87 }
88 }
89 printf("write data %d size %d to file %s\n",
90 index, nbytes, tts_filename);
91 fwrite(buffer, nbytes, 1, fp);
92 fclose(fp);
93 return 0;
94 }
95
alicloud_tts_download_task(void * arg)96 static void alicloud_tts_download_task(void *arg)
97 {
98 tts_recv_callback_t recv_cb;
99 uvoice_tts_t *tts;
100 int ret = 0;
101
102 if (!tts_configred) {
103 ret = alicloud_tts_config();
104 if(ret != 0) {
105 printf("tts download %s fail\n", tts_filename);
106 return;
107 }
108 tts_configred = true;
109 }
110
111 tts = uvoice_tts_create();
112 if (!tts) {
113 APP_LOGE("create tts failed !\n");
114 return;
115 }
116
117 recv_cb.event = alicoud_tts_event;
118 recv_cb.recv_data = alicloud_tts_recv_data;
119
120 ret = tts->tts_request(tts_text, TTS_RECV_DATA, &recv_cb);//uvoice_tts_aliyun_request()
121 if (ret != 0) {
122 printf("tts failed\n");
123 return;
124 }
125
126 printf("tts download %s success\n", tts_filename);
127 }
128
alicloud_tts_download(char * text,char * filename)129 int alicloud_tts_download(char *text, char *filename)
130 {
131 if (!text || !filename) {
132 APP_LOGE("args null !\n");
133 return -1;
134 }
135
136 memset(tts_text, 0, sizeof(tts_text));
137 snprintf(tts_text, sizeof(tts_text), "%s", text);
138
139 memset(tts_filename, 0, sizeof(tts_filename));
140 snprintf(tts_filename, sizeof(tts_filename), "%s", filename);
141
142 os_task_create(&voicelouder_task, "tts_download_task",
143 alicloud_tts_download_task,
144 NULL, 8192*2, UVOICE_TASK_PRI_NORMAL);
145 return 0;
146 }
147
test_tts_handle(int argc,char ** argv)148 void test_tts_handle(int argc, char **argv)
149 {
150 if (argc < 3)
151 return;
152 alicloud_tts_download(argv[1], argv[2]);
153 }
154