|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Text; |
| 5 | +using OpenUtau.SimpleHelpers; |
| 6 | +using Serilog; |
| 7 | + |
| 8 | +namespace OpenUtau.Classic { |
| 9 | + |
| 10 | + /// <summary> |
| 11 | + /// A character. May contains multiple voice banks. |
| 12 | + /// </summary> |
| 13 | + public class Character { |
| 14 | + |
| 15 | + private Character() { |
| 16 | + } |
| 17 | + |
| 18 | + public string Name { private set; get; } |
| 19 | + public string DisplayName => Loaded ? Name : $"{Name}[Unloaded]"; |
| 20 | + public string BasePath { private set; get; } |
| 21 | + public string CharacterFile { private set; get; } |
| 22 | + public string Author { private set; get; } |
| 23 | + public string Website { private set; get; } |
| 24 | + public string Language { private set; get; } |
| 25 | + public List<Voicebank> Voicebanks { private set; get; } |
| 26 | + public bool Loaded { private set; get; } |
| 27 | + |
| 28 | + public static List<Character> SearchAndLoadCharacters(List<string> searchPaths) { |
| 29 | + const string CharacterFileName = "character.txt"; |
| 30 | + var characterFiles = new HashSet<string>(); |
| 31 | + foreach (var searchPath in searchPaths) { |
| 32 | + if (!Directory.Exists(searchPath)) { |
| 33 | + continue; |
| 34 | + } |
| 35 | + var files = Directory.GetFiles(searchPath, CharacterFileName, |
| 36 | + SearchOption.AllDirectories); |
| 37 | + foreach (var file in files) { |
| 38 | + characterFiles.Add(file); |
| 39 | + } |
| 40 | + } |
| 41 | + var result = new List<Character>(); |
| 42 | + foreach (var file in characterFiles) { |
| 43 | + var character = new Character { |
| 44 | + BasePath = Path.GetDirectoryName(file), |
| 45 | + CharacterFile = file, |
| 46 | + }; |
| 47 | + character.LoadVoicebanks(); |
| 48 | + result.Add(character); |
| 49 | + } |
| 50 | + return result; |
| 51 | + } |
| 52 | + |
| 53 | + public void LoadVoicebanks() { |
| 54 | + const string OtoFileName = "oto.ini"; |
| 55 | + var otoFiles = Directory.GetFiles(BasePath, OtoFileName, SearchOption.AllDirectories); |
| 56 | + var voicebanks = new List<Voicebank>(); |
| 57 | + foreach (var otoFile in otoFiles) { |
| 58 | + try { |
| 59 | + var voicebank = new Voicebank.Builder(otoFile) |
| 60 | + .SetOtoEncoding(FileEncoding.DetectFileEncoding( |
| 61 | + otoFile, Encoding.GetEncoding("shift_jis"))) |
| 62 | + .SetPathEncoding(Encoding.Default) |
| 63 | + .Build(); |
| 64 | + voicebanks.Add(voicebank); |
| 65 | + } catch (Exception e) { |
| 66 | + Log.Error(e, "Failed to load {0}", otoFile); |
| 67 | + } |
| 68 | + } |
| 69 | + Log.Information("Loaded {0} voicebanks from character {1}", voicebanks.Count, BasePath); |
| 70 | + Voicebanks = voicebanks; |
| 71 | + Loaded = true; |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments