Hi, @Frank
I modified the snippet to make sure Chinese can be stored in wstring.
You can copy the snippet below and replace with your key. To avoid cmd echo, use system("chcp 936 > nul")
std::wstring text; setlocale(LC_ALL, "chs"); getline(wcin,text);
#include "stdafx.h"
// <code>
#include <iostream>
#include <speechapi_cxx.h>
#include<Windows.h>
#include <string>
#pragma execution_character_set("utf-8")
using namespace std;
using namespace Microsoft::CognitiveServices::Speech;
void synthesizeSpeech()
{
system("chcp 936");
// Creates an instance of a speech config with specified subscription key and service region.
// Replace with your own subscription key and service region (e.g., "westus").
auto config = SpeechConfig::FromSubscription("key", "eastasia");
auto language = "zh-CN";
config->SetSpeechSynthesisLanguage(language);
// Set the voice name, refer to https://aka.ms/speech/voices/neural for full list.
config->SetSpeechSynthesisVoiceName("zh-CN-XiaoxiaoNeural");
// Creates a speech synthesizer using the default speaker as audio output. The default spoken language is "en-us".
auto synthesizer = SpeechSynthesizer::FromConfig(config);
// Receive a text from console input and synthesize it to speaker.
cout << "Type some text that you want to speak..." << std::endl;
cout << "> ";
std::wstring text;
setlocale(LC_ALL, "chs");
getline(wcin,text);
wcout << L"Input Speech synthesized to speaker for text [" << text << "]" << std::endl;
auto result = synthesizer->SpeakTextAsync(text).get();
// Checks result.
if (result->Reason == ResultReason::SynthesizingAudioCompleted)
{
wcout << L"Speech synthesized to speaker for text [" << text << "]" << std::endl;
}
else if (result->Reason == ResultReason::Canceled)
{
auto cancellation = SpeechSynthesisCancellationDetails::FromResult(result);
cout << "CANCELED: Reason=" << (int)cancellation->Reason << std::endl;
if (cancellation->Reason == CancellationReason::Error)
{
cout << "CANCELED: ErrorCode=" << (int)cancellation->ErrorCode << std::endl;
cout << "CANCELED: ErrorDetails=[" << cancellation->ErrorDetails << "]" << std::endl;
cout << "CANCELED: Did you update the subscription info?" << std::endl;
}
}
// This is to give some time for the speaker to finish playing back the audio
cout << "Press enter to exit..." << std::endl;
cin.get();
}
int wmain()
{
try
{
synthesizeSpeech();
}
catch (exception e)
{
cout << e.what();
}
return 0;
}
Best regards,
Minxin Yu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.