Skip to content

Commit

Permalink
Cleanup some unnecessary UI.
Browse files Browse the repository at this point in the history
  • Loading branch information
stakira committed Oct 9, 2020
1 parent 29d2027 commit 678704c
Show file tree
Hide file tree
Showing 17 changed files with 426 additions and 236 deletions.
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ indent_style = space
indent_size = 4
insert_final_newline = true
charset = utf-8-bom
guidelines = 100
###############################
# .NET Coding Conventions #
###############################
Expand Down
2 changes: 1 addition & 1 deletion OpenUtau/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
<configSections>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2"/>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/>
</startup>
</configuration>
5 changes: 4 additions & 1 deletion OpenUtau/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
Expand Down Expand Up @@ -46,6 +45,10 @@ private static void Main() {
NBug.Settings.StoragePath = NBug.Enums.StoragePath.CurrentDirectory;
NBug.Settings.UIMode = NBug.Enums.UIMode.Full;

var characters = Classic.Character.SearchAndLoadCharacters(Core.Util.Preferences.GetSingerSearchPaths());

Classic.VoicebankInstaller.IndexAllArchive(Core.Util.Preferences.GetSingerSearchPaths());

Core.DocManager.Inst.SearchAllSingers();
var pm = new Core.PartManager();

Expand Down
74 changes: 74 additions & 0 deletions OpenUtau/Classic/Character.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using OpenUtau.SimpleHelpers;
using Serilog;

namespace OpenUtau.Classic {

/// <summary>
/// A character. May contains multiple voice banks.
/// </summary>
public class Character {

private Character() {
}

public string Name { private set; get; }
public string DisplayName => Loaded ? Name : $"{Name}[Unloaded]";
public string BasePath { private set; get; }
public string CharacterFile { private set; get; }
public string Author { private set; get; }
public string Website { private set; get; }
public string Language { private set; get; }
public List<Voicebank> Voicebanks { private set; get; }
public bool Loaded { private set; get; }

public static List<Character> SearchAndLoadCharacters(List<string> searchPaths) {
const string CharacterFileName = "character.txt";
var characterFiles = new HashSet<string>();
foreach (var searchPath in searchPaths) {
if (!Directory.Exists(searchPath)) {
continue;
}
var files = Directory.GetFiles(searchPath, CharacterFileName,
SearchOption.AllDirectories);
foreach (var file in files) {
characterFiles.Add(file);
}
}
var result = new List<Character>();
foreach (var file in characterFiles) {
var character = new Character {
BasePath = Path.GetDirectoryName(file),
CharacterFile = file,
};
character.LoadVoicebanks();
result.Add(character);
}
return result;
}

public void LoadVoicebanks() {
const string OtoFileName = "oto.ini";
var otoFiles = Directory.GetFiles(BasePath, OtoFileName, SearchOption.AllDirectories);
var voicebanks = new List<Voicebank>();
foreach (var otoFile in otoFiles) {
try {
var voicebank = new Voicebank.Builder(otoFile)
.SetOtoEncoding(FileEncoding.DetectFileEncoding(
otoFile, Encoding.GetEncoding("shift_jis")))
.SetPathEncoding(Encoding.Default)
.Build();
voicebanks.Add(voicebank);
} catch (Exception e) {
Log.Error(e, "Failed to load {0}", otoFile);
}
}
Log.Information("Loaded {0} voicebanks from character {1}", voicebanks.Count, BasePath);
Voicebanks = voicebanks;
Loaded = true;
}
}
}
46 changes: 46 additions & 0 deletions OpenUtau/Classic/Oto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Linq;

namespace OpenUtau.Classic {

/// <summary>
/// A sound in a voice bank. Corresponds to one line in oto.ini file.
/// </summary>
public class Oto {
public string AudioFile;
public string Alias;
public int Offset;
public int Consonant;
public int Cutoff;
public int Preutter;
public int Overlap;

public static Oto Parse(string line) {
if (!line.Contains('=')) {
return null;
}
var parts = line.Split('=');
if (parts.Length != 2) {
return null;
}
var audioClip = parts[0].Trim();
parts = parts[1].Split(',');
if (parts.Length != 6) {
return null;
}
var result = new Oto {
AudioFile = audioClip,
Alias = parts[0].Trim()
};
int.TryParse(parts[1], out result.Offset);
int.TryParse(parts[2], out result.Consonant);
int.TryParse(parts[3], out result.Cutoff);
int.TryParse(parts[4], out result.Preutter);
int.TryParse(parts[5], out result.Overlap);
return result;
}

public override string ToString() {
return Alias;
}
}
}
91 changes: 91 additions & 0 deletions OpenUtau/Classic/VoiceBank.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using System.Collections.Generic;
using System.IO;
using System.Text;
using Serilog;

namespace OpenUtau.Classic {

/// <summary>
/// A sound bank. Corresponds to a single oto.ini file.
/// </summary>
public class Voicebank {

private Voicebank() {
}

public string BasePath { private set; get; }
public string OtoFile { private set; get; }
public List<Oto> OtoList { private set; get; }
public Encoding OtoEncoding { private set; get; }
public Encoding PathEncoding { private set; get; }

public class Builder {
private readonly Voicebank voicebank;
private readonly byte[] otoBytes;
private int audioFilesFound;

public Builder(string otoFile) {
otoBytes = File.ReadAllBytes(otoFile);
voicebank = new Voicebank() {
OtoFile = otoFile,
BasePath = Path.GetDirectoryName(otoFile),
PathEncoding = Encoding.Default,
};
}

public string TestOtoEncoding(Encoding encoding) {
return encoding.GetString(otoBytes);
}

public Builder SetOtoEncoding(Encoding encoding) {
voicebank.OtoEncoding = encoding;
var lines = new List<string>();
using (var stream = new StreamReader(new MemoryStream(otoBytes), encoding)) {
while (!stream.EndOfStream) {
lines.Add(stream.ReadLine());
}
}
var otoList = new List<Oto>();
foreach (var line in lines) {
var oto = Oto.Parse(line);
if (oto != null) {
otoList.Add(oto);
}
}
voicebank.OtoList = otoList;
return this;
}

public double TestPathEncoding(Encoding encoding) {
if (voicebank.OtoList == null) {
return 0;
}
if (voicebank.OtoList.Count == 0) {
return 1;
}
audioFilesFound = 0;
foreach (var oto in voicebank.OtoList) {
try {
var file = encoding.GetString(voicebank.OtoEncoding.GetBytes(oto.AudioFile));
if (File.Exists(Path.Combine(voicebank.BasePath, file))) {
audioFilesFound++;
}
} catch { }
}
return (double)audioFilesFound / voicebank.OtoList.Count;
}

public Builder SetPathEncoding(Encoding encoding) {
voicebank.PathEncoding = encoding;
return this;
}

public Voicebank Build() {
var score = TestPathEncoding(voicebank.PathEncoding);
Log.Information("Loaded {0} otos and {1:F2}% audio files from {2}",
voicebank.OtoList.Count, score * 100, voicebank.OtoFile);
return voicebank;
}
}
}
}
35 changes: 35 additions & 0 deletions OpenUtau/Classic/VoicebankInstaller.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Text;
using Serilog;

namespace OpenUtau.Classic {

internal class VoicebankInstaller {

public static void IndexAllArchive(IEnumerable<string> searchPaths) {
foreach (var path in searchPaths) {
if (Directory.Exists(path)) {
var zips = Directory.GetFiles(path, "*.zip", SearchOption.AllDirectories);
foreach (var zip in zips) {
Log.Information($"{zip}");
IndexArchive(zip);
}
}
}
}

public static void IndexArchive(string path) {
using (var stream = new FileStream(path, FileMode.Open)) {
using (var archive = new ZipArchive(stream, ZipArchiveMode.Read, false, Encoding.GetEncoding("shift_jis"))) {
foreach (var entry in archive.Entries) {
if (entry.Name == "character.txt" || entry.Name == "oto.ini") {
Log.Information($"{entry.Name} {entry.FullName}");
}
}
}
}
}
}
}
Loading

0 comments on commit 678704c

Please sign in to comment.