diff --git a/src/main/java/club/joylink/rtss/services/voice/VoiceTrainingService.java b/src/main/java/club/joylink/rtss/services/voice/VoiceTrainingService.java index 0520097b2..88e5e0b03 100644 --- a/src/main/java/club/joylink/rtss/services/voice/VoiceTrainingService.java +++ b/src/main/java/club/joylink/rtss/services/voice/VoiceTrainingService.java @@ -2,6 +2,7 @@ package club.joylink.rtss.services.voice; import club.joylink.rtss.dao.CompetitionErrorSetDAO; import club.joylink.rtss.entity.CompetitionErrorSet; +import club.joylink.rtss.exception.BusinessExceptionAssertEnum; import club.joylink.rtss.simulation.cbtc.conversation.SimulationVoiceHandler; import club.joylink.rtss.util.StrUtils; 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 com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.util.List; 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.stereotype.Service; import org.springframework.web.multipart.MultipartFile; @@ -58,8 +67,9 @@ public class VoiceTrainingService implements IVoiceTrainingService { @Override public VoiceVerifyResultVO verifyVoice(String contentType, byte[] fileData, String text) { + byte[] monoData = convertToMonoChannel(fileData); VoiceRecognitionResult voiceRecognitionResult = voiceRecognition( - new VoiceRecognitionVO(fileData, contentType)); + new VoiceRecognitionVO(monoData, contentType)); boolean match = StrUtils.isMatch(text, voiceRecognitionResult.getResult(), 20); return new VoiceVerifyResultVO(voiceRecognitionResult.getResult(), match); } @@ -70,4 +80,40 @@ public class VoiceTrainingService implements IVoiceTrainingService { result.setResult(simulationVoiceHandler.handle(result.getResult())); 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)); + } + } }