2
1

More than 3 years have passed since last update.

LaravelでGoogle API Text to Speech を使う

Posted at

やりたかったこと

Google の合成音声(Text to Speech)を使って、簡単なウェブアプリ作りたいなと思いました。

公式ドキュメントだけではつまづいたので、解説です。

まずは、公式ドキュメントのサンプルコードを実行して、音声ファイルをダウンロードできるところまでを目指します。

Google Cloud Text to Seech ドキュメント

Google Cloudでの設定

Google APIを使う時には、Google Cloud で設定をし、jsonファイルをダウンロードしておく必要があります。

ここまでは、公式ドキュメントを参照しながらでも問題はないので、以下をご覧ください。

クイックスタート: クライアント ライブラリの使用

このガイドに従い、1から「4.認証を設定します」まで進めてください。5以降は別の方法となります。

2. Composerでライブラリ追加

ここからがLaravel 独自の方法で進めます。

まずLaravelプロジェクトのディレクトリに移動し、Composer で google のライブラリ一式を追加します。Text to speech だけ入れることもできますが、この際全部入れます。

composer require google/apiclient:"^2.0"

この後、忘れずに dump-autoloadをします。

composer dump-autoload

これでGoogleのライブラリが自動で読み込まれるようになります。

3. jsonファイルの設置

1でダウンロードしたjsonファイルを、Laravel内のどこかに入れます。場所はどこでもいいのですが、今回は config フォルダ内に入れます。

jsonファイル名は仮に google.jsonとし、configフォルダ直下に入れます。

4.実行ファイルを作る

サンプルコードを実行するために、コントローラーを作成します。

php artisan make:controller GoogleController 

本来はModelも作って処理するべきですが、テストなのでコントローラだけで実行します。

Routeも設定します。

Route::get('google', 'GoogleController@index');

Tex to speechサンプルコードはクイックスタートに掲載されているものを使います。ただし、このまま貼り付けてもLaravelでは使えないので、いくつか加工が必要です。

まず、Google APIのドキュメントを参考に、Google APIへの認証情報を設定します。Client Authenticationの項目に、サンプルコードが2種類ありますが、このうち2番目のものを使います。2番目のサンプルコードは以下のものです。

require 'vendor/autoload.php';

use Google\Cloud\VideoIntelligence\V1\VideoIntelligenceServiceClient;

// Authenticating with keyfile data.
$video = new VideoIntelligenceServiceClient([
    'credentials' => json_decode(file_get_contents('/path/to/keyfile.json'), true)
]);

// Authenticating with a keyfile path.
$video = new VideoIntelligenceServiceClient([
    'credentials' => '/path/to/keyfile.json'
]);

1番目のコードでは認証ができなかったです。理由はおそらく、Text to SpeechはVersion 1の方法をとっているから(だと思われます。真相は不明)。

そして、Text to speechのサンプルコードと認証コードを合わせると、コントローラ全体は次のようになります。

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Google\Cloud\TextToSpeech\V1\AudioConfig;
use Google\Cloud\TextToSpeech\V1\AudioEncoding;
use Google\Cloud\TextToSpeech\V1\SsmlVoiceGender;
use Google\Cloud\TextToSpeech\V1\SynthesisInput;
use Google\Cloud\TextToSpeech\V1\TextToSpeechClient;
use Google\Cloud\TextToSpeech\V1\VoiceSelectionParams;

class GoogleController extends Controller
{

  public function index()
{

    // Authenticating with keyfile data.
    $textToSpeechClient = new TextToSpeechClient([
        'credentials' => json_decode(file_get_contents(config_path('google.json')), true)
    ]);

    // Authenticating with a keyfile path.
    $textToSpeechClient = new TextToSpeechClient([
        'credentials' => config_path('google.json')
    ]);

    $input = new SynthesisInput();
    $input->setText('Japan\'s national soccer team won against Colombia!');
    $voice = new VoiceSelectionParams();
    $voice->setLanguageCode('en-US');
    $audioConfig = new AudioConfig();
    $audioConfig->setAudioEncoding(AudioEncoding::MP3);

    $resp = $textToSpeechClient->synthesizeSpeech($input, $voice, $audioConfig);
    file_put_contents(resource_path('test.mp3'), $resp->getAudioContent());
  }
}

routeで設定したURL(/google)を実行すると、resourceフォルダ内に "text.mp3" というファイルが生成されているはずです。

参考

Google公式

LaravelでGoogle APIを使う

Tex to Speechを使う

Composer dump-autload

つまずきの原因は、これをしていないことでした。凡ミス。。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1