Commit e246480e authored by 赵啸非's avatar 赵啸非

修改音频识别结果

parent 01d4dea7
......@@ -97,17 +97,18 @@ public class ApiSendMsgController {
String jsonStr = "";
try {
if (file == null || file.getSize() == 0L) throw new AppException("文件为空!");
if (!"pcm".equalsIgnoreCase(FileUtil.getSuffix(file.getOriginalFilename())))
throw new AppException("只支持pcm!");
//if (file.getOriginalFilename())
String filePath = uploadService.saveFileUpload(file, prePath, null);
filePath = uploadService.getFilePath(filePath);
log.info("filePath==>" + filePath);
// byte[] bytes = file.getBytes();
IatModelMulUtil iatModelMulUtil = new IatModelMulUtil(filePath, appid);
// if (!"pcm".equalsIgnoreCase(FileUtil.getSuffix(file.getOriginalFilename())))
// throw new AppException("只支持pcm!");
// //if (file.getOriginalFilename())
// String filePath = uploadService.saveFileUpload(file, prePath, null);
// filePath = uploadService.getFilePath(filePath);
//
// log.info("filePath==>" + filePath);
byte[] bytes = file.getBytes();
// IatModelMulUtil iatModelMulUtil = new IatModelMulUtil(filePath, appid);
IatModelMulUtil iatModelMulUtil = new IatModelMulUtil(bytes, appid);
String authUrl = IatModelMulUtil.getAuthUrl(hostUrl, apiKey, apiSecret);
//log.info("authUrl==>" + authUrl);
......
......@@ -17,6 +17,7 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
......@@ -77,184 +78,231 @@ public class IatModelMulUtil extends WebSocketListener {
int frameSize = 8092; //每一帧音频的大小,建议每 40ms 发送 122B
int intervel = 40;
int status = 0; // 音频的状态
int totalLength = bytes.length;
boolean isFirst = true;
int seq = 0; //数据序号
try (FileInputStream fs = new FileInputStream(file)) {
// try{
byte[] buffer = new byte[frameSize];
// 发送音频
end:
while (true) {
seq++; // 每次循环更新下seq
//int len = bytes.length;
int len = fs.read(buffer);
//int len = bytes.length;
if (len == -1) {
status = StatusLastFrame; //文件读完,改变status 为 2
}
switch (status) {
case StatusFirstFrame: // 第一帧音频status = 0
AudioRoot audioRoot = new AudioRoot();
com.mortals.xhx.common.pdu.Header header = new com.mortals.xhx.common.pdu.Header();
header.setAppId(appid);
header.setStatus(StatusFirstFrame);
audioRoot.setHeader(header);
Parameter parameter = new Parameter();
Iat iat = new Iat();
iat.setDomain("slm");
iat.setLanguage("zh_cn");
iat.setAccent("mulacc");
iat.setEos(6000);
iat.setVinfo(1);
com.mortals.xhx.common.pdu.Result result = new com.mortals.xhx.common.pdu.Result();
result.setEncoding("utf8");
result.setCompress("raw");
result.setFormat("json");
iat.setResult(result);
parameter.setIat(iat);
audioRoot.setParameter(parameter);
com.mortals.xhx.common.pdu.Payload payload = new com.mortals.xhx.common.pdu.Payload();
Audio audio = new Audio();
audio.setEncoding(encoding);
audio.setSampleRate(16000);
audio.setChannels(1);
audio.setBitDepth(16);
audio.setSeq(seq);
audio.setStatus(status);
audio.setAudio(Base64.getEncoder().encodeToString(Arrays.copyOf(buffer, len)));
payload.setAudio(audio);
audioRoot.setPayload(payload);
String json = JSON.toJSONString(audioRoot);
/*
String json = "{\n" +
" \"header\": {\n" +
" \"app_id\": \"" + appid + "\",\n" +
" \"status\": " + StatusFirstFrame + "\n" +
" },\n" +
" \"parameter\": {\n" +
" \"iat\": {\n" +
" \"domain\": \"slm\",\n" +
" \"language\": \"zh_cn\",\n" +
" \"accent\": \"mulacc\",\n" +
" \"eos\": 6000,\n" +
" \"vinfo\": 1,\n" +
" \"result\": {\n" +
" \"encoding\": \"utf8\",\n" +
" \"compress\": \"raw\",\n" +
" \"format\": \"json\"\n" +
" }\n" +
" }\n" +
" },\n" +
" \"payload\": {\n" +
" \"audio\": {\n" +
" \"encoding\": \"raw\",\n" +
" \"sample_rate\": 16000,\n" +
" \"channels\": 1,\n" +
" \"bit_depth\": 16,\n" +
" \"seq\": " + seq + ",\n" +
" \"status\": 0,\n" +
" \"audio\": \"" + Base64.getEncoder().encodeToString(Arrays.copyOf(buffer, len)) + "\"\n" +
" }\n" +
" }\n" +
"}";*/
log.info("first json==>" + json);
webSocket.send(json);
status = StatusContinueFrame; // 发送完第一帧改变status 为 1
break;
case StatusContinueFrame: //中间帧status = 1
audioRoot = new AudioRoot();
header = new com.mortals.xhx.common.pdu.Header();
header.setAppId(appid);
header.setStatus(status);
audioRoot.setHeader(header);
payload = new com.mortals.xhx.common.pdu.Payload();
audio = new Audio();
audio.setEncoding(encoding);
audio.setSampleRate(16000);
audio.setChannels(1);
audio.setBitDepth(16);
audio.setSeq(seq);
audio.setStatus(status);
audio.setAudio(Base64.getEncoder().encodeToString(Arrays.copyOf(buffer, len)));
payload.setAudio(audio);
audioRoot.setPayload(payload);
json = JSON.toJSONString(audioRoot);
/* json = "{\n" +
" \"header\": {\n" +
" \"app_id\": \"" + appid + "\",\n" +
" \"status\": 1\n" +
" },\n" +
" \"payload\": {\n" +
" \"audio\": {\n" +
" \"encoding\": \"raw\",\n" +
" \"sample_rate\": 16000,\n" +
" \"channels\": 1,\n" +
" \"bit_depth\": 16,\n" +
" \"seq\": " + seq + ",\n" +
" \"status\": 1,\n" +
" \"audio\": \"" + Base64.getEncoder().encodeToString(Arrays.copyOf(buffer, len)) + "\"\n" +
" }\n" +
" }\n" +
"}";*/
// log.info("mid json==>" + json);
webSocket.send(json);
break;
case StatusLastFrame: // 最后一帧音频status = 2 ,标志音频发送结束
audioRoot = new AudioRoot();
header = new com.mortals.xhx.common.pdu.Header();
header.setAppId(appid);
header.setStatus(status);
audioRoot.setHeader(header);
payload = new com.mortals.xhx.common.pdu.Payload();
audio = new Audio();
audio.setEncoding(encoding);
audio.setSampleRate(16000);
audio.setChannels(1);
audio.setBitDepth(16);
audio.setSeq(seq);
audio.setStatus(status);
audio.setAudio("");
payload.setAudio(audio);
audioRoot.setPayload(payload);
json = JSON.toJSONString(audioRoot);
/* json = "{\n" +
" \"header\": {\n" +
" \"app_id\": \"" + appid + "\",\n" +
" \"status\": 2\n" +
" },\n" +
" \"payload\": {\n" +
" \"audio\": {\n" +
" \"encoding\": \"raw\",\n" +
" \"sample_rate\": 16000,\n" +
" \"channels\": 1,\n" +
" \"bit_depth\": 16,\n" +
" \"seq\": " + seq + ",\n" +
" \"status\": 2,\n" +
" \"audio\": \"\"\n" +
" }\n" +
" }\n" +
"}";*/
// log.info("last json==>" + json);
webSocket.send(json);
break end;
}
int offset = 0;
while (offset < totalLength) {
int length = Math.min(frameSize, bytes.length - offset);
byte[] buffer = new byte[length];
System.arraycopy(bytes, offset, buffer, 0, length);
boolean isLast = (offset + length) >= totalLength;
if (isFirst) {
status = StatusFirstFrame; // 仅一帧
} else if (isLast) {
status = StatusLastFrame; // 尾帧
} else {
status = StatusContinueFrame; // 中间帧
}
switch (status) {
case StatusFirstFrame: // 第一帧音频status = 0
AudioRoot audioRoot = new AudioRoot();
com.mortals.xhx.common.pdu.Header header = new com.mortals.xhx.common.pdu.Header();
header.setAppId(appid);
header.setStatus(StatusFirstFrame);
audioRoot.setHeader(header);
Parameter parameter = new Parameter();
Iat iat = new Iat();
iat.setDomain("slm");
iat.setLanguage("zh_cn");
iat.setAccent("mulacc");
iat.setEos(6000);
iat.setVinfo(1);
com.mortals.xhx.common.pdu.Result result = new com.mortals.xhx.common.pdu.Result();
result.setEncoding("utf8");
result.setCompress("raw");
result.setFormat("json");
iat.setResult(result);
parameter.setIat(iat);
audioRoot.setParameter(parameter);
com.mortals.xhx.common.pdu.Payload payload = new com.mortals.xhx.common.pdu.Payload();
Audio audio = new Audio();
audio.setEncoding(encoding);
audio.setSampleRate(16000);
audio.setChannels(1);
audio.setBitDepth(16);
audio.setSeq(seq);
audio.setStatus(status);
audio.setAudio(Base64.getEncoder().encodeToString(buffer));
payload.setAudio(audio);
audioRoot.setPayload(payload);
String json = JSON.toJSONString(audioRoot);
log.info("first json==>" + json);
webSocket.send(json);
status = StatusContinueFrame; // 发送完第一帧改变status 为 1
break;
case StatusContinueFrame: //中间帧status = 1
audioRoot = new AudioRoot();
header = new com.mortals.xhx.common.pdu.Header();
header.setAppId(appid);
header.setStatus(status);
audioRoot.setHeader(header);
payload = new com.mortals.xhx.common.pdu.Payload();
audio = new Audio();
audio.setEncoding(encoding);
audio.setSampleRate(16000);
audio.setChannels(1);
audio.setBitDepth(16);
audio.setSeq(seq);
audio.setStatus(status);
audio.setAudio(Base64.getEncoder().encodeToString(buffer));
payload.setAudio(audio);
audioRoot.setPayload(payload);
json = JSON.toJSONString(audioRoot);
webSocket.send(json);
break;
case StatusLastFrame: // 最后一帧音频status = 2 ,标志音频发送结束
audioRoot = new AudioRoot();
header = new com.mortals.xhx.common.pdu.Header();
header.setAppId(appid);
header.setStatus(status);
audioRoot.setHeader(header);
payload = new com.mortals.xhx.common.pdu.Payload();
audio = new Audio();
audio.setEncoding(encoding);
audio.setSampleRate(16000);
audio.setChannels(1);
audio.setBitDepth(16);
audio.setSeq(seq);
audio.setStatus(status);
audio.setAudio("");
payload.setAudio(audio);
audioRoot.setPayload(payload);
json = JSON.toJSONString(audioRoot);
// log.info("last json==>" + json);
webSocket.send(json);
}
try {
Thread.sleep(intervel); //模拟音频采样延时
} catch (InterruptedException e) {
}
} catch (Exception e) {
log.info("error==>" + e.getMessage());
offset += length;
isFirst = false;
}
}).start();
// // try (FileInputStream fs = new FileInputStream(file)) {
// try{
// // 发送音频
// end:
// while (true) {
// seq++; // 每次循环更新下seq
// //int len = bytes.length;
// byte[] buffer = new byte[frameSize];
// System.arraycopy(buffer, seq*frameSize, buffer, 0, frameSize);
//
// //int len = fs.read(buffer);
// int len = bytes.length;
// if (len == -1) {
// status = StatusLastFrame; //文件读完,改变status 为 2
// }
// switch (status) {
// case StatusFirstFrame: // 第一帧音频status = 0
// AudioRoot audioRoot = new AudioRoot();
// com.mortals.xhx.common.pdu.Header header = new com.mortals.xhx.common.pdu.Header();
// header.setAppId(appid);
// header.setStatus(StatusFirstFrame);
// audioRoot.setHeader(header);
//
// Parameter parameter = new Parameter();
//
// Iat iat = new Iat();
// iat.setDomain("slm");
// iat.setLanguage("zh_cn");
// iat.setAccent("mulacc");
// iat.setEos(6000);
// iat.setVinfo(1);
// com.mortals.xhx.common.pdu.Result result = new com.mortals.xhx.common.pdu.Result();
// result.setEncoding("utf8");
// result.setCompress("raw");
// result.setFormat("json");
// iat.setResult(result);
// parameter.setIat(iat);
// audioRoot.setParameter(parameter);
// com.mortals.xhx.common.pdu.Payload payload = new com.mortals.xhx.common.pdu.Payload();
//
// Audio audio = new Audio();
// audio.setEncoding(encoding);
// audio.setSampleRate(16000);
// audio.setChannels(1);
// audio.setBitDepth(16);
// audio.setSeq(seq);
// audio.setStatus(status);
// audio.setAudio(Base64.getEncoder().encodeToString(Arrays.copyOf(buffer, len)));
// payload.setAudio(audio);
// audioRoot.setPayload(payload);
//
// String json = JSON.toJSONString(audioRoot);
// log.info("first json==>" + json);
// webSocket.send(json);
// status = StatusContinueFrame; // 发送完第一帧改变status 为 1
// break;
// case StatusContinueFrame: //中间帧status = 1
// audioRoot = new AudioRoot();
// header = new com.mortals.xhx.common.pdu.Header();
// header.setAppId(appid);
// header.setStatus(status);
// audioRoot.setHeader(header);
//
// payload = new com.mortals.xhx.common.pdu.Payload();
// audio = new Audio();
// audio.setEncoding(encoding);
// audio.setSampleRate(16000);
// audio.setChannels(1);
// audio.setBitDepth(16);
// audio.setSeq(seq);
// audio.setStatus(status);
// audio.setAudio(Base64.getEncoder().encodeToString(Arrays.copyOf(buffer, len)));
// payload.setAudio(audio);
// audioRoot.setPayload(payload);
//
// json = JSON.toJSONString(audioRoot);
//
// webSocket.send(json);
// break;
// case StatusLastFrame: // 最后一帧音频status = 2 ,标志音频发送结束
// audioRoot = new AudioRoot();
// header = new com.mortals.xhx.common.pdu.Header();
// header.setAppId(appid);
// header.setStatus(status);
// audioRoot.setHeader(header);
//
// payload = new com.mortals.xhx.common.pdu.Payload();
// audio = new Audio();
// audio.setEncoding(encoding);
// audio.setSampleRate(16000);
// audio.setChannels(1);
// audio.setBitDepth(16);
// audio.setSeq(seq);
// audio.setStatus(status);
// audio.setAudio("");
// payload.setAudio(audio);
// audioRoot.setPayload(payload);
// json = JSON.toJSONString(audioRoot);
// // log.info("last json==>" + json);
// webSocket.send(json);
// break end;
// }
// Thread.sleep(intervel); //模拟音频采样延时
// }
// } catch (Exception e) {
// log.info("error==>" + e.getMessage());
// }
// }).start();
}
@Override
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment