Skip to content

Commit 2b4e485

Browse files
authored
feat: add generic SetOption/GetOption to OnlineStream and OfflineStream (#3307)
Add a generic key-value option mechanism to both OnlineStream and OfflineStream C++ core classes. This is the foundation for the SetOption API — follow-up PRs add C API, CXX wrapper, language bindings, and Paraformer migration. - Use ToIntOrDefault/ToFloatOrDefault for safe parsing (no exceptions) - GetOption returns empty string for missing keys (no exceptions)
1 parent af748e8 commit 2b4e485

4 files changed

Lines changed: 146 additions & 0 deletions

File tree

sherpa-onnx/csrc/offline-stream.cc

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <limits>
1212
#include <memory>
1313
#include <string>
14+
#include <unordered_map>
1415
#include <utility>
1516
#include <vector>
1617

@@ -20,6 +21,7 @@
2021
#include "sherpa-onnx/csrc/math.h"
2122
#include "sherpa-onnx/csrc/offline-recognizer.h"
2223
#include "sherpa-onnx/csrc/resample.h"
24+
#include "sherpa-onnx/csrc/text-utils.h"
2325

2426
namespace sherpa_onnx {
2527

@@ -222,6 +224,39 @@ class OfflineStream::Impl {
222224

223225
const ContextGraphPtr &GetContextGraph() const { return context_graph_; }
224226

227+
void SetOption(const std::string &key, const std::string &value) {
228+
options_[key] = value;
229+
}
230+
231+
bool HasOption(const std::string &key) const {
232+
return options_.count(key) != 0;
233+
}
234+
235+
const std::string &GetOption(const std::string &key) const {
236+
auto it = options_.find(key);
237+
if (it != options_.end()) {
238+
return it->second;
239+
}
240+
static const std::string kEmpty;
241+
return kEmpty;
242+
}
243+
244+
int32_t GetOptionInt(const std::string &key, int32_t default_value) const {
245+
auto it = options_.find(key);
246+
if (it != options_.end()) {
247+
return ToIntOrDefault(it->second, default_value);
248+
}
249+
return default_value;
250+
}
251+
252+
float GetOptionFloat(const std::string &key, float default_value) const {
253+
auto it = options_.find(key);
254+
if (it != options_.end()) {
255+
return ToFloatOrDefault(it->second, default_value);
256+
}
257+
return default_value;
258+
}
259+
225260
private:
226261
// see
227262
// https://github.com/pytorch/audio/blob/main/src/torchaudio/functional/functional.py#L359
@@ -298,6 +333,8 @@ class OfflineStream::Impl {
298333

299334
// used only when (is_moonshine_ || is_omnilingual_asr_) == true
300335
std::vector<float> samples_;
336+
337+
std::unordered_map<std::string, std::string> options_;
301338
};
302339

303340
OfflineStream::OfflineStream(const FeatureExtractorConfig &config /*= {}*/,
@@ -339,6 +376,30 @@ const ContextGraphPtr &OfflineStream::GetContextGraph() const {
339376
const OfflineRecognitionResult &OfflineStream::GetResult() const {
340377
return impl_->GetResult();
341378
}
379+
380+
void OfflineStream::SetOption(const std::string &key,
381+
const std::string &value) {
382+
impl_->SetOption(key, value);
383+
}
384+
385+
bool OfflineStream::HasOption(const std::string &key) const {
386+
return impl_->HasOption(key);
387+
}
388+
389+
const std::string &OfflineStream::GetOption(const std::string &key) const {
390+
return impl_->GetOption(key);
391+
}
392+
393+
int32_t OfflineStream::GetOptionInt(const std::string &key,
394+
int32_t default_value) const {
395+
return impl_->GetOptionInt(key, default_value);
396+
}
397+
398+
float OfflineStream::GetOptionFloat(const std::string &key,
399+
float default_value) const {
400+
return impl_->GetOptionFloat(key, default_value);
401+
}
402+
342403
std::string OfflineRecognitionResult::AsJsonString() const {
343404
std::ostringstream os;
344405
os << "{";

sherpa-onnx/csrc/offline-stream.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,18 @@ class OfflineStream {
115115
/** Get the ContextGraph of this stream */
116116
const ContextGraphPtr &GetContextGraph() const;
117117

118+
// Generic per-stream option mechanism (key-value string pairs).
119+
void SetOption(const std::string &key, const std::string &value);
120+
bool HasOption(const std::string &key) const;
121+
122+
// Returns the value for the given key, or an empty string if the key
123+
// does not exist. No exception is thrown for missing keys.
124+
const std::string &GetOption(const std::string &key) const;
125+
int32_t GetOptionInt(const std::string &key,
126+
int32_t default_value = 0) const;
127+
float GetOptionFloat(const std::string &key,
128+
float default_value = 0.0f) const;
129+
118130
private:
119131
class Impl;
120132
std::unique_ptr<Impl> impl_;

sherpa-onnx/csrc/online-stream.cc

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
#include "sherpa-onnx/csrc/online-stream.h"
55

66
#include <memory>
7+
#include <string>
8+
#include <unordered_map>
79
#include <utility>
810
#include <vector>
911

1012
#include "sherpa-onnx/csrc/features.h"
13+
#include "sherpa-onnx/csrc/text-utils.h"
1114
#include "sherpa-onnx/csrc/transducer-keyword-decoder.h"
1215

1316
namespace sherpa_onnx {
@@ -128,6 +131,39 @@ class OnlineStream::Impl {
128131
return paraformer_alpha_cache_;
129132
}
130133

134+
void SetOption(const std::string &key, const std::string &value) {
135+
options_[key] = value;
136+
}
137+
138+
bool HasOption(const std::string &key) const {
139+
return options_.count(key) != 0;
140+
}
141+
142+
const std::string &GetOption(const std::string &key) const {
143+
auto it = options_.find(key);
144+
if (it != options_.end()) {
145+
return it->second;
146+
}
147+
static const std::string kEmpty;
148+
return kEmpty;
149+
}
150+
151+
int32_t GetOptionInt(const std::string &key, int32_t default_value) const {
152+
auto it = options_.find(key);
153+
if (it != options_.end()) {
154+
return ToIntOrDefault(it->second, default_value);
155+
}
156+
return default_value;
157+
}
158+
159+
float GetOptionFloat(const std::string &key, float default_value) const {
160+
auto it = options_.find(key);
161+
if (it != options_.end()) {
162+
return ToFloatOrDefault(it->second, default_value);
163+
}
164+
return default_value;
165+
}
166+
131167
void SetFasterDecoder(std::unique_ptr<kaldi_decoder::FasterDecoder> decoder) {
132168
faster_decoder_ = std::move(decoder);
133169
}
@@ -159,6 +195,7 @@ class OnlineStream::Impl {
159195
std::vector<float> paraformer_encoder_out_cache_;
160196
std::vector<float> paraformer_alpha_cache_;
161197
OnlineParaformerDecoderResult paraformer_result_;
198+
std::unordered_map<std::string, std::string> options_;
162199
std::unique_ptr<kaldi_decoder::FasterDecoder> faster_decoder_;
163200
int32_t faster_decoder_processed_frames_ = 0;
164201
};
@@ -282,4 +319,27 @@ std::vector<float> &OnlineStream::GetParaformerAlphaCache() {
282319
return impl_->GetParaformerAlphaCache();
283320
}
284321

322+
void OnlineStream::SetOption(const std::string &key,
323+
const std::string &value) {
324+
impl_->SetOption(key, value);
325+
}
326+
327+
bool OnlineStream::HasOption(const std::string &key) const {
328+
return impl_->HasOption(key);
329+
}
330+
331+
const std::string &OnlineStream::GetOption(const std::string &key) const {
332+
return impl_->GetOption(key);
333+
}
334+
335+
int32_t OnlineStream::GetOptionInt(const std::string &key,
336+
int32_t default_value) const {
337+
return impl_->GetOptionInt(key, default_value);
338+
}
339+
340+
float OnlineStream::GetOptionFloat(const std::string &key,
341+
float default_value) const {
342+
return impl_->GetOptionFloat(key, default_value);
343+
}
344+
285345
} // namespace sherpa_onnx

sherpa-onnx/csrc/online-stream.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define SHERPA_ONNX_CSRC_ONLINE_STREAM_H_
77

88
#include <memory>
9+
#include <string>
910
#include <vector>
1011

1112
#include "kaldi-decoder/csrc/faster-decoder.h"
@@ -111,6 +112,18 @@ class OnlineStream {
111112
std::vector<float> &GetParaformerEncoderOutCache();
112113
std::vector<float> &GetParaformerAlphaCache();
113114

115+
// Generic per-stream option mechanism (key-value string pairs).
116+
void SetOption(const std::string &key, const std::string &value);
117+
bool HasOption(const std::string &key) const;
118+
119+
// Returns the value for the given key, or an empty string if the key
120+
// does not exist. No exception is thrown for missing keys.
121+
const std::string &GetOption(const std::string &key) const;
122+
int32_t GetOptionInt(const std::string &key,
123+
int32_t default_value = 0) const;
124+
float GetOptionFloat(const std::string &key,
125+
float default_value = 0.0f) const;
126+
114127
private:
115128
class Impl;
116129
std::unique_ptr<Impl> impl_;

0 commit comments

Comments
 (0)