音乐识别相关的特殊动作

This commit is contained in:
ZouJin 2023-08-10 21:34:11 +10:00
parent 3ecc9aa55a
commit b7034e7a1b
13 changed files with 241 additions and 20 deletions

View File

@ -395,14 +395,14 @@ namespace VPet_Simulator.Core
if (wavetimes++ > 4)
if (wavetop == true)
{
if (wavetimes >= 10 || DisplayType.Type == GraphType.Default || DisplayType.Type == GraphType.Touch_Head)
if (wavetimes >= 10 || IsIdel || DisplayType.Type == GraphType.Touch_Head)
DisplayTouchHead();
//Console.WriteLine(wavetimes);
LastInteractionTime = DateTime.Now;
}
else
{
if (wavetimes >= 10 || DisplayType.Type == GraphType.Default || DisplayType.Type == GraphType.Touch_Body)
if (wavetimes >= 10 || IsIdel || DisplayType.Type == GraphType.Touch_Body)
DisplayTouchBody();
LastInteractionTime = DateTime.Now;
}

View File

@ -433,7 +433,7 @@ namespace VPet_Simulator.Core
/// <param name="name">动画名称</param>
/// <param name="EndAction">动画结束后操作(附带名字)</param>
/// <param name="animat">动画的动作 Start Loop End</param>
public void Display(string name, AnimatType animat, Action<string> EndAction = null)
public void Display(string name, AnimatType animat, Action<string> EndAction)
{
Display(Core.Graph.FindGraph(name, animat, Core.Save.Mode), new Action(() => EndAction.Invoke(name)));
}

View File

@ -45,7 +45,7 @@ namespace VPet_Simulator.Core
Task.Run(() =>
{
OnSay?.Invoke(text);
if (force || !string.IsNullOrWhiteSpace(graphname) && DisplayType.Type == GraphType.Default)
if (force || !string.IsNullOrWhiteSpace(graphname) && IsIdel)
Display(graphname, AnimatType.A_Start, () =>
{
Dispatcher.Invoke(() => MsgBar.Show(Core.Save.Name, text, graphname));
@ -278,6 +278,9 @@ namespace VPet_Simulator.Core
/// 想要随机显示的接口 (return:是否成功)
/// </summary>
public List<Func<bool>> RandomInteractionAction = new List<Func<bool>>();
public bool IsIdel => (DisplayType.Type == GraphType.Default || DisplayType.Type == GraphType.Work) && !isPress;
/// <summary>
/// 每隔指定时间自动触发计算 可以关闭EventTimer后手动计算
/// </summary>
@ -297,9 +300,9 @@ namespace VPet_Simulator.Core
}
//UIHandle
Dispatcher.Invoke(() => TimeUIHandle.Invoke(this));
Dispatcher.Invoke(() => TimeUIHandle?.Invoke(this));
if (DisplayType.Type == GraphType.Default && !isPress)
if (IsIdel)
switch (Function.Rnd.Next(Math.Max(20, Core.Controller.InteractionCycle - CountNomal)))
{
case 0:
@ -308,7 +311,7 @@ namespace VPet_Simulator.Core
//显示移动
DisplayMove();
break;
case 3:
case 3:
case 4:
case 5:
//显示待机
@ -321,8 +324,8 @@ namespace VPet_Simulator.Core
DisplaySleep();
break;
case 8:
case 9:
case 10:
case 9:
case 10:
//给其他显示留个机会
var list = RandomInteractionAction.ToList();
for (int i = Function.Rnd.Next(list.Count); 0 != list.Count; i = Function.Rnd.Next(list.Count))
@ -337,7 +340,7 @@ namespace VPet_Simulator.Core
list.RemoveAt(i);
}
}
break;
break;
}
}

View File

@ -40,7 +40,7 @@
Foreground="{DynamicResource DARKPrimary}" Grid.ColumnSpan="3" />
<Button Grid.Column="2" VerticalAlignment="Center" Background="Transparent"
Foreground="{DynamicResource DARKPrimary}" Content="{ll:Str 详细}" Cursor="Hand"
Click="MenuPanel_Click" IsEnabled="False" Grid.ColumnSpan="3" HorizontalAlignment="Right">
Click="MenuPanel_Click" Visibility="Collapsed" Grid.ColumnSpan="3" HorizontalAlignment="Right">
<Button.ContentTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" TextDecorations="Underline" />

View File

@ -102,7 +102,12 @@ namespace VPet_Simulator.Windows.Interface
/// <summary>
/// 存档 Hash检查 是否通过
/// </summary>
bool HashCheck { get; }
bool HashCheck { get; }
/// <summary>
/// 获得当前系统音乐播放音量
/// </summary>
float AudioPlayingVolume();
}
}

