Skip to content

Commit

Permalink
Merge pull request #3 from rokujyushi/AddVoicevox
Browse files Browse the repository at this point in the history
Add voicevox
  • Loading branch information
rokujyushi authored Apr 4, 2024
2 parents 871d548 + 003c8e0 commit aef6570
Show file tree
Hide file tree
Showing 8 changed files with 612 additions and 131 deletions.
53 changes: 53 additions & 0 deletions OpenUtau.Core/Voicevox/Phonemizers/SimpleVoicevoxPhonemizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System.Collections.Generic;
using System.Linq;
using OpenUtau.Api;
using OpenUtau.Core.Ustx;
using OpenUtau.Core.Voicevox;

namespace Voicevox {
[Phonemizer("Simple Voicevox Japanese Phonemizer", "S-VOICEVOX JA", language: "JA")]
public class SimpleVoicevoxPhonemizer : Phonemizer {

protected VoicevoxSinger singer;

public override void SetSinger(USinger singer) {
this.singer = singer as VoicevoxSinger;
if (this.singer != null) {
this.singer.voicevoxConfig.Tag = this.Tag;
}
}

public override Result Process(Note[] notes, Note? prev, Note? next, Note? prevNeighbour, Note? nextNeighbour, Note[] prevNeighbours) {
Phoneme[] phonemes = new Phoneme[notes.Length];
for (int i = 0; i < notes.Length; i++) {
var currentLyric = notes[i].lyric.Normalize(); //measures for Unicode
if (currentLyric.StartsWith("+")) {
continue;
}
int toneShift = 0;
int? alt = null;
if (notes[i].phonemeAttributes != null) {
var attr = notes[i].phonemeAttributes.FirstOrDefault(attr => attr.index == 0);
toneShift = attr.toneShift;
alt = attr.alternate;
}

Note[][] simplenotes = new Note[1][];
var lyricList = notes[i].lyric.Split(" ");
if (lyricList.Length > 1) {
notes[i].lyric = lyricList[1];
}
if (VoicevoxUtils.IsHiraKana(notes[i].lyric)) {
phonemes[i] = new Phoneme { phoneme = notes[i].lyric };
} else if (VoicevoxUtils.IsPau(notes[i].lyric)) {
phonemes[i] = new Phoneme { phoneme = notes[i].lyric };
} else {
phonemes[i] = new Phoneme {
phoneme = "error",
};
}
}
return new Result { phonemes = phonemes };
}
}
}
103 changes: 103 additions & 0 deletions OpenUtau.Core/Voicevox/Phonemizers/VoicevoxPhonemizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using OpenUtau.Api;
using OpenUtau.Core.Ustx;

