#include "stdafx.h" #include #include #include using namespace Microsoft::CognitiveServices::Speech; using namespace Microsoft::CognitiveServices::Speech::Audio; std::string GetEnvironmentVariable(const char* name); int main() { // This example requires environment variables named "SPEECH_KEY" and "SPEECH_REGION" auto speechKey = GetEnvironmentVariable("SPEECH_KEY"); auto speechRegion = GetEnvironmentVariable("SPEECH_REGION"); if ((size(speechKey) == 0) || (size(speechRegion) == 0)) { std::cout << "Please set both SPEECH_KEY and SPEECH_REGION environment variables." << std::endl; return -1; } auto speechConfig = SpeechConfig::FromSubscription(speechKey, speechRegion); // The language of the voice that speaks. speechConfig->SetSpeechSynthesisVoiceName("zh-CN-XiaoxiaoNeural"); auto speechSynthesizer = SpeechSynthesizer::FromConfig(speechConfig); // Get text from the console and synthesize to the default speaker. std::cout << "Enter some text that you want to speak >" << std::endl; std::wstring text; getline(std::cin, text); text = "I am a student, my name is 王先生"; auto result = speechSynthesizer->SpeakTextAsync(text).get(); // Checks result. if (result->Reason == ResultReason::SynthesizingAudioCompleted) { std::cout << "Speech synthesized to speaker for text [" << text << "]" << std::endl; } else if (result->Reason == ResultReason::Canceled) { auto cancellation = SpeechSynthesisCancellationDetails::FromResult(result); std::cout << "CANCELED: Reason=" << (int)cancellation->Reason << std::endl; if (cancellation->Reason == CancellationReason::Error) { std::cout << "CANCELED: ErrorCode=" << (int)cancellation->ErrorCode << std::endl; std::cout << "CANCELED: ErrorDetails=[" << cancellation->ErrorDetails << "]" << std::endl; std::cout << "CANCELED: Did you set the speech resource key and region values?" << std::endl; } } std::cout << "Press enter to exit..." << std::endl; std::cin.get(); } std::string GetEnvironmentVariable(const char* name) { #if defined(_MSC_VER) size_t requiredSize = 0; (void)getenv_s(&requiredSize, nullptr, 0, name); if (requiredSize == 0) { return ""; } auto buffer = std::make_unique(requiredSize); (void)getenv_s(&requiredSize, buffer.get(), requiredSize, name); return buffer.get(); #else auto value = getenv(name); return value ? value : ""; #endif }