|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using OpenUtau.Api; |
| 7 | +using OpenUtau.Core.G2p; |
| 8 | + |
| 9 | +namespace OpenUtau.Core.DiffSinger { |
| 10 | + [Phonemizer("DiffSinger English+ Phonemizer", "DIFFS EN+", language: "EN", author: "Cadlaxa")] |
| 11 | + public class DiffSingerARPAPlusEnglishPhonemizer : DiffSingerG2pPhonemizer |
| 12 | + // cadlaxa here, this diffsinger english phonemizer just uses the ARPA+ G2p so arpasing+ and this phonemizer |
| 13 | + // have same g2p mechanics such as triggering of glottal stop with ('), manual relaxed consonants |
| 14 | + // plus other ds features |
| 15 | + { |
| 16 | + protected override string GetDictionaryName() => "dsdict-en.yaml"; |
| 17 | + protected override string GetLangCode() => "en"; |
| 18 | + protected override IG2p LoadBaseG2p() => new ArpabetPlusG2p(); |
| 19 | + protected override string[] GetBaseG2pVowels() => new string[] { |
| 20 | + "aa", "ae", "ah", "ao", "aw", "ax", "ay", "eh", "er", |
| 21 | + "ey","ih", "iy", "ow", "oy","uh", "uw" |
| 22 | + }; |
| 23 | + protected override string[] GetBaseG2pConsonants() => new string[] { |
| 24 | + "b", "ch", "d", "dh", "dr", "dx", "f", "g", "hh", "jh", |
| 25 | + "k", "l", "m", "n", "ng", "p", "q", "r", "s", "sh", "t", |
| 26 | + "th", "tr", "v", "w", "y", "z", "zh" |
| 27 | + }; |
| 28 | + public override Result Process(Note[] notes, Note? prev, Note? next, Note? prevNeighbour, Note? nextNeighbour, Note[] prevs) { |
| 29 | + |
| 30 | + if (notes[0].lyric == "-") { |
| 31 | + return MakeSimpleResult("SP"); |
| 32 | + } |
| 33 | + if (notes[0].lyric == "br") { |
| 34 | + return MakeSimpleResult("AP"); |
| 35 | + } |
| 36 | + if (!partResult.TryGetValue(notes[0].position, out var phonemes)) { |
| 37 | + throw new Exception("Result not found in the part"); |
| 38 | + } |
| 39 | + var processedPhonemes = new List<Phoneme>(); |
| 40 | + |
| 41 | + for (int i = 0; i < phonemes.Count; i++) { |
| 42 | + var tu = phonemes[i]; |
| 43 | + |
| 44 | + // Check for "n dx" sequence and replace it with "n" |
| 45 | + // the actual phoneme for this is "nx" like (winner [w ih nx er]) |
| 46 | + if (i < phonemes.Count - 1 && tu.Item1 == "n" && phonemes[i + 1].Item1 == "dx") { |
| 47 | + processedPhonemes.Add(new Phoneme() { |
| 48 | + phoneme = "n", |
| 49 | + position = tu.Item2 |
| 50 | + }); |
| 51 | + // Skip the next phoneme ("dx") |
| 52 | + i++; |
| 53 | + } else if (ShouldReplacePhoneme(tu.Item1, prev, next, prevNeighbour, nextNeighbour, out string replacement)) { |
| 54 | + processedPhonemes.Add(new Phoneme() { |
| 55 | + phoneme = replacement, |
| 56 | + position = tu.Item2 |
| 57 | + }); |
| 58 | + } else { |
| 59 | + processedPhonemes.Add(new Phoneme() { |
| 60 | + phoneme = tu.Item1, |
| 61 | + position = tu.Item2 |
| 62 | + }); |
| 63 | + } |
| 64 | + } |
| 65 | + return new Result { |
| 66 | + phonemes = processedPhonemes.ToArray() |
| 67 | + }; |
| 68 | + } |
| 69 | + |
| 70 | + // Method to determine if a phoneme should be replaced based on specific conditions |
| 71 | + private bool ShouldReplacePhoneme(string phoneme, Note? prev, Note? next, Note? prevNeighbour, Note? nextNeighbour, out string replacement) { |
| 72 | + replacement = phoneme; |
| 73 | + if (phoneme == "q") { |
| 74 | + replacement = "cl"; |
| 75 | + return true; |
| 76 | + } |
| 77 | + if (phoneme == "q") { |
| 78 | + // vocal fry the vowel is the prevNeighbour is null |
| 79 | + if (!prevNeighbour.HasValue || string.IsNullOrWhiteSpace(prevNeighbour.Value.lyric)) { |
| 80 | + replacement = "vf"; |
| 81 | + return true; |
| 82 | + } |
| 83 | + } |
| 84 | + // automatic relaxed consonants |
| 85 | + if ((phoneme == "t" || phoneme == "d") && (nextNeighbour.HasValue && IsVowel(nextNeighbour.Value))) { |
| 86 | + replacement = "dx"; |
| 87 | + return true; |
| 88 | + } |
| 89 | + return false; |
| 90 | + } |
| 91 | + // Method to check if a phoneme is a vowel |
| 92 | + private bool IsVowel(Note note) { |
| 93 | + string[] vowels = GetBaseG2pVowels(); |
| 94 | + return vowels.Contains(note.lyric); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments