Esempio n. 1
0
        public static void RecordMode(string libraryName, string text, string outputFilename)
        {
            SoundRecorder recorder = new SoundRecorder(outputFilename);

            recorder.PostWait = 300;

            var engines = SpeechController.GetVoiceroid2SpeechEngine();
            var engine  = SpeechController.GetVoiceroid2Instance(libraryName);

            if (engine == null)
            {
                Console.WriteLine($"{libraryName} を起動できませんでした。");
                return;
            }

            engine.Activate();
            engine.Finished += (s, a) =>
            {
                Task t = recorder.Stop();
                t.Wait();
                finished = true;
                engine.Dispose();
            };
            recorder.Start();
            engine.Play(text);
        }
Esempio n. 2
0
        private static void WhisperMode(string libraryName, string text)
        {
            string tempFile    = "normal.wav";
            string whisperFile = "whisper.wav";

            var engines = SpeechController.GetVoiceroid2SpeechEngine();
            var engine  = SpeechController.GetVoiceroid2Instance(libraryName);

            if (engine == null)
            {
                Console.WriteLine($"{libraryName} を起動できませんでした。");
                return;
            }
            engine.Activate();

            SoundRecorder recorder = new SoundRecorder(tempFile);

            {
                recorder.PostWait = 300;

                engine.Finished += (s, a) =>
                {
                    finished = true;
                };

                recorder.Start();
                engine.Play(text);
            }

            while (!finished)
            {
                Thread.Sleep(100);
            }
            engine.Dispose();
            Task t = recorder.Stop();

            t.Wait();
            // ささやき声に変換
            Whisper whisper = new Whisper();
            Wave    wave    = new Wave();

            wave.Read(tempFile);
            whisper.Convert(wave);
            wave.Write(whisperFile, wave.Data);

            //// 変換した音声を再生
            SoundPlayer sp = new SoundPlayer();

            sp.Play(whisperFile);
        }
Esempio n. 3
0
        private static void OneShotPlayMode(string libraryName, string text)
        {
            var engines = SpeechController.GetVoiceroid2SpeechEngine();
            var engine  = SpeechController.GetVoiceroid2Instance(libraryName);

            if (engine == null)
            {
                Console.WriteLine($"{libraryName} を起動できませんでした。");
                return;
            }
            engine.Activate();
            engine.Finished += (s, a) =>
            {
                finished = true;
                engine.Dispose();
            };
            engine.Play(text);
        }
