Skip to content

Commit

Permalink
Merge pull request #953 from oxygen-dioxide/diffs-zh
Browse files Browse the repository at this point in the history
Fix diffsinger phoneme misplace if lyric contains space; separate diffsinger Chinese phonemizer
  • Loading branch information
stakira authored Nov 30, 2023
2 parents 5fc0fec + 9efadbe commit b630642
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 9 deletions.
20 changes: 18 additions & 2 deletions OpenUtau.Core/BaseChinesePhonemizer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using OpenUtau.Core.G2p;
using OpenUtau.Api;
Expand All @@ -19,7 +20,22 @@ public static Note[] ChangeLyric(Note[] group, string lyric) {
}

public static string[] Romanize(IEnumerable<string> lyrics) {
return ZhG2p.MandarinInstance.Convert(lyrics.ToList(), false, true).Split(" ");
var lyricsArray = lyrics.ToArray();
var hanziLyrics = lyricsArray
.Where(ZhG2p.MandarinInstance.IsHanzi)
.ToList();
var pinyinResult = ZhG2p.MandarinInstance.Convert(hanziLyrics, false, false).ToLower().Split();
if(pinyinResult == null) {
return lyricsArray;
}
var pinyinIndex = 0;
for(int i=0; i < lyricsArray.Length; i++) {
if (lyricsArray[i].Length == 1 && ZhG2p.MandarinInstance.IsHanzi(lyricsArray[i])) {
lyricsArray[i] = pinyinResult[pinyinIndex];
pinyinIndex++;
}
}
return lyricsArray;
}

public static void RomanizeNotes(Note[][] groups) {
Expand Down
14 changes: 14 additions & 0 deletions OpenUtau.Core/DiffSinger/DiffSingerChinesePhonemizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Collections.Generic;

using OpenUtau.Api;

namespace OpenUtau.Core.DiffSinger
{
[Phonemizer("DiffSinger Chinese Phonemizer", "DIFFS ZH", language: "ZH")]
public class DiffSingerChinesePhonemizer : DiffSingerBasePhonemizer
{
protected override string[] Romanize(IEnumerable<string> lyrics) {
return BaseChinesePhonemizer.Romanize(lyrics);
}
}
}
4 changes: 2 additions & 2 deletions OpenUtau.Core/DiffSinger/DiffSingerG2pPhonemizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public Dictionary<string, string> toDict(){
}
}

public abstract class DiffSingerG2pPhonemizer : DiffSingerPhonemizer
public abstract class DiffSingerG2pPhonemizer : DiffSingerBasePhonemizer
{
protected virtual string GetDictionaryName()=>"dsdict.yaml";

Expand Down Expand Up @@ -89,4 +89,4 @@ protected override IG2p LoadG2p(string rootPath) {
return new G2pFallbacks(g2ps.ToArray());
}
}
}
}
5 changes: 0 additions & 5 deletions OpenUtau.Core/DiffSinger/DiffSingerPhonemizer.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
using System.Collections.Generic;

using OpenUtau.Api;

namespace OpenUtau.Core.DiffSinger
{
[Phonemizer("DiffSinger Phonemizer", "DIFFS")]
public class DiffSingerPhonemizer : DiffSingerBasePhonemizer
{
protected override string[] Romanize(IEnumerable<string> lyrics) {
return BaseChinesePhonemizer.Romanize(lyrics);
}
}
}
15 changes: 15 additions & 0 deletions OpenUtau.Core/G2p/ZhG2p.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,21 @@ private void ZhPosition(List<string> input, List<string> res, List<int> position
}
}

public bool IsHanzi(string input){
///<summary>
/// Whether the input string is a single hanzi.
///</summary>
return WordDict.ContainsKey(input) || TransDict.ContainsKey(input);
}

public bool IsHanziOrNum(string input){
return IsHanzi(input) || NumMap.ContainsKey(input);
}

public bool IsHanzi(string input, bool convertNum){
return IsHanzi(input) || (convertNum && NumMap.ContainsKey(input));
}

public string Convert(string input, bool tone, bool covertNum) {
return Convert(SplitString(input), tone, covertNum);
}
Expand Down

0 comments on commit b630642

Please sign in to comment.