テキストを日本語ボイスに変換してくれるしょぼいWindowsソフトを作ったよ。

2020.01.21

Logging

VS2017のC#で、テキストを日本語ボイスに変換してくれるしょぼいWindowsソフト( 滑舌が悪いけどYOUTUBERしたい人用)を作ったよ。自分はかなり滑舌悪いくてどもるので、YOUTUBER向かないのですがユーチューバーしてみたい願望があったので、Gさんの「Cloud Text-to-Speech API」で適当に作ってみた。ちなみにまだYOUTUBERするかは決めてない!

APIですが月に0?100万文字までは無料枠なので、大量に使わない限り無料枠で収まると思います。これを他の人に提供しようとするとアウトだろうけど・・。
自分だけが使用するのには何とかその範囲内かと思います。

尚、ソースコードを提供します可変してお好みで使ってください。大量のテキストをボイス変換した場合、ビジーになるかもしれません。そこら辺の処理は入れてません。またGさんからダウンロードしたJSONファイルを置いている階層に合わしてください。

参考にしたサイトは下記になります。
https://cloud.google.com/text-to-speech/docs/quickstart-client-libraries?hl=ja#client-libraries-install-csharp

フォームのオブジェクトの配置は下記になります。画像を参照ください。

ソースコードは下記になります。

using System;
using System.IO;
using System.Windows.Forms;
using Google.Cloud.TextToSpeech.V1;
namespace テキストを日本語ボイスする
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if(richTextBox1.Text.Replace("\r", "").Replace("\n", "") == "")
            {
                MessageBox.Show("文字を入力してください");
                return;
            }
            Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", "C:\\xxxxx\\xxxxx\\xxxxx\\xxxxx.json", EnvironmentVariableTarget.Process);
            TextToSpeechClient client = TextToSpeechClient.Create();
            SynthesisInput input = new SynthesisInput
            {
                Text = richTextBox1.Text.Replace("\r", "").Replace("\n", "")
            };
            VoiceSelectionParams voice = new VoiceSelectionParams
            {
                LanguageCode = "ja-JP",
                Name = "ja-JP-Wavenet-A",
                SsmlGender = SsmlVoiceGender.Neutral,
            };
            AudioConfig config = new AudioConfig
            {
                AudioEncoding = AudioEncoding.Mp3,
                SpeakingRate = f1(trackBar1.Value),
                Pitch = f2(trackBar2.Value),
            };
            var response = client.SynthesizeSpeech(new SynthesizeSpeechRequest
            {
                Input = input,
                Voice = voice,
                AudioConfig = config
            });
            DateTime dt = DateTime.Now;
            string dttimename = dt.ToString("yyyy-MM-dd-HH-mm-ss");
            using (Stream output = File.Create("voice-" + dttimename + ".mp3"))
            {
                response.AudioContent.WriteTo(output);
                Console.WriteLine($"Audio content written to file 'voice - " + dttimename + ".mp3'");
                MessageBox.Show("生成しました=>>voice - " + dttimename + ".mp3");
                System.Diagnostics.Process.Start(Directory.GetCurrentDirectory());
            }
        }
        private void trackBar1_ValueChanged(object sender, EventArgs e)
        {
            label3.Text = string.Format("{0:0.00}", f1(trackBar1.Value));
        }
        private void trackBar2_ValueChanged(object sender, EventArgs e)
        {
            label4.Text = string.Format("{0:0.00}", f2(trackBar2.Value));
        }
        private double f1(int a) {
            return Convert.ToDouble(a) / 100;
        }
        private double f2(int a)
        {
            return Convert.ToDouble(a) - 20;
        }
    }
}

変換したボイスはこんな感じです!

タグ

0, 100, 2017, API, Cloud, json, Text-to-Speech, VS, Windows, YOUTUBER, アウト, お好み, かなり, コード, これ, ソース, そこら辺, ソフト, ダウンロード, テキスト, ビジー, ファイル, ボイス, ユーチューバー, , , , 使用, 処理, 場合, 変換, 大量, 提供, 日本語, , , 滑舌, 無料, 範囲, 自分, 適当, 限り, 願望,