用于女声文本转语音的Java代码

问题描述 投票:0回答:1

我想要用于男声和女声文本到语音的java代码,之后我想将该音频保存到wav文件。我尝试使用此代码,但它只能发出男性声音。 请建议我一种无需下载 jar 文件即可获得女声的方法(只需添加 Maven 依赖项即可)。

当前使用此代码 -

     try

     {
     // set property as Kevin Dictionary
     System.setProperty("freetts.voices",
     "com.sun.speech.freetts.en.us.cmu_us_kal.KevinVoiceDirectory");

     // Register Engine
     Central.registerEngineCentral("com.sun.speech.freetts.jsapi.FreeTTSEngineCentral");

     // Create a Synthesizer
     Synthesizer synthesizer = Central.createSynthesizer(new
     SynthesizerModeDesc(Locale.US));

     // Allocate synthesizer
     synthesizer.allocate();

     // Resume Synthesizer
     synthesizer.resume();

     // speaks the given text until queue is empty.
     synthesizer.speakPlainText("Hi, this is me. Fine.", null);
     synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY);

     // Deallocate the Synthesizer.
     synthesizer.deallocate();
     }

     catch (Exception e) {
     e.printStackTrace();
     }

和这段代码 -

  try {
        System.setProperty("FreeTTSSynthEngineCentral", "com.sun.speech.freetts.jsapi.FreeTTSEngineCentral");
        System.setProperty("freetts.voices", "com.sun.speech.freetts.en.us.cmu_us_kal.KevinVoiceDirectory");
        Central.registerEngineCentral("com.sun.speech.freetts.jsapi.FreeTTSEngineCentral");

        SynthesizerModeDesc desc = new SynthesizerModeDesc(null, "general", Locale.US, null, null);

        Synthesizer synth = Central.createSynthesizer(desc);
        synth.allocate();
        desc = (SynthesizerModeDesc) synth.getEngineModeDesc();
        Voice voice = new Voice();
        // "business", "casual", "robotic", "breathy"
        voice.setAge(Voice.AGE_TEENAGER);
        voice.setGender(Voice.GENDER_FEMALE);
        voice.setStyle("breathy");
        synth.getSynthesizerProperties().setVoice(voice);
        synth.resume();
        synth.speakPlainText("Hi, this is me. Fine.", null);
        synth.waitEngineState(Synthesizer.QUEUE_EMPTY);
        synth.deallocate();
    } catch (Exception ex) {
        ex.printStackTrace();
    }

但是两者都提供男性声音(并且KevinVoiceDirectory中没有任何女性声音可用)。

java text-to-speech freetts
1个回答
0
投票

使用云提供商:Amazon、Asure、Google、IBM。他们都提供文本转语音服务,并提供教程和示例。

以下使用 Asure 的示例:

import com.javaghost.scrapperutil.ReadWriteUtil;
import com.microsoft.cognitiveservices.speech.CancellationReason;
import com.microsoft.cognitiveservices.speech.ResultReason;
import com.microsoft.cognitiveservices.speech.SpeechConfig;
import com.microsoft.cognitiveservices.speech.SpeechSynthesisCancellationDetails;
import com.microsoft.cognitiveservices.speech.SpeechSynthesisOutputFormat;
import com.microsoft.cognitiveservices.speech.SpeechSynthesisResult;
import com.microsoft.cognitiveservices.speech.SpeechSynthesizer;
import com.microsoft.cognitiveservices.speech.audio.AudioConfig;
import java.io.File;

/**
 *
 */
public class AsureTts {

    private static String speechKey = "";
    private static String speechRegion = "";


    public static void generateAudio(String ssml,
            boolean isMp3, String pathToOutputFile) throws Exception {

        SpeechConfig speechConfig = SpeechConfig.fromSubscription(speechKey, speechRegion);
        if (isMp3) {
            speechConfig.setSpeechSynthesisOutputFormat(SpeechSynthesisOutputFormat.Audio48Khz192KBitRateMonoMp3);
        }

        AudioConfig fileOutput = AudioConfig.fromWavFileOutput(pathToOutputFile);

        SpeechSynthesizer speechSynthesizer = new SpeechSynthesizer(speechConfig, fileOutput);

        SpeechSynthesisResult speechSynthesisResult = speechSynthesizer.SpeakSsmlAsync(ssml).get();

        if (speechSynthesisResult.getReason() == ResultReason.SynthesizingAudioCompleted) {
            System.out.println("Speech synthesis completed!");
        } else if (speechSynthesisResult.getReason() == ResultReason.Canceled) {
            SpeechSynthesisCancellationDetails cancellation = SpeechSynthesisCancellationDetails.fromResult(speechSynthesisResult);
            System.out.println("CANCELED: Reason=" + cancellation.getReason());

            if (cancellation.getReason() == CancellationReason.Error) {
                System.out.println("CANCELED: ErrorCode=" + cancellation.getErrorCode());
                System.out.println("CANCELED: ErrorDetails=" + cancellation.getErrorDetails());
                System.out.println("CANCELED: Did you set the speech resource key and region values?");
            }
        }

    }
}
© www.soinside.com 2019 - 2024. All rights reserved.