diff --git a/SAPIForVOICEVOX/SAPIForVOICEVOX.csproj b/SAPIForVOICEVOX/SAPIForVOICEVOX.csproj index 3efbc74..e066f1f 100644 --- a/SAPIForVOICEVOX/SAPIForVOICEVOX.csproj +++ b/SAPIForVOICEVOX/SAPIForVOICEVOX.csproj @@ -56,8 +56,12 @@ prompt + + ..\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll + + @@ -92,6 +96,7 @@ + diff --git a/SAPIForVOICEVOX/VoiceVoxTTSEngine.cs b/SAPIForVOICEVOX/VoiceVoxTTSEngine.cs index a800722..5d2dea0 100644 --- a/SAPIForVOICEVOX/VoiceVoxTTSEngine.cs +++ b/SAPIForVOICEVOX/VoiceVoxTTSEngine.cs @@ -9,6 +9,8 @@ using System.Net.Http; using System.Runtime.CompilerServices; using System.Diagnostics; +using Newtonsoft.Json.Linq; +using Newtonsoft.Json; namespace SAPIForVOICEVOX { @@ -85,9 +87,22 @@ public void Speak(uint dwSpeakFlags, ref Guid rguidFormatId, ref WAVEFORMATEX pW SPVTEXTFRAG currentTextList = pTextFragList; while (true) { + pOutputSite.GetRate(out int tempInt); + //SAPIは0が真ん中 + double speed; + if (tempInt < 0) + { + speed = Map(tempInt, -10, 0, 0.5, 1.0); + } + else + { + speed = Map(tempInt, 0, 10, 1.0, 2.0); + } + pOutputSite.GetVolume(out ushort tempUshort); + double volume = Map(tempUshort, 0, 100, 0.0, 1.0); //VOICEVOXへ送信 //asyncメソッドにはref引数を指定できないらしいので、awaitも使用できない。awaitを使用しない実装にした。 - Task waveDataTask = SendToVoiceVox(currentTextList.pTextStart, SpeakerNumber); + Task waveDataTask = SendToVoiceVox(currentTextList.pTextStart, SpeakerNumber, speed, 0, volume); waveDataTask.Wait(); byte[] waveData = waveDataTask.Result; @@ -250,8 +265,11 @@ public static void UnregisterClass(string key) /// /// セリフ /// 話者番号 + /// 話速 0.5~2.0 中央=1 + /// 音高 -0.15~0.15 中央=0 + /// 音量 0.0~1.0 /// waveデータ - async Task SendToVoiceVox(string text, int speakerNum) + async Task SendToVoiceVox(string text, int speakerNum, double speedScale, double pitchScale, double volumeScale) { //エンジンが起動中か確認を行う Process[] ps = Process.GetProcessesByName("run"); @@ -282,8 +300,15 @@ async Task SendToVoiceVox(string text, int speakerNum) //戻り値を文字列にする string resBodyStr = await resultAudioQuery.Content.ReadAsStringAsync(); + //jsonの値変更 + JObject jsonObj = JObject.Parse(resBodyStr); + jsonObj["speedScale"] = speedScale; + jsonObj["pitchScale"] = pitchScale; + jsonObj["volumeScale"] = volumeScale; + string jsonString = JsonConvert.SerializeObject(jsonObj, Formatting.None); + //jsonコンテンツに変換 - var content = new StringContent(resBodyStr, Encoding.UTF8, @"application/json"); + var content = new StringContent(jsonString, Encoding.UTF8, @"application/json"); //synthesis送信 using (var resultSynthesis = await httpClient.PostAsync(@"http://localhost:50021/synthesis?speaker=" + speakerString, content)) { @@ -305,5 +330,18 @@ async Task SendToVoiceVox(string text, int speakerNum) } } + /// + /// 数値をある範囲から別の範囲に変換します。 + /// + /// 変換したい数値 + /// 現在の範囲の下限 + /// 現在の範囲の上限 + /// 変換後の範囲の下限 + /// 変換後の範囲の上限 + /// 変換結果 + double Map(double x, double in_min, double in_max, double out_min, double out_max) + { + return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; + } } } diff --git a/SAPIForVOICEVOX/packages.config b/SAPIForVOICEVOX/packages.config new file mode 100644 index 0000000..d7b227b --- /dev/null +++ b/SAPIForVOICEVOX/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file