Skip to content

Commit

Permalink
Use filename as alias
Browse files Browse the repository at this point in the history
  • Loading branch information
maiko3tattun committed Nov 15, 2023
1 parent 01f1728 commit 9675286
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 9 deletions.
2 changes: 2 additions & 0 deletions OpenUtau.Core/Classic/ClassicSinger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public class ClassicSinger : USinger {
Dictionary<string, UOto> otoMap = new Dictionary<string, UOto>();
OtoWatcher otoWatcher;

public bool? UseFilenameAsAlias { get => voicebank.UseFilenameAsAlias; set => voicebank.UseFilenameAsAlias = value; }

public ClassicSinger(Voicebank voicebank) {
this.voicebank = voicebank;
found = true;
Expand Down
3 changes: 2 additions & 1 deletion OpenUtau.Core/Classic/VoiceBank.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using OpenUtau.Core.Ustx;

Expand All @@ -25,6 +24,7 @@ public class Voicebank {
public List<OtoSet> OtoSets = new List<OtoSet>();
public List<Subbank> Subbanks = new List<Subbank>();
public string Id;
public bool? UseFilenameAsAlias = null;

public void Reload() {
Name = null;
Expand All @@ -44,6 +44,7 @@ public void Reload() {
OtoSets.Clear();
Subbanks.Clear();
Id = null;
UseFilenameAsAlias = null;
VoicebankLoader.LoadVoicebank(this);
}

Expand Down
1 change: 1 addition & 0 deletions OpenUtau.Core/Classic/VoicebankConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class VoicebankConfig {
public string DefaultPhonemizer;
public SymbolSet SymbolSet { get; set; }
public Subbank[] Subbanks { get; set; }
public bool? UseFilenameAsAlias = null;

public void Save(Stream stream) {
using (var writer = new StreamWriter(stream, Encoding.UTF8)) {
Expand Down
35 changes: 33 additions & 2 deletions OpenUtau.Core/Classic/VoicebankLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static void LoadVoicebank(Voicebank voicebank) {
public static void LoadOtoSets(Voicebank voicebank, string dirPath) {
var otoFile = Path.Combine(dirPath, kOtoIni);
if (File.Exists(otoFile)) {
var otoSet = ParseOtoSet(otoFile, voicebank.TextFileEncoding);
var otoSet = ParseOtoSet(otoFile, voicebank.TextFileEncoding, voicebank.UseFilenameAsAlias);
var voicebankDir = Path.GetDirectoryName(voicebank.File);
otoSet.Name = Path.GetRelativePath(voicebankDir, dirPath);
if (otoSet.Name == ".") {
Expand Down Expand Up @@ -228,6 +228,9 @@ public static void ApplyConfig(Voicebank bank, VoicebankConfig bankConfig) {
}
bank.Subbanks.AddRange(bankConfig.Subbanks);
}
if (bank.SingerType is USingerType.Classic && bankConfig.UseFilenameAsAlias != null) {
bank.UseFilenameAsAlias = bankConfig.UseFilenameAsAlias;
}
}

public static void LoadPrefixMap(Voicebank voicebank) {
Expand Down Expand Up @@ -304,11 +307,14 @@ public static Dictionary<Tuple<string, string>, SortedSet<int>> ParsePrefixMap(S
}
}

public static OtoSet ParseOtoSet(string filePath, Encoding encoding) {
public static OtoSet ParseOtoSet(string filePath, Encoding encoding , bool? useFilenameAsAlias) {
try {
using (var stream = File.OpenRead(filePath)) {
var otoSet = ParseOtoSet(stream, filePath, encoding);
AddAliasForMissingFiles(otoSet);
if (useFilenameAsAlias == true) {
AddFilenameAlias(otoSet);
}
return otoSet;
}
} catch (Exception e) {
Expand Down Expand Up @@ -362,6 +368,31 @@ static void AddAliasForMissingFiles(OtoSet otoSet) {
}
}

static void AddFilenameAlias(OtoSet otoSet) {
// Use filename as alias.
var files = otoSet.Otos.Where(oto => oto.IsValid).Select(oto => oto.Wav).Distinct().ToList();
foreach (var wav in files) {
string filename = Path.GetFileNameWithoutExtension(wav);
if (!otoSet.Otos.Any(oto => oto.Alias == filename)) {
var reference = otoSet.Otos.OrderBy(oto => oto.Offset).First(oto => oto.Wav == wav);
var oto = new Oto {
Alias = filename,
Phonetic = filename,
Wav = wav,
Offset = reference.Offset,
Consonant = reference.Consonant,
Cutoff = reference.Cutoff,
Preutter = reference.Preutter,
Overlap = reference.Overlap,
IsValid = true,
Error = reference.Error,
FileTrace = reference.FileTrace
};
otoSet.Otos.Add(oto);
}
}
}

static Oto ParseOto(string line, FileTrace trace) {
const string format = "<wav>=<alias>,<offset>,<consonant>,<cutoff>,<preutter>,<overlap>";
var oto = new Oto {
Expand Down
3 changes: 2 additions & 1 deletion OpenUtau/Strings/Strings.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -371,9 +371,9 @@ Warning: this option removes custom presets.</system:String>
<system:String x:Key="singers.otoview.showall">Show All</system:String>
<system:String x:Key="singers.otoview.zoomin">Zoom In</system:String>
<system:String x:Key="singers.otoview.zoomout">Zoom Out</system:String>
<system:String x:Key="singers.playsample">Play sample</system:String>
<system:String x:Key="singers.readme">Open readme.txt</system:String>
<system:String x:Key="singers.readme.notfound">readme.txt not found.</system:String>
<system:String x:Key="singers.playsample">Play sample</system:String>
<system:String x:Key="singers.refresh">Refresh</system:String>
<system:String x:Key="singers.setdefaultphonemizer">Set Default Phonemizer</system:String>
<system:String x:Key="singers.setencoding">Set Encoding</system:String>
Expand All @@ -394,6 +394,7 @@ Warning: this option removes custom presets.</system:String>
<system:String x:Key="singers.subbanks.set">Set</system:String>
<system:String x:Key="singers.subbanks.tone">Tone</system:String>
<system:String x:Key="singers.subbanks.toneranges">Tone Ranges</system:String>
<system:String x:Key="singers.usefilename">Use filename as alias</system:String>
<system:String x:Key="singers.visitwebsite">Visit Website</system:String>

<system:String x:Key="singersetup.archivefileencoding">Archive File Encoding</system:String>
Expand Down
3 changes: 2 additions & 1 deletion OpenUtau/Strings/Strings.ja-JP.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,9 @@
<system:String x:Key="singers.otoview.showall">全て表示</system:String>
<system:String x:Key="singers.otoview.zoomin">ズームイン</system:String>
<system:String x:Key="singers.otoview.zoomout">ズームアウト</system:String>
<system:String x:Key="singers.playsample">サンプルボイスを再生</system:String>
<system:String x:Key="singers.readme">readme.txtを開く</system:String>
<system:String x:Key="singers.readme.notfound">readme.txtが見つかりません</system:String>
<system:String x:Key="singers.playsample">サンプルボイスを再生</system:String>
<system:String x:Key="singers.refresh">再読み込み</system:String>
<system:String x:Key="singers.setdefaultphonemizer">デフォルトのPhonemizerを設定</system:String>
<system:String x:Key="singers.setencoding">文字コードを設定</system:String>
Expand All @@ -393,6 +393,7 @@
<system:String x:Key="singers.subbanks.set">セット</system:String>
<system:String x:Key="singers.subbanks.tone">音程</system:String>
<system:String x:Key="singers.subbanks.toneranges">音域</system:String>
<system:String x:Key="singers.usefilename">ファイル名をエイリアスとして使用する</system:String>
<system:String x:Key="singers.visitwebsite">ウェブサイトを開く</system:String>

<system:String x:Key="singersetup.archivefileencoding">アーカイブファイルのエンコード</system:String>
Expand Down
16 changes: 16 additions & 0 deletions OpenUtau/Styles/Styles.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,22 @@
<Setter Property="VerticalAlignment" Value="Top" />
</Style>

<Style Selector="CheckBox.menu">
<Setter Property="IsHitTestVisible" Value="False" />
<Style Selector="^:unchecked /template/ Border#NormalRectangle">
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Background" Value="Transparent"/>
</Style>
<Style Selector="^:checked /template/ Path#CheckGlyph">
<Setter Property="Fill" Value="{DynamicResource AccentBrush1}" />
<Setter Property="Width" Value="12" />
</Style>
<Style Selector="^:checked /template/ Border#NormalRectangle">
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Background" Value="Transparent"/>
</Style>
</Style>

<Style Selector="DataGridColumnHeader">
<Setter Property="FontSize" Value="12" />
<Setter Property="MinHeight" Value="20" />
Expand Down
24 changes: 20 additions & 4 deletions OpenUtau/ViewModels/SingersViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class SingersViewModel : ViewModelBase {
[Reactive] public int SelectedIndex { get; set; }
public List<MenuItemViewModel> SetEncodingMenuItems => setEncodingMenuItems;
public List<MenuItemViewModel> SetDefaultPhonemizerMenuItems => setDefaultPhonemizerMenuItems;
[Reactive] public bool UseFilenameAsAlias { get; set; } = false;

[Reactive] public string SearchAlias { get; set; } = "";

Expand Down Expand Up @@ -64,6 +65,9 @@ public SingersViewModel() {
DisplayedOtos.AddRange(singer.Otos);
Info = $"Author: {singer.Author}\nVoice: {singer.Voice}\nWeb: {singer.Web}\nVersion: {singer.Version}\n{singer.OtherInfo}\n\n{string.Join("\n", singer.Errors)}";
HasWebsite = !string.IsNullOrEmpty(singer.Web);
if (Singer is ClassicSinger cSinger) {
UseFilenameAsAlias = cSinger.UseFilenameAsAlias ?? false;
}
LoadSubbanks();
DocManager.Inst.ExecuteCmd(new OtoChangedNotification());
this.RaisePropertyChanged(nameof(IsClassic));
Expand Down Expand Up @@ -152,6 +156,18 @@ private void SetDefaultPhonemizer(Api.PhonemizerFactory factory) {
Refresh();
}

public void SetUseFilenameAsAlias() {
if (Singer == null || !IsClassic) {
return;
}
try {
ModifyConfig(Singer, config => config.UseFilenameAsAlias = !this.UseFilenameAsAlias);
} catch (Exception e) {
DocManager.Inst.ExecuteCmd(new ErrorMessageNotification("Failed to set use filename", e));
}
Refresh();
}

private static void ModifyConfig(USinger singer, Action<VoicebankConfig> modify) {
var yamlFile = Path.Combine(singer.Location, "character.yaml");
VoicebankConfig? config = null;
Expand Down Expand Up @@ -192,13 +208,13 @@ public void ErrorReport() {
}

public void Refresh() {
if (Singer == null) {
return;
string singerId = string.Empty;
if (Singer != null) {
singerId = Singer.Id;
}
var singerId = Singer.Id;
SingerManager.Inst.SearchAllSingers();
this.RaisePropertyChanged(nameof(Singers));
if (SingerManager.Inst.Singers.TryGetValue(singerId, out var singer)) {
if (!string.IsNullOrEmpty(singerId) && SingerManager.Inst.Singers.TryGetValue(singerId, out var singer)) {
Singer = singer;
} else {
Singer = Singers.FirstOrDefault();
Expand Down
5 changes: 5 additions & 0 deletions OpenUtau/Views/SingersDialog.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@
</DataTemplate>
</MenuItem.DataTemplates>
</MenuItem>
<MenuItem Header="{DynamicResource singers.usefilename}" IsVisible="{Binding IsClassic}" Click="OnSetUseFilenameAsAlias">
<MenuItem.Icon>
<CheckBox Classes="menu" IsChecked="{Binding UseFilenameAsAlias}" />
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="{DynamicResource singers.errorreport}" Command="{Binding ErrorReport}"/>
<MenuItem Header="{DynamicResource singers.refresh}" Command="{Binding Refresh}"/>
</ContextMenu>
Expand Down
5 changes: 5 additions & 0 deletions OpenUtau/Views/SingersDialog.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ async void OnSetPortrait(object sender, RoutedEventArgs args) {
}
}

void OnSetUseFilenameAsAlias(object sender, RoutedEventArgs args) {
var viewModel = (DataContext as SingersViewModel)!;
viewModel.SetUseFilenameAsAlias();
}

async void OnEditSubbanksButton(object sender, RoutedEventArgs args) {
var viewModel = (DataContext as SingersViewModel)!;
if (viewModel.Singer == null) {
Expand Down

0 comments on commit 9675286

Please sign in to comment.