From ee6619c58930c4dde41469460aac7351c747c56f Mon Sep 17 00:00:00 2001 From: InuInu2022 <97830071+InuInu2022@users.noreply.github.com> Date: Sun, 14 Apr 2024 21:40:48 +0900 Subject: [PATCH] feat: Add an option to automatically adjust the minimum cutting-off analysis value from the score ("Auto tuning bottom estimation threshold") [Experimental] #15 --- KotoKanade.Core/Models/ScoreParser.cs | 12 ++++- KotoKanade.Core/Models/SettingManager.cs | 46 +++++++++++-------- KotoKanade.Core/Models/SongData.cs | 19 ++++++++ .../TabPages/TabScTmgPitViewModel.cs | 12 ++++- .../Views/Settings/SettingPitch.axaml | 17 ++++++- 5 files changed, 84 insertions(+), 22 deletions(-) diff --git a/KotoKanade.Core/Models/ScoreParser.cs b/KotoKanade.Core/Models/ScoreParser.cs index ee8adc2..197d813 100644 --- a/KotoKanade.Core/Models/ScoreParser.cs +++ b/KotoKanade.Core/Models/ScoreParser.cs @@ -130,10 +130,20 @@ await hasher var wp = new WorldParam(fs); var hasCachedEstimated = EstimatedCache .TryGetValue(wavHash, out var cachedEstimated); + var bottom = SettingManager.DoAutoTuneThreshold + ? songData.GetBottomPitch() + : SettingManager.BottomEstimateThrethold; + bottom = Math.Min(bottom, 70.0); + if(SettingManager.DoAutoTuneThreshold) + { + SettingManager.BottomEstimateThrethold = bottom; + } var estimated = hasCachedEstimated ? cachedEstimated : await WorldUtil - .EstimateF0Async(x, len, wp, doParallel:SettingManager.DoParallelEstimate) + .EstimateF0Async(x, len, wp, + bottomPitch:bottom, + doParallel:SettingManager.DoParallelEstimate) .ConfigureAwait(false); if (!hasCachedEstimated) { diff --git a/KotoKanade.Core/Models/SettingManager.cs b/KotoKanade.Core/Models/SettingManager.cs index 722c652..2bbd8ab 100644 --- a/KotoKanade.Core/Models/SettingManager.cs +++ b/KotoKanade.Core/Models/SettingManager.cs @@ -1,4 +1,4 @@ -using System.Collections.Concurrent; +using System.ComponentModel; using System.Runtime.CompilerServices; using Avalonia.Preferences; @@ -7,6 +7,7 @@ namespace KotoKanade.Core.Models; public static class SettingManager { private static readonly Preferences _pref = new(); + public static event EventHandler? PropertyChanged; [MethodImpl(MethodImplOptions.AggressiveInlining)] static T? Get(string name, T defaultValue) @@ -14,7 +15,10 @@ public static class SettingManager [MethodImpl(MethodImplOptions.AggressiveInlining)] static void Set(string name, T value) - => _pref.Set(name, value); + { + _pref.Set(name, value); + PropertyChanged?.Invoke(null, new PropertyChangedEventArgs(name)); + } public static async ValueTask ResetAllAsync( CancellationToken ctx = default @@ -86,22 +90,28 @@ public static decimal ConsonantOffset { public const decimal DefaultConsonantOffset = -0.05m; #region ModeC - public static bool DoParallelEstimate { - get => Get(nameof(DoParallelEstimate), true); - set => Set(nameof(DoParallelEstimate), value); - } - - public static double BottomEstimateThrethold { - get => Get(nameof(BottomEstimateThrethold), DefaultBottomEstimateThrethold); - set => Set(nameof(BottomEstimateThrethold), value); - } - public const double DefaultBottomEstimateThrethold = 50.0; - - public static bool IsForceUseDownloadedFFMpeg - { - get => Get(nameof(IsForceUseDownloadedFFMpeg), false); - set => Set(nameof(IsForceUseDownloadedFFMpeg), value); - } + public static bool DoParallelEstimate { + get => Get(nameof(DoParallelEstimate), true); + set => Set(nameof(DoParallelEstimate), value); + } + + public static double BottomEstimateThrethold { + get => Get(nameof(BottomEstimateThrethold), DefaultBottomEstimateThrethold); + set => Set(nameof(BottomEstimateThrethold), value); + } + + public const double DefaultBottomEstimateThrethold = 50.0; + + public static bool DoAutoTuneThreshold { + get => Get(nameof(DoAutoTuneThreshold), false); + set => Set(nameof(DoAutoTuneThreshold), value); + } + + public static bool IsForceUseDownloadedFFMpeg + { + get => Get(nameof(IsForceUseDownloadedFFMpeg), false); + set => Set(nameof(IsForceUseDownloadedFFMpeg), value); + } #endregion } diff --git a/KotoKanade.Core/Models/SongData.cs b/KotoKanade.Core/Models/SongData.cs index e83a463..fe9acfc 100644 --- a/KotoKanade.Core/Models/SongData.cs +++ b/KotoKanade.Core/Models/SongData.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using LibSasara; using LibSasara.Model; namespace KotoKanade.Core.Models; @@ -34,4 +35,22 @@ public sealed record SongData{ //g Husky //Volume => VOL //Alpha => ALP + + /// + /// 楽譜データの最小値を求める + /// + /// + public double GetBottomPitch( + double offset = 5.0 + ) + { + if(PhraseList is null){ + return SettingManager.DefaultBottomEstimateThrethold; + } + + var minHz = PhraseList + .SelectMany(v => v) + .Min(n => SasaraUtil.OctaveStepToFreq(n.PitchOctave, n.PitchStep)); + return minHz - offset; + } } diff --git a/KotoKanade.UI/ViewModels/TabPages/TabScTmgPitViewModel.cs b/KotoKanade.UI/ViewModels/TabPages/TabScTmgPitViewModel.cs index 5409e42..50dd5ff 100644 --- a/KotoKanade.UI/ViewModels/TabPages/TabScTmgPitViewModel.cs +++ b/KotoKanade.UI/ViewModels/TabPages/TabScTmgPitViewModel.cs @@ -15,8 +15,16 @@ public static bool DoParallelEstimate set => SettingManager.DoParallelEstimate = value; } - public double BottomEstimateThrethold { get; set; } - = SettingManager.BottomEstimateThrethold; + public static bool DoAutoTuneThreshold + { + get => SettingManager.DoAutoTuneThreshold; + set => SettingManager.DoAutoTuneThreshold = value; + } + + public double BottomEstimateThrethold { + get => SettingManager.BottomEstimateThrethold; + set => SettingManager.BottomEstimateThrethold = value; + } public static bool IsForceUseDownloadedFFMpeg { diff --git a/KotoKanade.UI/Views/Settings/SettingPitch.axaml b/KotoKanade.UI/Views/Settings/SettingPitch.axaml index cd8cb88..5a1857a 100644 --- a/KotoKanade.UI/Views/Settings/SettingPitch.axaml +++ b/KotoKanade.UI/Views/Settings/SettingPitch.axaml @@ -33,9 +33,24 @@ /> + + + + + + Description="解析の時何ヘルツ以上を解析するか" + IsEnabled="{Binding !#DoAutoTuneThresholdButton.IsChecked}" + >