Skip to content

Commit

Permalink
first working version
Browse files Browse the repository at this point in the history
  • Loading branch information
csukuangfj committed Oct 13, 2023
1 parent 222580c commit 65d338b
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
32 changes: 31 additions & 1 deletion sherpa-onnx/csrc/offline-tts-vits-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#ifndef SHERPA_ONNX_CSRC_OFFLINE_TTS_VITS_IMPL_H_
#define SHERPA_ONNX_CSRC_OFFLINE_TTS_VITS_IMPL_H_

#include <fstream>
#include <string>

#include "sherpa-onnx/csrc/macros.h"
Expand All @@ -19,7 +20,36 @@ class OfflineTtsVitsImpl : public OfflineTtsImpl {
SHERPA_ONNX_LOGE("config: %s\n", config.ToString().c_str());
}

GeneratedAudio Generate(const std::string &text) const override { return {}; }
GeneratedAudio Generate(const std::string &text) const override {
SHERPA_ONNX_LOGE("txt: %s", text.c_str());
auto memory_info =
Ort::MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeDefault);

std::vector<int64_t> x = {
0, 54, 0, 157, 0, 102, 0, 54, 0, 51, 0, 158, 0, 156, 0, 72,
0, 56, 0, 83, 0, 3, 0, 16, 0, 157, 0, 43, 0, 135, 0, 85,
0, 16, 0, 55, 0, 156, 0, 57, 0, 135, 0, 61, 0, 62, 0, 16,
0, 44, 0, 52, 0, 156, 0, 63, 0, 158, 0, 125, 0, 102, 0, 48,
0, 83, 0, 54, 0, 16, 0, 72, 0, 56, 0, 46, 0, 16, 0, 54,
0, 156, 0, 138, 0, 64, 0, 54, 0, 51, 0, 16, 0, 70, 0, 61,
0, 156, 0, 102, 0, 61, 0, 62, 0, 83, 0, 56, 0, 62, 0};
std::array<int64_t, 2> x_shape = {1, static_cast<int32_t>(x.size())};
Ort::Value x_tensor = Ort::Value::CreateTensor(
memory_info, x.data(), x.size(), x_shape.data(), x_shape.size());
Ort::Value audio = model_->Run(std::move(x_tensor));

std::vector<int64_t> audio_shape =
audio.GetTensorTypeAndShapeInfo().GetShape();

SHERPA_ONNX_LOGE("%d, %d", int(audio_shape.size()), int(audio_shape[2]));
const float *p = audio.GetTensorData<float>();
std::ofstream os("t.pcm", std::ios::binary);
os.write(reinterpret_cast<const char *>(p), sizeof(float) * audio_shape[2]);

// sox -t raw -r 22050 -b 32 -e floating-point -c 1 ./t.pcm ./t.wav

return {};
}

private:
std::unique_ptr<OfflineTtsVitsModel> model_;
Expand Down
40 changes: 39 additions & 1 deletion sherpa-onnx/csrc/offline-tts-vits-model.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,45 @@ class OfflineTtsVitsModel::Impl {
Init(buf.data(), buf.size());
}

Ort::Value Run(Ort::Value x) { return Ort::Value(nullptr); }
Ort::Value Run(Ort::Value x) {
auto memory_info =
Ort::MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeDefault);

std::vector<int64_t> x_shape = x.GetTensorTypeAndShapeInfo().GetShape();
if (x_shape[0] != 1) {
SHERPA_ONNX_LOGE("Support only batch_size == 1. Given: %d",
static_cast<int32_t>(x_shape[0]));
exit(-1);
}

int64_t len = x_shape[1];
int64_t len_shape = 1;

Ort::Value x_length =
Ort::Value::CreateTensor(memory_info, &len, 1, &len_shape, 1);

int64_t scale_shape = 1;
float noise_scale = 1;
float length_scale = 1;
float noise_scale_w = 1;

Ort::Value noise_scale_tensor =
Ort::Value::CreateTensor(memory_info, &noise_scale, 1, &scale_shape, 1);
Ort::Value length_scale_tensor = Ort::Value::CreateTensor(
memory_info, &length_scale, 1, &scale_shape, 1);
Ort::Value noise_scale_w_tensor = Ort::Value::CreateTensor(
memory_info, &noise_scale_w, 1, &scale_shape, 1);

std::array<Ort::Value, 5> inputs = {
std::move(x), std::move(x_length), std::move(noise_scale_tensor),
std::move(length_scale_tensor), std::move(noise_scale_w_tensor)};

auto out =
sess_->Run({}, input_names_ptr_.data(), inputs.data(), inputs.size(),
output_names_ptr_.data(), output_names_ptr_.size());

return std::move(out[0]);
}

int32_t SampleRate() const { return sample_rate_; }

Expand Down
1 change: 1 addition & 0 deletions sherpa-onnx/csrc/sherpa-onnx-offline-tts.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ It will generate a file test.wav.
}

sherpa_onnx::OfflineTts tts(config);
tts.Generate("hello world\n");

return 0;
}

0 comments on commit 65d338b

Please sign in to comment.