From feb82249e355461fb6a5cee331867c0787fa52ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?100=E3=81=AE=E4=BA=BA?= <100@pokemori.jp> Date: Mon, 4 Sep 2023 14:25:12 +0900 Subject: [PATCH] =?UTF-8?q?=E6=8C=87=E5=AE=9A=E3=81=97=E3=81=9F=E5=87=BA?= =?UTF-8?q?=E5=8A=9B=E3=83=87=E3=83=90=E3=82=A4=E3=82=B9=E3=81=A7=E5=86=8D?= =?UTF-8?q?=E7=94=9F=E3=81=99=E3=82=8B=E6=A9=9F=E8=83=BD=E3=82=92=E5=89=8A?= =?UTF-8?q?=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit OBS Studioのアプリケーション音声キャプチャとモニタリングデバイスの設定で同様のことが可能であるため。 --- App.config | 11 +-- App.xaml.cs | 115 -------------------------------- NAudio - license.txt | 8 --- Properties/Settings.Designer.cs | 35 +--------- Properties/Settings.settings | 9 --- ProxyServer.cs | 105 ----------------------------- ncv-voicevox.csproj | 7 -- 7 files changed, 2 insertions(+), 288 deletions(-) delete mode 100644 NAudio - license.txt delete mode 100644 ProxyServer.cs diff --git a/App.config b/App.config index 07b8770..cd588c7 100644 --- a/App.config +++ b/App.config @@ -1,4 +1,4 @@ - + @@ -14,12 +14,6 @@ - - - - - 1 - @@ -30,9 +24,6 @@ ./run.exe - - 49430 - 50021 diff --git a/App.xaml.cs b/App.xaml.cs index 5bea9b8..f74b6fd 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -1,11 +1,8 @@ using System; -using System.Collections.Generic; using System.IO; -using System.Linq; using System.Reflection; using Application = System.Windows.Application; using System.Windows.Forms; -using NAudio.Wave; using Esperecyan.NCVVoicevox.Properties; namespace Esperecyan.NCVVoicevox; @@ -16,7 +13,6 @@ namespace Esperecyan.NCVVoicevox; public partial class App : Application { internal static readonly string Title; - private static readonly int PossibleVolumeValueCount = 100; static App() { @@ -25,75 +21,6 @@ static App() + " " + assembly.GetCustomAttribute()?.InformationalVersion; } - /// - /// 音声出力デバイス名の一覧を取得します。 - /// - /// へ指定するインデックス順で返します。 - private static IEnumerable GetOutputDeviceNames() - { - var indexNamePairs = new List(); - for (var i = 0; i < WaveOut.DeviceCount; i++) - { - indexNamePairs.Add(WaveOut.GetCapabilities(i).ProductName); - } - return indexNamePairs; - } - - /// - /// ユーザー設定から、選択中の音声出力デバイス名を取得します。 - /// - /// 既定のデバイスが選択されていた場合、もしくは存在しないデバイス名が選択されていた場合は、null を返します。 - private static string? GetCurrentOutputDeviceName() - { - var name = Settings.Default.OuputDeviceName; - return !string.IsNullOrEmpty(name) && App.GetOutputDeviceNames().Contains(name) ? name : null; - } - - /// - /// ユーザー設定で選択中の音声出力デバイス名をもとに、 へ指定する値を取得します。 - /// - /// 既定のデバイスが選択されていた場合、もしくは存在しないデバイス名が選択されていた場合は、-1 を返します。 - private static int GetCurrentOutputDeviceIndex() - { - var name = App.GetCurrentOutputDeviceName(); - return name == null ? -1 : App.GetOutputDeviceNames().ToList().IndexOf(name); - } - - /// - /// メニュー内の音声出力デバイス選択アイテムを更新します。 - /// - /// - private static void UpdateDeviceMenuItems(ToolStripItemCollection menuItems) - { - var currentDeviceName = App.GetCurrentOutputDeviceName(); - - foreach (var item - in menuItems.Cast().Where(item => item.Name.StartsWith("output-device")).ToList()) - { - menuItems.Remove(item); - } - - var labels = new[] { "既定のデバイス" }.Concat(App.GetOutputDeviceNames()); - for (var i = 0; i < labels.Count(); i++) - { - var name = labels.ElementAt(i); - var item = new ToolStripMenuItem(name) - { - Name = "output-device" + (i == 0 ? "-default" : ""), - Checked = currentDeviceName == (i == 0 ? null : name), - }; - if (!item.Checked) - { - item.Click += (_, _) => - { - Settings.Default.OuputDeviceName = item.Name == "output-device-default" ? null : item.Text; - Settings.Default.Save(); - }; - } - menuItems.Insert(menuItems.Count - 1, item); - } - } - private App() { Settings.Default.Upgrade(); @@ -127,57 +54,15 @@ private App() return; } - var waveOut = new WaveOut(); - var engineServer = new EngineServer(Settings.Default.EnginePort); - var proxyServer = new ProxyServer(Settings.Default.ProxyServerPort, Settings.Default.EnginePort); - - proxyServer.ProxyEvent += async (_, args) => - { - if (!args.Response.IsSuccessStatusCode - || args.Response.RequestMessage?.RequestUri?.LocalPath != "/synthesis") - { - return null; - } - - var reader = new WaveFileReader(await args.Response.Content.ReadAsStreamAsync()); - waveOut.DeviceNumber = App.GetCurrentOutputDeviceIndex(); - waveOut.Init(reader); - waveOut.Play(); - - // 同じ長さの無音のWAVデータを返却 - var stream = new MemoryStream(); - using var writer = new WaveFileWriter(stream, reader.WaveFormat); - await writer.WriteAsync(new byte[reader.Length]); - await writer.FlushAsync(); - stream.Seek(0, SeekOrigin.Begin); - var responseEntity = new byte[stream.Length]; - await stream.ReadAsync(responseEntity); - return responseEntity; - }; var contextMenuStrip = new ContextMenuStrip() { Renderer = new VolumeIconRenderer(), }; var menuItems = contextMenuStrip.Items; - var volumeBar = new TrackBar() - { - Maximum = App.PossibleVolumeValueCount, - Width = 200, - TickFrequency = 10, - Value = (int)((waveOut.Volume = Settings.Default.Volume) * App.PossibleVolumeValueCount), - }; - volumeBar.ValueChanged += (_, _) => - { - Settings.Default.Volume = waveOut.Volume = (float)volumeBar.Value / App.PossibleVolumeValueCount; - Settings.Default.Save(); - }; - menuItems.Add(new ToolStripControlHost(volumeBar, "volume")); menuItems.Add(new ToolStripMenuItem("終了", image: null, (sender, e) => this.Shutdown())); - contextMenuStrip.Opening += (_, _) => App.UpdateDeviceMenuItems(menuItems); - var notifyIcon = new NotifyIcon() { Text = App.Title, diff --git a/NAudio - license.txt b/NAudio - license.txt deleted file mode 100644 index 82ddf14..0000000 --- a/NAudio - license.txt +++ /dev/null @@ -1,8 +0,0 @@ -Copyright 2020 Mark Heath - -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - diff --git a/Properties/Settings.Designer.cs b/Properties/Settings.Designer.cs index ca6704c..fa52403 100644 --- a/Properties/Settings.Designer.cs +++ b/Properties/Settings.Designer.cs @@ -12,7 +12,7 @@ namespace Esperecyan.NCVVoicevox.Properties { [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.5.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.7.0.0")] internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); @@ -41,15 +41,6 @@ public string EngineFileRelativePath { } } - [global::System.Configuration.ApplicationScopedSettingAttribute()] - [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Configuration.DefaultSettingValueAttribute("49430")] - public int ProxyServerPort { - get { - return ((int)(this["ProxyServerPort"])); - } - } - [global::System.Configuration.ApplicationScopedSettingAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Configuration.DefaultSettingValueAttribute("50021")] @@ -79,29 +70,5 @@ public string VoicevoxPath { this["VoicevoxPath"] = value; } } - - [global::System.Configuration.UserScopedSettingAttribute()] - [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Configuration.DefaultSettingValueAttribute("")] - public string OuputDeviceName { - get { - return ((string)(this["OuputDeviceName"])); - } - set { - this["OuputDeviceName"] = value; - } - } - - [global::System.Configuration.UserScopedSettingAttribute()] - [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Configuration.DefaultSettingValueAttribute("1")] - public float Volume { - get { - return ((float)(this["Volume"])); - } - set { - this["Volume"] = value; - } - } } } diff --git a/Properties/Settings.settings b/Properties/Settings.settings index 7d4983a..259518f 100644 --- a/Properties/Settings.settings +++ b/Properties/Settings.settings @@ -8,9 +8,6 @@ ./run.exe - - 49430 - 50021 @@ -20,11 +17,5 @@ - - - - - 1 - \ No newline at end of file diff --git a/ProxyServer.cs b/ProxyServer.cs deleted file mode 100644 index 1f38ae9..0000000 --- a/ProxyServer.cs +++ /dev/null @@ -1,105 +0,0 @@ -using System; -using System.Linq; -using System.Net; -using System.Net.Http; -using System.Net.Http.Headers; -using System.Threading.Tasks; - -namespace Esperecyan.NCVVoicevox; - -internal class ProxyServer -{ - internal class ProxyEventArgs : EventArgs - { - internal readonly HttpResponseMessage Response; - - internal ProxyEventArgs(HttpResponseMessage response) - { - this.Response = response; - } - } - - internal delegate Task ProxyEventHandler(object sender, ProxyEventArgs e); - - private static readonly HttpClient Client; - - internal event ProxyEventHandler? ProxyEvent; - - static ProxyServer() - { - ProxyServer.Client = new HttpClient(); - } - - private static async Task Proxy(HttpListenerRequest request, Uri uri) - { - var requestMessage = new HttpRequestMessage(new HttpMethod(request.HttpMethod), uri); - - foreach (var name in request.Headers.AllKeys) - { - if (name == null || new[] { "content-length", "content-type" }.Contains(name.ToLowerInvariant())) - { - continue; - } - requestMessage.Headers.Add(name, request.Headers[name]); - } - - using var content = new StreamContent(request.InputStream); - if (request.ContentType != null) - { - content.Headers.ContentType = MediaTypeHeaderValue.Parse(request.ContentType); - } - requestMessage.Content = content; - - return await ProxyServer.Client.SendAsync(requestMessage); - } - - internal ProxyServer(int port, int destinationPort) - { - this.Listen(port, destinationPort); - } - - private async void Listen(int port, int destinationPort) - { - var listener = new HttpListener(); - listener.Prefixes.Add($"http://localhost:{port}/"); - listener.Prefixes.Add($"http://127.0.0.1:{port}/"); - listener.Start(); - - while (true) - { - var context = await listener.GetContextAsync(); - try - { - var responseMessage = await ProxyServer.Proxy(context.Request, new UriBuilder(context.Request.Url!) - { - Port = destinationPort, - }.Uri); - - byte[]? responseEntity = null; - if (this.ProxyEvent != null) - { - responseEntity = await this.ProxyEvent(this, new ProxyEventArgs(responseMessage)); - } - - context.Response.StatusCode = (int)responseMessage.StatusCode; - var headers = responseMessage.Headers; - foreach (var (name, values) in headers) - { - foreach (var value in values) - { - context.Response.AppendHeader(name, value); - } - } - - context.Response.Close( - responseEntity ?? await responseMessage.Content.ReadAsByteArrayAsync(), - willBlock: true - ); - } - finally - { - context.Response.Close(); - } - } - } -} diff --git a/ncv-voicevox.csproj b/ncv-voicevox.csproj index ef9f902..2230526 100644 --- a/ncv-voicevox.csproj +++ b/ncv-voicevox.csproj @@ -13,10 +13,6 @@ True - - - - True @@ -26,9 +22,6 @@ - - PreserveNewest - SettingsSingleFileGenerator Settings.Designer.cs