[修改]校验语音识别接口增加立体声转单声道逻辑
All checks were successful
CI / Docker-Build (push) Successful in 2m36s

This commit is contained in:
thesai 2024-05-17 17:38:22 +08:00
parent 0d10203613
commit cee9eda430

View File

@ -2,6 +2,7 @@ package club.joylink.rtss.services.voice;
import club.joylink.rtss.dao.CompetitionErrorSetDAO; import club.joylink.rtss.dao.CompetitionErrorSetDAO;
import club.joylink.rtss.entity.CompetitionErrorSet; import club.joylink.rtss.entity.CompetitionErrorSet;
import club.joylink.rtss.exception.BusinessExceptionAssertEnum;
import club.joylink.rtss.simulation.cbtc.conversation.SimulationVoiceHandler; import club.joylink.rtss.simulation.cbtc.conversation.SimulationVoiceHandler;
import club.joylink.rtss.util.StrUtils; import club.joylink.rtss.util.StrUtils;
import club.joylink.rtss.vo.client.PageQueryVO; import club.joylink.rtss.vo.client.PageQueryVO;
@ -12,8 +13,16 @@ import club.joylink.rtss.vo.client.voice.VoiceRecognitionVO;
import club.joylink.rtss.vo.voice.VoiceVerifyResultVO; import club.joylink.rtss.vo.voice.VoiceVerifyResultVO;
import com.github.pagehelper.Page; import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageHelper;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
@ -58,8 +67,9 @@ public class VoiceTrainingService implements IVoiceTrainingService {
@Override @Override
public VoiceVerifyResultVO verifyVoice(String contentType, byte[] fileData, String text) { public VoiceVerifyResultVO verifyVoice(String contentType, byte[] fileData, String text) {
byte[] monoData = convertToMonoChannel(fileData);
VoiceRecognitionResult voiceRecognitionResult = voiceRecognition( VoiceRecognitionResult voiceRecognitionResult = voiceRecognition(
new VoiceRecognitionVO(fileData, contentType)); new VoiceRecognitionVO(monoData, contentType));
boolean match = StrUtils.isMatch(text, voiceRecognitionResult.getResult(), 20); boolean match = StrUtils.isMatch(text, voiceRecognitionResult.getResult(), 20);
return new VoiceVerifyResultVO(voiceRecognitionResult.getResult(), match); return new VoiceVerifyResultVO(voiceRecognitionResult.getResult(), match);
} }
@ -70,4 +80,40 @@ public class VoiceTrainingService implements IVoiceTrainingService {
result.setResult(simulationVoiceHandler.handle(result.getResult())); result.setResult(simulationVoiceHandler.handle(result.getResult()));
return result; return result;
} }
private byte[] convertToMonoChannel(byte[] audioData) {
try {
// 假设输入和输出音频都存储在字节数组中
byte[] outputAudioBytes;
// 将输入音频字节数组转换为音频输入流
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(audioData);
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(byteArrayInputStream);
AudioFormat format = audioInputStream.getFormat();
// 确保输入音频是立体声
if (format.getChannels() != 2) {
System.out.println("Input audio is not stereo.");
return audioData;
}
// 创建单声道格式
AudioFormat monoFormat = new AudioFormat(format.getEncoding(), format.getSampleRate(),
format.getSampleSizeInBits(), 1, format.getFrameSize() / 2, format.getFrameRate(), false);
// 从立体声转换为单声道
AudioInputStream monoInputStream = AudioSystem.getAudioInputStream(monoFormat,
audioInputStream);
// 将单声道音频写入字节数组输出流
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
AudioSystem.write(monoInputStream, AudioFileFormat.Type.WAVE, byteArrayOutputStream);
// 获取输出音频的字节数组
return byteArrayOutputStream.toByteArray();
} catch (UnsupportedAudioFileException | IOException e) {
throw BusinessExceptionAssertEnum.SYSTEM_EXCEPTION.exception(
String.format("音频转单声道失败:%s", e));
}
}
} }