namespace OpenUtau.Core.Voicevox {
[Phonemizer("Voicevox Japanese Phonemizer", "VOICEVOX JA", language: "JA")]
public class VoicevoxPhonemizer : Phonemizer {

protected VoicevoxSinger singer;
Dictionary<Note[], Phoneme[]> partResult = new Dictionary<Note[], Phoneme[]>();

public override void SetSinger(USinger singer) {
this.singer = singer as VoicevoxSinger;
if (this.singer != null) {
this.singer.voicevoxConfig.Tag = this.Tag;
}
}

public override void SetUp(Note[][] notes, UProject project, UTrack track) {
partResult.Clear();
foreach(var lyric in notes) {
lyric[0].lyric = lyric[0].lyric.Normalize();
var lyricList = lyric[0].lyric.Split(" ");
if (lyricList.Length > 1) {
lyric[0].lyric = lyricList[1];
}
}
var qNotes = VoicevoxUtils.NoteGroupsToVoicevox(notes, timeAxis,this.singer);
var vvNotes = new VoicevoxNote();
string singerID = VoicevoxUtils.defaultID;
if (this.singer.voicevoxConfig.base_singer_style != null) {
foreach (var s in this.singer.voicevoxConfig.base_singer_style) {
if (s.name.Equals(this.singer.voicevoxConfig.base_singer_name)) {
vvNotes = VoicevoxUtils.VoicevoxVoiceBase(qNotes, s.styles.id.ToString());
if (s.styles.name.Equals(this.singer.voicevoxConfig.base_singer_style_name)) {
break;
}
} else {
vvNotes = VoicevoxUtils.VoicevoxVoiceBase(qNotes, singerID);
break;
}
}
} else {
vvNotes = VoicevoxUtils.VoicevoxVoiceBase(qNotes, singerID);
}

var parentDirectory = Directory.GetParent(singer.Location).ToString();
var yamlPath = Path.Join(parentDirectory, "phonemes.yaml");
var yamlTxt = File.ReadAllText(yamlPath);
var phonemes_list = Yaml.DefaultDeserializer.Deserialize<Phoneme_list>(yamlTxt);

var list = new List<Phonemes>(vvNotes.phonemes);
foreach (var note in qNotes.notes) {
if (note.vqnindex < 0) {
list.Remove(list[0]);
continue;
}
var noteGroup = notes[note.vqnindex];
var phoneme = new List<Phoneme>();
int index = 0;
while (list.Count > 0) {
if (phonemes_list.vowels.Contains(list[0].phoneme)) {
phoneme.Add(new Phoneme() { phoneme = list[0].phoneme, position = noteGroup[0].position });
index++;
list.Remove(list[0]);
break;
}else if (phonemes_list.consonants.Contains(list[0].phoneme)) {
phoneme.Add(new Phoneme() { phoneme = list[0].phoneme, position = noteGroup[0].position - (int)timeAxis.MsPosToTickPos((list[0].frame_length / VoicevoxUtils.fps) * 1000) });
}
list.Remove(list[0]);
}
partResult[noteGroup] = phoneme.ToArray();
}
}

public override Result Process(Note[] notes, Note? prev, Note? next, Note? prevNeighbour, Note? nextNeighbour, Note[] prevs) {
var ps = new List<Phoneme>();
if (partResult.TryGetValue(notes, out var phonemes)) {
return new Result {
phonemes = phonemes.Select(p => {
p.position = p.position - notes[0].position;
return p;
}).ToArray(),
};
}
return new Result {
phonemes = new Phoneme[] {
new Phoneme {
phoneme = "error",
}
},
};

}

public override void CleanUp() {
partResult.Clear();
}
}
}
68 changes: 0 additions & 68 deletions OpenUtau.Core/Voicevox/SimpleVoicevoxPhonemizer.cs

This file was deleted.

9 changes: 6 additions & 3 deletions OpenUtau.Core/Voicevox/VoicevoxConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ public class VoicevoxConfig {
public string version = string.Empty;
public string policy = string.Empty;
public string portraitPath = string.Empty;
//So that the renderer can distinguish between phonemizers.
public string Tag = "DEFAULT";

public List<Style_infos> style_infos;
//Prepare for future additions of Teacher Singer.
public List<(string name, Styles styles)> base_singer_style;
public string base_singer_name = string.Empty;
public string base_singer_style_name = string.Empty;

//So that the renderer can distinguish between phonemizers.
public string Tag = "DEFAULT";
public Phoneme_list phonemes_list;

public static VoicevoxConfig Load(USinger singer) {
try {
var response = VoicevoxClient.Inst.SendRequest(new VoicevoxURL() { method = "GET", path = "/singers" });
Expand Down Expand Up @@ -66,6 +68,7 @@ public static VoicevoxConfig Load(USinger singer) {
} catch {
Log.Error("Could not load VOICEVOX singer.");
}

return new VoicevoxConfig();
}
public void LoadInfo(VoicevoxConfig voicevoxConfig, string location) {
Expand All @@ -89,7 +92,7 @@ public void LoadInfo(VoicevoxConfig voicevoxConfig, string location) {
public class Phoneme_list {
public string[] vowels;
public string[] consonants;
public string[] kana;
public Dictionary<string,string> kanas;
}

public class Dictionary_list {
Expand Down
Loading

0 comments on commit aef6570

Please sign in to comment.