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

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

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

Recommended Posts

How to run a quickstart for Google Cloud Text-to-Speech .NET in Unity
How to run a quickstart for Google Cloud Text-to-Speech .NET in Unity
How to run python in virtual space (for MacOS)
How to define multiple variables in a python for statement
How to run TensorFlow 1.0 code in 2.0
How to install OpenCV on Cloud9 and run it in Python
[Introduction to Python] How to use the in operator in a for statement?
[For beginners] How to register a library created in Python in PyPI
[Unity] How to run ML-Agents Release 8 even for transcendental beginners [Windows]
How to use Google Test in C
How to get a stacktrace in python
How to switch python versions in cloud9
How to run a Maya Python script
How to make a model for object detection using YOLO in 3 hours
How to clear tuples in a list (Python)
How to embed a variable in a python string
How to create a JSON file in Python
How to drop Google Docs in one folder in a .txt file with python
How to run CNN in 1 system notation in Tensorflow 2
How to use the Google Cloud Translation API
How to notify a Discord channel in Python
How to create a shortcut command for LINUX
How to run Leap Motion in non-Apple Python
[Python] How to draw a histogram in Matplotlib
How to create a Rest Api in Django
How to write a named tuple document in 2020
How to count numbers in a specific range
How to read a file in a different directory
Instructions for connecting Google Colab. To the local runtime in a Windows environment
How to Mock a Public function in Pytest
How to use Docker to containerize your application and how to use Docker Compose to run your application in a development environment
How to specify a schema in Django's database settings
[For beginners] How to use say command in python!
How to convert / restore a string with [] in python
How to run the Ansible module added in Ansible Tower
How to deploy a Django application on Alibaba Cloud
How to run tests in bulk with Python unittest
[Python] How to expand variables in a character string
Create a plugin to run Python Doctest in Vim (2)
[Go] How to create a custom error for Sentry
A memorandum on how to use keras.preprocessing.image in Keras
How to load files in Google Drive with Google Colaboratory
Create a plugin to run Python Doctest in Vim (1)
How to install Google Test / Google Mock in Visual Studio 2019
A memorandum to run a python script in a bat file
How to display DataFrame as a table in Markdown
How to run setUp only once in python unittest
Convenient to use matplotlib subplots in a for statement
How to build a development environment for TensorFlow (1.0.0) (Mac)
When you want to plt.save in a for statement
How to execute a command using subprocess in Python
How to run Django on IIS on a Windows server
How to reference static files in a Django project
How to use Spacy Japanese model in Google Colaboratory
[Linux] How to put your IP in a variable
Run the program without building a Python environment! !! (How to get started with Google Colaboratory)
How to connect to Cloud SQL PostgreSQL on Google Cloud Platform from a local environment with Java
Send a message from the server to your Chrome extension using Google Cloud Messaging for Chrome
How to support x-nullable for swagger in python's jsonschema library
How to run a trained transformer model locally on CloudTPU
[TensorFlow 2 / Keras] How to run learning with CTC Loss in Keras