mirror of
https://github.com/BoardWare-Genius/jarvis-models.git
synced 2025-12-14 00:53:25 +00:00
feat: tts
This commit is contained in:
121
tts/vits/Libtorch C++ Infer/VITS-LibTorch.cpp
Normal file
121
tts/vits/Libtorch C++ Infer/VITS-LibTorch.cpp
Normal file
@ -0,0 +1,121 @@
|
||||
#include <iostream>
|
||||
#include <torch/torch.h>
|
||||
#include <torch/script.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <locale>
|
||||
#include <codecvt>
|
||||
#include <direct.h>
|
||||
#include <fstream>
|
||||
typedef int64_t int64;
|
||||
namespace Shirakana {
|
||||
|
||||
struct WavHead {
|
||||
char RIFF[4];
|
||||
long int size0;
|
||||
char WAVE[4];
|
||||
char FMT[4];
|
||||
long int size1;
|
||||
short int fmttag;
|
||||
short int channel;
|
||||
long int samplespersec;
|
||||
long int bytepersec;
|
||||
short int blockalign;
|
||||
short int bitpersamples;
|
||||
char DATA[4];
|
||||
long int size2;
|
||||
};
|
||||
|
||||
int conArr2Wav(int64 size, int16_t* input, const char* filename) {
|
||||
WavHead head = { {'R','I','F','F'},0,{'W','A','V','E'},{'f','m','t',' '},16,
|
||||
1,1,22050,22050 * 2,2,16,{'d','a','t','a'},
|
||||
0 };
|
||||
head.size0 = size * 2 + 36;
|
||||
head.size2 = size * 2;
|
||||
std::ofstream ocout;
|
||||
char* outputData = (char*)input;
|
||||
ocout.open(filename, std::ios::out | std::ios::binary);
|
||||
ocout.write((char*)&head, 44);
|
||||
ocout.write(outputData, (int32_t)(size * 2));
|
||||
ocout.close();
|
||||
return 0;
|
||||
}
|
||||
|
||||
inline std::wstring to_wide_string(const std::string& input)
|
||||
{
|
||||
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
|
||||
return converter.from_bytes(input);
|
||||
}
|
||||
|
||||
inline std::string to_byte_string(const std::wstring& input)
|
||||
{
|
||||
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
|
||||
return converter.to_bytes(input);
|
||||
}
|
||||
}
|
||||
|
||||
#define val const auto
|
||||
int main()
|
||||
{
|
||||
torch::jit::Module Vits;
|
||||
std::string buffer;
|
||||
std::vector<int64> text;
|
||||
std::vector<int16_t> data;
|
||||
while(true)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
std::cin >> buffer;
|
||||
if (buffer == "end")
|
||||
return 0;
|
||||
if(buffer == "model")
|
||||
{
|
||||
std::cin >> buffer;
|
||||
Vits = torch::jit::load(buffer);
|
||||
continue;
|
||||
}
|
||||
if (buffer == "endinfer")
|
||||
{
|
||||
Shirakana::conArr2Wav(data.size(), data.data(), "temp\\tmp.wav");
|
||||
data.clear();
|
||||
std::cout << "endofinfe";
|
||||
continue;
|
||||
}
|
||||
if (buffer == "line")
|
||||
{
|
||||
std::cin >> buffer;
|
||||
while (buffer.find("endline")==std::string::npos)
|
||||
{
|
||||
text.push_back(std::atoi(buffer.c_str()));
|
||||
std::cin >> buffer;
|
||||
}
|
||||
val InputTensor = torch::from_blob(text.data(), { 1,static_cast<int64>(text.size()) }, torch::kInt64);
|
||||
std::array<int64, 1> TextLength{ static_cast<int64>(text.size()) };
|
||||
val InputTensor_length = torch::from_blob(TextLength.data(), { 1 }, torch::kInt64);
|
||||
std::vector<torch::IValue> inputs;
|
||||
inputs.push_back(InputTensor);
|
||||
inputs.push_back(InputTensor_length);
|
||||
if (buffer.length() > 7)
|
||||
{
|
||||
std::array<int64, 1> speakerIndex{ (int64)atoi(buffer.substr(7).c_str()) };
|
||||
inputs.push_back(torch::from_blob(speakerIndex.data(), { 1 }, torch::kLong));
|
||||
}
|
||||
val output = Vits.forward(inputs).toTuple()->elements()[0].toTensor().multiply(32276.0F);
|
||||
val outputSize = output.sizes().at(2);
|
||||
val floatOutput = output.data_ptr<float>();
|
||||
int16_t* outputTmp = (int16_t*)malloc(sizeof(float) * outputSize);
|
||||
if (outputTmp == nullptr) {
|
||||
throw std::exception("内存不足");
|
||||
}
|
||||
for (int i = 0; i < outputSize; i++) {
|
||||
*(outputTmp + i) = (int16_t) * (floatOutput + i);
|
||||
}
|
||||
data.insert(data.end(), outputTmp, outputTmp+outputSize);
|
||||
free(outputTmp);
|
||||
text.clear();
|
||||
std::cout << "endofline";
|
||||
}
|
||||
}
|
||||
}
|
||||
//model S:\VSGIT\ShirakanaTTSUI\build\x64\Release\Mods\AtriVITS\AtriVITS_LJS.pt
|
||||
}
|
||||
142
tts/vits/Libtorch C++ Infer/toLibTorch.ipynb
Normal file
142
tts/vits/Libtorch C++ Infer/toLibTorch.ipynb
Normal file
@ -0,0 +1,142 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%matplotlib inline\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import IPython.display as ipd\n",
|
||||
"\n",
|
||||
"import os\n",
|
||||
"import json\n",
|
||||
"import math\n",
|
||||
"import torch\n",
|
||||
"from torch import nn\n",
|
||||
"from torch.nn import functional as F\n",
|
||||
"from torch.utils.data import DataLoader\n",
|
||||
"\n",
|
||||
"import ../commons\n",
|
||||
"import ../utils\n",
|
||||
"from ../data_utils import TextAudioLoader, TextAudioCollate, TextAudioSpeakerLoader, TextAudioSpeakerCollate\n",
|
||||
"from ../models import SynthesizerTrn\n",
|
||||
"from ../text.symbols import symbols\n",
|
||||
"from ../text import text_to_sequence\n",
|
||||
"\n",
|
||||
"from scipy.io.wavfile import write\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def get_text(text, hps):\n",
|
||||
" text_norm = text_to_sequence(text, hps.data.text_cleaners)\n",
|
||||
" if hps.data.add_blank:\n",
|
||||
" text_norm = commons.intersperse(text_norm, 0)\n",
|
||||
" text_norm = torch.LongTensor(text_norm)\n",
|
||||
" return text_norm"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"#############################################################\n",
|
||||
"# #\n",
|
||||
"# Single Speakers #\n",
|
||||
"# #\n",
|
||||
"#############################################################"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"hps = utils.get_hparams_from_file(\"configs/XXX.json\") #将\"\"内的内容修改为你的模型路径与config路径\n",
|
||||
"net_g = SynthesizerTrn(\n",
|
||||
" len(symbols),\n",
|
||||
" hps.data.filter_length // 2 + 1,\n",
|
||||
" hps.train.segment_size // hps.data.hop_length,\n",
|
||||
" **hps.model).cuda()\n",
|
||||
"_ = net_g.eval()\n",
|
||||
"\n",
|
||||
"_ = utils.load_checkpoint(\"/path/to/model.pth\", net_g, None)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"stn_tst = get_text(\"こんにちは\", hps)\n",
|
||||
"with torch.no_grad():\n",
|
||||
" x_tst = stn_tst.cuda().unsqueeze(0)\n",
|
||||
" x_tst_lengths = torch.LongTensor([stn_tst.size(0)]).cuda()\n",
|
||||
" traced_mod = torch.jit.trace(net_g,(x_tst, x_tst_lengths,sid))\n",
|
||||
" torch.jit.save(traced_mod,\"OUTPUTLIBTORCHMODEL.pt\")\n",
|
||||
" audio = net_g.infer(x_tst, x_tst_lengths, noise_scale=.667, noise_scale_w=0.8, length_scale=1)[0][0,0].data.cpu().float().numpy()\n",
|
||||
"ipd.display(ipd.Audio(audio, rate=hps.data.sampling_rate, normalize=False))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"#############################################################\n",
|
||||
"# #\n",
|
||||
"# Multiple Speakers #\n",
|
||||
"# #\n",
|
||||
"#############################################################"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"hps = utils.get_hparams_from_file(\"./configs/XXX.json\") #将\"\"内的内容修改为你的模型路径与config路径\n",
|
||||
"net_g = SynthesizerTrn(\n",
|
||||
" len(symbols),\n",
|
||||
" hps.data.filter_length // 2 + 1,\n",
|
||||
" hps.train.segment_size // hps.data.hop_length,\n",
|
||||
" n_speakers=hps.data.n_speakers,\n",
|
||||
" **hps.model).cuda()\n",
|
||||
"_ = net_g.eval()\n",
|
||||
"\n",
|
||||
"_ = utils.load_checkpoint(\"/path/to/model.pth\", net_g, None)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"stn_tst = get_text(\"こんにちは\", hps)\n",
|
||||
"with torch.no_grad():\n",
|
||||
" x_tst = stn_tst.cuda().unsqueeze(0)\n",
|
||||
" x_tst_lengths = torch.LongTensor([stn_tst.size(0)]).cuda()\n",
|
||||
" sid = torch.LongTensor([4]).cuda()\n",
|
||||
" traced_mod = torch.jit.trace(net_g,(x_tst, x_tst_lengths,sid))\n",
|
||||
" torch.jit.save(traced_mod,\"OUTPUTLIBTORCHMODEL.pt\")\n",
|
||||
" audio = net_g.infer(x_tst, x_tst_lengths, sid=sid, noise_scale=.667, noise_scale_w=0.8, length_scale=1)[0][0,0].data.cpu().float().numpy()\n",
|
||||
"ipd.display(ipd.Audio(audio, rate=hps.data.sampling_rate, normalize=False))"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"language_info": {
|
||||
"name": "python"
|
||||
},
|
||||
"orig_nbformat": 4
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
Reference in New Issue
Block a user