Aida-chiyo-Talk-app

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

VoiceVox.cs (5162B)


      1 using Microsoft.Scripting.Hosting.Shell;
      2 using System;
      3 using System.Collections.Generic;
      4 using System.Diagnostics;
      5 using System.Linq;
      6 using System.Media;
      7 using System.Net.Http.Headers;
      8 using System.Text;
      9 using System.Text.Json;
     10 using System.Text.Json.Serialization;
     11 using System.Threading.Tasks;
     12 using System.Xml.Serialization;
     13 
     14 namespace Aida_chiyo_Talk_app
     15 {
     16     internal class VoiceVox
     17     {
     18         public async Task VoiceVoxCon(string input)
     19         {
     20             //https://qiita.com/oyahun/items/e01e56878dc011cdc094
     21 
     22             using (var httpClient = new HttpClient())
     23             {
     24                 string query;
     25                 int speaker = 10;
     26                 string text = input;
     27 
     28                 // 音声クエリを生成
     29                 using (var request = new HttpRequestMessage(new HttpMethod("POST"), $"http://localhost:50021/audio_query?text={text}&speaker={speaker}"))
     30                 {
     31                     request.Headers.TryAddWithoutValidation("accept", "application/json");
     32 
     33                     request.Content = new StringContent("");
     34                     request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded");
     35 
     36                     var response = await httpClient.SendAsync(request);
     37 
     38                     query = response.Content.ReadAsStringAsync().Result;
     39                     //Console.WriteLine(query);
     40                 }
     41 
     42                 // 音声クエリから音声合成
     43                 using (var request = new HttpRequestMessage(new HttpMethod("POST"), "http://localhost:50021/synthesis?speaker=10&enable_interrogative_upspeak=true"))
     44                 {
     45                     request.Headers.TryAddWithoutValidation("accept", "audio/wav");
     46 
     47                     request.Content = new StringContent(query);
     48                     request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
     49 
     50                     var response = await httpClient.SendAsync(request);
     51 
     52                     // 音声を保存
     53                     using (var fileStream = System.IO.File.Create("test.wav"))
     54                     {
     55                         using (var httpStream = await response.Content.ReadAsStreamAsync())
     56                         {
     57                             httpStream.CopyTo(fileStream);
     58                             fileStream.Flush();
     59                         }
     60                     }
     61                 }
     62             }
     63 
     64             //読み込む
     65             var player = new SoundPlayer("test.wav");
     66             //再生する
     67             player.PlaySync();
     68             //Console.WriteLine("再生完了");
     69         }
     70 
     71         [JsonPropertyName("name")]
     72         public string Name { get; set; }
     73 
     74         [JsonPropertyName("id")]
     75         public int Id { get; set; }
     76 
     77         public void VoiceVoxOnChecker()
     78         {
     79             if (Process.GetProcessesByName("VOICEVOX").Length > 0)
     80             {
     81                 Console.WriteLine("voicevox was woked up");
     82             }
     83             else
     84             {
     85                 Console.WriteLine("Pleace wokeup voicevox!!");
     86                 while(Process.GetProcessesByName("VOICEVOX").Length == 0)
     87                 {
     88 
     89                 }
     90             }
     91         }
     92     }
     93 
     94     public class SupportedFeatures
     95     {
     96         [JsonPropertyName("permitted_synthesis_morphing")]
     97         public string PermittedSynthesisMorphing { get; set; }
     98     }
     99 
    100     public class Speaker
    101     {
    102         [JsonPropertyName("supported_features")]
    103         public SupportedFeatures SupportedFeatures { get; set; }
    104 
    105         [JsonPropertyName("name")]
    106         public string Name { get; set; }
    107 
    108         [JsonPropertyName("speaker_uuid")]
    109         public string SpeakerUuid { get; set; }
    110 
    111         [JsonPropertyName("styles")]
    112         public List<Style> Styles { get; set; }
    113 
    114         [JsonPropertyName("version")]
    115         public string Version { get; set; }
    116 
    117     }
    118 
    119     public static class VoicevoxUtility
    120     {
    121         const string baseUrl = "http://127.0.0.1:50021/";
    122         // localhostだとレスポンスが遅いのアドレス指定
    123         private static readonly HttpClient httpClient = new HttpClient();
    124 
    125         public static IEnumerable<Speaker> EnumerateSpeakers()
    126         {
    127             var jsonStr = GetSpeakersAsJson().Result;
    128             var deserialized = JsonSerializer.Deserialize<List<Speaker>>(jsonStr);
    129             if (deserialized is null)
    130             {
    131                 yield break;
    132             }
    133 
    134             foreach (var speakerInfo in deserialized)
    135             {
    136                 yield return speakerInfo;
    137             }
    138         }
    139 
    140         private static async Task<string> GetSpeakersAsJson()
    141         {
    142             using var requestMessage = new HttpRequestMessage(new HttpMethod("GET"), $"{baseUrl}speakers");
    143             requestMessage.Headers.TryAddWithoutValidation("accept", "application/json");
    144 
    145             requestMessage.Content = new StringContent("");
    146             requestMessage.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded");
    147             var response = await httpClient.SendAsync(requestMessage);
    148             return await response.Content.ReadAsStringAsync();
    149         }
    150 
    151 
    152     }
    153 }