Skip to content

Commit

Permalink
Merge pull request #482 from filtercodes/v5_cpp_support
Browse files Browse the repository at this point in the history
cpp example
  • Loading branch information
adamnsandle authored Jul 1, 2024
2 parents a395853 + 60ae7ab commit fdbb0a3
Showing 1 changed file with 15 additions and 23 deletions.
38 changes: 15 additions & 23 deletions examples/cpp/silero-vad-onnx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,7 @@ class VadIterator
void reset_states()
{
// Call reset before each audio start
std::memset(_h.data(), 0.0f, _h.size() * sizeof(float));
std::memset(_c.data(), 0.0f, _c.size() * sizeof(float));
std::memset(_state.data(), 0.0f, _state.size() * sizeof(float));
triggered = false;
temp_end = 0;
current_sample = 0;
Expand All @@ -139,19 +138,16 @@ class VadIterator
input.assign(data.begin(), data.end());
Ort::Value input_ort = Ort::Value::CreateTensor<float>(
memory_info, input.data(), input.size(), input_node_dims, 2);
Ort::Value state_ort = Ort::Value::CreateTensor<float>(
memory_info, _state.data(), _state.size(), state_node_dims, 3);
Ort::Value sr_ort = Ort::Value::CreateTensor<int64_t>(
memory_info, sr.data(), sr.size(), sr_node_dims, 1);
Ort::Value h_ort = Ort::Value::CreateTensor<float>(
memory_info, _h.data(), _h.size(), hc_node_dims, 3);
Ort::Value c_ort = Ort::Value::CreateTensor<float>(
memory_info, _c.data(), _c.size(), hc_node_dims, 3);

// Clear and add inputs
ort_inputs.clear();
ort_inputs.emplace_back(std::move(input_ort));
ort_inputs.emplace_back(std::move(state_ort));
ort_inputs.emplace_back(std::move(sr_ort));
ort_inputs.emplace_back(std::move(h_ort));
ort_inputs.emplace_back(std::move(c_ort));

// Infer
ort_outputs = session->Run(
Expand All @@ -161,10 +157,8 @@ class VadIterator

// Output probability & update h,c recursively
float speech_prob = ort_outputs[0].GetTensorMutableData<float>()[0];
float *hn = ort_outputs[1].GetTensorMutableData<float>();
std::memcpy(_h.data(), hn, size_hc * sizeof(float));
float *cn = ort_outputs[2].GetTensorMutableData<float>();
std::memcpy(_c.data(), cn, size_hc * sizeof(float));
float *stateN = ort_outputs[1].GetTensorMutableData<float>();
std::memcpy(_state.data(), stateN, size_state * sizeof(float));

// Push forward sample index
current_sample += window_size_samples;
Expand Down Expand Up @@ -376,27 +370,26 @@ class VadIterator
// Inputs
std::vector<Ort::Value> ort_inputs;

std::vector<const char *> input_node_names = {"input", "sr", "h", "c"};
std::vector<const char *> input_node_names = {"input", "state", "sr"};
std::vector<float> input;
unsigned int size_state = 2 * 1 * 128; // It's FIXED.
std::vector<float> _state;
std::vector<int64_t> sr;
unsigned int size_hc = 2 * 1 * 64; // It's FIXED.
std::vector<float> _h;
std::vector<float> _c;

int64_t input_node_dims[2] = {};
int64_t input_node_dims[2] = {};
const int64_t state_node_dims[3] = {2, 1, 128};
const int64_t sr_node_dims[1] = {1};
const int64_t hc_node_dims[3] = {2, 1, 64};

// Outputs
std::vector<Ort::Value> ort_outputs;
std::vector<const char *> output_node_names = {"output", "hn", "cn"};
std::vector<const char *> output_node_names = {"output", "stateN"};

public:
// Construction
VadIterator(const std::wstring ModelPath,
int Sample_rate = 16000, int windows_frame_size = 64,
int Sample_rate = 16000, int windows_frame_size = 32,
float Threshold = 0.5, int min_silence_duration_ms = 0,
int speech_pad_ms = 64, int min_speech_duration_ms = 64,
int speech_pad_ms = 32, int min_speech_duration_ms = 32,
float max_speech_duration_s = std::numeric_limits<float>::infinity())
{
init_onnx_model(ModelPath);
Expand All @@ -422,8 +415,7 @@ class VadIterator
input_node_dims[0] = 1;
input_node_dims[1] = window_size_samples;

_h.resize(size_hc);
_c.resize(size_hc);
_state.resize(size_state);
sr.resize(1);
sr[0] = sample_rate;
};
Expand Down

0 comments on commit fdbb0a3

Please sign in to comment.