View File

@ -390,5 +390,21 @@ namespace VPet_Simulator.Windows.Interface
line.SetDouble("y", value.Y);
}
}
/// <summary>
/// 当实时播放音量达到该值时运行音乐动作
/// </summary>
public double MusicCatch
{
get => this["gameconfig"].GetDouble("musiccatch", 0.3);
set => this["gameconfig"].SetDouble("musiccatch", value);
}
/// <summary>
/// 当实时播放音量达到该值时运行特殊音乐动作
/// </summary>
public double MusicMax
{
get => this["gameconfig"].GetDouble("musicmax", 0.70);
set => this["gameconfig"].SetDouble("musicmax", value);
}
}
}

View File

@ -45,7 +45,7 @@ namespace VPet_Simulator.Windows
public void ShowSetting()
{
mw.Topmost = false;
mw.winSetting.Show();
mw.ShowSetting();
}
public void ShowPanel()

View File

@ -1,16 +1,21 @@
using ChatGPT.API.Framework;
using CSCore.CoreAudioAPI;
using LinePutScript;
using LinePutScript.Localization.WPF;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using VPet_Simulator.Core;
using VPet_Simulator.Windows.Interface;
using static VPet_Simulator.Core.GraphInfo;
using Timer = System.Timers.Timer;
using ToolBar = VPet_Simulator.Core.ToolBar;
namespace VPet_Simulator.Windows
@ -392,5 +397,93 @@ namespace VPet_Simulator.Windows
HashCheck = line.GetLongHashCode() == hash;
}
}
/// <summary>
/// 获得当前系统音乐播放音量
/// </summary>
public float AudioPlayingVolume()
{
using (var enumerator = new MMDeviceEnumerator())
{
using (var meter = AudioMeterInformation.FromDevice(enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia)))
{
return meter.GetPeakValue();
}
}
}
/// <summary>
/// 音乐检测器
/// </summary>
private void Handle_Music(Main obj)
{
if (MusicTimer.Enabled == false && Core.Graph.FindGraphs("Music", AnimatType.A_Start, Core.Save.Mode) != null &&
Main.IsIdel && AudioPlayingVolume() > Set.MusicCatch)
{
catch_MusicVolSum = 0;
catch_MusicVolCount = 0;
CurrMusicType = null;
MusicTimer.Start();
Task.Run(() =>
{//等2秒看看识别结果
Thread.Sleep(2500);
if (CurrMusicType != null && Main.IsIdel)
{//识别通过,开始跑跳舞动画
Main.Display(Core.Graph.FindGraph("Music", AnimatType.A_Start, Core.Save.Mode), Display_Music);
}
else
{ //失败或有东西阻塞,停止检测
MusicTimer.Stop();
}
});
}
}
private void Display_Music()
{
if (CurrMusicType.HasValue)
{
if (CurrMusicType.Value)
{//播放更刺激的
var mg = Core.Graph.FindGraph("Music", AnimatType.Single, Core.Save.Mode);
mg ??= Core.Graph.FindGraph("Music", AnimatType.B_Loop, Core.Save.Mode);
Main.Display(mg, Display_Music);
}
else
{
Main.Display(Core.Graph.FindGraph("Music", AnimatType.B_Loop, Core.Save.Mode), Display_Music);
}
}
else
{
Main.Display("Music", AnimatType.C_End);
}
}
private void MusicTimer_Elapsed(object sender, ElapsedEventArgs e)
{
if (catch_MusicVolCount >= 10)
{
double ans = catch_MusicVolSum / catch_MusicVolCount;
catch_MusicVolSum = 0;
catch_MusicVolCount = 0;
if (ans > Set.MusicCatch)
{
var bef = CurrMusicType;
CurrMusicType = ans > Set.MusicMax;
if (bef != CurrMusicType)
Display_Music();
MusicTimer.Start();
}
else
{
CurrMusicType = null;
if (Main.DisplayType.Name == "Music")
Main.Display("Music", AnimatType.C_End);
}
}
}
public Timer MusicTimer;
private double catch_MusicVolSum;
private int catch_MusicVolCount;
public bool? CurrMusicType { get; private set; }
}
}

View File

