java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringBoot MiniMax和CosyVoice文本转语音

SpringBoot集成MiniMax和CosyVoice实现文本转语音功能

作者:程序猿DD

在一些需要高质量文本转语音(TTS)的场景中(比如:有声书配音、播客等),比较推荐使用MiniMax、CosyVoice这些提供的音色,这些音色的效果会更加拟人、逼真,接近真人发音,本文将引导读者从零到一把MiniMax、CosyVoice的语音合成能力整合到你的SpringBoot应用中

在一些需要高质量文本转语音(TTS)的场景中(比如:有声书配音、播客等)。之前介绍的EdgeTTS方案可能效果没有那么好。此时就比较推荐使用 MiniMax、CosyVoice这些提供的音色,这些音色的效果会更加拟人、逼真,接近真人发音。这里依然通过 UnifiedTTS 的统一接口来对接,这样我们可以在不更换客户端代码的前提下,快速在 MiniMax、CosyVoice等引擎之间做无缝切换。本文将引导读者从零到一把MiniMax、CosyVoice的语音合成能力整合到你的Spring Boot应用中,最后也会给出一个可复制的 Spring Boot 集成示例,

实战

1. 构建 Spring Boot 应用

通过 start.spring.io 或其他构建基础的Spring Boot工程,根据你构建应用的需要增加一些依赖,比如最后用接口提供服务的话,可以加入web模块、lombok等常用依赖:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
</dependencies>

2. 注册 UnifiedTTS,获取 API Key

3. 集成 UnifiedTTS API(使用 MiniMax、CosyVoice)

下面给出参考实现,包括配置、DTO、服务与控制器。与 EdgeTTS 版本相比,主要是将 modelvoice 改为 MiniMax/CosyVoice 支持的参数。

3.1 配置文件(application.properties)

unified-tts.host=https://unifiedtts.com
unified-tts.api-key=${UNIFIEDTTS_API_KEY}

这里 unified-tts.api-key 请替换为你在 UnifiedTTS 控制台创建的 API Key。

3.2 配置加载类与请求/响应封装

// src/main/java/com/example/tts/config/UnifiedTtsProperties.java
@Data
@ConfigurationProperties(prefix = "unified-tts")
public class UnifiedTtsProperties {
    private String host;
    private String apiKey;
}

// src/main/java/com/example/tts/dto/UnifiedTtsRequest.java
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UnifiedTtsRequest {
    private String model;   // 例:minimax-tts 或 cosyvoice-tts
    private String voice;   // 例:zh_female_1(按模型支持的音色选择)
    private String text;
    private Double speed;   // 语速(可选)
    private Double pitch;   // 音高(可选)
    private Double volume;  // 音量(可选)
    private String format;  // mp3/wav/ogg
}

// src/main/java/com/example/tts/dto/UnifiedTtsResponse.java
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UnifiedTtsResponse {
    private boolean success;
    private String message;
    private long timestamp;
    private UnifiedTtsResponseData data;

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class UnifiedTtsResponseData {
        @JsonProperty("request_id")
        private String requestId;

        @JsonProperty("audio_url")
        private String audioUrl;

        @JsonProperty("file_size")
        private long fileSize;
    }
}

3.3 服务实现(RestClient 同步合成)

// src/main/java/com/example/tts/service/UnifiedTtsService.java
package com.example.tts.service;

import com.example.tts.dto.UnifiedTtsRequest;
import com.example.tts.config.UnifiedTtsProperties;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClient;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

@Service
public class UnifiedTtsService {

    private final RestClient restClient;
    private final UnifiedTtsProperties properties;

    public UnifiedTtsService(UnifiedTtsProperties properties) {
        this.properties = properties;
        this.restClient = RestClient.builder()
                .baseUrl(properties.getHost())
                .build();
    }

    public byte[] synthesize(UnifiedTtsRequest request) {
        ResponseEntity<byte[]> response = restClient
                .post()
                .uri("/api/v1/common/tts-sync")
                .contentType(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_OCTET_STREAM, MediaType.valueOf("audio/mpeg"), MediaType.valueOf("audio/mp3"))
                .header("X-API-Key", properties.getApiKey())
                .body(request)
                .retrieve()
                .toEntity(byte[].class);

        if (response.getStatusCode().is2xxSuccessful() && response.getBody() != null) {
            return response.getBody();
        }
        throw new IllegalStateException("UnifiedTTS synthesize failed: " + response.getStatusCode());
    }

    public Path synthesizeToFile(UnifiedTtsRequest request, Path outputPath) {
        byte[] data = synthesize(request);
        try {
            if (outputPath.getParent() != null) {
                Files.createDirectories(outputPath.getParent());
            }
            Files.write(outputPath, data);
            return outputPath;
        } catch (IOException e) {
            throw new RuntimeException("Failed to write TTS output to file: " + outputPath, e);
        }
    }
}

3.4 单元测试(MiniMax/CosyVoice)

// src/test/java/com/example/tts/UnifiedTtsServiceTest.java
@SpringBootTest
class UnifiedTtsServiceTest {

    @Autowired
    private UnifiedTtsService unifiedTtsService;

    @Test
    void testSynthesizeToFileWithMiniMax() throws Exception {
        UnifiedTtsRequest req = new UnifiedTtsRequest(
            "speech-02-turbo",
            "Chinese (Mandarin)_Gentle_Youth",
            "你好,欢迎使用 UnifiedTTS 的 MiniMax 模型配音。",
            1.0,
            0.0,
            1.0,
            "mp3"
        );

        Path projectDir = Paths.get(System.getProperty("user.dir"));
        Path resultDir = projectDir.resolve("test-result");
        Files.createDirectories(resultDir);
        Path out = resultDir.resolve(System.currentTimeMillis() + ".mp3");

        Path written = unifiedTtsService.synthesizeToFile(req, out);
        assertTrue(Files.exists(written), "Output file should exist");
        assertTrue(Files.size(written) > 0, "Output file size should be > 0");
    }
}

4. 运行与验证

执行单元测试之后,可以在工程目录 test-result 下找到生成的音频文件:

如果你希望拿到音频 URL 或 Base64,可将接口 accept 改为 application/json 并解析返回结果,再做下载或解码。

5. 常用参数与音色选择

模型model与音色voice 这里推荐使用 minimaxcosyvoice的模型和音色。

具体支持的参数可以在API文档中的接口查询可以填入的参数,比如:

model支持,调用一下可以看到,支持的模型有:

每个模型下支持的voice,也可以调用接口查询,比如这里尝试调用minimax支持的voice:

6. 异常处理与重试建议

7. 生产化建议

小结

通过 UnifiedTTS,我们在 Spring Boot 中仅需调整 `mo

以上就是SpringBoot集成MiniMax和CosyVoice实现文本转语音功能的详细内容,更多关于SpringBoot MiniMax和CosyVoice文本转语音的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文