Skip to content

Commit

Permalink
feat: Add an option to automatically adjust the minimum cutting-off a…
Browse files Browse the repository at this point in the history
…nalysis value from the score ("Auto tuning bottom estimation threshold") [Experimental] #15
  • Loading branch information
InuInu2022 committed Apr 14, 2024
1 parent 9c1a5b6 commit ee6619c
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 22 deletions.
12 changes: 11 additions & 1 deletion KotoKanade.Core/Models/ScoreParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
46 changes: 28 additions & 18 deletions KotoKanade.Core/Models/SettingManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Concurrent;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Avalonia.Preferences;

Expand All @@ -7,14 +7,18 @@ namespace KotoKanade.Core.Models;
public static class SettingManager
{
private static readonly Preferences _pref = new();
public static event EventHandler<PropertyChangedEventArgs>? PropertyChanged;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
static T? Get<T>(string name, T defaultValue)
=> _pref.Get(name, defaultValue);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
static void Set<T>(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
Expand Down Expand Up @@ -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

}
19 changes: 19 additions & 0 deletions KotoKanade.Core/Models/SongData.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using LibSasara;
using LibSasara.Model;

namespace KotoKanade.Core.Models;
Expand Down Expand Up @@ -34,4 +35,22 @@ public sealed record SongData{
//g Husky
//Volume => VOL
//Alpha => ALP

/// <summary>
/// 楽譜データの最小値を求める
/// </summary>
/// <returns></returns>
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;
}
}
12 changes: 10 additions & 2 deletions KotoKanade.UI/ViewModels/TabPages/TabScTmgPitViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
17 changes: 16 additions & 1 deletion KotoKanade.UI/Views/Settings/SettingPitch.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,24 @@
/>
</ui:SettingsExpanderItem.Footer>
</ui:SettingsExpanderItem>
<ui:SettingsExpanderItem
Content="Auto tuning bottom estimation threthold"
Description="楽譜から自動で解析値の最小を計算するか">
<ui:SettingsExpanderItem.Footer>
<ToggleSwitch
Name="DoAutoTuneThresholdButton"
IsChecked="{Binding $parent[UserControl;1].((viewmodels:TabScTmgPitViewModel)DataContext).DoAutoTuneThreshold}"
OnContent="Auto"
OffContent="Manual"
HorizontalAlignment="Right"
/>
</ui:SettingsExpanderItem.Footer>
</ui:SettingsExpanderItem>
<ui:SettingsExpanderItem
Content="Bottom estimation threthold Hz."
Description="解析の時何ヘルツ以上を解析するか">
Description="解析の時何ヘルツ以上を解析するか"
IsEnabled="{Binding !#DoAutoTuneThresholdButton.IsChecked}"
>
<ui:SettingsExpanderItem.Footer>
<StackPanel
VerticalAlignment="Center"
Expand Down

0 comments on commit ee6619c

Please sign in to comment.