Commit 5b0470eb02fc33378e518ecadde4923ca0cdfdd7
1 parent
c77172a1
1.生成线路文件时合成报站音频
Showing
9 changed files
with
888 additions
and
106 deletions
pom.xml
| @@ -429,7 +429,25 @@ | @@ -429,7 +429,25 @@ | ||
| 429 | <artifactId>pinyin4j</artifactId> | 429 | <artifactId>pinyin4j</artifactId> |
| 430 | <version>2.5.1</version> | 430 | <version>2.5.1</version> |
| 431 | </dependency> | 431 | </dependency> |
| 432 | - </dependencies> | 432 | + |
| 433 | + <dependency> | ||
| 434 | + <groupId>com.squareup.okhttp3</groupId> | ||
| 435 | + <artifactId>okhttp</artifactId> | ||
| 436 | + <version>4.10.0</version> | ||
| 437 | + </dependency> | ||
| 438 | + | ||
| 439 | + <dependency> | ||
| 440 | + <groupId>org.java-websocket</groupId> | ||
| 441 | + <artifactId>Java-WebSocket</artifactId> | ||
| 442 | + <version>1.5.3</version> | ||
| 443 | + </dependency> | ||
| 444 | + | ||
| 445 | + <dependency> | ||
| 446 | + <groupId>org.bytedeco</groupId> | ||
| 447 | + <artifactId>javacv-platform</artifactId> | ||
| 448 | + <version>1.5.11</version> | ||
| 449 | + </dependency> | ||
| 450 | + </dependencies> | ||
| 433 | 451 | ||
| 434 | <dependencyManagement> | 452 | <dependencyManagement> |
| 435 | <dependencies> | 453 | <dependencies> |
src/main/java/com/bsth/entity/speech/SpeechRequest.java
0 → 100644
| 1 | +package com.bsth.entity.speech; | ||
| 2 | + | ||
| 3 | +import com.fasterxml.jackson.annotation.JsonIgnore; | ||
| 4 | + | ||
| 5 | +import java.util.HashMap; | ||
| 6 | +import java.util.Map; | ||
| 7 | + | ||
| 8 | +public class SpeechRequest { | ||
| 9 | + | ||
| 10 | + /** | ||
| 11 | + * 包含appid | ||
| 12 | + */ | ||
| 13 | + private Map<String, Object> common = new HashMap<>(); | ||
| 14 | + | ||
| 15 | + /** | ||
| 16 | + * 音频合成参数 | ||
| 17 | + */ | ||
| 18 | + private Map<String, Object> business = new HashMap<>(); | ||
| 19 | + | ||
| 20 | + /** | ||
| 21 | + * 数据、状态参数 | ||
| 22 | + */ | ||
| 23 | + private Map<String, Object> data = new HashMap<>(); | ||
| 24 | + | ||
| 25 | + @JsonIgnore | ||
| 26 | + private boolean completed = false; | ||
| 27 | + | ||
| 28 | + public Map<String, Object> getCommon() { | ||
| 29 | + return common; | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | + public void setCommon(Map<String, Object> common) { | ||
| 33 | + this.common = common; | ||
| 34 | + } | ||
| 35 | + | ||
| 36 | + public Map<String, Object> getBusiness() { | ||
| 37 | + return business; | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + public void setBusiness(Map<String, Object> business) { | ||
| 41 | + this.business = business; | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + public Map<String, Object> getData() { | ||
| 45 | + return data; | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + public void setData(Map<String, Object> data) { | ||
| 49 | + this.data = data; | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + public boolean isCompleted() { | ||
| 53 | + return completed; | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | + public void setCompleted(boolean completed) { | ||
| 57 | + this.completed = completed; | ||
| 58 | + } | ||
| 59 | +} |
src/main/java/com/bsth/entity/speech/SpeechResponse.java
0 → 100644
| 1 | +package com.bsth.entity.speech; | ||
| 2 | + | ||
| 3 | +/** | ||
| 4 | + * 讯飞在线语音合成返回json | ||
| 5 | + */ | ||
| 6 | +public class SpeechResponse { | ||
| 7 | + | ||
| 8 | + private int code; | ||
| 9 | + | ||
| 10 | + private String message; | ||
| 11 | + | ||
| 12 | + private String sid; | ||
| 13 | + | ||
| 14 | + private AudioData data; | ||
| 15 | + | ||
| 16 | + public int getCode() { | ||
| 17 | + return code; | ||
| 18 | + } | ||
| 19 | + | ||
| 20 | + public void setCode(int code) { | ||
| 21 | + this.code = code; | ||
| 22 | + } | ||
| 23 | + | ||
| 24 | + public String getMessage() { | ||
| 25 | + return message; | ||
| 26 | + } | ||
| 27 | + | ||
| 28 | + public void setMessage(String message) { | ||
| 29 | + this.message = message; | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | + public String getSid() { | ||
| 33 | + return sid; | ||
| 34 | + } | ||
| 35 | + | ||
| 36 | + public void setSid(String sid) { | ||
| 37 | + this.sid = sid; | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + public AudioData getData() { | ||
| 41 | + return data; | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + public void setData(AudioData data) { | ||
| 45 | + this.data = data; | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + public final static class AudioData { | ||
| 49 | + | ||
| 50 | + private String audio; | ||
| 51 | + | ||
| 52 | + private String ced; | ||
| 53 | + | ||
| 54 | + private int status; | ||
| 55 | + | ||
| 56 | + public String getAudio() { | ||
| 57 | + return audio; | ||
| 58 | + } | ||
| 59 | + | ||
| 60 | + public void setAudio(String audio) { | ||
| 61 | + this.audio = audio; | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + public String getCed() { | ||
| 65 | + return ced; | ||
| 66 | + } | ||
| 67 | + | ||
| 68 | + public void setCed(String ced) { | ||
| 69 | + this.ced = ced; | ||
| 70 | + } | ||
| 71 | + | ||
| 72 | + public int getStatus() { | ||
| 73 | + return status; | ||
| 74 | + } | ||
| 75 | + | ||
| 76 | + public void setStatus(int status) { | ||
| 77 | + this.status = status; | ||
| 78 | + } | ||
| 79 | + | ||
| 80 | + @Override | ||
| 81 | + public String toString() { | ||
| 82 | + return "AudioData{" + | ||
| 83 | + "audio='" + audio + '\'' + | ||
| 84 | + ", ced='" + ced + '\'' + | ||
| 85 | + ", status=" + status + | ||
| 86 | + '}'; | ||
| 87 | + } | ||
| 88 | + } | ||
| 89 | + | ||
| 90 | + @Override | ||
| 91 | + public String toString() { | ||
| 92 | + return "SpeechResponse{" + | ||
| 93 | + "code=" + code + | ||
| 94 | + ", message='" + message + '\'' + | ||
| 95 | + ", sid='" + sid + '\'' + | ||
| 96 | + ", audioData=" + data + | ||
| 97 | + '}'; | ||
| 98 | + } | ||
| 99 | +} |
src/main/java/com/bsth/service/impl/StationRouteServiceImpl.java
| @@ -5,27 +5,25 @@ import com.bsth.entity.*; | @@ -5,27 +5,25 @@ import com.bsth.entity.*; | ||
| 5 | import com.bsth.entity.search.CustomerSpecs; | 5 | import com.bsth.entity.search.CustomerSpecs; |
| 6 | import com.bsth.repository.*; | 6 | import com.bsth.repository.*; |
| 7 | import com.bsth.service.StationRouteService; | 7 | import com.bsth.service.StationRouteService; |
| 8 | -import com.bsth.util.CoordinateConverter; | ||
| 9 | -import com.bsth.util.ExcelUtil; | ||
| 10 | -import com.bsth.util.FTPClientUtils; | 8 | +import com.bsth.util.*; |
| 11 | import com.bsth.util.Geo.GeoUtils; | 9 | import com.bsth.util.Geo.GeoUtils; |
| 12 | import com.bsth.util.Geo.Point; | 10 | import com.bsth.util.Geo.Point; |
| 13 | -import com.bsth.util.PackTarGZUtils; | ||
| 14 | import com.bsth.util.db.DBUtils_MS; | 11 | import com.bsth.util.db.DBUtils_MS; |
| 15 | import com.google.common.base.Splitter; | 12 | import com.google.common.base.Splitter; |
| 16 | import org.apache.commons.lang3.StringUtils; | 13 | import org.apache.commons.lang3.StringUtils; |
| 17 | import org.geolatte.geom.Polygon; | 14 | import org.geolatte.geom.Polygon; |
| 18 | import org.springframework.beans.factory.annotation.Autowired; | 15 | import org.springframework.beans.factory.annotation.Autowired; |
| 16 | +import org.springframework.beans.factory.annotation.Value; | ||
| 19 | import org.springframework.data.domain.Sort; | 17 | import org.springframework.data.domain.Sort; |
| 20 | import org.springframework.data.domain.Sort.Direction; | 18 | import org.springframework.data.domain.Sort.Direction; |
| 21 | import org.springframework.stereotype.Service; | 19 | import org.springframework.stereotype.Service; |
| 22 | 20 | ||
| 23 | import javax.servlet.http.HttpServletResponse; | 21 | import javax.servlet.http.HttpServletResponse; |
| 24 | -import java.io.ByteArrayInputStream; | ||
| 25 | -import java.io.File; | ||
| 26 | -import java.io.InputStream; | 22 | +import java.io.*; |
| 27 | import java.text.DecimalFormat; | 23 | import java.text.DecimalFormat; |
| 28 | import java.util.*; | 24 | import java.util.*; |
| 25 | +import java.util.zip.ZipEntry; | ||
| 26 | +import java.util.zip.ZipOutputStream; | ||
| 29 | 27 | ||
| 30 | /** | 28 | /** |
| 31 | * | 29 | * |
| @@ -44,7 +42,13 @@ import java.util.*; | @@ -44,7 +42,13 @@ import java.util.*; | ||
| 44 | */ | 42 | */ |
| 45 | 43 | ||
| 46 | @Service | 44 | @Service |
| 47 | -public class StationRouteServiceImpl extends BaseServiceImpl<StationRoute, Integer> implements StationRouteService{ | 45 | +public class StationRouteServiceImpl extends BaseServiceImpl<StationRoute, Integer> implements StationRouteService { |
| 46 | + | ||
| 47 | + @Value("${path.speech.common}") | ||
| 48 | + private String commonPath; | ||
| 49 | + | ||
| 50 | + @Value("${path.speech.line}") | ||
| 51 | + private String linePathPattern; | ||
| 48 | 52 | ||
| 49 | @Autowired | 53 | @Autowired |
| 50 | private StationRouteRepository stationRouteRepository; | 54 | private StationRouteRepository stationRouteRepository; |
| @@ -431,6 +435,12 @@ public class StationRouteServiceImpl extends BaseServiceImpl<StationRoute, Integ | @@ -431,6 +435,12 @@ public class StationRouteServiceImpl extends BaseServiceImpl<StationRoute, Integ | ||
| 431 | List<Object[]> objects = stationRouteRepository.usingSingle(lineId); | 435 | List<Object[]> objects = stationRouteRepository.usingSingle(lineId); |
| 432 | List<LineRegion> lineRegions = lineRegionRepository.findAll(new CustomerSpecs<>(param)); | 436 | List<LineRegion> lineRegions = lineRegionRepository.findAll(new CustomerSpecs<>(param)); |
| 433 | if (objects.size()>0) { | 437 | if (objects.size()>0) { |
| 438 | + // 报站音频 | ||
| 439 | + Set<String> languages = new HashSet<>(); | ||
| 440 | + languages.add("cn"); | ||
| 441 | + languages.add("sh"); | ||
| 442 | + languages.add("en"); | ||
| 443 | + InputStream tts = ttsAndZip(objects, line, languages); | ||
| 434 | /** 获取配置文件里的ftp登录参数 */ | 444 | /** 获取配置文件里的ftp登录参数 */ |
| 435 | Map<String, Object> FTPParamMap = readPropertiesGetFTPParam(); | 445 | Map<String, Object> FTPParamMap = readPropertiesGetFTPParam(); |
| 436 | // 压缩文件名 | 446 | // 压缩文件名 |
| @@ -498,6 +508,7 @@ public class StationRouteServiceImpl extends BaseServiceImpl<StationRoute, Integ | @@ -498,6 +508,7 @@ public class StationRouteServiceImpl extends BaseServiceImpl<StationRoute, Integ | ||
| 498 | clientUtils.uploadFile(url, port, username, password, remotePath + "/voice/", textFileName, input); | 508 | clientUtils.uploadFile(url, port, username, password, remotePath + "/voice/", textFileName, input); |
| 499 | 509 | ||
| 500 | // textFile.delete(); | 510 | // textFile.delete(); |
| 511 | + // 线路区间 | ||
| 501 | if (lineRegions.size() > 0) { | 512 | if (lineRegions.size() > 0) { |
| 502 | FTPClientUtils.deleteFileByPrefix(String.format("%s-", line.getLineCode()), url, port, username, password, String.format("%s/voice/", remotePath)); | 513 | FTPClientUtils.deleteFileByPrefix(String.format("%s-", line.getLineCode()), url, port, username, password, String.format("%s/voice/", remotePath)); |
| 503 | for (LineRegion lineRegion : lineRegions) { | 514 | for (LineRegion lineRegion : lineRegions) { |
| @@ -507,6 +518,10 @@ public class StationRouteServiceImpl extends BaseServiceImpl<StationRoute, Integ | @@ -507,6 +518,10 @@ public class StationRouteServiceImpl extends BaseServiceImpl<StationRoute, Integ | ||
| 507 | } | 518 | } |
| 508 | } | 519 | } |
| 509 | 520 | ||
| 521 | + | ||
| 522 | + clientUtils.deleteFtpFile(url, port, username, password, remotePath + "/voice/", String.format("%s.zip", line.getLineCode())); | ||
| 523 | + clientUtils.uploadFile(url, port, username, password, remotePath + "/voice/", String.format("%s.zip", line.getLineCode()), tts); | ||
| 524 | + | ||
| 510 | resultMap.put("status", ResponseCode.SUCCESS); | 525 | resultMap.put("status", ResponseCode.SUCCESS); |
| 511 | }else { | 526 | }else { |
| 512 | resultMap.put("status","NOTDATA"); | 527 | resultMap.put("status","NOTDATA"); |
| @@ -1043,4 +1058,240 @@ public class StationRouteServiceImpl extends BaseServiceImpl<StationRoute, Integ | @@ -1043,4 +1058,240 @@ public class StationRouteServiceImpl extends BaseServiceImpl<StationRoute, Integ | ||
| 1043 | 1058 | ||
| 1044 | return builder.toString(); | 1059 | return builder.toString(); |
| 1045 | } | 1060 | } |
| 1061 | + | ||
| 1062 | + /** | ||
| 1063 | + * tts合成及打包 | ||
| 1064 | + * @param objects | ||
| 1065 | + * @param line | ||
| 1066 | + * @param languages 语言 如:cn、en、sh | ||
| 1067 | + */ | ||
| 1068 | + private InputStream ttsAndZip(List<Object[]> objects, Line line, Set<String> languages) throws Exception { | ||
| 1069 | + String lineId = line.getLineCode(); | ||
| 1070 | + StringBuilder cnBuilder = new StringBuilder(line.getName()).append("[p1000]"), enBuilder = new StringBuilder("Hello[p1000]"); | ||
| 1071 | + int ups = 0, downs = 0; | ||
| 1072 | + for (int i = 0;i < objects.size();i++) { | ||
| 1073 | + Object[] objArr = objects.get(i); | ||
| 1074 | + int direction = (int) objArr[8]; | ||
| 1075 | + String stationName = objArr[7] == null ? null : objArr[7].toString(), stationNameEn = objArr[9] == null ? null : objArr[9].toString(); | ||
| 1076 | + if (StringUtils.isEmpty(stationName)) { | ||
| 1077 | + throw new RuntimeException("存在异常的中文站点名称"); | ||
| 1078 | + } | ||
| 1079 | + // 如果要生成英语报站语音 | ||
| 1080 | + if (languages.contains("en")) { | ||
| 1081 | + if (StringUtils.isEmpty(stationNameEn)) { | ||
| 1082 | + throw new RuntimeException("存在异常的英文站点名称"); | ||
| 1083 | + } | ||
| 1084 | + enBuilder.append(stationNameEn).append("[p1000]"); | ||
| 1085 | + } | ||
| 1086 | + cnBuilder.append(stationName).append("[p1000]"); | ||
| 1087 | + if (direction == 0) { | ||
| 1088 | + ups++; | ||
| 1089 | + } else if (direction == 1) { | ||
| 1090 | + // 环线 | ||
| 1091 | + if (line.getLinePlayType() == 1) { | ||
| 1092 | + break; | ||
| 1093 | + } | ||
| 1094 | + downs++; | ||
| 1095 | + } | ||
| 1096 | + } | ||
| 1097 | + cnBuilder.delete(cnBuilder.length() - 8, cnBuilder.length() - 1); | ||
| 1098 | + enBuilder.delete(enBuilder.length() - 8, enBuilder.length() - 1); | ||
| 1099 | + | ||
| 1100 | + // 文本转语音并进行分割 | ||
| 1101 | + // 音频存放及压缩文件路径 | ||
| 1102 | + String linePath = String.format(linePathPattern, lineId), voicePath = String.format("%s%s.zip", linePath, lineId); | ||
| 1103 | + try { | ||
| 1104 | + String path = String.format("%scn.mp3", linePath); | ||
| 1105 | + IFlyUtils.textToSpeechCn(cnBuilder.toString(), path); | ||
| 1106 | + AudioOperationUtils.splitBySilence(path, String.format("%scn", linePath), 500, -40); | ||
| 1107 | + } catch (Exception e) { | ||
| 1108 | + throw new RuntimeException(e); | ||
| 1109 | + } | ||
| 1110 | + if (languages.contains("sh")) { | ||
| 1111 | + try { | ||
| 1112 | + String path = String.format("%ssh.mp3", linePath); | ||
| 1113 | + IFlyUtils.textToSpeechSh(cnBuilder.toString(), path); | ||
| 1114 | + AudioOperationUtils.splitBySilence(path, String.format("%ssh", linePath), 500, -40); | ||
| 1115 | + } catch (Exception e) { | ||
| 1116 | + throw new RuntimeException(e); | ||
| 1117 | + } | ||
| 1118 | + } | ||
| 1119 | + if (languages.contains("en")) { | ||
| 1120 | + try { | ||
| 1121 | + String path = String.format("%sen.mp3", linePath); | ||
| 1122 | + IFlyUtils.textToSpeechEn(enBuilder.toString(), path); | ||
| 1123 | + AudioOperationUtils.splitBySilence(path, String.format("%sen", linePath), 500, -40); | ||
| 1124 | + } catch (Exception e) { | ||
| 1125 | + throw new RuntimeException(e); | ||
| 1126 | + } | ||
| 1127 | + } | ||
| 1128 | + // 删除原线路音频 | ||
| 1129 | + File file = new File(linePath); | ||
| 1130 | + for (File f : file.listFiles()) { | ||
| 1131 | + if (f.isFile() && f.getName().endsWith(".mp3")) { | ||
| 1132 | + file.delete(); | ||
| 1133 | + } | ||
| 1134 | + } | ||
| 1135 | + | ||
| 1136 | + // 合并每站起步、到达语音 | ||
| 1137 | + int seq = 1, startSeq = 1, direction = 0; | ||
| 1138 | + seq = merge(lineId, seq, startSeq, direction, ups, languages); | ||
| 1139 | + startSeq = ups + 1; | ||
| 1140 | + direction = 1; | ||
| 1141 | + merge(lineId, seq, startSeq, direction, ups + downs, languages); | ||
| 1142 | + | ||
| 1143 | + // 压缩音频到zip | ||
| 1144 | + ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(voicePath)); | ||
| 1145 | + for (File f : file.listFiles()) { | ||
| 1146 | + if (f.isFile() && f.getName().endsWith(".mp3")) { | ||
| 1147 | + addFileToZip(zos, f); | ||
| 1148 | + } | ||
| 1149 | + } | ||
| 1150 | + // Key打头音频 | ||
| 1151 | + file = new File(commonPath); | ||
| 1152 | + for (File f : file.listFiles()) { | ||
| 1153 | + if (f.isFile() && f.getName().startsWith("Key-")) { | ||
| 1154 | + addFileToZip(zos, f); | ||
| 1155 | + } | ||
| 1156 | + } | ||
| 1157 | + zos.flush(); | ||
| 1158 | + zos.close(); | ||
| 1159 | + | ||
| 1160 | + return new FileInputStream(voicePath); | ||
| 1161 | + } | ||
| 1162 | + | ||
| 1163 | + private static void addFileToZip(ZipOutputStream zos, File file) throws IOException { | ||
| 1164 | + FileInputStream fis = new FileInputStream(file); | ||
| 1165 | + ZipEntry zipEntry = new ZipEntry(file.getName()); | ||
| 1166 | + zos.putNextEntry(zipEntry); | ||
| 1167 | + | ||
| 1168 | + byte[] bytes = new byte[4096]; | ||
| 1169 | + int length; | ||
| 1170 | + while ((length = fis.read(bytes)) >= 0) { | ||
| 1171 | + zos.write(bytes, 0, length); | ||
| 1172 | + } | ||
| 1173 | + | ||
| 1174 | + zos.closeEntry(); | ||
| 1175 | + fis.close(); | ||
| 1176 | + } | ||
| 1177 | + | ||
| 1178 | + private int merge(String lineId, int seq, int startSeq, int direction, int terminal, Set<String> languages) throws Exception { | ||
| 1179 | + for (int i = startSeq;i <= terminal;i++) { | ||
| 1180 | + if (i > startSeq) { | ||
| 1181 | + if (i == startSeq + 1) { | ||
| 1182 | + // 生成首站 | ||
| 1183 | + mergeOriginStart(lineId, seq, i, direction, terminal, languages); | ||
| 1184 | + mergeNormalArrive(lineId, seq, i, direction, terminal, languages); | ||
| 1185 | + } else if (i == terminal) { | ||
| 1186 | + // 生成终点站 | ||
| 1187 | + mergeNormalStart(lineId, seq, i, direction, terminal, languages); | ||
| 1188 | + mergeTerminalArrive(lineId, seq, i, direction, terminal, languages); | ||
| 1189 | + } else { | ||
| 1190 | + // 生成中途站 | ||
| 1191 | + mergeNormalStart(lineId, seq, i, direction, terminal, languages); | ||
| 1192 | + mergeNormalArrive(lineId, seq, i, direction, terminal, languages); | ||
| 1193 | + } | ||
| 1194 | + seq++; | ||
| 1195 | + } | ||
| 1196 | + } | ||
| 1197 | + | ||
| 1198 | + return seq; | ||
| 1199 | + } | ||
| 1200 | + | ||
| 1201 | + private void mergeOriginStart(String lineId, int seq, int stationLevel, int direction, int terminal, Set<String> languages) throws Exception { | ||
| 1202 | + String linePath = String.format(linePathPattern, lineId); | ||
| 1203 | + List<String> arr = Arrays.asList(commonPath + "cn_origin_1.mp3", linePath + String.format("cn/%03d.mp3", 0), commonPath + "cn_origin_2.mp3", linePath + String.format("cn/%03d.mp3", terminal), commonPath + "sh_origin_1.mp3", linePath + String.format("sh/%03d.mp3", 0), commonPath + "sh_origin_2.mp3", linePath + String.format("cn/%03d.mp3", terminal), commonPath + "cn_start_1.mp3", linePath + String.format("cn/%03d.mp3", stationLevel), commonPath + "cn_start_2.mp3", commonPath + "sh_start_1.mp3", linePath + String.format("sh/%03d.mp3", stationLevel), commonPath + "sh_start_2.mp3", commonPath + "en_1.mp3", linePath + String.format("en/%03d.mp3", stationLevel), commonPath + "cn_start_3.mp3", commonPath + "sh_start_3.mp3"); | ||
| 1204 | + List<String> inputPaths = new ArrayList<>(); | ||
| 1205 | + for (String path : arr) { | ||
| 1206 | + for (String lang : languages) { | ||
| 1207 | + if (path.indexOf(lang) > -1) { | ||
| 1208 | + inputPaths.add(path); | ||
| 1209 | + break; | ||
| 1210 | + } | ||
| 1211 | + } | ||
| 1212 | + } | ||
| 1213 | + AudioOperationUtils.merge(inputPaths, String.format("%s%03da%s-%03d-%s-Start.mp3", linePath, seq, direction == 0 ? "u" : "d", direction == 0 ? stationLevel : terminal - stationLevel + 1, direction == 0 ? "Up" : "Dn")); | ||
| 1214 | + } | ||
| 1215 | + | ||
| 1216 | + private void mergeNormalStart(String lineId, int seq, int stationLevel, int direction, int terminal, Set<String> languages) throws Exception { | ||
| 1217 | + String linePath = String.format(linePathPattern, lineId); | ||
| 1218 | + List<String> arr = Arrays.asList(commonPath + "cn_start.mp3", linePath + String.format("cn/%03d.mp3", stationLevel), commonPath + "cn_start_2.mp3", commonPath + "sh_start_1.mp3", linePath + String.format("sh/%03d.mp3", stationLevel), commonPath + "sh_start_2.mp3", commonPath + "en_1.mp3", linePath + String.format("en/%03d.mp3", stationLevel), commonPath + "cn_start_3.mp3", commonPath + "sh_start_3.mp3"); | ||
| 1219 | + List<String> inputPaths = new ArrayList<>(); | ||
| 1220 | + for (String path : arr) { | ||
| 1221 | + for (String lang : languages) { | ||
| 1222 | + if (path.indexOf(lang) > -1) { | ||
| 1223 | + inputPaths.add(path); | ||
| 1224 | + break; | ||
| 1225 | + } | ||
| 1226 | + } | ||
| 1227 | + } | ||
| 1228 | + AudioOperationUtils.merge(inputPaths, String.format("%s%03da%s-%03d-%s-Start.mp3", linePath, seq, direction == 0 ? "u" : "d", direction == 0 ? stationLevel : terminal - stationLevel + 1, direction == 0 ? "Up" : "Dn")); | ||
| 1229 | + } | ||
| 1230 | + | ||
| 1231 | + private void mergeNormalArrive(String lineId, int seq, int stationLevel, int direction, int terminal, Set<String> languages) throws Exception { | ||
| 1232 | + String linePath = String.format(linePathPattern, lineId); | ||
| 1233 | + List<String> arr = new ArrayList<>(Arrays.asList(commonPath + "cn_arrive.mp3", linePath + String.format("cn/%03d.mp3", stationLevel), commonPath + "cn_arrive_1.mp3", linePath + String.format("sh/%03d.mp3", stationLevel), commonPath + "sh_arrive_1.mp3", commonPath + "en_2.mp3", linePath + String.format("en/%03d.mp3", stationLevel))); | ||
| 1234 | + List<String> inputPaths = new ArrayList<>(); | ||
| 1235 | + if (languages.contains("sh")) { | ||
| 1236 | + for (int i = 0;i < 3;i++) { | ||
| 1237 | + arr.add(linePath + String.format("cn/%03d.mp3", 0)); | ||
| 1238 | + arr.add(commonPath + "cn_origin_2.mp3"); | ||
| 1239 | + arr.add(linePath + String.format("cn/%03d.mp3", terminal)); | ||
| 1240 | + arr.add(linePath + String.format("sh/%03d.mp3", 0)); | ||
| 1241 | + arr.add(commonPath + "sh_origin_2.mp3"); | ||
| 1242 | + arr.add(linePath + String.format("sh/%03d.mp3", terminal)); | ||
| 1243 | + } | ||
| 1244 | + } else { | ||
| 1245 | + arr.add(linePath + String.format("cn/%03d.mp3", 0)); | ||
| 1246 | + arr.add(commonPath + "cn_origin_2.mp3"); | ||
| 1247 | + arr.add(linePath + String.format("cn/%03d.mp3", terminal)); | ||
| 1248 | + } | ||
| 1249 | + for (String path : arr) { | ||
| 1250 | + for (String lang : languages) { | ||
| 1251 | + if (path.indexOf(lang) > -1) { | ||
| 1252 | + inputPaths.add(path); | ||
| 1253 | + break; | ||
| 1254 | + } | ||
| 1255 | + } | ||
| 1256 | + } | ||
| 1257 | + AudioOperationUtils.merge(inputPaths, String.format("%s%03db%s-%03d-%s-Arrive.mp3", linePath, seq, direction == 0 ? "u" : "d", direction == 0 ? stationLevel : terminal - stationLevel + 1, direction == 0 ? "Up" : "Dn")); | ||
| 1258 | + } | ||
| 1259 | + | ||
| 1260 | + private void mergeTerminalArrive(String lineId, int seq, int stationLevel, int direction, int terminal, Set<String> languages) throws Exception { | ||
| 1261 | + String linePath = String.format(linePathPattern, lineId); | ||
| 1262 | + List<String> arr = Arrays.asList(commonPath + "cn_terminal.mp3", linePath + String.format("cn/%03d.mp3", stationLevel), commonPath + "cn_arrive_1.mp3", commonPath + "sh_terminal.mp3", linePath + String.format("sh/%03d.mp3", stationLevel), commonPath + "sh_arrive_1.mp3", commonPath + "en_3.mp3", linePath + String.format("en/%03d.mp3", stationLevel), commonPath + "terminal_music.mp3"); | ||
| 1263 | + List<String> inputPaths = new ArrayList<>(); | ||
| 1264 | + for (String path : arr) { | ||
| 1265 | + for (String lang : languages) { | ||
| 1266 | + if (path.indexOf(lang) > -1 || path.indexOf("terminal_music") > -1) { | ||
| 1267 | + inputPaths.add(path); | ||
| 1268 | + break; | ||
| 1269 | + } | ||
| 1270 | + } | ||
| 1271 | + } | ||
| 1272 | + AudioOperationUtils.merge(inputPaths, String.format("%s%03db%s-%03d-%s-Arrive.mp3", linePath, seq, direction == 0 ? "u" : "d", direction == 0 ? stationLevel : terminal - stationLevel + 1, direction == 0 ? "Up" : "Dn")); | ||
| 1273 | + } | ||
| 1274 | + | ||
| 1275 | + private void description() { | ||
| 1276 | + // Next stop is (en_1.mp3) | ||
| 1277 | + // We are arrival at (en_2.mp3) | ||
| 1278 | + // We are arrival at the terminal (en_3.mp3) | ||
| 1279 | + // 叮咚+欢迎乘坐 (cn_origin_1.mp3) | ||
| 1280 | + // 公交车方向 (cn_origin_2.mp3) | ||
| 1281 | + // 叮咚+车辆起步请拉好扶手投币后请配合朝里走下一站 (cn_start.mp3) | ||
| 1282 | + // 下一站 (cn_start_1.mp3) | ||
| 1283 | + // 请准备从后门下车 (cn_start_2.mp3) | ||
| 1284 | + // 乘客们请给需要帮助的乘客让个座谢谢 (cn_start_3.mp3) | ||
| 1285 | + // 叮咚+车辆进站请注意安全 (cn_arrive.mp3) | ||
| 1286 | + // 到了请配合从后门下车开门请当心 (cn_arrive_1.mp3) | ||
| 1287 | + // 叮咚+终点站 (cn_terminal.mp3) | ||
| 1288 | + // 欢迎乘坐(沪) (sh_origin_1.mp3) | ||
| 1289 | + // 公交车方向(沪) (sh_origin_2.mp3) | ||
| 1290 | + // 下一站(沪) (sh_start_1.mp3) | ||
| 1291 | + // 请准备从后门下车 (sh_start_2.mp3) | ||
| 1292 | + // 乘客们请给需要帮助的乘客让个座谢谢(沪) (sh_start_3.mp3) | ||
| 1293 | + // 到了请配合从后门下车开门请当心(沪) (sh_arrive_1.mp3) | ||
| 1294 | + // 终点站(沪) (sh_terminal.mp3) | ||
| 1295 | + // 终点音乐 (terminal_music.mp3) | ||
| 1296 | + } | ||
| 1046 | } | 1297 | } |
src/main/java/com/bsth/util/AudioOperationUtils.java
0 → 100644
| 1 | +package com.bsth.util; | ||
| 2 | + | ||
| 3 | + | ||
| 4 | +import org.bytedeco.javacv.FFmpegFrameGrabber; | ||
| 5 | +import org.bytedeco.javacv.FFmpegFrameRecorder; | ||
| 6 | +import org.bytedeco.javacv.Frame; | ||
| 7 | + | ||
| 8 | +import java.io.*; | ||
| 9 | +import java.nio.ShortBuffer; | ||
| 10 | +import java.util.ArrayList; | ||
| 11 | +import java.util.List; | ||
| 12 | + | ||
| 13 | +public class AudioOperationUtils { | ||
| 14 | + public static void main(String[] args) throws Exception { | ||
| 15 | +// int duration = 450000; | ||
| 16 | +// double silenceThreshold = -40; // 声音阈值,可以根据需要调整,单位通常是分贝dB | ||
| 17 | +// splitBySilence("D:\\tts.mp3", "D:\\88814\\cn", duration, silenceThreshold); | ||
| 18 | + | ||
| 19 | + List<String> paths = new ArrayList<>(); | ||
| 20 | + paths.add("D:/speech/803111/cn/000.mp3"); | ||
| 21 | + paths.add("D:/speech/803111/cn/001.mp3"); | ||
| 22 | + merge(paths, "D:/speech/803111/results.mp3"); | ||
| 23 | + } | ||
| 24 | + | ||
| 25 | + public static void merge(List<String> inputPaths, String outputPath) throws Exception { | ||
| 26 | + File file = new File(outputPath); | ||
| 27 | + if (!file.getParentFile().exists()) { | ||
| 28 | + file.getParentFile().mkdirs(); | ||
| 29 | + } | ||
| 30 | + FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(new File(outputPath), 1); | ||
| 31 | + | ||
| 32 | + int i = 0; | ||
| 33 | + Frame frame = null; | ||
| 34 | + for (String inputPath : inputPaths) { | ||
| 35 | + FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(inputPath); | ||
| 36 | + grabber.start(); | ||
| 37 | + if (i == 0) { | ||
| 38 | + recorder.setAudioCodec(grabber.getAudioCodec()); | ||
| 39 | + recorder.setFrameRate(grabber.getFrameRate()); | ||
| 40 | + recorder.setFormat(grabber.getFormat()); | ||
| 41 | + recorder.start(); | ||
| 42 | + } | ||
| 43 | + while ((frame = grabber.grabSamples()) != null) { | ||
| 44 | + recorder.record(frame); | ||
| 45 | + } | ||
| 46 | + grabber.stop(); | ||
| 47 | + grabber.close(); | ||
| 48 | + i++; | ||
| 49 | + } | ||
| 50 | + recorder.stop(); | ||
| 51 | + recorder.close(); | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + public static void splitBySilence(String inputPath, String outputPath, int duration, double silenceThreshold) throws Exception { | ||
| 55 | + File file = new File(outputPath); | ||
| 56 | + if (!file.exists()) { | ||
| 57 | + file.mkdirs(); | ||
| 58 | + } | ||
| 59 | + boolean isSilent = false; | ||
| 60 | + FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(inputPath); | ||
| 61 | + grabber.start(); | ||
| 62 | + Frame frame = null; | ||
| 63 | + List<SilenceInfo> silenceInfos = new ArrayList<>(); | ||
| 64 | + long start = 0, end = 0; | ||
| 65 | + while((frame = grabber.grabSamples()) != null) { | ||
| 66 | + ShortBuffer buffer = (ShortBuffer) frame.samples[0]; | ||
| 67 | + double energy = 0; | ||
| 68 | + while (buffer.hasRemaining()) { | ||
| 69 | + short sample = buffer.get(); | ||
| 70 | + energy += sample * sample; | ||
| 71 | + } | ||
| 72 | + energy /= buffer.capacity(); | ||
| 73 | + double db = 20 * Math.log10(Math.sqrt(energy)); | ||
| 74 | + if (db < silenceThreshold) { | ||
| 75 | + if (!isSilent && start == 0) { | ||
| 76 | + isSilent = true; | ||
| 77 | + start = frame.timestamp; | ||
| 78 | + } | ||
| 79 | + } else { | ||
| 80 | + if (start != 0 && frame.timestamp - start >= duration) { | ||
| 81 | + SilenceInfo info = new SilenceInfo(end, start); | ||
| 82 | + silenceInfos.add(info); | ||
| 83 | + end = frame.timestamp; | ||
| 84 | + } | ||
| 85 | + isSilent = false; | ||
| 86 | + start = 0; | ||
| 87 | + } | ||
| 88 | + } | ||
| 89 | + if (silenceInfos.size() > 0) { | ||
| 90 | + SilenceInfo info = new SilenceInfo(end, grabber.getLengthInTime()); | ||
| 91 | + silenceInfos.add(info); | ||
| 92 | + } | ||
| 93 | + grabber.stop(); | ||
| 94 | + grabber.close(); | ||
| 95 | + grabber = new FFmpegFrameGrabber(inputPath); | ||
| 96 | + grabber.start(); | ||
| 97 | + int idx = 0; | ||
| 98 | + boolean flag = false; | ||
| 99 | + FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(new File(String.format("%s%s%03d.mp3", outputPath, File.separator, idx)), 1); | ||
| 100 | + recorder.setAudioCodec(grabber.getAudioCodec()); | ||
| 101 | + recorder.setFrameRate(grabber.getFrameRate()); | ||
| 102 | + recorder.setFormat(grabber.getFormat()); | ||
| 103 | + recorder.start(); | ||
| 104 | + SilenceInfo info = silenceInfos.get(idx); | ||
| 105 | + while ((frame = grabber.grabSamples()) != null) { | ||
| 106 | + if (idx < silenceInfos.size()) { | ||
| 107 | + if (frame.timestamp >= info.start && frame.timestamp <= info.end) { | ||
| 108 | + recorder.record(frame); | ||
| 109 | + flag = false; | ||
| 110 | + } else { | ||
| 111 | + if (!flag) { | ||
| 112 | + flag = true; | ||
| 113 | + recorder.stop(); | ||
| 114 | + recorder.close(); | ||
| 115 | + idx++; | ||
| 116 | + info = silenceInfos.get(idx); | ||
| 117 | + recorder = new FFmpegFrameRecorder(new File(String.format("%s%s%03d.mp3", outputPath, File.separator, idx)), 1); | ||
| 118 | + recorder.start(); | ||
| 119 | + recorder.setAudioCodec(grabber.getAudioCodec()); | ||
| 120 | + recorder.setFrameRate(grabber.getFrameRate()); | ||
| 121 | + recorder.setFormat(grabber.getFormat()); | ||
| 122 | + } | ||
| 123 | + } | ||
| 124 | + } | ||
| 125 | + } | ||
| 126 | + if (recorder != null) { | ||
| 127 | + recorder.stop(); | ||
| 128 | + recorder.close(); | ||
| 129 | + } | ||
| 130 | + } | ||
| 131 | + | ||
| 132 | + public final static class SilenceInfo { | ||
| 133 | + | ||
| 134 | + private long start; | ||
| 135 | + | ||
| 136 | + private long end; | ||
| 137 | + | ||
| 138 | + public SilenceInfo() {} | ||
| 139 | + | ||
| 140 | + public SilenceInfo(long start, long end) { | ||
| 141 | + this.start = start; | ||
| 142 | + this.end = end; | ||
| 143 | + } | ||
| 144 | + | ||
| 145 | + public long getStart() { | ||
| 146 | + return start; | ||
| 147 | + } | ||
| 148 | + | ||
| 149 | + public void setStart(long start) { | ||
| 150 | + this.start = start; | ||
| 151 | + } | ||
| 152 | + | ||
| 153 | + public long getEnd() { | ||
| 154 | + return end; | ||
| 155 | + } | ||
| 156 | + | ||
| 157 | + public void setEnd(long end) { | ||
| 158 | + this.end = end; | ||
| 159 | + } | ||
| 160 | + } | ||
| 161 | +} |
src/main/java/com/bsth/util/IFlyUtils.java
0 → 100644
| 1 | +package com.bsth.util; | ||
| 2 | + | ||
| 3 | +import com.bsth.entity.speech.SpeechRequest; | ||
| 4 | +import com.bsth.entity.speech.SpeechResponse; | ||
| 5 | +import com.fasterxml.jackson.core.JsonProcessingException; | ||
| 6 | +import com.fasterxml.jackson.databind.ObjectMapper; | ||
| 7 | +import org.java_websocket.client.WebSocketClient; | ||
| 8 | +import org.java_websocket.handshake.ServerHandshake; | ||
| 9 | +import org.slf4j.Logger; | ||
| 10 | +import org.slf4j.LoggerFactory; | ||
| 11 | + | ||
| 12 | +import javax.crypto.Mac; | ||
| 13 | +import javax.crypto.spec.SecretKeySpec; | ||
| 14 | +import java.io.*; | ||
| 15 | +import java.net.URI; | ||
| 16 | +import java.net.URL; | ||
| 17 | +import java.net.URLEncoder; | ||
| 18 | +import java.nio.charset.StandardCharsets; | ||
| 19 | +import java.text.SimpleDateFormat; | ||
| 20 | +import java.util.*; | ||
| 21 | + | ||
| 22 | +public class IFlyUtils { | ||
| 23 | + | ||
| 24 | + private final static ObjectMapper mapper = new ObjectMapper(); | ||
| 25 | + | ||
| 26 | + private final static Logger log = LoggerFactory.getLogger(IFlyUtils.class); | ||
| 27 | + | ||
| 28 | + private final static String apiKey = "46780e6779b6b1ba93503f24f097b771"; | ||
| 29 | + | ||
| 30 | + private final static String apiSecret = "ZWFjNzkzMTkzNzI3YmMzMTgwMWUzMWE0"; | ||
| 31 | + | ||
| 32 | + private final static String appId = "b4b21ad4"; | ||
| 33 | + | ||
| 34 | + /** | ||
| 35 | + * 生成普通话语音 | ||
| 36 | + * text以","进行分割 | ||
| 37 | + * @param text | ||
| 38 | + */ | ||
| 39 | + public static void textToSpeechCn(String text, String outputPath) throws Exception { | ||
| 40 | + String wsUrl = getAuthUrl("https://tts-api.xfyun.cn/v2/tts", apiKey, apiSecret).replace("https://", "wss://"); | ||
| 41 | + SpeechRequest request = new SpeechRequest(); | ||
| 42 | + request.getCommon().put("app_id", appId); | ||
| 43 | + request.getBusiness().put("aue", "lame"); | ||
| 44 | + request.getBusiness().put("sfl", 1); | ||
| 45 | + request.getBusiness().put("vcn", "x4_lingxiaoshan_profnews"); | ||
| 46 | + request.getData().put("text", Base64.getEncoder().encodeToString(text.replace(",", "[p1000]").getBytes("GBK"))); | ||
| 47 | + request.getData().put("status", 2); | ||
| 48 | + websocketWork(wsUrl, request, new FileOutputStream(outputPath)); | ||
| 49 | + while (!request.isCompleted()) { | ||
| 50 | + Thread.sleep(500); | ||
| 51 | + } | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + /** | ||
| 55 | + * 生成上海话语音 | ||
| 56 | + * text以","进行分割 | ||
| 57 | + * @param text | ||
| 58 | + */ | ||
| 59 | + public static void textToSpeechSh(String text, String outputPath) throws Exception { | ||
| 60 | + String wsUrl = getAuthUrl("https://tts-api.xfyun.cn/v2/tts", apiKey, apiSecret).replace("https://", "wss://"); | ||
| 61 | + SpeechRequest request = new SpeechRequest(); | ||
| 62 | + request.getCommon().put("app_id", appId); | ||
| 63 | + request.getBusiness().put("aue", "lame"); | ||
| 64 | + request.getBusiness().put("sfl", 1); | ||
| 65 | + request.getBusiness().put("vcn", "x3_ziling"); | ||
| 66 | + request.getData().put("text", Base64.getEncoder().encodeToString(text.replace(",", "[p1000]").getBytes("GBK"))); | ||
| 67 | + request.getData().put("status", 2); | ||
| 68 | + websocketWork(wsUrl, request, new FileOutputStream(outputPath)); | ||
| 69 | + while (!request.isCompleted()) { | ||
| 70 | + Thread.sleep(500); | ||
| 71 | + } | ||
| 72 | + } | ||
| 73 | + | ||
| 74 | + /** | ||
| 75 | + * 生成英语语音 | ||
| 76 | + * text以","进行分割 | ||
| 77 | + * @param text | ||
| 78 | + */ | ||
| 79 | + public static void textToSpeechEn(String text, String outputPath) throws Exception { | ||
| 80 | + String wsUrl = getAuthUrl("https://tts-api.xfyun.cn/v2/tts", apiKey, apiSecret).replace("https://", "wss://"); | ||
| 81 | + SpeechRequest request = new SpeechRequest(); | ||
| 82 | + request.getCommon().put("app_id", appId); | ||
| 83 | + request.getBusiness().put("aue", "lame"); | ||
| 84 | + request.getBusiness().put("sfl", 1); | ||
| 85 | + request.getBusiness().put("vcn", "x4_enus_luna_assist"); | ||
| 86 | + request.getData().put("text", Base64.getEncoder().encodeToString(text.replace(",", "[p1000]").getBytes(StandardCharsets.UTF_8))); | ||
| 87 | + request.getData().put("status", 2); | ||
| 88 | + websocketWork(wsUrl, request, new FileOutputStream(outputPath)); | ||
| 89 | + while (!request.isCompleted()) { | ||
| 90 | + Thread.sleep(500); | ||
| 91 | + } | ||
| 92 | + } | ||
| 93 | + | ||
| 94 | + public static void websocketWork(String wsUrl, SpeechRequest request, OutputStream out) { | ||
| 95 | + try { | ||
| 96 | + URI uri = new URI(wsUrl); | ||
| 97 | + WebSocketClient webSocketClient = new WebSocketClient(uri) { | ||
| 98 | + @Override | ||
| 99 | + public void onOpen(ServerHandshake serverHandshake) { | ||
| 100 | + try { | ||
| 101 | + this.send(mapper.writeValueAsString(request)); | ||
| 102 | + } catch (JsonProcessingException e) { | ||
| 103 | + throw new RuntimeException(e); | ||
| 104 | + } | ||
| 105 | + } | ||
| 106 | + | ||
| 107 | + @Override | ||
| 108 | + public void onMessage(String text) { | ||
| 109 | + try { | ||
| 110 | + SpeechResponse response = mapper.readValue(text, SpeechResponse.class); | ||
| 111 | + log.info("response: {}", response); | ||
| 112 | + if (response.getCode() != 0) { | ||
| 113 | + log.error("在线语音合成发生错误"); | ||
| 114 | + } | ||
| 115 | + if (response.getData() != null) { | ||
| 116 | + byte[] bytes = Base64.getDecoder().decode(response.getData().getAudio()); | ||
| 117 | + out.write(bytes); | ||
| 118 | + out.flush(); | ||
| 119 | + if (response.getData().getStatus() == 2) { | ||
| 120 | + request.setCompleted(true); | ||
| 121 | + out.close(); | ||
| 122 | + } | ||
| 123 | + } | ||
| 124 | + } catch (IOException e) { | ||
| 125 | + throw new RuntimeException(e); | ||
| 126 | + } | ||
| 127 | + } | ||
| 128 | + | ||
| 129 | + @Override | ||
| 130 | + public void onClose(int i, String s, boolean b) { | ||
| 131 | + System.out.println("ws链接已关闭,本次请求完成..."); | ||
| 132 | + } | ||
| 133 | + | ||
| 134 | + @Override | ||
| 135 | + public void onError(Exception e) { | ||
| 136 | + log.error("发生错误", e); | ||
| 137 | + } | ||
| 138 | + }; | ||
| 139 | + webSocketClient.connect(); | ||
| 140 | + } catch (Exception e) { | ||
| 141 | + log.error("", e); | ||
| 142 | + } | ||
| 143 | + } | ||
| 144 | + | ||
| 145 | + /** | ||
| 146 | + * 讯飞语音合成鉴权 | ||
| 147 | + * @param hostUrl | ||
| 148 | + * @param apiKey | ||
| 149 | + * @param apiSecret | ||
| 150 | + * @return | ||
| 151 | + * @throws Exception | ||
| 152 | + */ | ||
| 153 | + public static String getAuthUrl(String hostUrl, String apiKey, String apiSecret) throws Exception { | ||
| 154 | + URL url = new URL(hostUrl); | ||
| 155 | + // 时间 | ||
| 156 | + SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US); | ||
| 157 | + format.setTimeZone(TimeZone.getTimeZone("GMT")); | ||
| 158 | + String date = format.format(new Date()); | ||
| 159 | + // 拼接 | ||
| 160 | + String preStr = "host: " + url.getHost() + "\n" + | ||
| 161 | + "date: " + date + "\n" + | ||
| 162 | + "GET " + url.getPath() + " HTTP/1.1"; | ||
| 163 | + // SHA256加密 | ||
| 164 | + Mac mac = Mac.getInstance("hmacsha256"); | ||
| 165 | + SecretKeySpec spec = new SecretKeySpec(apiSecret.getBytes(StandardCharsets.UTF_8), "hmacsha256"); | ||
| 166 | + mac.init(spec); | ||
| 167 | + byte[] hexDigits = mac.doFinal(preStr.getBytes(StandardCharsets.UTF_8)); | ||
| 168 | + // Base64加密 | ||
| 169 | + String sha = Base64.getEncoder().encodeToString(hexDigits); | ||
| 170 | + // 拼接 | ||
| 171 | + String authorization = String.format("api_key=\"%s\", algorithm=\"%s\", headers=\"%s\", signature=\"%s\"", apiKey, "hmac-sha256", "host date request-line", sha); | ||
| 172 | + StringBuilder sb = new StringBuilder(hostUrl); | ||
| 173 | + sb.append("?authorization=").append(Base64.getEncoder().encodeToString(authorization.getBytes(StandardCharsets.UTF_8))) | ||
| 174 | + .append("&date=").append(URLEncoder.encode(date)) | ||
| 175 | + .append("&host=").append(URLEncoder.encode(url.getHost())); | ||
| 176 | +// // 拼接地址 | ||
| 177 | +// HttpUrl httpUrl = Objects.requireNonNull(HttpUrl.parse("https://" + url.getHost() + url.getPath())).newBuilder().// | ||
| 178 | +// addQueryParameter("authorization", Base64.getEncoder().encodeToString(authorization.getBytes(StandardCharsets.UTF_8))).// | ||
| 179 | +// addQueryParameter("date", date).// | ||
| 180 | +// addQueryParameter("host", url.getHost()).// | ||
| 181 | +// build(); | ||
| 182 | + | ||
| 183 | + return sb.toString(); | ||
| 184 | + } | ||
| 185 | +} |
src/main/resources/application-dev.properties
| @@ -42,4 +42,7 @@ sso.enabled= true | @@ -42,4 +42,7 @@ sso.enabled= true | ||
| 42 | sso.systemcode = SYSUS023 | 42 | sso.systemcode = SYSUS023 |
| 43 | sso.http.url.login= http://10.10.200.142:9112/login?redirect=%2Findex | 43 | sso.http.url.login= http://10.10.200.142:9112/login?redirect=%2Findex |
| 44 | sso.http.url.logout= http://10.10.200.142:9112/login?redirect=%2Findex | 44 | sso.http.url.logout= http://10.10.200.142:9112/login?redirect=%2Findex |
| 45 | -sso.http.url.auth= http://10.10.200.142:9112/prod-api/system/utilitySystem/checkToken | ||
| 46 | \ No newline at end of file | 45 | \ No newline at end of file |
| 46 | +sso.http.url.auth= http://10.10.200.142:9112/prod-api/system/utilitySystem/checkToken | ||
| 47 | + | ||
| 48 | +path.speech.common = D:/speech/common/ | ||
| 49 | +path.speech.line = D:/speech/%s/ | ||
| 47 | \ No newline at end of file | 50 | \ No newline at end of file |
src/main/resources/application-prod.properties
| 1 | -server.port=9088 | ||
| 2 | - | ||
| 3 | -# dubbo\uFFFD\uFFFD\uFFFD\uFFFD\u02B9\uFFFD\u00FF\uFFFD\uFFFD\uFFFDflag | ||
| 4 | -dubbo.use=false | ||
| 5 | - | ||
| 6 | -#JPA | ||
| 7 | -spring.jpa.hibernate.ddl-auto= none | ||
| 8 | -spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyHbmImpl | ||
| 9 | -spring.jpa.hibernate.naming.physical-strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy | ||
| 10 | -spring.jpa.database= MYSQL | ||
| 11 | -spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true | ||
| 12 | -spring.jpa.show-sql= false | ||
| 13 | -spring.jpa.properties.hibernate.dialect= org.hibernate.spatial.dialect.mysql.MySQL56InnoDBSpatialDialect | ||
| 14 | - | ||
| 15 | -#DATABASE | ||
| 16 | -spring.datasource.driver-class-name= com.mysql.jdbc.Driver | ||
| 17 | -spring.datasource.url= jdbc:mysql://10.10.150.103:3306/control?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai | ||
| 18 | -spring.datasource.username= root | ||
| 19 | -spring.datasource.password= fsodlgjiuigAQF2$9fs9 | ||
| 20 | -spring.datasource.type= com.zaxxer.hikari.HikariDataSource | ||
| 21 | - | ||
| 22 | -#DATASOURCE SETTING | ||
| 23 | -spring.datasource.hikari.minimum-idle= 8 | ||
| 24 | -spring.datasource.hikari.maximum-pool-size= 100 | ||
| 25 | -#spring.datasource.hikari.auto-commit= true | ||
| 26 | -spring.datasource.hikari.idle-timeout= 60000 | ||
| 27 | -#spring.datasource.hikari.pool-name= HikariPool | ||
| 28 | -spring.datasource.hikari.max-lifetime= 1800000 | ||
| 29 | -spring.datasource.hikari.connection-timeout= 3000 | ||
| 30 | -spring.datasource.hikari.connection-test-query= SELECT 1 | ||
| 31 | -spring.datasource.hikari.validation-timeout= 3000 | ||
| 32 | -spring.datasource.hikari.register-mbeans=true | ||
| 33 | - | ||
| 34 | -sso.enabled= true | ||
| 35 | -sso.systemcode = SYSUS023 | ||
| 36 | -sso.http.url.login= http://10.10.200.142:9112/login?redirect=%2Findex | ||
| 37 | -sso.http.url.logout= http://10.10.200.142:9112/login?redirect=%2Findex | ||
| 38 | -sso.http.url.auth= http://10.10.200.142:9112/prod-api/system/utilitySystem/checkToken | ||
| 39 | - | ||
| 40 | -dc.imgurl= /home/control/klimg | ||
| 41 | -dc.profile= profile | ||
| 42 | - | ||
| 43 | -baidu.ak=AYiBOs3f9qBQFhdKFsaboX6CfObmKwRP | ||
| 44 | -passengerFlow.url = http://192.168.168.32:9999/images/ | ||
| 45 | - | ||
| 46 | -electricity.importFile.path= /home/control/elecImportFile | ||
| 47 | -baidu.akyd=WnSDHZgtPbSbw2LfsH3KO3DDKWONmlYK | ||
| 48 | -report_register.complaint.url= http://10.10.200.113:8060/complaint/TsReport/input.do | 1 | +server.port=9088 |
| 2 | + | ||
| 3 | +# dubbo\uFFFD\uFFFD\uFFFD\uFFFD\u02B9\uFFFD\u00FF\uFFFD\uFFFD\uFFFDflag | ||
| 4 | +dubbo.use=false | ||
| 5 | + | ||
| 6 | +#JPA | ||
| 7 | +spring.jpa.hibernate.ddl-auto= none | ||
| 8 | +spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyHbmImpl | ||
| 9 | +spring.jpa.hibernate.naming.physical-strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy | ||
| 10 | +spring.jpa.database= MYSQL | ||
| 11 | +spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true | ||
| 12 | +spring.jpa.show-sql= false | ||
| 13 | +spring.jpa.properties.hibernate.dialect= org.hibernate.spatial.dialect.mysql.MySQL56InnoDBSpatialDialect | ||
| 14 | + | ||
| 15 | +#DATABASE | ||
| 16 | +spring.datasource.driver-class-name= com.mysql.jdbc.Driver | ||
| 17 | +spring.datasource.url= jdbc:mysql://10.10.150.103:3306/control?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai | ||
| 18 | +spring.datasource.username= root | ||
| 19 | +spring.datasource.password= fsodlgjiuigAQF2$9fs9 | ||
| 20 | +spring.datasource.type= com.zaxxer.hikari.HikariDataSource | ||
| 21 | + | ||
| 22 | +#DATASOURCE SETTING | ||
| 23 | +spring.datasource.hikari.minimum-idle= 8 | ||
| 24 | +spring.datasource.hikari.maximum-pool-size= 100 | ||
| 25 | +#spring.datasource.hikari.auto-commit= true | ||
| 26 | +spring.datasource.hikari.idle-timeout= 60000 | ||
| 27 | +#spring.datasource.hikari.pool-name= HikariPool | ||
| 28 | +spring.datasource.hikari.max-lifetime= 1800000 | ||
| 29 | +spring.datasource.hikari.connection-timeout= 3000 | ||
| 30 | +spring.datasource.hikari.connection-test-query= SELECT 1 | ||
| 31 | +spring.datasource.hikari.validation-timeout= 3000 | ||
| 32 | +spring.datasource.hikari.register-mbeans=true | ||
| 33 | + | ||
| 34 | +sso.enabled= true | ||
| 35 | +sso.systemcode = SYSUS023 | ||
| 36 | +sso.http.url.login= http://10.10.200.142:9112/login?redirect=%2Findex | ||
| 37 | +sso.http.url.logout= http://10.10.200.142:9112/login?redirect=%2Findex | ||
| 38 | +sso.http.url.auth= http://10.10.200.142:9112/prod-api/system/utilitySystem/checkToken | ||
| 39 | + | ||
| 40 | +dc.imgurl= /home/control/klimg | ||
| 41 | +dc.profile= profile | ||
| 42 | + | ||
| 43 | +baidu.ak=AYiBOs3f9qBQFhdKFsaboX6CfObmKwRP | ||
| 44 | +passengerFlow.url = http://192.168.168.32:9999/images/ | ||
| 45 | + | ||
| 46 | +electricity.importFile.path= /home/control/elecImportFile | ||
| 47 | +baidu.akyd=WnSDHZgtPbSbw2LfsH3KO3DDKWONmlYK | ||
| 48 | +report_register.complaint.url= http://10.10.200.113:8060/complaint/TsReport/input.do | ||
| 49 | + | ||
| 50 | +path.speech.common = /home/control/speech/common/ | ||
| 51 | +path.speech.line = /home/control/speech/%s/ |
src/main/resources/application-test.properties
| 1 | -server.port=9088 | ||
| 2 | - | ||
| 3 | -# dubbo\uFFFD\uFFFD\uFFFD\uFFFD\u02B9\uFFFD\u00FF\uFFFD\uFFFD\uFFFDflag | ||
| 4 | -dubbo.use=false | ||
| 5 | - | ||
| 6 | -#JPA | ||
| 7 | -spring.jpa.hibernate.ddl-auto= none | ||
| 8 | -spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyHbmImpl | ||
| 9 | -spring.jpa.hibernate.naming.physical-strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy | ||
| 10 | -spring.jpa.database= MYSQL | ||
| 11 | -spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true | ||
| 12 | -spring.jpa.show-sql= false | ||
| 13 | -spring.jpa.properties.hibernate.dialect= org.hibernate.spatial.dialect.mysql.MySQL56InnoDBSpatialDialect | ||
| 14 | - | ||
| 15 | -#DATABASE | ||
| 16 | -spring.datasource.driver-class-name= com.mysql.cj.jdbc.Driver | ||
| 17 | -spring.datasource.url= jdbc:mysql://10.10.150.101/lg_control?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=Asia/Shanghai | ||
| 18 | -spring.datasource.username= root | ||
| 19 | -spring.datasource.password= A123456ab | ||
| 20 | -spring.datasource.type= com.zaxxer.hikari.HikariDataSource | ||
| 21 | - | ||
| 22 | -#DATASOURCE SETTING | ||
| 23 | -spring.datasource.hikari.minimum-idle= 8 | ||
| 24 | -spring.datasource.hikari.maximum-pool-size= 100 | ||
| 25 | -#spring.datasource.hikari.auto-commit= true | ||
| 26 | -spring.datasource.hikari.idle-timeout= 60000 | ||
| 27 | -#spring.datasource.hikari.pool-name= HikariPool | ||
| 28 | -spring.datasource.hikari.max-lifetime= 1800000 | ||
| 29 | -spring.datasource.hikari.connection-timeout= 3000 | ||
| 30 | -spring.datasource.hikari.connection-test-query= SELECT 1 | ||
| 31 | -spring.datasource.hikari.validation-timeout= 3000 | ||
| 32 | -spring.datasource.hikari.register-mbeans=true | ||
| 33 | - | ||
| 34 | -sso.enabled= true | ||
| 35 | -sso.systemcode = SYSUS023 | ||
| 36 | -sso.http.url.login= http://10.10.200.142:9112/login?redirect=%2Findex | ||
| 37 | -sso.http.url.logout= http://10.10.200.142:9112/login?redirect=%2Findex | ||
| 38 | -sso.http.url.auth= http://10.10.200.142:9112/prod-api/system/utilitySystem/checkToken | ||
| 39 | - | ||
| 40 | -dc.imgurl= E:/klimg | ||
| 41 | -dc.profile= profile | ||
| 42 | - | ||
| 43 | -baidu.ak=AYiBOs3f9qBQFhdKFsaboX6CfObmKwRP | ||
| 44 | -passengerFlow.url = http://127.0.0.1:9999/images/ | ||
| 45 | -baidu.akyd=WnSDHZgtPbSbw2LfsH3KO3DDKWONmlYK | ||
| 46 | -electricity.importFile.path= E:/elecImportFile | ||
| 47 | - | ||
| 48 | -report_register.complaint.url= http://192.168.168.172:8080/complaint/TsReport/input.do | 1 | +server.port=9088 |
| 2 | + | ||
| 3 | +# dubbo\uFFFD\uFFFD\uFFFD\uFFFD\u02B9\uFFFD\u00FF\uFFFD\uFFFD\uFFFDflag | ||
| 4 | +dubbo.use=false | ||
| 5 | + | ||
| 6 | +#JPA | ||
| 7 | +spring.jpa.hibernate.ddl-auto= none | ||
| 8 | +spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyHbmImpl | ||
| 9 | +spring.jpa.hibernate.naming.physical-strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy | ||
| 10 | +spring.jpa.database= MYSQL | ||
| 11 | +spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true | ||
| 12 | +spring.jpa.show-sql= false | ||
| 13 | +spring.jpa.properties.hibernate.dialect= org.hibernate.spatial.dialect.mysql.MySQL56InnoDBSpatialDialect | ||
| 14 | + | ||
| 15 | +#DATABASE | ||
| 16 | +spring.datasource.driver-class-name= com.mysql.cj.jdbc.Driver | ||
| 17 | +spring.datasource.url= jdbc:mysql://10.10.150.101/lg_control?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=Asia/Shanghai | ||
| 18 | +spring.datasource.username= root | ||
| 19 | +spring.datasource.password= A123456ab | ||
| 20 | +spring.datasource.type= com.zaxxer.hikari.HikariDataSource | ||
| 21 | + | ||
| 22 | +#DATASOURCE SETTING | ||
| 23 | +spring.datasource.hikari.minimum-idle= 8 | ||
| 24 | +spring.datasource.hikari.maximum-pool-size= 100 | ||
| 25 | +#spring.datasource.hikari.auto-commit= true | ||
| 26 | +spring.datasource.hikari.idle-timeout= 60000 | ||
| 27 | +#spring.datasource.hikari.pool-name= HikariPool | ||
| 28 | +spring.datasource.hikari.max-lifetime= 1800000 | ||
| 29 | +spring.datasource.hikari.connection-timeout= 3000 | ||
| 30 | +spring.datasource.hikari.connection-test-query= SELECT 1 | ||
| 31 | +spring.datasource.hikari.validation-timeout= 3000 | ||
| 32 | +spring.datasource.hikari.register-mbeans=true | ||
| 33 | + | ||
| 34 | +sso.enabled= true | ||
| 35 | +sso.systemcode = SYSUS023 | ||
| 36 | +sso.http.url.login= http://10.10.200.142:9112/login?redirect=%2Findex | ||
| 37 | +sso.http.url.logout= http://10.10.200.142:9112/login?redirect=%2Findex | ||
| 38 | +sso.http.url.auth= http://10.10.200.142:9112/prod-api/system/utilitySystem/checkToken | ||
| 39 | + | ||
| 40 | +dc.imgurl= E:/klimg | ||
| 41 | +dc.profile= profile | ||
| 42 | + | ||
| 43 | +baidu.ak=AYiBOs3f9qBQFhdKFsaboX6CfObmKwRP | ||
| 44 | +passengerFlow.url = http://127.0.0.1:9999/images/ | ||
| 45 | +baidu.akyd=WnSDHZgtPbSbw2LfsH3KO3DDKWONmlYK | ||
| 46 | +electricity.importFile.path= E:/elecImportFile | ||
| 47 | + | ||
| 48 | +report_register.complaint.url= http://192.168.168.172:8080/complaint/TsReport/input.do | ||
| 49 | + | ||
| 50 | +path.speech.common = /home/control/speech/common/ | ||
| 51 | +path.speech.line = /home/control/speech/%s/ | ||
| 49 | \ No newline at end of file | 52 | \ No newline at end of file |