sherpa-onnx-offline-denoiser.cc
2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// sherpa-onnx/csrc/sherpa-onnx-offline-denoiser.cc
//
// Copyright (c) 2025 Xiaomi Corporation
#include <stdio.h>
#include <chrono> // NOLINT
#include "sherpa-onnx/csrc/offline-speech-denoiser.h"
#include "sherpa-onnx/csrc/wave-reader.h"
#include "sherpa-onnx/csrc/wave-writer.h"
int main(int32_t argc, char *argv[]) {
const char *kUsageMessage = R"usage(
Non-stremaing speech denoising with sherpa-onnx.
Please visit
https://github.com/k2-fsa/sherpa-onnx/releases/tag/speech-enhancement-models
to download models.
Usage:
(1) Use gtcrn models
wget https://github.com/k2-fsa/sherpa-onnx/releases/download/speech-enhancement-models/gtcrn_simple.onnx
./bin/sherpa-onnx-offline-denoiser \
--speech-denoiser-gtcrn-model=gtcrn_simple.onnx \
--input-wav input.wav \
--output-wav output_16k.wav
)usage";
sherpa_onnx::ParseOptions po(kUsageMessage);
sherpa_onnx::OfflineSpeechDenoiserConfig config;
std::string input_wave;
std::string output_wave;
config.Register(&po);
po.Register("input-wav", &input_wave, "Path to input wav.");
po.Register("output-wav", &output_wave, "Path to output wav");
po.Read(argc, argv);
if (po.NumArgs() != 0) {
fprintf(stderr, "Please don't give positional arguments\n");
po.PrintUsage();
exit(EXIT_FAILURE);
}
fprintf(stderr, "%s\n", config.ToString().c_str());
if (input_wave.empty()) {
fprintf(stderr, "Please provide --input-wav\n");
po.PrintUsage();
exit(EXIT_FAILURE);
}
if (output_wave.empty()) {
fprintf(stderr, "Please provide --output-wav\n");
po.PrintUsage();
exit(EXIT_FAILURE);
}
sherpa_onnx::OfflineSpeechDenoiser denoiser(config);
int32_t sampling_rate = -1;
bool is_ok = false;
std::vector<float> samples =
sherpa_onnx::ReadWave(input_wave, &sampling_rate, &is_ok);
if (!is_ok) {
fprintf(stderr, "Failed to read '%s'\n", input_wave.c_str());
return -1;
}
fprintf(stderr, "Started\n");
const auto begin = std::chrono::steady_clock::now();
auto result = denoiser.Run(samples.data(), samples.size(), sampling_rate);
const auto end = std::chrono::steady_clock::now();
float elapsed_seconds =
std::chrono::duration_cast<std::chrono::milliseconds>(end - begin)
.count() /
1000.;
fprintf(stderr, "Done\n");
is_ok = sherpa_onnx::WriteWave(output_wave, result.sample_rate,
result.samples.data(), result.samples.size());
if (is_ok) {
fprintf(stderr, "Saved to %s\n", output_wave.c_str());
} else {
fprintf(stderr, "Failed to save to %s\n", output_wave.c_str());
}
float duration = samples.size() / static_cast<float>(sampling_rate);
fprintf(stderr, "num threads: %d\n", config.model.num_threads);
fprintf(stderr, "Elapsed seconds: %.3f s\n", elapsed_seconds);
float rtf = elapsed_seconds / duration;
fprintf(stderr, "Real time factor (RTF): %.3f / %.3f = %.3f\n",
elapsed_seconds, duration, rtf);
}