forked from stakira/OpenUtau
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from oxygen-dioxide/diffsinger
Diffsinger
- Loading branch information
Showing
7 changed files
with
344 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
OpenUtau.Core/DiffSinger/DiffSingerSpeakerEmbedManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
|
||
using Microsoft.ML.OnnxRuntime.Tensors; | ||
using NumSharp; | ||
|
||
using OpenUtau.Core.Render; | ||
|
||
namespace OpenUtau.Core.DiffSinger | ||
{ | ||
public class DiffSingerSpeakerEmbedManager | ||
{ | ||
DsConfig dsConfig; | ||
string rootPath; | ||
public NDArray speakerEmbeds = null; | ||
const string VoiceColorHeader = DiffSingerUtils.VoiceColorHeader; | ||
|
||
public DiffSingerSpeakerEmbedManager(DsConfig dsConfig, string rootPath) { | ||
this.dsConfig = dsConfig; | ||
this.rootPath = rootPath; | ||
} | ||
public NDArray loadSpeakerEmbed(string speaker) { | ||
string path = Path.Join(rootPath, speaker + ".emb"); | ||
if(File.Exists(path)) { | ||
var reader = new BinaryReader(File.OpenRead(path)); | ||
return np.array<float>(Enumerable.Range(0, dsConfig.hiddenSize) | ||
.Select(i => reader.ReadSingle())); | ||
} else { | ||
throw new Exception("Speaker embed file {path} not found"); | ||
} | ||
} | ||
|
||
public NDArray getSpeakerEmbeds() { | ||
if(speakerEmbeds == null) { | ||
if(dsConfig.speakers == null) { | ||
return null; | ||
} else { | ||
speakerEmbeds = np.zeros<float>(dsConfig.hiddenSize, dsConfig.speakers.Count); | ||
foreach(var spkId in Enumerable.Range(0, dsConfig.speakers.Count)) { | ||
speakerEmbeds[":", spkId] = loadSpeakerEmbed(dsConfig.speakers[spkId]); | ||
} | ||
} | ||
} | ||
return speakerEmbeds; | ||
} | ||
|
||
public bool IsVoiceColorCurve(string abbr, out int subBankId) { | ||
subBankId = 0; | ||
if (abbr.StartsWith(VoiceColorHeader) && int.TryParse(abbr.Substring(2), out subBankId)) {; | ||
subBankId -= 1; | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
public int getSpeakerIndexBySuffix(string suffix){ | ||
var speakerIndex = dsConfig.speakers.IndexOf(suffix); | ||
if(speakerIndex == -1){ | ||
speakerIndex = 0; | ||
} | ||
return speakerIndex; | ||
} | ||
|
||
public Tensor<float> PhraseSpeakerEmbed(RenderPhrase phrase, IList<int> durations, float frameMs, int totalFrames, int headFrames, int tailFrames){ | ||
var singer = phrase.singer; | ||
var hiddenSize = dsConfig.hiddenSize; | ||
var speakerEmbeds = getSpeakerEmbeds(); | ||
//get default speaker for each phoneme | ||
var headDefaultSpk = getSpeakerIndexBySuffix(phrase.phones[0].suffix); | ||
var tailDefaultSpk = getSpeakerIndexBySuffix(phrase.phones[^1].suffix); | ||
var defaultSpkByFrame = Enumerable.Repeat(headDefaultSpk, headFrames).ToList(); | ||
defaultSpkByFrame.AddRange(Enumerable.Range(0, phrase.phones.Length) | ||
.SelectMany(phIndex => Enumerable.Repeat(getSpeakerIndexBySuffix(phrase.phones[phIndex].suffix), durations[phIndex+1]))); | ||
defaultSpkByFrame.AddRange(Enumerable.Repeat(tailDefaultSpk, tailFrames)); | ||
//get speaker curves | ||
NDArray spkCurves = np.zeros<float>(totalFrames, dsConfig.speakers.Count); | ||
foreach(var curve in phrase.curves) { | ||
if(IsVoiceColorCurve(curve.Item1,out int subBankId) && subBankId < singer.Subbanks.Count) { | ||
var spkId = getSpeakerIndexBySuffix(singer.Subbanks[subBankId].Suffix); | ||
spkCurves[":", spkId] += DiffSingerUtils.SampleCurve(phrase, curve.Item2, 0, | ||
frameMs, totalFrames, headFrames, tailFrames, x => x * 0.01f) | ||
.Select(f => (float)f).ToArray(); | ||
} | ||
} | ||
foreach(int frameId in Enumerable.Range(0,totalFrames)) { | ||
//standarization | ||
var spkSum = spkCurves[frameId, ":"].ToArray<float>().Sum(); | ||
if (spkSum > 1) { | ||
spkCurves[frameId, ":"] /= spkSum; | ||
} else { | ||
spkCurves[frameId,defaultSpkByFrame[frameId]] += 1 - spkSum; | ||
} | ||
} | ||
var spkEmbedResult = np.dot(spkCurves, speakerEmbeds.T); | ||
var spkEmbedTensor = new DenseTensor<float>(spkEmbedResult.ToArray<float>(), | ||
new int[] { totalFrames, hiddenSize }) | ||
.Reshape(new int[] { 1, totalFrames, hiddenSize }); | ||
return spkEmbedTensor; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.