VOICEVOXをC#でREST-API経由で喋らせる（その２　高速で発話）

# 概要
VOICEVOXをC#でREST-API経由で喋らせる（その１　発話）
https://qiita.com/oyahun/items/e01e56878dc011cdc094
の続き

- 等速の音声を倍速にする

## 環境
VOICEVOX Ver. 0.14.4
Visual Studio 2022
.net7.0
コンソール アプリ

# 流れ
- 音声クエリを生成
- （NEW）音声クエリを加工
- 音声合成
- 音声を保存
- 音声再生する
NuGetからSystem.Windows.Extensionを追加する

# 参考文献
https://voicevox.github.io/voicevox_engine/api/#:~:text=%E3%82%AF%E3%82%A8%E3%83%AA%E4%BD%9C%E6%88%90-,%E9%9F%B3%E5%A3%B0%E5%90%88%E6%88%90%E7%94%A8%E3%81%AE%E3%82%AF%E3%82%A8%E3%83%AA%E3%82%92%E4%BD%9C%E6%88%90%E3%81%99%E3%82%8B,-%E3%82%AF%E3%82%A8%E3%83%AA%E3%81%AE

http://surferonwww.info/BlogEngine/post/2020/05/10/generate-class-definition-from-json-string.aspx

# 音声クエリ
http://localhost:50021/docs のクエリ作成から「text:かき」を実行
```json
{
  "accent_phrases": [
    {
      "moras": [
        {
          "text": "カ",
          "consonant": "k",
          "consonant_length": 0.09681473672389984,
          "vowel": "a",
          "vowel_length": 0.1569003015756607,
          "pitch": 5.77046537399292
        },
        {
          "text": "キ",
          "consonant": "k",
          "consonant_length": 0.12597404420375824,
          "vowel": "i",
          "vowel_length": 0.21633665263652802,
          "pitch": 5.944890975952148
        }
      ],
      "accent": 2,
      "pause_mora": null,
      "is_interrogative": false
    }
  ],
  "speedScale": 1,
  "pitchScale": 0,
  "intonationScale": 1,
  "volumeScale": 1,
  "prePhonemeLength": 0.1,
  "postPhonemeLength": 0.1,
  "outputSamplingRate": 24000,
  "outputStereo": false,
  "kana": "カキ'"
}
```
# プログラム
## 音声クエリのクラス
参考文献を元にJSON形式の音声クエリをC# のクラス定義生成する
エラーは適時修正した（つもり）
```c#:
public class VOICEVOX_query
{
    public Accent_phrases[] accent_phrases { get; set; }
    public double speedScale { get; set; }
    public double pitchScale { get; set; }
    public double intonationScale { get; set; }
    public double volumeScale { get; set; }
    public double prePhonemeLength { get; set; }
    public double postPhonemeLength { get; set; }
    public int outputSamplingRate { get; set; }
    public bool outputStereo { get; set; }
    public string kana { get; set; }
}

public class Accent_phrases
{
    public Moras[] moras { get; set; }
    public int accent { get; set; }
    public Moras? pause_mora { get; set; }
    public bool is_interrogative { get; set; }
}

public class Moras
{
    public string text { get; set; }
    public string? consonant { get; set; }
    public double? consonant_length { get; set; }
    public string? vowel { get; set; }
    public double vowel_length { get; set; }
    public double pitch { get; set; }
}

```
## 倍速プログラム
```c#:console.cs
using System.Media;
using System.Net.Http.Headers;
using System.Text.Json;

using (var httpClient = new HttpClient())
{
    string query;
    int speaker = 1;
    string text = "あかさたな";

    // 音声クエリを生成
    using (var request = new HttpRequestMessage(new HttpMethod("POST"), $"http://localhost:50021/audio_query?text={text}&speaker={speaker}"))
    {
        var response = await httpClient.SendAsync(request);

        query = response.Content.ReadAsStringAsync().Result;
        Console.WriteLine(query);
    }

    //音声クエリの加工
    VOICEVOX_query query2 = JsonSerializer.Deserialize<VOICEVOX_query>(query);
    query2.speedScale = 2;
    query = JsonSerializer.Serialize<VOICEVOX_query>(query2);


    // 音声クエリから音声合成
    using (var request = new HttpRequestMessage(new HttpMethod("POST"), "http://localhost:50021/synthesis?speaker=1&enable_interrogative_upspeak=true"))
    {
        request.Content = new StringContent(query);
        request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");

        var response = await httpClient.SendAsync(request);

        using (var httpStream = await response.Content.ReadAsStreamAsync())
        {
            //読み込む
            var player = new SoundPlayer(httpStream);
            //再生する
            player.PlaySync();
        }
    }
}

```