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

Phonemizers #77

Closed
wants to merge 4 commits into from
Closed
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
61 changes: 61 additions & 0 deletions EnunuOnnxPhonemizer/EnunuConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System.Collections.Generic;
using System.IO;
using System.Text;
using OpenUtau.Core;

//Instead of using Enunu/EnunuConfig.cs, we created a copy to add EnunuOnnx-specific features
//without potentially breaking the existing Enunu Phonemizers and renderers.
namespace EnunuOnnxPhonemizer.EnunuOnnx {
class EnunuConfig {
public string tablePath;
public string questionPath;
public int sampleRate;
public double framePeriod;
public string modelDir;
public string statsDir;
public EnunuDurationConfig duration;
public EnunuTimelagConfig timelag;

public static EnunuConfig Load(string configPath, Encoding encoding = null) {
encoding = encoding ?? Encoding.UTF8;
var configTxt = File.ReadAllText(configPath,encoding);
RawEnunuConfig config = Yaml.DefaultDeserializer.Deserialize<RawEnunuConfig>(configTxt);
return config.Convert();
}
}


class EnunuTimelagConfig {
public string checkpoint;
public List<int> allowedRange;
public List<int> allowedRangeRest;
}
class EnunuDurationConfig {
public string checkpoint;

}

class RawEnunuConfig {
public string tablePath;
public string questionPath;
public int sampleRate;
public double framePeriod;
public string modelDir;
public string statsDir;
public EnunuDurationConfig duration;
public EnunuTimelagConfig timelag;

public EnunuConfig Convert() {
EnunuConfig enunuConfig = new EnunuConfig();
enunuConfig.tablePath = this.tablePath;
enunuConfig.questionPath = this.questionPath;
enunuConfig.sampleRate = this.sampleRate;
enunuConfig.framePeriod = this.framePeriod;
enunuConfig.duration = this.duration;
enunuConfig.modelDir = this.modelDir;
enunuConfig.statsDir = this.statsDir;
enunuConfig.timelag = this.timelag;
return enunuConfig;
}
}
}
63 changes: 63 additions & 0 deletions EnunuOnnxPhonemizer/EnunuOnnxConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using OpenUtau.Core;

//data class used to deserialize enunux.yaml, ENUNU X specific settings

namespace EnunuOnnxPhonemizer.EnunuOnnx {
public struct RedirectionData {
public string[] from;
public string to;
}

class EnunuOnnxConfig {

public RedirectionData[] redirections;

public static EnunuOnnxConfig Load(string configPath, Encoding encoding = null) {
encoding = encoding ?? Encoding.UTF8;
var configTxt = File.ReadAllText(configPath, encoding);
EnunuOnnxConfig config = Yaml.DefaultDeserializer.Deserialize<EnunuOnnxConfig>(configTxt);
if(config.redirections == null) {
config.redirections = new RedirectionData[] { };
}
return config;
}
}

class RedirectionDict {
//reference: https://stackoverflow.com/questions/1321331/replace-multiple-string-elements-in-c-sharp
//if no redirection, regex is null
Regex? regex = null;
Dictionary<string, string> replacements = new Dictionary<string, string>();

public RedirectionDict(RedirectionData[] datas) {
if (datas == null || datas.Length == 0) {
return;
}
//sort redirection keys from long to short
Array.Sort(datas, (x1,x2)=>- x1.from.Length.CompareTo(x2.from.Length));
StringBuilder regexBuilder = new StringBuilder("(");
foreach(var line in datas) {
string key = string.Join("\n", line.from);
replacements[key] = line.to + new string('\n',line.from.Length - 1);
regexBuilder.Append(Regex.Escape(key)+"|");
}
regexBuilder[^1] = ')';
regex = new Regex(regexBuilder.ToString());
}

public string[] process(IEnumerable<string> symbols) {
if (regex == null) {
return symbols.ToArray();
}
string input = string.Join("\n", symbols);
string output = regex.Replace(input, delegate (Match m) { return replacements[m.Value]; });
return output.Split("\n");
}
}
}
29 changes: 29 additions & 0 deletions EnunuOnnxPhonemizer/EnunuOnnxEnglishPhonemizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using OpenUtau.Api;
using OpenUtau.Core.G2p;
using Serilog;

namespace EnunuOnnxPhonemizer {
[Phonemizer("Enunu Onnx English Phonemizer", "ENUNU X EN", language:"EN")]
public class EnunuOnnxEnglishPhonemizer : EnunuOnnxPhonemizer {
protected override IG2p LoadG2p(string rootPath) {
var g2ps = new List<IG2p>();

// Load dictionary from singer folder.
string file = Path.Combine(rootPath, "enunux.yaml");
if (File.Exists(file)) {
try {
g2ps.Add(G2pDictionary.NewBuilder().Load(File.ReadAllText(file)).Build());
} catch (Exception e) {
Log.Error(e, $"Failed to load {file}");
}
}
g2ps.Add(new ArpabetG2p());
return new G2pFallbacks(g2ps.ToArray());
}

}
}
Loading