Esempio n. 4
0
        private static void InteractiveMode()
        {
            ShowVerbose();

            // ライブラリ名を入力(c.LibraryName列)
            Console.Write("\r\nLibraryName> ");
            name = Console.ReadLine().Trim();

            // 対象となるライブラリを実行
            var engine = SpeechController.GetVoiceroid2Instance(name);

            if (engine == null)
            {
                Console.WriteLine($"{name} を起動できませんでした。");
                Console.ReadKey();
                return;
            }
            // 設定した音声の再生が終了したときに呼び出される処理を設定
            engine.Finished += Engine_Finished;

            // 音声合成エンジンを起動
            engine.Activate();
            engine.SetVolume(1.0f);
            engine.SetPitch(1.0f);
            engine.SetSpeed(1.0f);
            engine.SetPitchRange(1.0f);
            string message = $"音声合成エンジン {engine.Info.EngineName}、{engine.Info.LibraryName}を起動しました。";

            engine.Play(message); // 音声再生は非同期実行される
            Console.WriteLine(message);

            string fileName = "";
            string line     = "";

            while (true)
            {
                Console.WriteLine($"保存ファイル名を入力: ");
                fileName = Console.ReadLine();
                Console.WriteLine($"読み上げテキストを入力: ");
                line = Console.ReadLine();
                if (line.Trim() == "")
                {
                    engine.Dispose();
                    return;
                }
                try
                {
                    engine.Stop(); // 喋っている途中に文字が入力されたら再生をストップ
                    if (fileName == "")
                    {
                        engine.Play(line);
                    }
                    else
                    {
                        engine.Save(fileName, line);
                    }
                    Console.WriteLine($"Volume: {engine.GetVolume()}, Speed: {engine.GetSpeed()}, Pitch: {engine.GetPitch()}, PitchRange: {engine.GetPitchRange()}");
                }catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
Esempio n. 5
0
        static void Main(string[] args)
        {
            var options = new Options();

            if (!CommandLine.Parser.Default.ParseArguments(args, options))
            {
                Environment.Exit(CommandLine.Parser.DefaultExitCodeFail);
            }

            if (args.Length == 0)
            {
                Console.Error.WriteLine(options.GetUsage());
                Environment.Exit(CommandLine.Parser.DefaultExitCodeFail);
            }

            if (options.IsPrintList)
            {
                // -l, --list オプションが指定されている場合、
                // 使用可能 VOICEROID 一覧を表示する。
                var engines = SpeechController.GetVoiceroid2SpeechEngine();
                foreach (var c in engines)
                {
                    Console.WriteLine($"{c.LibraryName},{c.EngineName},{c.EnginePath}");
                }
                return;
            }

            // 入力ファイル、出力ファイルの指定確認
            if (options.InputFile == null)
            {
                Console.Error.WriteLine("入力ファイルパス(--input-file)は必須です。");
                Console.Error.WriteLine(options.GetUsage());
                Environment.Exit(CommandLine.Parser.DefaultExitCodeFail);
            }

            if (options.OutputFile == null)
            {
                Console.Error.WriteLine("出力ファイルパス(--output-file)は必須です。");
                Console.Error.WriteLine(options.GetUsage());
                Environment.Exit(CommandLine.Parser.DefaultExitCodeFail);
            }

            // Voiceroid2Engine 作成
            var engine = SpeechController.GetVoiceroid2Instance(options.Voiceroid);

            if (engine == null)
            {
                Console.WriteLine($"{options.Voiceroid} を起動できませんでした。");
                Console.ReadKey();
                return;
            }
            engine.Activate();

            // ファイル読み込み
            System.Text.Encoding enc = System.Text.Encoding.GetEncoding("utf-8");
            string text = System.IO.File.ReadAllText(options.InputFile, enc);

            // 出力ファイルのベースを取得
            string outputDir = Path.GetDirectoryName(options.OutputFile);

            // 出力ファイルのベースパスが空の場合、 './' が省略されたものとみなす
            if (string.IsNullOrEmpty(outputDir))
            {
                outputDir = ".";
            }
            string outputDirFullPath = System.IO.Path.GetFullPath(outputDir);
            string outputFileBase    = outputDirFullPath + "\\" + Path.GetFileNameWithoutExtension(options.OutputFile);

            // 改行処理
            string replaceString;

            if (options.IsLinebreakToPeriod)
            {
                replaceString = "。";
            }
            else
            {
                replaceString = "";
            }

            // 改行を句点に置換
            foreach (string delete_char in DELETE_CHARS)
            {
                text = text.Replace(delete_char, replaceString);
            }

            // テキストを句点で分割。
            string[] splitedText = text.Split(DELIMITERS, System.StringSplitOptions.RemoveEmptyEntries);

            // 音声保存 1 回分の文字列を記録するバッファを作成
            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            // 大体 --split-size 文字毎にひとまとめにして音声保存していく。
            short count = 1;

            foreach (string sentence in splitedText)
            {
                sb.Append(sentence);
                sb.Append("。");

                // 指定文字数を超える場合、
                // 今までため込んでいたものを読み上げる。
                // ただし、初回から超えていた場合はあきらめる。
                if (sb.Length > options.SplitSize)
                {
                    // ファイル名組み立て
                    string fileName = String.Format("{0}_{1:D3}.wav", outputFileBase, count);
                    count++;

                    string tmp = sb.ToString().Normalize(NormalizationForm.FormKC);

                    engine.Save(fileName, tmp);

                    // sb リセット
                    sb.Clear();
                }
            }

            // 最後に残った文字列があれば音声保存
            if (sb.Length > 0)
            {
                // ファイル名組み立て
                string fileName = String.Format("{0}_{1:D3}.wav", outputFileBase, count);
                engine.Save(fileName, sb.ToString());
            }

            return;
        }