-
Notifications
You must be signed in to change notification settings - Fork 296
/
USinger.cs
331 lines (295 loc) · 11.4 KB
/
USinger.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Text;
using OpenUtau.Classic;
using OpenUtau.Core.Util;
namespace OpenUtau.Core.Ustx {
public class UOto : INotifyPropertyChanged {
public string Alias { get; private set; }
public string Phonetic { get; private set; }
public string Set { get; private set; }
public string Color { get; private set; }
public string Prefix { get; private set; }
public string Suffix { get; private set; }
public SortedSet<int> ToneSet { get; private set; }
public string File { get; private set; }
public string DisplayFile { get; private set; }
public double Offset {
get => offset;
set {
offset = Math.Max(0, Math.Round(value, 3));
NotifyPropertyChanged(nameof(Offset));
}
}
public double Consonant {
get => consonant;
set {
consonant = Math.Max(0, Math.Round(value, 3));
NotifyPropertyChanged(nameof(Consonant));
}
}
public double Cutoff {
get => cutoff;
set {
cutoff = Math.Round(value, 3);
NotifyPropertyChanged(nameof(Cutoff));
}
}
public double Preutter {
get => preutter;
set {
preutter = Math.Max(0, Math.Round(value, 3));
NotifyPropertyChanged(nameof(Preutter));
}
}
public double Overlap {
get => overlap;
set {
overlap = Math.Round(value, 3);
NotifyPropertyChanged(nameof(Overlap));
}
}
public List<string> SearchTerms { get; private set; }
public event PropertyChangedEventHandler PropertyChanged;
private Oto oto;
private double offset;
private double consonant;
private double cutoff;
private double preutter;
private double overlap;
public UOto() { }
public UOto(Oto oto, UOtoSet set, USubbank subbank) {
this.oto = oto;
Alias = oto.Alias;
Phonetic = oto.Phonetic;
Set = set.Name;
Color = subbank?.Color;
Prefix = subbank?.Prefix;
Suffix = subbank?.Suffix;
ToneSet = subbank?.toneSet;
if (!string.IsNullOrEmpty(oto.Wav)) {
File = Path.Combine(set.Location, oto.Wav);
} else {
File = string.Empty;
}
DisplayFile = oto?.Wav;
Offset = oto.Offset;
Consonant = oto.Consonant;
Cutoff = oto.Cutoff;
Preutter = oto.Preutter;
Overlap = oto.Overlap;
SearchTerms = new List<string>();
}
public static UOto OfDummy(string alias) => new UOto() {
Alias = alias,
Phonetic = alias,
};
public void WriteBack() {
oto.Offset = offset;
oto.Consonant = consonant;
oto.Cutoff = cutoff;
oto.Preutter = preutter;
oto.Overlap = overlap;
}
private void NotifyPropertyChanged(string propertyName = "") {
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public override string ToString() => Alias;
}
public class UOtoSet {
public string Name => otoSet.Name;
public readonly string Location;
private readonly OtoSet otoSet;
public UOtoSet(OtoSet otoSet, string singersPath) {
this.otoSet = otoSet;
if (!string.IsNullOrEmpty(otoSet.File)) {
Location = Path.Combine(singersPath, Path.GetDirectoryName(otoSet.File));
} else {
Location = string.Empty;
}
}
public override string ToString() => Name;
}
public class USubbank {
public string Color {
get => subbank.Color;
set => subbank.Color = value;
}
public string Prefix {
get => subbank.Prefix;
set => subbank.Prefix = value;
}
public string Suffix {
get => subbank.Suffix;
set => subbank.Suffix = value;
}
public string ToneRangesString {
get => toneRangesString;
set {
subbank.ToneRanges = value.Split(',', StringSplitOptions.RemoveEmptyEntries);
toneRangesString = value;
}
}
public readonly SortedSet<int> toneSet;
public readonly Subbank subbank;
private string toneRangesString;
public USubbank(Subbank subbank) {
this.subbank = subbank;
toneSet = new SortedSet<int>();
if (subbank.ToneRanges != null) {
toneRangesString = string.Join(',', subbank.ToneRanges);
foreach (var range in subbank.ToneRanges) {
AddToneRange(range, toneSet);
}
} else {
toneRangesString = string.Empty;
}
}
private static void AddToneRange(string range, SortedSet<int> set) {
var parts = range.Split('-');
if (parts.Length == 1) {
int tone = MusicMath.NameToTone(parts[0]);
if (tone > 0) {
set.Add(tone);
}
} else if (parts.Length == 2) {
int start = MusicMath.NameToTone(parts[0]);
int end = MusicMath.NameToTone(parts[1]);
if (start > 0 && end > 0) {
for (int i = start; i <= end; ++i) {
set.Add(i);
}
}
}
}
}
[Flags] public enum USingerType { Classic = 0x1, Enunu = 0x2, Vogen = 0x4, DiffSinger=0x5 }
public static class SingerTypeUtils{
public static Dictionary<USingerType?, string> SingerTypeNames = new Dictionary<USingerType?, string>(){
{USingerType.Classic, "utau"},
{USingerType.Enunu, "enunu"},
{USingerType.DiffSinger, "diffsinger"},
};
public static Dictionary<string, USingerType> SingerTypeFromName = new Dictionary<string, USingerType>(){
{"utau", USingerType.Classic},
{"enunu", USingerType.Enunu},
{"diffsinger", USingerType.DiffSinger},
};
}
public class USinger : INotifyPropertyChanged, IEquatable<USinger> {
protected static readonly List<UOto> emptyOtos = new List<UOto>();
public virtual string Id { get; }
public virtual string Name => name;
public virtual Dictionary<string, string> LocalizedNames { get; }
public virtual USingerType SingerType { get; }
public virtual string BasePath { get; }
public virtual string Author { get; }
public virtual string Voice { get; }
public virtual string Location { get; }
public virtual string Web { get; }
public virtual string Version { get; }
public virtual string OtherInfo { get; }
public virtual IList<string> Errors { get; }
public virtual string Avatar { get; }
public virtual byte[] AvatarData { get; }
public virtual string Portrait { get; }
public virtual float PortraitOpacity { get; }
public virtual int PortraitHeight { get; }
public virtual string Sample { get; }
public virtual string DefaultPhonemizer { get; }
public virtual Encoding TextFileEncoding => Encoding.UTF8;
public virtual IList<USubbank> Subbanks { get; }
public virtual IList<UOto> Otos => emptyOtos;
public bool Found => found;
public bool Loaded => found && loaded;
public bool OtoDirty {
get => otoDirty;
set {
otoDirty = value;
NotifyPropertyChanged(nameof(OtoDirty));
}
}
public bool IsFavourite {
get => Preferences.Default.FavoriteSingers.Contains(Id);
set {
if (value) {
if (!Preferences.Default.FavoriteSingers.Contains(Id)) {
Preferences.Default.FavoriteSingers.Add(Id);
}
} else {
Preferences.Default.FavoriteSingers.Remove(Id);
}
Preferences.Save();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected bool found;
protected bool loaded;
protected bool otoDirty;
private string name;
public string DisplayName { get { return Found ? name : $"[Missing] {name}"; } }
public string LocalizedName {
get {
if(LocalizedNames == null) {
return Name;
}
string language = Preferences.Default.SortingOrder;
if(String.IsNullOrEmpty(language)){
language = Preferences.Default.Language;
}
if(LocalizedNames.TryGetValue(language, out var localizedName)){
return localizedName;
} else {
return Name;
}
}
}
public virtual void EnsureLoaded() { }
public virtual void Reload() { }
public virtual void Save() { }
public virtual bool TryGetOto(string phoneme, out UOto oto) {
oto = default;
return false;
}
public virtual bool TryGetMappedOto(string phoneme, int tone, out UOto oto) {
return TryGetOto(phoneme, out oto);
}
public virtual bool TryGetMappedOto(string phoneme, int tone, string color, out UOto oto) {
return TryGetOto(phoneme, out oto);
}
public virtual IEnumerable<UOto> GetSuggestions(string text) { return emptyOtos; }
public virtual byte[] LoadPortrait() => null;
public virtual byte[] LoadSample() => null;
public override string ToString() => Name;
public bool Equals(USinger other) {
// Tentative: Since only the singer's Id is recorded in ustx and preferences, singers with the same Id are considered identical.
// Singer with the same directory name in different locations may be identical.
if (other != null && other.Id == this.Id) {
return true;
} else {
return false;
}
}
public override int GetHashCode() => Id.GetHashCode();
public static USinger CreateMissing(string name) {
return new USinger() {
found = false,
loaded = false,
name = name,
};
}
private void NotifyPropertyChanged(string propertyName = "") {
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
/// <summary>
/// Some types of singers store their data in memory when rendering.
/// This method is called when the singer is no longer used.
/// Note:
/// - the voicebank may be used again even after this method is called.
/// - this method may be called even when the singer has not been used
/// </summary>
public virtual void FreeMemory(){ }
}
}