Skip to content

Commit

Permalink
Merge pull request #922 from maiko3tattun/1103_UseFilenameAsAlias
Browse files Browse the repository at this point in the history
Use filename as alias
  • Loading branch information
stakira authored Nov 30, 2023
2 parents f9a0441 + 35185fe commit 7cbc7fb
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 13 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
43 changes: 37 additions & 6 deletions OpenUtau.Core/Classic/VoicebankLoader.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
Expand Down Expand Up @@ -95,7 +95,7 @@ public static void LoadInfo(Voicebank voicebank, string filePath, string basePat
var dsconfigFile = Path.Combine(dir, kDsconfigYaml);
if (File.Exists(enuconfigFile)) {
voicebank.SingerType = USingerType.Enunu;
} else if(File.Exists(dsconfigFile)){
} else if (File.Exists(dsconfigFile)) {
voicebank.SingerType = USingerType.DiffSinger;
} else if (voicebank.SingerType != USingerType.Enunu) {
voicebank.SingerType = USingerType.Classic;
Expand Down Expand Up @@ -206,6 +206,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 LoadSubbanks(Voicebank voicebank) {
Expand Down Expand Up @@ -296,7 +299,7 @@ public static Dictionary<Tuple<string, string>, SortedSet<int>> ParsePrefixMap(S
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 All @@ -310,14 +313,17 @@ public static void LoadOtoSets(Voicebank voicebank, string dirPath) {
}
}

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);
if (!IsTest) {
CheckWavExist(otoSet);
}
AddAliasForMissingFiles(otoSet);
if (useFilenameAsAlias == true) {
AddFilenameAlias(otoSet);
}
return otoSet;
}
} catch (Exception e) {
Expand Down Expand Up @@ -374,12 +380,12 @@ static void AddAliasForMissingFiles(OtoSet otoSet) {

static void CheckWavExist(OtoSet otoSet) {
var wavGroups = otoSet.Otos.GroupBy(oto => oto.Wav);
foreach(var group in wavGroups) {
foreach (var group in wavGroups) {
string path = Path.Combine(Path.GetDirectoryName(otoSet.File), group.Key);
if (!File.Exists(path)) {
Log.Error($"Sound file missing. {path}");
foreach (Oto oto in group) {
if(string.IsNullOrEmpty(oto.Error)) {
if (string.IsNullOrEmpty(oto.Error)) {
oto.Error = $"Sound file missing. {path}";
}
oto.IsValid = false;
Expand All @@ -388,6 +394,31 @@ static void CheckWavExist(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
1 change: 1 addition & 0 deletions OpenUtau/Strings/Strings.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,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
1 change: 1 addition & 0 deletions OpenUtau/Strings/Strings.ja-JP.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,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 @@ -221,6 +221,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
28 changes: 22 additions & 6 deletions OpenUtau/ViewModels/SingersViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -36,6 +36,7 @@ public class SingersViewModel : ViewModelBase {
public List<MenuItemViewModel> SetEncodingMenuItems => setEncodingMenuItems;
public List<MenuItemViewModel> SetSingerTypeMenuItems => setSingerTypeMenuItems;
public List<MenuItemViewModel> SetDefaultPhonemizerMenuItems => setDefaultPhonemizerMenuItems;
[Reactive] public bool UseFilenameAsAlias { get; set; } = false;

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

Expand Down Expand Up @@ -85,6 +86,9 @@ void AttachSinger() {
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 @@ -201,6 +205,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 @@ -256,15 +272,15 @@ public void ErrorReport() {
}

public void Refresh() {
if (Singer == null) {
return;
string singerId = string.Empty;
if (Singer != null) {
singerId = Singer.Id;
}
DocManager.Inst.ExecuteCmd(new LoadingNotification(typeof(SingersDialog), true, "singer"));
try {
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 Expand Up @@ -295,7 +311,7 @@ public void OpenLocation() {
try {
if (Singer != null) {
var location = Singer.Location;
if(File.Exists(location)) {
if (File.Exists(location)) {
//Vogen voicebank is a singlefile
OS.GotoFile(location);
} else {
Expand Down
5 changes: 5 additions & 0 deletions OpenUtau/Views/SingersDialog.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,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 7cbc7fb

Please sign in to comment.