Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DIFFS EN+] fixes to phoneme replacements with LangCode #1275

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using OpenUtau.Api;
using OpenUtau.Core.G2p;
using OpenUtau.Core.Ustx;
using Serilog;

namespace OpenUtau.Core.DiffSinger {
[Phonemizer("DiffSinger English+ Phonemizer", "DIFFS EN+", language: "EN", author: "Cadlaxa")]
Expand Down Expand Up @@ -37,25 +40,35 @@ public override Result Process(Note[] notes, Note? prev, Note? next, Note? prevN
throw new Exception("Result not found in the part");
}
var processedPhonemes = new List<Phoneme>();
var langCode = GetLangCode() + "/";

for (int i = 0; i < phonemes.Count; i++) {
var tu = phonemes[i];

// Check for "n dx" sequence and replace it with "n"
// the actual phoneme for this is "nx" like (winner [w ih nx er])
if (i < phonemes.Count - 1 && tu.Item1 == "n" && phonemes[i + 1].Item1 == "dx") {
if (i < phonemes.Count - 1 && tu.Item1 == langCode + "n" && HasPhoneme(langCode + "n") && phonemes[i + 1].Item1 == langCode + "dx" && HasPhoneme(langCode + "dx")) {
// If phoneme "n" and "dx" exist, process "n" and skip "dx"
processedPhonemes.Add(new Phoneme() {
phoneme = langCode + "n",
position = tu.Item2
});
i++; // Skip next phoneme
} else if (i < phonemes.Count - 1 && tu.Item1 == "n" && HasPhoneme("n") && phonemes[i + 1].Item1 == "dx" && HasPhoneme("dx") && !HasPhoneme(langCode + "n") && !HasPhoneme(langCode + "dx")) {
// If phoneme "n" and "dx" exist, but language-specific "n" and "dx" don't exist, process "n"
processedPhonemes.Add(new Phoneme() {
phoneme = "n",
position = tu.Item2
});
// Skip the next phoneme ("dx")
i++;
} else if (ShouldReplacePhoneme(tu.Item1, prev, next, prevNeighbour, nextNeighbour, out string replacement)) {
// If phoneme should be replaced, process the replacement
processedPhonemes.Add(new Phoneme() {
phoneme = replacement,
position = tu.Item2
});
} else {
// If no conditions are met, just add the current phoneme
processedPhonemes.Add(new Phoneme() {
phoneme = tu.Item1,
position = tu.Item2
Expand All @@ -67,31 +80,44 @@ public override Result Process(Note[] notes, Note? prev, Note? next, Note? prevN
};
}

// Method to determine if a phoneme should be replaced based on specific conditions
private bool ShouldReplacePhoneme(string phoneme, Note? prev, Note? next, Note? prevNeighbour, Note? nextNeighbour, out string replacement) {
replacement = phoneme;
if (phoneme == "q") {
var langCode = GetLangCode() + "/";

if ((phoneme == langCode + "cl" || !HasPhoneme("q")) && HasPhoneme("vf")) {
if (!prevNeighbour.HasValue || string.IsNullOrWhiteSpace(prevNeighbour.Value.lyric)) {
replacement = "vf";
return true;
}
}
if ((phoneme == langCode + "q" || phoneme == "q") && HasPhoneme("vf")) {
if (!prevNeighbour.HasValue || string.IsNullOrWhiteSpace(prevNeighbour.Value.lyric)) {
replacement = "vf";
return true;
}
}
if ((phoneme == langCode + "q" || phoneme == "q") && HasPhoneme("cl")) {
replacement = "cl";
return true;
}
if (phoneme == "q") {
// vocal fry the vowel is the prevNeighbour is null
if (!prevNeighbour.HasValue || string.IsNullOrWhiteSpace(prevNeighbour.Value.lyric)) {
replacement = "vf";
if (phoneme == langCode + "q" && !HasPhoneme("cl")) {
replacement = "q";
return true;
}
}
// automatic relaxed consonants
if ((phoneme == "t" || phoneme == "d") && (nextNeighbour.HasValue && IsVowel(nextNeighbour.Value))) {
replacement = "dx";
if (phoneme == langCode + "q" && !HasPhoneme("cl") && HasPhoneme(langCode + "q")) {
replacement = langCode + "q"; // Keep the language-specific "q"
return true;
}
return false;
}
// Method to check if a phoneme is a vowel
private bool IsVowel(Note note) {
string[] vowels = GetBaseG2pVowels();
return vowels.Contains(note.lyric);
if (phoneme == "ax" && !HasPhoneme("ax")) {
return true;
}
if (phoneme == langCode + "ax" && !HasPhoneme(langCode + "ax")) {
replacement = langCode + "ah"; // Replace language-specific "ax" with "ah"
return true;
}

return false;
}

}
}
18 changes: 15 additions & 3 deletions OpenUtau.Core/DiffSinger/Phonemizers/DiffSingerG2pPhonemizer.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using Serilog;
using Serilog;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

using OpenUtau.Api;
using OpenUtau.Classic;

namespace OpenUtau.Core.DiffSinger
{
Expand Down Expand Up @@ -35,7 +36,12 @@ public abstract class DiffSingerG2pPhonemizer : DiffSingerBasePhonemizer
//vowels and consonants of BaseG2p
protected virtual string[] GetBaseG2pVowels()=>new string[]{};
protected virtual string[] GetBaseG2pConsonants()=>new string[]{};


private Dictionary<string, bool> phonemeSymbols = new Dictionary<string, bool>();
protected bool HasPhoneme(string phoneme) {
return phonemeSymbols.ContainsKey(phoneme);
}

protected override IG2p LoadG2p(string rootPath, bool useLangId = false) {
//Each phonemizer has a delicated dictionary name, such as dsdict-en.yaml, dsdict-ru.yaml.
//If this dictionary exists, load it.
Expand All @@ -54,6 +60,13 @@ protected override IG2p LoadG2p(string rootPath, bool useLangId = false) {
var dictData = Yaml.DefaultDeserializer.Deserialize<DiffSingerG2pDictionaryData>(dictText);
g2pBuilder.Load(dictData);
replacements = dictData.replacementsDict();
// Collect all symbols from the dictionary and add them to phonemeSymbols
if (dictData.symbols != null) {
foreach (var symbol in dictData.symbols) {
phonemeSymbols[symbol.symbol.Trim()] = true;
}
}
Log.Error("Loaded symbols: " + string.Join(", ", phonemeSymbols.Keys));
} catch (Exception e) {
Log.Error(e, $"Failed to load {dictionaryPath}");
}
Expand All @@ -70,7 +83,6 @@ protected override IG2p LoadG2p(string rootPath, bool useLangId = false) {
if(baseG2p == null){
return new G2pFallbacks(g2ps.ToArray());
}
var phonemeSymbols = new Dictionary<string, bool>();
foreach(var v in GetBaseG2pVowels()){
phonemeSymbols[v]=true;
}
Expand Down
Loading