Path: blob/master/examples/cppwin/TensorflowTTSCppInference/VoxCommon.cpp
1559 views
#include "VoxCommon.hpp"1#include "ext/json.hpp"2using namespace nlohmann;34const std::vector<std::string> Text2MelNames = {"FastSpeech2","Tacotron2"};5const std::vector<std::string> VocoderNames = {"Multi-Band MelGAN"};6const std::vector<std::string> RepoNames = {"TensorflowTTS"};78const std::vector<std::string> LanguageNames = {"English","Spanish"};91011void VoxUtil::ExportWAV(const std::string & Filename, const std::vector<float>& Data, unsigned SampleRate) {12AudioFile<float>::AudioBuffer Buffer;13Buffer.resize(1);141516Buffer[0] = Data;17size_t BufSz = Data.size();181920AudioFile<float> File;21File.setAudioBuffer(Buffer);22File.setAudioBufferSize(1, (int)BufSz);23File.setNumSamplesPerChannel((int)BufSz);24File.setNumChannels(1);25File.setBitDepth(32);26File.setSampleRate(SampleRate);2728File.save(Filename, AudioFileFormat::Wave);29303132}3334VoiceInfo VoxUtil::ReadModelJSON(const std::string &InfoFilename)35{36const size_t MaxNoteSize = 80;3738std::ifstream JFile(InfoFilename);39json JS;4041JFile >> JS;424344JFile.close();4546auto Arch = JS["architecture"];4748ArchitectureInfo CuArch;49CuArch.Repo = Arch["repo"].get<int>();50CuArch.Text2Mel = Arch["text2mel"].get<int>();51CuArch.Vocoder = Arch["vocoder"].get<int>();5253// Now fill the strings54CuArch.s_Repo = RepoNames[CuArch.Repo];55CuArch.s_Text2Mel = Text2MelNames[CuArch.Text2Mel];56CuArch.s_Vocoder = VocoderNames[CuArch.Vocoder];575859uint32_t Lang = JS["language"].get<uint32_t>();60VoiceInfo Inf{JS["name"].get<std::string>(),61JS["author"].get<std::string>(),62JS["version"].get<int>(),63JS["description"].get<std::string>(),64CuArch,65JS["note"].get<std::string>(),66JS["sarate"].get<uint32_t>(),67Lang,68LanguageNames[Lang],69" " + JS["pad"].get<std::string>()}; // Add a space for separation since we directly append the value to the prompt7071if (Inf.Note.size() > MaxNoteSize)72Inf.Note = Inf.Note.substr(0,MaxNoteSize);7374return Inf;7576777879808182}838485