音频模型调整

This commit is contained in:
spllzh
2025-10-21 09:23:23 +08:00
parent a029d57e7d
commit 9aba49a817
25 changed files with 254 additions and 4 deletions

View File

@@ -1,15 +1,22 @@
package com.rj.service.impl;
import com.alibaba.dashscope.audio.asr.transcription.*;
import com.rj.entity.TtsRequestLog;
import com.rj.mapper.TtsRequestLogMapper;
import com.rj.service.ITtsRequestLogService;
import com.rj.utils.MinIOUrlGenerator;
import com.rj.dto.AsrRequest;
import com.rj.dto.AsrResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* TTS请求日志服务实现类
@@ -27,6 +34,15 @@ public class TtsRequestLogServiceImpl implements ITtsRequestLogService {
@Autowired
private MinIOUrlGenerator urlGenerator;
@Value("${dashscope.api.key}")
private String apiKey;
@Value("${asr.enabled:true}")
private boolean asrEnabled;
@Value("${asr.default-model:paraformer-v2}")
private String defaultAsrModel;
@Override
public boolean saveTtsRequestLog(TtsRequestLog requestLog) {
try {
@@ -52,7 +68,7 @@ public class TtsRequestLogServiceImpl implements ITtsRequestLogService {
@Override
public boolean setShortUrlByAudioName(MinIOUrlGenerator.UrlInfo urlInfo ) {
try {
if (urlInfo == null || urlInfo.getFileName().trim().isEmpty()) {
if (urlInfo == null || urlInfo.getFileName() == null || urlInfo.getFileName().trim().isEmpty()) {
log.error("音频名称不能为空");
return false;
}
@@ -92,4 +108,131 @@ public class TtsRequestLogServiceImpl implements ITtsRequestLogService {
public boolean setShortUrlByAudioName(String audioName) {
return false;
}
@Override
public AsrResponse speechToText(AsrRequest request) {
long startTime = System.currentTimeMillis();
try {
if (!isAsrServiceAvailable()) {
return AsrResponse.error("ASR服务不可用请检查配置");
}
if (request.getAudioUrl() == null || request.getAudioUrl().trim().isEmpty()) {
return AsrResponse.error("音频文件URL不能为空");
}
// 设置默认值
if (request.getModel() == null || request.getModel().trim().isEmpty()) {
request.setModel(defaultAsrModel);
}
if (request.getFormat() == null || request.getFormat().trim().isEmpty()) {
request.setFormat("wav");
}
if (request.getSampleRate() == null) {
request.setSampleRate(16000);
}
if (request.getEnablePunctuation() == null) {
request.setEnablePunctuation(true);
}
if (request.getEnableNumberConversion() == null) {
request.setEnableNumberConversion(true);
}
if (request.getEnableSpeakerDiarization() == null) {
request.setEnableSpeakerDiarization(false);
}
if (request.getSpeakerCount() == null) {
request.setSpeakerCount(1);
}
log.info("开始语音识别 - 音频URL: {}, 模型: {}, 格式: {}, 采样率: {}",
request.getAudioUrl(), request.getModel(), request.getFormat(), request.getSampleRate());
// 创建转写请求参数
TranscriptionParam param = TranscriptionParam.builder()
.apiKey(apiKey)
.model(request.getModel())
.fileUrls(Arrays.asList(request.getAudioUrl()))
.build();
// 创建转写对象
Transcription transcription = new Transcription();
// 提交转写请求
TranscriptionResult result = transcription.asyncCall(param);
String taskId = result.getTaskId();
String requestId = result.getRequestId();
log.info("ASR任务已提交 - TaskId: {}, RequestId: {}", taskId, requestId);
// 等待任务完成
TranscriptionQueryParam queryParam = TranscriptionQueryParam.FromTranscriptionParam(param, taskId);
result = transcription.wait(queryParam);
log.info("语音识别完成 - result: {} ", result);
long processingTime = System.currentTimeMillis() - startTime;
if (result.getOutput() != null) {
// 解析识别结果 - 从JSON中提取文本
String fullText = "";
Double duration = 0.0;
try {
// 从输出中提取文本和时长信息
if (result.getOutput().toString().contains("\"text\"")) {
// 简单解析JSON获取文本
String outputStr = result.getOutput().toString();
int textStart = outputStr.indexOf("\"text\":\"") + 8;
int textEnd = outputStr.indexOf("\"", textStart);
if (textStart > 7 && textEnd > textStart) {
fullText = outputStr.substring(textStart, textEnd);
}
}
// 提取时长信息
if (result.getOutput().toString().contains("\"duration\"")) {
String outputStr = result.getOutput().toString();
int durationStart = outputStr.indexOf("\"duration\":") + 11;
int durationEnd = outputStr.indexOf(",", durationStart);
if (durationEnd == -1) {
durationEnd = outputStr.indexOf("}", durationStart);
}
if (durationStart > 10 && durationEnd > durationStart) {
try {
duration = Double.parseDouble(outputStr.substring(durationStart, durationEnd).trim());
} catch (NumberFormatException e) {
duration = 0.0;
}
}
}
} catch (Exception e) {
log.warn("解析ASR结果时出错: {}", e.getMessage());
}
List<AsrResponse.AsrResult> asrResults = new ArrayList<>();
// 创建简单的结果对象
AsrResponse.AsrResult asrResult = new AsrResponse.AsrResult();
asrResult.setText(fullText);
asrResult.setStartTime(0.0);
asrResult.setEndTime(duration);
asrResults.add(asrResult);
log.info("语音识别完成 - 识别文本长度: {}, 处理时间: {}ms", fullText.length(), processingTime);
return AsrResponse.success(fullText, asrResults, duration, processingTime, taskId, requestId);
} else {
log.warn("ASR识别结果为空 - TaskId: {}", taskId);
return AsrResponse.error("语音识别结果为空");
}
} catch (Exception e) {
log.error("语音识别失败: {}", e.getMessage(), e);
return AsrResponse.error("语音识别失败: " + e.getMessage());
}
}
@Override
public boolean isAsrServiceAvailable() {
return asrEnabled && apiKey != null && !apiKey.trim().isEmpty();
}
}