Quantcast
Channel: C#タグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 9743

Google Cloud Text-to-Speech .NET用クイックスタートをUnityで実行する方法

$
0
0

やりたいこと

Google Cloud Text-to-SpeechにはUnity用のSDKが用意されていません。
そこで、.NET用のクイックスタートをUnityで実行します。
ソースコードは以下にアップロードしてあります。
https://github.com/AzetaTakuya/GoogleCloudText-to-SpeechForUnity

結果

分かりづらいですが、sample.mp3を保存することができました。
sample.mp3を再生するとHello, World!と再生されます。
結果.png

実装

環境

  • 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
    ※2020/08/30現在 ver2.0.0を使用すると2度目の実行からフリーズしてしまったのでver1.0.0を使用します。原因/対策が分かる方いましたら教えてください。

手順

  1. 認証ファイルの作成
  2. .NET用ライブラリ(Nuget Package)をインストール
  3. ライブラリをUnityにインポート
  4. QuickStartをUnity用に修正
  5. 実行

    ※手順は基本的に公式ドキュメント:クイックスタートに従います。

①認証ファイルの作成

公式ドキュメント:クイックスタートの①~④までを完了すると、JSONファイルが生成されます。
今回、環境変数の設定はスクリプトから行うので省略して構いません。

②.NET用ライブラリ(Nuget Package)をインストール

NugetPackageをUnityにインストールする方法として、NuGetForUnityが有名ですが宗教上の理由で使用しません。
VisualStudioのパッケージマネージャーコンソールからインストールします。

VisualStudioプロジェクト作成

VisualStudioを開いて、【ファイル -> 新規作成 -> プロジェクト】からコンソールアプリ(.NET Framework)を作成します。
今回は、プロジェクト名は[TextToSpeechV1]とし、.NET Framework 4.7.1を使用しました。
コンソールアプリ作成.png

プロジェクトの作成ができたら、【ツール -> Nuget パッケージマネージャー -> パッケージマネージャーコンソール】から、パッケージマネージャーコンソールを開きます。
パッケージマネージャ.png

パッケージマネージャーコンソールが開いたら以下を実行します。

PM> Install-Package Google.Cloud.TextToSpeech.V1 -Version 1.0.0

実行が終了したら、【TextToSpeechV1(※作成したプロジェクト)/Packages】を確認します。
パッケージフォルダ.png

③ライブラリをUnityにインポート

先ほどのフォルダの中にはDLLが入っているので、Unityにインポートできる様に修正します。
作業内容としては、
① Grpc.Core.1.22.0以外のフォルダ内にある【lib/net45/】内のファイルを全てpacakes直下に移動し、Grpc.Core.1.22.0以外のフォルダを全て削除
② 【Grpc.Core.1.22.0/lib/netstandard2.0】と【Grpc.Core.1.22.0/lib/netstandard1.5】を削除
③ 【Grpc.Core.1.22.0/runtimes/win/native】内のgrpc_csharp_ext.x64.dllかgrpc_csharp_ext.x84.dllのどちらかをgrpc_csharp_ext.dllに名前を変更

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

以上が完了したら、Unityプロジェクトを作成します。
Unityプロジェクトを作成したら、先程修正したpakegesフォルダの名前をPluginsに変更し、Unityにインポートします。
これでUnityにライブラリのインポートが完了しました。

④QuickStartをUnity用に修正

.NET用のクイックスタートにあるスクリプトをUnity用に修正・環境変数の追加をしたものが以下となります。

usingSystem.IO;usingUnityEngine;usingSystem;usingGoogle.Cloud.TextToSpeech.V1;publicclassQuickStart:MonoBehaviour{publicstringcredentialsPath;publicstringsaveFile;voidStart(){#regionEnvironmentVariableif(!File.Exists(credentialsPath)){Debug.LogError("failure"+credentialsPath);return;}else{Debug.Log("success: "+credentialsPath);}Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS",credentialsPath);#endregion#regionQuickStart// Instantiate a clientTextToSpeechClientclient=TextToSpeechClient.Create();// Set the text input to be synthesized.SynthesisInputinput=newSynthesisInput{Text="Hello, World!"};// Build the voice request, select the language code ("en-US"),// and the SSML voice gender ("neutral").VoiceSelectionParamsvoice=newVoiceSelectionParams{LanguageCode="en-US",SsmlGender=SsmlVoiceGender.Neutral};// Select the type of audio file you want returned.AudioConfigconfig=newAudioConfig{AudioEncoding=AudioEncoding.Mp3};// Perform the Text-to-Speech request, passing the text input// with the selected voice parameters and audio file typevarresponse=client.SynthesizeSpeech(newSynthesizeSpeechRequest{Input=input,Voice=voice,AudioConfig=config});// Write the binary AudioContent of the response to an MP3 file.using(Streamoutput=File.Create(saveFile)){response.AudioContent.WriteTo(output);Debug.Log($"Audio content written to file "+saveFile);}#endregion}}

⑤実行

credentialsPathに認証ファイルのパスを、saveFileに保存ファイルパス(.mp3)を入れ実行すると音声ファイルが保存されます。

まとめ

上手くできないという声が多い気がしたので書いてみました。
GoogleCloutPlatform自体の使い方については結構雑な感じなので要望があれば書こうと思います。
なんでGoogle.Cloud.TextToSpeech.V1 -Version 2.0.0では安定動作しないんだ...?
気が向いたらWindows以外も対応します。

参考

なし


Viewing all articles
Browse latest Browse all 9743

Trending Articles