package com.cmos.nomsapp.web.controller.audiometry;
import java.io.File;
import java.util.UUID;
import java.util.concurrent.Callable;
import org.apache.commons.lang.StringUtils;
import com.cmos.core.logger.Logger;
import com.cmos.core.logger.LoggerFactory;
import com.cmos.nomsapp.beans.prompt.PromptFileConfig;
import com.cmos.onest.ONestUtil;
public class OnestCallable implements Callable<String>{
static Logger logger = LoggerFactory.getLogger(OnestCallable.class);
private static String onext_sys;
private static String onext_file;
public static void setOnext_sys(String onext_sys) {
OnestCallable.onext_sys = onext_sys;
}
public static void setOnext_file(String onext_file) {
OnestCallable.onext_file = onext_file;
}
public static String getOnext_sys() {
return onext_sys;
}
public static String getOnext_file() {
return onext_file;
}
private String warAddr;
private String audioFile;
public OnestCallable(String warAddr) {
super();
this.warAddr = warAddr;
}
public OnestCallable() {
super();
}
public String getWarAddr() {
return warAddr;
}
public void setWarAddr(String warAddr) {
this.warAddr = warAddr;
}
public String getAudioFile() {
return audioFile;
}
public void setAudioFile(String audioFile) {
this.audioFile = audioFile;
}
@Override
public String call() throws Exception {
return getAudioNameByPath();
}
public String getAudioNameByPath(){
String audioName=null;
try {
if( null != warAddr && StringUtils.isNotBlank(warAddr) && !"null".equals(warAddr)){
String cannonicalPath = PromptFileConfig.voiceFilePath + File.separator;
audioFile = cannonicalPath + UUID.randomUUID().toString() + ".wav";
audioName = warAddr.substring(warAddr.lastIndexOf("/")+1,warAddr.indexOf(".wav")+4);
ONestUtil.download(onext_sys,onext_file+"/"+audioName,audioFile);
return audioFile;
}
} catch (Exception e) {
logger.error("試聽音頻下載下傳失敗>>>"+audioName+" "+e);
audioFile = null;
return null;
}
return null;
}
}
public String[] getWarPathByCallable(String[] arrText, String[] arrWar) {
ArrayList<Future<String>> ttsFu = new ArrayList<>();
ArrayList<Future<String>> onestFu = new ArrayList<>();
String cannonicalPath = PromptFileConfig.voiceFilePath + File.separator;
// TTS合成提示音文本
for (String promptText : arrText) {
String path = cannonicalPath + UUID.randomUUID().toString() + ".pcm";
TTSCallable tc = new TTSCallable(promptText, path);
Future<String> future = ExecutorUtile.executor.submit(tc);
ttsFu.add(future);
}
// Onest下載下傳使用者語音音頻檔案
for (String warPath : arrWar) {
OnestCallable oc = new OnestCallable(warPath);
Future<String> future = ExecutorUtile.executor2.submit(oc);
onestFu.add(future);
}
ArrayList<String> ttsResult = new ArrayList<>();
// 擷取future結果(TTS音頻檔案合成的位址)
for (Future<String> futureItem : ttsFu) {
// ss.get(timeout, unit)
try {
ttsResult.add(futureItem.get());
} catch (Exception e) {
logger.info("tts的future擷取音頻檔案位址失敗", e);
}
}
ArrayList<String> onestResult = new ArrayList<>();
for (Future<String> futureItem : onestFu) {
try {
onestResult.add(futureItem.get());
} catch (Exception e) {
logger.info("onest下載下傳音頻檔案失敗", e);
}
}
// 程序結束擷取音頻檔案的位址
// 存放音頻檔案的位址
ArrayList<String> paths = new ArrayList<String>();
for (int i = 0; i < ttsResult.size(); i++) {
String ttsPath = ttsResult.get(i);
if (StringUtils.isNotBlank(ttsPath)) {
paths.add(ttsPath);
}
String onestPath = onestResult.get(i);
if (StringUtils.isNotBlank(onestPath)) {
paths.add(onestPath);
}
}
String[] a = new String[paths.size()];
paths.toArray(a);
return a;
}