Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
Y
yzg-util
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
YZG
yzg-util
Commits
ad624ef5
Commit
ad624ef5
authored
Jun 28, 2020
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
SQL层级处理的支持
parent
a8447a23
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
311 additions
and
2 deletions
+311
-2
ChineseHelper.java
...ese/src/main/java/com/yanzuoguang/util/ChineseHelper.java
+69
-0
BaiduConfig.java
...se/src/main/java/com/yanzuoguang/util/vo/BaiduConfig.java
+210
-0
HanziVo.java
...hinese/src/main/java/com/yanzuoguang/util/vo/HanziVo.java
+1
-1
TestChineseHelper.java
yzg-util-chinese/src/test/java/helper/TestChineseHelper.java
+31
-1
No files found.
yzg-util-chinese/src/main/java/com/yanzuoguang/util/ChineseHelper.java
View file @
ad624ef5
package
com
.
yanzuoguang
.
util
;
import
com.baidu.aip.speech.AipSpeech
;
import
com.baidu.aip.speech.TtsResponse
;
import
com.baidu.aip.util.Util
;
import
com.yanzuoguang.util.exception.CodeException
;
import
com.yanzuoguang.util.helper.JsonHelper
;
import
com.yanzuoguang.util.helper.StringHelper
;
import
com.yanzuoguang.util.vo.BaiduConfig
;
import
com.yanzuoguang.util.vo.HanziVo
;
import
net.sourceforge.pinyin4j.PinyinHelper
;
import
org.json.JSONObject
;
import
java.io.File
;
import
java.io.IOException
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.regex.Pattern
;
...
...
@@ -52,4 +63,62 @@ public class ChineseHelper {
}
return
rets
;
}
/**
* 生成语音
*
* @param config 配置
* @param from 语音文字 合成的文本,使用UTF-8编码, 请注意文本长度必须小于1024字节
* @param to 目标文件
*/
public
static
void
saveVideo
(
BaiduConfig
config
,
String
from
,
File
to
)
{
// 初始化一个AipSpeech
AipSpeech
client
=
new
AipSpeech
(
config
.
getAppId
(),
config
.
getApiKey
(),
config
.
getSecretKey
());
// 可选:设置网络连接参数
if
(
config
.
getConnectionTimeoutInMillis
()
>
0
)
{
client
.
setConnectionTimeoutInMillis
(
config
.
getConnectionTimeoutInMillis
());
}
if
(
config
.
getSocketTimeoutInMillis
()
>
0
)
{
client
.
setSocketTimeoutInMillis
(
config
.
getSocketTimeoutInMillis
());
}
// 可选:设置代理服务器地址, http和socket二选一,或者均不设置
// 设置http代理
if
(!
StringHelper
.
isEmpty
(
config
.
getHttpProxyHost
())
&&
config
.
getHttpProxyPort
()
>
0
)
{
client
.
setHttpProxy
(
config
.
getHttpProxyHost
(),
config
.
getHttpProxyPort
());
}
// 设置socket代理
else
if
(!
StringHelper
.
isEmpty
(
config
.
getSocketProxyHost
())
&&
config
.
getSocketProxyPort
()
>
0
)
{
client
.
setSocketProxy
(
config
.
getSocketProxyHost
(),
config
.
getSocketProxyPort
());
}
// 可选:设置log4j日志输出格式,若不设置,则使用默认配置
// 也可以直接通过jvm启动参数设置此环境变量
// System.setProperty("aip.log4j.conf", "path/to/your/log4j.properties");
// 设置可选参数
HashMap
<
String
,
Object
>
options
=
new
HashMap
<
String
,
Object
>();
options
.
put
(
"spd"
,
config
.
getSpd
());
options
.
put
(
"pit"
,
config
.
getPit
());
options
.
put
(
"per"
,
config
.
getPer
());
options
.
put
(
"vol"
,
config
.
getVol
());
// 调用接口
TtsResponse
res
=
client
.
synthesis
(
from
,
config
.
getLang
(),
config
.
getCtp
(),
options
);
byte
[]
data
=
res
.
getData
();
JSONObject
res1
=
res
.
getResult
();
if
(
data
!=
null
)
{
try
{
Util
.
writeBytesToFileSystem
(
data
,
to
.
getAbsolutePath
());
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
else
{
System
.
out
.
println
(
JsonHelper
.
serialize
(
res1
,
true
));
int
code
=
res1
.
getInt
(
"err_no"
);
String
message
=
res1
.
getString
(
"err_msg"
);
throw
new
CodeException
(
StringHelper
.
toString
(
code
),
message
,
res1
);
}
}
}
yzg-util-chinese/src/main/java/com/yanzuoguang/util/vo/BaiduConfig.java
0 → 100644
View file @
ad624ef5
package
com
.
yanzuoguang
.
util
.
vo
;
/**
* 百度语音配置
*
* @author 颜佐光
*/
public
class
BaiduConfig
extends
BaseVo
{
/**
* 百度App_ID
*/
private
String
appId
;
/**
* 百度apiKey
*/
private
String
apiKey
;
/**
* 百度secretKey
*/
private
String
secretKey
;
/**
* 设置网络连接参数
*/
private
int
connectionTimeoutInMillis
;
/**
* 设置网络连接参数
*/
private
int
socketTimeoutInMillis
;
/**
* 设置http代理服务器
*/
private
String
httpProxyHost
;
/**
* 设置http代理端口
*/
private
int
httpProxyPort
;
/**
* 设置http代理服务器
*/
private
String
socketProxyHost
;
/**
* 设置http代理端口
*/
private
int
socketProxyPort
;
/**
* 语言,默认中文
*/
private
String
lang
=
"zh"
;
/**
* ctp
*/
private
int
ctp
=
1
;
/**
* 语速,取值0-9,默认为5中语速
*/
private
int
spd
=
5
;
/**
* 音调,取值0-9,默认为5中语调
*/
private
int
pit
=
5
;
/**
* 音量,取值0-15,默认为5中音量 否
*/
private
int
vol
=
15
;
/**
* 发音人选择,0为女声,1为男声, 3为情感合成-度逍遥,4为情感合成-度丫丫,默认为普通女 否
*/
private
int
per
=
0
;
public
BaiduConfig
()
{
}
public
BaiduConfig
(
String
appId
,
String
apiKey
,
String
secretKey
)
{
this
.
appId
=
appId
;
this
.
apiKey
=
apiKey
;
this
.
secretKey
=
secretKey
;
}
public
String
getAppId
()
{
return
appId
;
}
public
void
setAppId
(
String
appId
)
{
this
.
appId
=
appId
;
}
public
String
getApiKey
()
{
return
apiKey
;
}
public
void
setApiKey
(
String
apiKey
)
{
this
.
apiKey
=
apiKey
;
}
public
String
getSecretKey
()
{
return
secretKey
;
}
public
void
setSecretKey
(
String
secretKey
)
{
this
.
secretKey
=
secretKey
;
}
public
int
getConnectionTimeoutInMillis
()
{
return
connectionTimeoutInMillis
;
}
public
void
setConnectionTimeoutInMillis
(
int
connectionTimeoutInMillis
)
{
this
.
connectionTimeoutInMillis
=
connectionTimeoutInMillis
;
}
public
int
getSocketTimeoutInMillis
()
{
return
socketTimeoutInMillis
;
}
public
void
setSocketTimeoutInMillis
(
int
socketTimeoutInMillis
)
{
this
.
socketTimeoutInMillis
=
socketTimeoutInMillis
;
}
public
String
getHttpProxyHost
()
{
return
httpProxyHost
;
}
public
void
setHttpProxyHost
(
String
httpProxyHost
)
{
this
.
httpProxyHost
=
httpProxyHost
;
}
public
int
getHttpProxyPort
()
{
return
httpProxyPort
;
}
public
void
setHttpProxyPort
(
int
httpProxyPort
)
{
this
.
httpProxyPort
=
httpProxyPort
;
}
public
String
getSocketProxyHost
()
{
return
socketProxyHost
;
}
public
void
setSocketProxyHost
(
String
socketProxyHost
)
{
this
.
socketProxyHost
=
socketProxyHost
;
}
public
int
getSocketProxyPort
()
{
return
socketProxyPort
;
}
public
void
setSocketProxyPort
(
int
socketProxyPort
)
{
this
.
socketProxyPort
=
socketProxyPort
;
}
public
String
getLang
()
{
return
lang
;
}
public
void
setLang
(
String
lang
)
{
this
.
lang
=
lang
;
}
public
int
getCtp
()
{
return
ctp
;
}
public
void
setCtp
(
int
ctp
)
{
this
.
ctp
=
ctp
;
}
public
int
getSpd
()
{
return
spd
;
}
public
void
setSpd
(
int
spd
)
{
this
.
spd
=
spd
;
}
public
int
getPit
()
{
return
pit
;
}
public
void
setPit
(
int
pit
)
{
this
.
pit
=
pit
;
}
public
int
getVol
()
{
return
vol
;
}
public
void
setVol
(
int
vol
)
{
this
.
vol
=
vol
;
}
public
int
getPer
()
{
return
per
;
}
public
void
setPer
(
int
per
)
{
this
.
per
=
per
;
}
}
yzg-util-chinese/src/main/java/com/yanzuoguang/util/HanziVo.java
→
yzg-util-chinese/src/main/java/com/yanzuoguang/util/
vo/
HanziVo.java
View file @
ad624ef5
package
com
.
yanzuoguang
.
util
;
package
com
.
yanzuoguang
.
util
.
vo
;
/**
* 汉字处理
...
...
yzg-util-chinese/src/test/java/helper/TestChineseHelper.java
View file @
ad624ef5
package
helper
;
import
com.yanzuoguang.util.ChineseHelper
;
import
com.yanzuoguang.util.HanziVo
;
import
com.yanzuoguang.util.vo.BaiduConfig
;
import
com.yanzuoguang.util.vo.HanziVo
;
import
com.yanzuoguang.util.helper.JsonHelper
;
import
org.junit.Test
;
import
java.io.File
;
import
java.util.List
;
public
class
TestChineseHelper
{
...
...
@@ -15,4 +17,32 @@ public class TestChineseHelper {
System
.
out
.
println
(
"json:"
+
JsonHelper
.
serialize
(
items
,
true
));
}
@Test
public
void
testConvertMp3
()
{
String
text
=
"三分钟前,由北京市顺义区二经路与二纬路交汇处北侧,北京首都国际机场T3航站楼 去往 东城区北三环东路36号喜来登大酒店(北京金隅店)"
;
BaiduConfig
config
=
new
BaiduConfig
(
"20624068"
,
"aeo4CBIjiW4wLuiIAKD4ZMrk"
,
"BLzIf80xq6v1cN4n231dSGPYqD1GDHy8"
);
ChineseHelper
.
saveVideo
(
config
,
text
,
getTargetFile
(
"test.mp3"
));
}
@Test
public
void
testConvertMp3Yzg
()
{
String
text
=
"成人票15元"
;
BaiduConfig
config
=
new
BaiduConfig
(
"20624068"
,
"aeo4CBIjiW4wLuiIAKD4ZMrk"
,
"BLzIf80xq6v1cN4n231dSGPYqD1GDHy8"
);
ChineseHelper
.
saveVideo
(
config
,
text
,
getTargetFile
(
"product.mp3"
));
}
private
File
getTargetFile
(
String
to
)
{
// 注意,路径应为文件在工程中的相对路径
File
f
=
new
File
(
"target/"
+
to
);
return
f
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment