forked from dyf/fys-Multi-tenant
音频处理服务
This commit is contained in:
@ -1,24 +1,24 @@
|
||||
package com.fuyuanshen;
|
||||
package com.fuyuanshen;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.context.metrics.buffering.BufferingApplicationStartup;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.context.metrics.buffering.BufferingApplicationStartup;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
/**
|
||||
* 启动程序
|
||||
*
|
||||
* @author Lion Li
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@EnableScheduling
|
||||
public class DromaraApplication {
|
||||
/**
|
||||
* 启动程序
|
||||
*
|
||||
* @author Lion Li
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@EnableScheduling
|
||||
public class DromaraApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication application = new SpringApplication(DromaraApplication.class);
|
||||
application.setApplicationStartup(new BufferingApplicationStartup(2048));
|
||||
application.run(args);
|
||||
System.out.println("(♥◠‿◠)ノ゙ fys-Vue-Plus启动成功 ლ(´ڡ`ლ)゙");
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication application = new SpringApplication(DromaraApplication.class);
|
||||
application.setApplicationStartup(new BufferingApplicationStartup(2048));
|
||||
application.run(args);
|
||||
System.out.println("(♥◠‿◠)ノ゙ fys-Vue-Plus启动成功 ლ(´ڡ`ლ)゙");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -8,6 +8,7 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.util.Arrays;
|
||||
@ -101,6 +102,12 @@ public class AudioProcessService {
|
||||
// log.info("测试文件已保存: {}", savedPath);
|
||||
// }
|
||||
|
||||
// 保存WAV文件到本地
|
||||
String savedPath = saveByteArrayToFile(pcmData, "tts_output.wav");
|
||||
if (savedPath != null) {
|
||||
log.info("WAV文件已保存: {}", savedPath);
|
||||
}
|
||||
|
||||
// 将byte[]转换为16进制字符串列表
|
||||
List<String> hexList = audioProcessUtil.bytesToHexList(pcmData);
|
||||
|
||||
@ -113,6 +120,55 @@ public class AudioProcessService {
|
||||
}
|
||||
}
|
||||
|
||||
public String saveWavFileLocally(String text, String filename) throws IOException {
|
||||
// 参数校验
|
||||
if (text == null || text.trim().isEmpty()) {
|
||||
throw new IllegalArgumentException("文本内容不能为空");
|
||||
}
|
||||
|
||||
if (filename == null || filename.trim().isEmpty()) {
|
||||
filename = "tts_output.wav"; // 默认文件名
|
||||
}
|
||||
|
||||
try {
|
||||
// 生成PCM数据
|
||||
byte[] rawPcmData = alibabaTTSUtil.generateStandardPcmData(text);
|
||||
|
||||
// 转换为标准WAV格式(添加44字节头部)
|
||||
byte[] wavData = audioProcessUtil.rawPcmToStandardWav(rawPcmData);
|
||||
|
||||
// 保存到本地文件
|
||||
String filePath = saveByteArrayToFile(wavData, filename);
|
||||
|
||||
log.info("WAV文件已保存: {}", filePath);
|
||||
return filePath;
|
||||
} catch (Exception e) {
|
||||
log.error("保存WAV文件失败: {}", e.getMessage(), e);
|
||||
throw new IOException("保存WAV文件失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
private String saveByteArrayToFile(byte[] data, String filename) throws IOException {
|
||||
// 确定保存路径(可以是临时目录或指定目录)
|
||||
String directory = System.getProperty("java.io.tmpdir"); // 使用系统临时目录
|
||||
File dir = new File(directory);
|
||||
if (!dir.exists()) {
|
||||
dir.mkdirs();
|
||||
}
|
||||
|
||||
// 创建完整文件路径
|
||||
File file = new File(dir, filename);
|
||||
|
||||
// 写入文件
|
||||
try (FileOutputStream fos = new FileOutputStream(file)) {
|
||||
fos.write(data);
|
||||
}
|
||||
|
||||
return file.getAbsolutePath();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 验证音频文件
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user