Google Cloud Text-to-Speech does not have an SDK for Unity. So, run Quickstart for .NET (https://cloud.google.com/text-to-speech/docs/quickstart-client-libraries?hl=ja) in Unity. The source code is uploaded below. https://github.com/AzetaTakuya/GoogleCloudText-to-SpeechForUnity
It's hard to understand, but I was able to save sample.mp3. When you play sample.mp3, it will play Hello, World !.
Official Document: Quick Start After completing steps ① to ④, a JSON file will be generated. I will. This time, the environment variables are set from the script, so you can omit them.
NuGetForUnity is a well-known way to install NugetPackage on Unity, but I don't use it for religious reasons. Install from the Visual Studio Package Manager Console.
Open Visual Studio and create a console application (.NET Framework) from [File-> New-> Project]. This time, the project name was [TextToSpeechV1] and .NET Framework 4.7.1 was used.
Once you've created your project, open the Package Manager Console from Tools-> Nuget Package Manager-> Package Manager Console.
When the package manager console opens, do the following:
PM> Install-Package Google.Cloud.TextToSpeech.V1 -Version 1.0.0
After the execution is completed, check [TextToSpeechV1 (* created project) / Packages].
Since the DLL is in the previous folder, modify it so that it can be imported into Unity. As for the work content, (1) Move all the files in [lib / net45 /] in the folder other than Grpc.Core.1.22.0 directly under pacakes, and delete all the folders other than Grpc.Core.1.22.0. (2) Delete [Grpc.Core.1.22.0 / lib / netstandard2.0] and [Grpc.Core.1.22.0 / lib / netstandard1.5] ③ Rename either grpc_csharp_ext.x64.dll or grpc_csharp_ext.x84.dll in [Grpc.Core.1.22.0 / runtimes / win / native] to grpc_csharp_ext.dll
Once that's done, create a Unity project. After creating the Unity project, rename the pakeges folder you modified earlier to Plugins and import it into Unity. This completes the import of the library into Unity.
Modify the script in Quickstart for .NET for Unity-Add environment variables The following is what you have done.
using System.IO;
using UnityEngine;
using System;
using Google.Cloud.TextToSpeech.V1;
public class QuickStart : MonoBehaviour
{
public string credentialsPath;
public string saveFile;
void Start()
{
#region Environment Variable
if (!File.Exists(credentialsPath))
{
Debug.LogError("failure" + credentialsPath);
return;
}
else
{
Debug.Log("success: " + credentialsPath);
}
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", credentialsPath);
#endregion
#region QuickStart
// Instantiate a client
TextToSpeechClient client = TextToSpeechClient.Create();
// Set the text input to be synthesized.
SynthesisInput input = new SynthesisInput
{
Text = "Hello, World!"
};
// Build the voice request, select the language code ("en-US"),
// and the SSML voice gender ("neutral").
VoiceSelectionParams voice = new VoiceSelectionParams
{
LanguageCode = "en-US",
SsmlGender = SsmlVoiceGender.Neutral
};
// Select the type of audio file you want returned.
AudioConfig config = new AudioConfig
{
AudioEncoding = AudioEncoding.Mp3
};
// Perform the Text-to-Speech request, passing the text input
// with the selected voice parameters and audio file type
var response = client.SynthesizeSpeech(new SynthesizeSpeechRequest
{
Input = input,
Voice = voice,
AudioConfig = config
});
// Write the binary AudioContent of the response to an MP3 file.
using (Stream output = File.Create(saveFile))
{
response.AudioContent.WriteTo(output);
Debug.Log($"Audio content written to file " + saveFile);
}
#endregion
}
}
Enter the authentication file path in credentialsPath and the save file path (.mp3) in saveFile, and the audio file will be saved.
I felt that there were many people who said that it couldn't be done well, so I wrote it. The usage of GoogleCloutPlatform itself seems quite complicated, so I will write it if there is a request. ~~ Why doesn't Google.Cloud.TextToSpeech.V1 -Version 2.0.0 work stably ...? ~~ If you feel like it, it will be compatible with other than Windows.
None
2020/09/09
Recommended Posts