How to run a quickstart for Google Cloud Text-to-Speech .NET in Unity

3 minute read

Thing you want to do

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

result

It’s hard to understand, but I was able to save sample.mp3.
When you play sample.mp3, it will play Hello, World !.
結果.png

Implementation

environment

  • Windows10 Home
  • Unity 2019.4.8f1
  • Visual Studio 2017 (Community)
  • Google.Cloud.TextToSpeech.V1 -Version 2.0.0
  • Google.Cloud.TextToSpeech.V1 -Version 1.0.0

    * As of 08/30/2020, if you use ver2.0.0, it freezes from the second execution, so use ver1.0.0.

    ~~ If you know the cause / countermeasure, please let me know. ~~

    Unity 2019.4 is equivalent to .NET 4.6 and Google.Cloud.TextToSpeech.V1 -Version 2.0.0 is .NET 4.7.

    https://docs.unity3d.com/ja/2019.4/Manual/CSharpCompiler.html

procedure

  1. Create an authentication file
  2. Install the .NET library (Nuget Package)
  3. Import the library into Unity
  4. Modify QuickStart for Unity
  5. Run


    * The procedure basically follows Official Document: Quick Start ..

① Create an authentication file

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.

(2) Install the .NET library (Nuget Package)

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.

Visual Studio project creation

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.
コンソールアプリ作成.png

Once you’ve created your project, open the Package Manager Console from Tools-> Nuget Package Manager-> Package Manager Console.
パッケージマネージャ.png

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].
パッケージフォルダ.png

③ Import the library into Unity

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

packges編集後.png
lib.png
runtime.png

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 Quick Start for 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

    }

}

⑤ Execution

Enter the authentication file path in credentialsPath and the save file path (.mp3) in saveFile, and the audio file will be saved.

Summary

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.

reference

None

Postscript

2020/09/09