TTS 返回语音优化
This commit is contained in:
@@ -145,7 +145,9 @@ public class ChatWebSocketHandler {
|
||||
//持续返回数据流给客户端
|
||||
try {
|
||||
String resultOutPathUrl = RuoYiConfig.getProfile() + VOICE_STORAGE_RESULT_DIR + "123_"+resultFileName;
|
||||
removeSilence(resultPathUrl, resultOutPathUrl);
|
||||
File inputFile = new File(resultPathUrl);
|
||||
File outputFile = new File(resultOutPathUrl);
|
||||
trimSilence(inputFile, outputFile);
|
||||
//文件转换成文件流
|
||||
ByteBuffer outByteBuffer = convertFileToByteBuffer(resultOutPathUrl);
|
||||
//发送文件流数据
|
||||
@@ -406,26 +408,60 @@ public class ChatWebSocketHandler {
|
||||
}
|
||||
}
|
||||
|
||||
public void removeSilence(String inputFile, String outputFile) {
|
||||
try {
|
||||
// FFmpeg 命令,去除音频后面的静音部分
|
||||
String command = "ffmpeg -i " + inputFile +
|
||||
" -af silenceremove=start_periods=1:start_duration=0.2:start_threshold=-40dB " +
|
||||
"-af silenceremove=end_periods=1:end_duration=0.2:end_threshold=-40dB " +
|
||||
outputFile;
|
||||
|
||||
// 执行命令
|
||||
Process process = Runtime.getRuntime().exec(command);
|
||||
int exitCode = process.waitFor();
|
||||
if (exitCode == 0) {
|
||||
System.out.println("静音部分已去除");
|
||||
} else {
|
||||
System.out.println("音频处理失败");
|
||||
}
|
||||
} catch (IOException | InterruptedException e) {
|
||||
// 截取音频流,去除静音部分
|
||||
public void trimSilence(File inputFile, File outputFile) {
|
||||
try {
|
||||
// 获取音频流
|
||||
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(inputFile);
|
||||
AudioFormat format = audioInputStream.getFormat();
|
||||
|
||||
// 将音频流转为字节数组
|
||||
byte[] audioBytes = audioInputStream.readAllBytes();
|
||||
|
||||
// 去除前后静音部分
|
||||
int start = findNonSilenceStart(audioBytes, format);
|
||||
int end = findNonSilenceEnd(audioBytes, format);
|
||||
|
||||
// 截取音频流
|
||||
byte[] trimmedAudio = new byte[end - start];
|
||||
System.arraycopy(audioBytes, start, trimmedAudio, 0, trimmedAudio.length);
|
||||
|
||||
// 创建新的音频输入流
|
||||
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(trimmedAudio);
|
||||
AudioInputStream trimmedAudioStream = new AudioInputStream(byteArrayInputStream, format, trimmedAudio.length / format.getFrameSize());
|
||||
|
||||
// 保存裁剪后的音频文件
|
||||
AudioSystem.write(trimmedAudioStream, AudioFileFormat.Type.WAVE, outputFile);
|
||||
System.out.println("裁剪后的音频已保存到: " + outputFile.getAbsolutePath());
|
||||
|
||||
audioInputStream.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// 找到音频流中非静音部分的开始位置
|
||||
private int findNonSilenceStart(byte[] audioBytes, AudioFormat format) {
|
||||
int threshold = 10; // 假设小于这个值的幅度为静音
|
||||
for (int i = 0; i < audioBytes.length; i++) {
|
||||
if (Math.abs(audioBytes[i]) > threshold) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return 0; // 如果音频全是静音,返回 0
|
||||
}
|
||||
|
||||
// 找到音频流中非静音部分的结束位置
|
||||
private int findNonSilenceEnd(byte[] audioBytes, AudioFormat format) {
|
||||
int threshold = 10; // 假设小于这个值的幅度为静音
|
||||
for (int i = audioBytes.length - 1; i >= 0; i--) {
|
||||
if (Math.abs(audioBytes[i]) > threshold) {
|
||||
return i + 1;
|
||||
}
|
||||
}
|
||||
return audioBytes.length; // 如果音频全是静音,返回音频的最后位置
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user