Skip to content

Commit

Permalink
指定した出力デバイスで再生する機能を削除
Browse files Browse the repository at this point in the history
OBS Studioのアプリケーション音声キャプチャとモニタリングデバイスの設定で同様のことが可能であるため。
  • Loading branch information
esperecyan committed Sep 4, 2023
1 parent 43315c4 commit feb8224
Show file tree
Hide file tree
Showing 7 changed files with 2 additions and 288 deletions.
11 changes: 1 addition & 10 deletions App.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
Expand All @@ -14,12 +14,6 @@
<setting name="VoicevoxPath" serializeAs="String">
<value />
</setting>
<setting name="OuputDeviceName" serializeAs="String">
<value />
</setting>
<setting name="Volume" serializeAs="String">
<value>1</value>
</setting>
</Esperecyan.NCVVoicevox.Properties.Settings>
</userSettings>
<applicationSettings>
Expand All @@ -30,9 +24,6 @@
<setting name="EngineFileRelativePath" serializeAs="String">
<value>./run.exe</value>
</setting>
<setting name="ProxyServerPort" serializeAs="String">
<value>49430</value>
</setting>
<setting name="EnginePort" serializeAs="String">
<value>50021</value>
</setting>
Expand Down
115 changes: 0 additions & 115 deletions App.xaml.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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()
{
Expand All @@ -25,75 +21,6 @@ static App()
+ " " + assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion;
}

/// <summary>
/// 音声出力デバイス名の一覧を取得します。
/// </summary>
/// <returns><see cref="WaveOut.DeviceNumber"/> へ指定するインデックス順で返します。</returns>
private static IEnumerable<string> GetOutputDeviceNames()
{
var indexNamePairs = new List<string>();
for (var i = 0; i < WaveOut.DeviceCount; i++)
{
indexNamePairs.Add(WaveOut.GetCapabilities(i).ProductName);
}
return indexNamePairs;
}

/// <summary>
/// ユーザー設定から、選択中の音声出力デバイス名を取得します。
/// </summary>
/// <returns>既定のデバイスが選択されていた場合、もしくは存在しないデバイス名が選択されていた場合は、<c>null</c> を返します。</returns>
private static string? GetCurrentOutputDeviceName()
{
var name = Settings.Default.OuputDeviceName;
return !string.IsNullOrEmpty(name) && App.GetOutputDeviceNames().Contains(name) ? name : null;
}

/// <summary>
/// ユーザー設定で選択中の音声出力デバイス名をもとに、<see cref="WaveOut.DeviceNumber"/> へ指定する値を取得します。
/// </summary>
/// <returns>既定のデバイスが選択されていた場合、もしくは存在しないデバイス名が選択されていた場合は、<c>-1</c> を返します。</returns>
private static int GetCurrentOutputDeviceIndex()
{
var name = App.GetCurrentOutputDeviceName();
return name == null ? -1 : App.GetOutputDeviceNames().ToList().IndexOf(name);
}

/// <summary>
/// メニュー内の音声出力デバイス選択アイテムを更新します。
/// </summary>
/// <param name="menuItems"></param>
private static void UpdateDeviceMenuItems(ToolStripItemCollection menuItems)
{
var currentDeviceName = App.GetCurrentOutputDeviceName();

foreach (var item
in menuItems.Cast<ToolStripItem>().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();
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 0 additions & 8 deletions NAudio - license.txt

This file was deleted.

35 changes: 1 addition & 34 deletions Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 0 additions & 9 deletions Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
<Setting Name="EngineFileRelativePath" Type="System.String" Scope="Application">
<Value Profile="(Default)">./run.exe</Value>
</Setting>
<Setting Name="ProxyServerPort" Type="System.Int32" Scope="Application">
<Value Profile="(Default)">49430</Value>
</Setting>
<Setting Name="EnginePort" Type="System.Int32" Scope="Application">
<Value Profile="(Default)">50021</Value>
</Setting>
Expand All @@ -20,11 +17,5 @@
<Setting Name="VoicevoxPath" Type="System.String" Scope="User">
<Value Profile="(Default)" />
</Setting>
<Setting Name="OuputDeviceName" Type="System.String" Scope="User">
<Value Profile="(Default)" />
</Setting>
<Setting Name="Volume" Type="System.Single" Scope="User">
<Value Profile="(Default)">1</Value>
</Setting>
</Settings>
</SettingsFile>
105 changes: 0 additions & 105 deletions ProxyServer.cs

This file was deleted.

7 changes: 0 additions & 7 deletions ncv-voicevox.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
<UseWindowsForms>True</UseWindowsForms>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NAudio.WinForms" Version="2.1.0" />
</ItemGroup>

<ItemGroup>
<Compile Update="Properties\Settings.Designer.cs">
<DesignTimeSharedInput>True</DesignTimeSharedInput>
Expand All @@ -26,9 +22,6 @@
</ItemGroup>

<ItemGroup>
<None Update="NAudio - license.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
Expand Down

0 comments on commit feb8224

Please sign in to comment.