audio-tagging-label-file.cc
1.6 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
// sherpa-onnx/csrc/audio-tagging-label-file.cc
//
// Copyright (c) 2024 Xiaomi Corporation
#include "sherpa-onnx/csrc/audio-tagging-label-file.h"
#include <fstream>
#include <sstream>
#include <string>
#include "sherpa-onnx/csrc/macros.h"
#include "sherpa-onnx/csrc/text-utils.h"
namespace sherpa_onnx {
AudioTaggingLabels::AudioTaggingLabels(const std::string &filename) {
std::ifstream is(filename);
Init(is);
}
// Format of a label file
/*
index,mid,display_name
0,/m/09x0r,"Speech"
1,/m/05zppz,"Male speech, man speaking"
*/
void AudioTaggingLabels::Init(std::istream &is) {
std::string line;
std::getline(is, line); // skip the header
std::string index;
std::string tmp;
std::string name;
while (std::getline(is, line)) {
index.clear();
name.clear();
std::istringstream input2(line);
std::getline(input2, index, ',');
std::getline(input2, tmp, ',');
std::getline(input2, name);
std::size_t pos{};
int32_t i = std::stoi(index, &pos);
if (index.size() == 0 || pos != index.size()) {
SHERPA_ONNX_LOGE("Invalid line: %s", line.c_str());
exit(-1);
}
if (i != names_.size()) {
SHERPA_ONNX_LOGE(
"Index should be sorted and contiguous. Expected index: %d, given: "
"%d.",
static_cast<int32_t>(names_.size()), i);
}
if (name.empty() || name.front() != '"' || name.back() != '"') {
SHERPA_ONNX_LOGE("Invalid line: %s", line.c_str());
exit(-1);
}
names_.emplace_back(name.begin() + 1, name.end() - 1);
}
}
const std::string &AudioTaggingLabels::GetEventName(int32_t index) const {
return names_.at(index);
}
} // namespace sherpa_onnx