概要
VOICEVOXをC#でREST-API経由で喋らせる(その1 発話)
https://qiita.com/oyahun/items/e01e56878dc011cdc094
の続き
- 等速の音声を倍速にする
環境
VOICEVOX Ver. 0.14.4
Visual Studio 2022
.net7.0
コンソール アプリ
流れ
- 音声クエリを生成
- (NEW)音声クエリを加工
- 音声合成
- 音声を保存
- 音声再生する
NuGetからSystem.Windows.Extensionを追加する
参考文献
音声クエリ
http://localhost:50021/docs のクエリ作成から「text:かき」を実行
{
"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# のクラス定義生成する
エラーは適時修正した(つもり)
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; }
}
倍速プログラム
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();
}
}
}