Skip to content

Commit

Permalink
Merge pull request #12 from shigobu/話速・音量対応
Browse files Browse the repository at this point in the history
jsonを編集して話速・音量を調整できるようにした。
  • Loading branch information
shigobu authored Aug 28, 2021
2 parents 0462b4f + 9f8b7e9 commit 0ea68e6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
5 changes: 5 additions & 0 deletions SAPIForVOICEVOX/SAPIForVOICEVOX.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,12 @@
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Numerics" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand Down Expand Up @@ -92,6 +96,7 @@
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="ボイスボックスと通信ができません.wav" />
</ItemGroup>
<ItemGroup>
Expand Down
44 changes: 41 additions & 3 deletions SAPIForVOICEVOX/VoiceVoxTTSEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Diagnostics;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace SAPIForVOICEVOX
{
Expand Down Expand Up @@ -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<byte[]> waveDataTask = SendToVoiceVox(currentTextList.pTextStart, SpeakerNumber);
Task<byte[]> waveDataTask = SendToVoiceVox(currentTextList.pTextStart, SpeakerNumber, speed, 0, volume);
waveDataTask.Wait();
byte[] waveData = waveDataTask.Result;

Expand Down Expand Up @@ -250,8 +265,11 @@ public static void UnregisterClass(string key)
/// </summary>
/// <param name="text">セリフ</param>
/// <param name="speakerNum">話者番号</param>
/// <param name="speedScale">話速 0.5~2.0 中央=1</param>
/// <param name="pitchScale">音高 -0.15~0.15 中央=0</param>
/// <param name="volumeScale">音量 0.0~1.0</param>
/// <returns>waveデータ</returns>
async Task<byte[]> SendToVoiceVox(string text, int speakerNum)
async Task<byte[]> SendToVoiceVox(string text, int speakerNum, double speedScale, double pitchScale, double volumeScale)
{
//エンジンが起動中か確認を行う
Process[] ps = Process.GetProcessesByName("run");
Expand Down Expand Up @@ -282,8 +300,15 @@ async Task<byte[]> 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))
{
Expand All @@ -305,5 +330,18 @@ async Task<byte[]> SendToVoiceVox(string text, int speakerNum)
}
}

/// <summary>
/// 数値をある範囲から別の範囲に変換します。
/// </summary>
/// <param name="x">変換したい数値</param>
/// <param name="in_min">現在の範囲の下限</param>
/// <param name="in_max">現在の範囲の上限</param>
/// <param name="out_min">変換後の範囲の下限</param>
/// <param name="out_max">変換後の範囲の上限</param>
/// <returns>変換結果</returns>
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;
}
}
}
4 changes: 4 additions & 0 deletions SAPIForVOICEVOX/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="13.0.1" targetFramework="net471" />
</packages>

0 comments on commit 0ea68e6

Please sign in to comment.