@ -287,6 +287,13 @@ namespace VPet_Simulator.Windows
}
Set["CGPT"].Remove(cgpteb);
}
//音乐识别timer加载
MusicTimer = new System.Timers.Timer(200)
{
AutoReset = false
};
MusicTimer.Elapsed += MusicTimer_Elapsed;
Dispatcher.Invoke(new Action(() =>
{
@ -297,7 +304,7 @@ namespace VPet_Simulator.Windows
winSetting = new winGameSetting(this);
winBetterBuy = new winBetterBuy(this);
Main = new Main(Core) { };
Main = new Main(Core);
Main.NoFunctionMOD = Set.CalFunState;
switch (Set["CGPT"][(gstr)"type"])
@ -315,6 +322,7 @@ namespace VPet_Simulator.Windows
break;
}
LoadingText.Content = "正在加载游戏".Translate();
var m = new System.Windows.Controls.MenuItem()
{
@ -341,7 +349,7 @@ namespace VPet_Simulator.Windows
new winReport(this, "由于插件引起的游戏启动错误".Translate() + "\n" + e.ToString()).Show();
}
Foods.ForEach(item => item.LoadImageSource(this));
Main.TimeHandle += Handle_Music;
Main.DefaultClickAction = () =>
{
if (new TimeSpan(DateTime.Now.Ticks - lastclicktime).TotalSeconds > 20)
@ -492,6 +500,7 @@ namespace VPet_Simulator.Windows
}
private void Main_Event_TouchBody()
{
Set.Statistics[(gint)"stat_touch_body"]++;

View File

@ -94,6 +94,9 @@
<Reference Include="ChatGPT.API.Framework, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\ChatGPT.API.Framework.1.0.4\lib\net462\ChatGPT.API.Framework.dll</HintPath>
</Reference>
<Reference Include="CSCore, Version=1.2.1.2, Culture=neutral, PublicKeyToken=5a08f2b6f4415dea, processorArchitecture=MSIL">
<HintPath>..\packages\CSCore.1.2.1.2\lib\net35-client\CSCore.dll</HintPath>
</Reference>
<Reference Include="LinePutScript, Version=1.5.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\LinePutScript.1.9.2\lib\net462\LinePutScript.dll</HintPath>
</Reference>

View File

@ -270,9 +270,9 @@
</Grid>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" Background="{x:Null}"
TextWrapping="Wrap" Margin="0,10,0,0">
<Run FontWeight="Bold" FontSize="18" Text="{ll:Str 聊天设置}" /><LineBreak />
<Run Text="{ll:Str 使用ChatGPT进行聊天等相关设置}" />
TextWrapping="Wrap" Margin="0,5,0,0">
<Run FontWeight="Bold" FontSize="18" Text="{ll:Str 聊天设置}" /><LineBreak />
<Run Text="{ll:Str 使用ChatGPT进行聊天等相关设置}" />
</TextBlock>
<Grid>
<Grid.ColumnDefinitions>
@ -307,12 +307,52 @@
Grid.Row="1" />
<RadioButton x:Name="RBCGPTDIY" Style="{DynamicResource StandardRadioButtonStyle}"
Content="{ll:Str '自定义聊天接口'}" GroupName="cgpttype" Checked="CGPType_Checked"
Grid.Row="1" Grid.Column="1" IsEnabled="False"/>
Grid.Row="1" Grid.Column="1" IsEnabled="False" />
</Grid>
<Button x:Name="BtnCGPTReSet" pu:ButtonHelper.CornerRadius="4" Content="{ll:Str 初始化桌宠聊天程序}"
Margin="4" Grid.Column="2" Background="{DynamicResource SecondaryLight}" Padding="1"
Grid.Row="1" Click="ChatGPT_Reset_Click" />
</Grid>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Top" Background="{x:Null}"
TextWrapping="Wrap" Margin="0,5,0,0">
<Run FontWeight="Bold" FontSize="18" Text="{ll:Str 音乐识别设置}" /><LineBreak />
<Run Text="{ll:Str 设置识别音乐播放特殊动作阈值}" /> <LineBreak />
<Run Text="{ll:Str 当前播放音量大小}" />: <Run x:Name="RVoice" />
</TextBlock>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="15" />
<ColumnDefinition />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="35" />
<RowDefinition Height="35" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="{ll:Str 触发音量}" VerticalAlignment="Center"
ToolTip="{ll:Str 当实时播放音量达到该值时运行音乐动作}" />
<Slider x:Name="VoiceCatchSilder" TickFrequency="0.01" IsSnapToTickEnabled="True"
ToolTip="{ll:Str 当实时播放音量达到该值时运行音乐动作}" Style="{DynamicResource StandardSliderStyle}"
VerticalAlignment="Center" Maximum="1" SmallChange=".02" Minimum="0"
LargeChange=".5" Value="0.3" Grid.Column="2"
ValueChanged="VoiceCatchSilder_ValueChanged" />
<TextBlock Grid.Column="3" Margin="15,0,0,0" VerticalAlignment="Center"
Text="{Binding ElementName=VoiceCatchSilder,Path=Value,StringFormat=p0}"
FontSize="18" Foreground="{DynamicResource DARKPrimaryDarker}" FontWeight="Bold"
Background="{x:Null}" ToolTip="{ll:Str 当实时播放音量达到该值时运行特殊音乐动作}" />
<TextBlock Grid.Row="1" Text="{ll:Str 高潮音量}" VerticalAlignment="Center"
ToolTip="{ll:Str 当实时播放音量达到该值时运行特殊音乐动作}" />
<Slider x:Name="VoiceMaxSilder" TickFrequency="0.01" IsSnapToTickEnabled="True"
Style="{DynamicResource StandardSliderStyle}" VerticalAlignment="Center" Maximum="1"
SmallChange=".01" Minimum="{Binding ElementName=VoiceCatchSilder,Path=Value}"
LargeChange=".05" Value="0.75" Grid.Row="1" Grid.Column="2"
ToolTip="{ll:Str 当实时播放音量达到该值时运行特殊音乐动作}" ValueChanged="VoiceCatchSilder_ValueChanged"/>
<TextBlock Grid.Column="3" Margin="15,0,0,0" VerticalAlignment="Center"
Text="{Binding ElementName=VoiceMaxSilder,Path=Value,StringFormat=p0}" FontSize="18"
Foreground="{DynamicResource DARKPrimaryDarker}" FontWeight="Bold"
Background="{x:Null}" Grid.Row="1" ToolTip="{ll:Str 当实时播放音量达到该值时运行特殊音乐动作}" />
</Grid>
</StackPanel>
</ScrollViewer>
</TabItem>

View File

@ -8,12 +8,14 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using VPet_Simulator.Core;
namespace VPet_Simulator.Windows
@ -166,8 +168,33 @@ namespace VPet_Simulator.Windows
ListMod.SelectedIndex = 0;
ShowMod((string)((ListBoxItem)ListMod.SelectedItem).Content);
voicetimer = new DispatcherTimer()
{
Interval = TimeSpan.FromSeconds(0.2)
};
voicetimer.Tick += Voicetimer_Tick;
AllowChange = true;
}
private void Voicetimer_Tick(object sender, EventArgs e)
{
var v = mw.AudioPlayingVolume();
RVoice.Text = v.ToString("p2");
if (v > mw.Set.MusicCatch)
{
RVoice.Foreground = new SolidColorBrush(Colors.Green);
if (v > mw.Set.MusicMax)
{
RVoice.FontWeight = FontWeights.Bold;
}
else
RVoice.FontWeight = FontWeights.Normal;
}
else
RVoice.Foreground = Function.ResourcesBrush(Function.BrushType.PrimaryText);
}
public void ShowModList()
{
ListMod.Items.Clear();
@ -524,12 +551,23 @@ namespace VPet_Simulator.Windows
Process.Start("https://www.exlb.net/Diagnosis");
}
public bool Shutdown = false;
public new void Show()
{
if (MainTab.SelectedIndex == 1)
{
voicetimer.Start();
}
base.Show();
}
private void WindowX_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (Shutdown)
return;
mw.Topmost = mw.Set.TopMost;
e.Cancel = true;
voicetimer.Stop();
Hide();
}
@ -1013,7 +1051,7 @@ namespace VPet_Simulator.Windows
bool ischangename = mw.Core.Save.Name == petloader.PetName.Translate();
LocalizeCore.LoadCulture((string)LanguageBox.SelectedItem);
mw.Set.Language = LocalizeCore.CurrentCulture;
if(ischangename)
if (ischangename)
mw.Core.Save.Name = petloader.PetName.Translate();
}
@ -1021,8 +1059,12 @@ namespace VPet_Simulator.Windows
{
if (!AllowChange)
return;
voicetimer.Stop();
switch (MainTab.SelectedIndex)
{
case 1://启动音量探测
voicetimer.Start();
break;
case 4:
if (mw.HashCheck)
{
@ -1035,5 +1077,14 @@ namespace VPet_Simulator.Windows
break;
}
}
DispatcherTimer voicetimer;
private void VoiceCatchSilder_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
if (!AllowChange)
return;
mw.Set.MusicCatch = VoiceCatchSilder.Value;
mw.Set.MusicMax = VoiceMaxSilder.Value;
}
}
}

View File

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="ChatGPT.API.Framework" version="1.0.4" targetFramework="net462" />
<package id="CSCore" version="1.2.1.2" targetFramework="net462" />
<package id="Facepunch.Steamworks" version="2.3.3" targetFramework="net462" />
<package id="Facepunch.Steamworks.win32" version="2.3.3" targetFramework="net462" />
<package id="LinePutScript" version="1.9.2" targetFramework="net462" />