@@ -212,13 +212,15 @@ void SherpaOnnxDestroyOnlineRecognizer(
212212
213213const SherpaOnnxOnlineStream *SherpaOnnxCreateOnlineStream (
214214 const SherpaOnnxOnlineRecognizer *recognizer) {
215+ if (!recognizer) return nullptr ;
215216 SherpaOnnxOnlineStream *stream =
216217 new SherpaOnnxOnlineStream (recognizer->impl ->CreateStream ());
217218 return stream;
218219}
219220
220221const SherpaOnnxOnlineStream *SherpaOnnxCreateOnlineStreamWithHotwords (
221222 const SherpaOnnxOnlineRecognizer *recognizer, const char *hotwords) {
223+ if (!recognizer) return nullptr ;
222224 SherpaOnnxOnlineStream *stream =
223225 new SherpaOnnxOnlineStream (recognizer->impl ->CreateStream (hotwords));
224226 return stream;
@@ -232,23 +234,27 @@ void SherpaOnnxDestroyOnlineStream(const SherpaOnnxOnlineStream *stream) {
232234void SherpaOnnxOnlineStreamAcceptWaveform (const SherpaOnnxOnlineStream *stream,
233235 int32_t sample_rate,
234236 const float *samples, int32_t n) {
237+ if (!stream) return ;
235238 stream->impl ->AcceptWaveform (sample_rate, samples, n);
236239}
237240
238241int32_t SherpaOnnxIsOnlineStreamReady (
239242 const SherpaOnnxOnlineRecognizer *recognizer,
240243 const SherpaOnnxOnlineStream *stream) {
244+ if (!recognizer || !stream) return 0 ;
241245 return recognizer->impl ->IsReady (stream->impl .get ());
242246}
243247
244248void SherpaOnnxDecodeOnlineStream (const SherpaOnnxOnlineRecognizer *recognizer,
245249 const SherpaOnnxOnlineStream *stream) {
250+ if (!recognizer || !stream) return ;
246251 recognizer->impl ->DecodeStream (stream->impl .get ());
247252}
248253
249254void SherpaOnnxDecodeMultipleOnlineStreams (
250255 const SherpaOnnxOnlineRecognizer *recognizer,
251256 const SherpaOnnxOnlineStream **streams, int32_t n) {
257+ if (!recognizer || !streams) return ;
252258 std::vector<sherpa_onnx::OnlineStream *> ss (n);
253259 for (int32_t i = 0 ; i != n; ++i) {
254260 ss[i] = streams[i]->impl .get ();
@@ -259,6 +265,7 @@ void SherpaOnnxDecodeMultipleOnlineStreams(
259265const SherpaOnnxOnlineRecognizerResult *SherpaOnnxGetOnlineStreamResult (
260266 const SherpaOnnxOnlineRecognizer *recognizer,
261267 const SherpaOnnxOnlineStream *stream) {
268+ if (!recognizer || !stream) return nullptr ;
262269 sherpa_onnx::OnlineRecognizerResult result =
263270 recognizer->impl ->GetResult (stream->impl .get ());
264271 const auto &text = result.text ;
@@ -351,10 +358,12 @@ void SherpaOnnxDestroyOnlineStreamResultJson(const char *s) {
351358
352359void SherpaOnnxOnlineStreamReset (const SherpaOnnxOnlineRecognizer *recognizer,
353360 const SherpaOnnxOnlineStream *stream) {
361+ if (!recognizer || !stream) return ;
354362 recognizer->impl ->Reset (stream->impl .get ());
355363}
356364
357365void SherpaOnnxOnlineStreamInputFinished (const SherpaOnnxOnlineStream *stream) {
366+ if (!stream) return ;
358367 stream->impl ->InputFinished ();
359368}
360369
@@ -379,6 +388,7 @@ int32_t SherpaOnnxOnlineStreamHasOption(const SherpaOnnxOnlineStream *stream,
379388int32_t SherpaOnnxOnlineStreamIsEndpoint (
380389 const SherpaOnnxOnlineRecognizer *recognizer,
381390 const SherpaOnnxOnlineStream *stream) {
391+ if (!recognizer || !stream) return 0 ;
382392 return recognizer->impl ->IsEndpoint (stream->impl .get ());
383393}
384394
@@ -681,6 +691,7 @@ const SherpaOnnxOfflineRecognizer *SherpaOnnxCreateOfflineRecognizer(
681691void SherpaOnnxOfflineRecognizerSetConfig (
682692 const SherpaOnnxOfflineRecognizer *recognizer,
683693 const SherpaOnnxOfflineRecognizerConfig *config) {
694+ if (!recognizer || !config) return ;
684695 sherpa_onnx::OfflineRecognizerConfig recognizer_config =
685696 GetOfflineRecognizerConfig (config);
686697 recognizer->impl ->SetConfig (recognizer_config);
@@ -694,13 +705,15 @@ void SherpaOnnxDestroyOfflineRecognizer(
694705
695706const SherpaOnnxOfflineStream *SherpaOnnxCreateOfflineStream (
696707 const SherpaOnnxOfflineRecognizer *recognizer) {
708+ if (!recognizer) return nullptr ;
697709 SherpaOnnxOfflineStream *stream =
698710 new SherpaOnnxOfflineStream (recognizer->impl ->CreateStream ());
699711 return stream;
700712}
701713
702714const SherpaOnnxOfflineStream *SherpaOnnxCreateOfflineStreamWithHotwords (
703715 const SherpaOnnxOfflineRecognizer *recognizer, const char *hotwords) {
716+ if (!recognizer) return nullptr ;
704717 SherpaOnnxOfflineStream *stream =
705718 new SherpaOnnxOfflineStream (recognizer->impl ->CreateStream (hotwords));
706719 return stream;
@@ -714,6 +727,7 @@ void SherpaOnnxDestroyOfflineStream(const SherpaOnnxOfflineStream *stream) {
714727void SherpaOnnxAcceptWaveformOffline (const SherpaOnnxOfflineStream *stream,
715728 int32_t sample_rate, const float *samples,
716729 int32_t n) {
730+ if (!stream) return ;
717731 stream->impl ->AcceptWaveform (sample_rate, samples, n);
718732}
719733
@@ -738,12 +752,14 @@ int32_t SherpaOnnxOfflineStreamHasOption(const SherpaOnnxOfflineStream *stream,
738752void SherpaOnnxDecodeOfflineStream (
739753 const SherpaOnnxOfflineRecognizer *recognizer,
740754 const SherpaOnnxOfflineStream *stream) {
755+ if (!recognizer || !stream) return ;
741756 recognizer->impl ->DecodeStream (stream->impl .get ());
742757}
743758
744759void SherpaOnnxDecodeMultipleOfflineStreams (
745760 const SherpaOnnxOfflineRecognizer *recognizer,
746761 const SherpaOnnxOfflineStream **streams, int32_t n) {
762+ if (!recognizer || !streams) return ;
747763 std::vector<sherpa_onnx::OfflineStream *> ss (n);
748764 for (int32_t i = 0 ; i != n; ++i) {
749765 ss[i] = streams[i]->impl .get ();
@@ -753,6 +769,7 @@ void SherpaOnnxDecodeMultipleOfflineStreams(
753769
754770const SherpaOnnxOfflineRecognizerResult *SherpaOnnxGetOfflineStreamResult (
755771 const SherpaOnnxOfflineStream *stream) {
772+ if (!stream) return nullptr ;
756773 const sherpa_onnx::OfflineRecognitionResult &result =
757774 stream->impl ->GetResult ();
758775 const auto &text = result.text ;
@@ -1035,36 +1052,42 @@ void SherpaOnnxDestroyKeywordSpotter(const SherpaOnnxKeywordSpotter *spotter) {
10351052
10361053const SherpaOnnxOnlineStream *SherpaOnnxCreateKeywordStream (
10371054 const SherpaOnnxKeywordSpotter *spotter) {
1055+ if (!spotter) return nullptr ;
10381056 SherpaOnnxOnlineStream *stream =
10391057 new SherpaOnnxOnlineStream (spotter->impl ->CreateStream ());
10401058 return stream;
10411059}
10421060
10431061const SherpaOnnxOnlineStream *SherpaOnnxCreateKeywordStreamWithKeywords (
10441062 const SherpaOnnxKeywordSpotter *spotter, const char *keywords) {
1063+ if (!spotter) return nullptr ;
10451064 SherpaOnnxOnlineStream *stream =
10461065 new SherpaOnnxOnlineStream (spotter->impl ->CreateStream (keywords));
10471066 return stream;
10481067}
10491068
10501069int32_t SherpaOnnxIsKeywordStreamReady (const SherpaOnnxKeywordSpotter *spotter,
10511070 const SherpaOnnxOnlineStream *stream) {
1071+ if (!spotter || !stream) return 0 ;
10521072 return spotter->impl ->IsReady (stream->impl .get ());
10531073}
10541074
10551075void SherpaOnnxDecodeKeywordStream (const SherpaOnnxKeywordSpotter *spotter,
10561076 const SherpaOnnxOnlineStream *stream) {
1077+ if (!spotter || !stream) return ;
10571078 spotter->impl ->DecodeStream (stream->impl .get ());
10581079}
10591080
10601081void SherpaOnnxResetKeywordStream (const SherpaOnnxKeywordSpotter *spotter,
10611082 const SherpaOnnxOnlineStream *stream) {
1083+ if (!spotter || !stream) return ;
10621084 spotter->impl ->Reset (stream->impl .get ());
10631085}
10641086
10651087void SherpaOnnxDecodeMultipleKeywordStreams (
10661088 const SherpaOnnxKeywordSpotter *spotter,
10671089 const SherpaOnnxOnlineStream **streams, int32_t n) {
1090+ if (!spotter || !streams || n <= 0 ) return ;
10681091 std::vector<sherpa_onnx::OnlineStream *> ss (n);
10691092 for (int32_t i = 0 ; i != n; ++i) {
10701093 ss[i] = streams[i]->impl .get ();
@@ -1075,6 +1098,7 @@ void SherpaOnnxDecodeMultipleKeywordStreams(
10751098const SherpaOnnxKeywordResult *SherpaOnnxGetKeywordResult (
10761099 const SherpaOnnxKeywordSpotter *spotter,
10771100 const SherpaOnnxOnlineStream *stream) {
1101+ if (!spotter || !stream) return nullptr ;
10781102 const sherpa_onnx::KeywordResult &result =
10791103 spotter->impl ->GetResult (stream->impl .get ());
10801104 const auto &keyword = result.keyword ;
@@ -1152,6 +1176,7 @@ void SherpaOnnxDestroyKeywordResult(const SherpaOnnxKeywordResult *r) {
11521176const char *SherpaOnnxGetKeywordResultAsJson (
11531177 const SherpaOnnxKeywordSpotter *spotter,
11541178 const SherpaOnnxOnlineStream *stream) {
1179+ if (!spotter || !stream) return nullptr ;
11551180 const sherpa_onnx::KeywordResult &result =
11561181 spotter->impl ->GetResult (stream->impl .get ());
11571182
@@ -1189,15 +1214,21 @@ void SherpaOnnxDestroyCircularBuffer(const SherpaOnnxCircularBuffer *buffer) {
11891214
11901215void SherpaOnnxCircularBufferPush (const SherpaOnnxCircularBuffer *buffer,
11911216 const float *p, int32_t n) {
1217+ if (!buffer) return ;
11921218 buffer->impl ->Push (p, n);
11931219}
11941220
11951221const float *SherpaOnnxCircularBufferGet (const SherpaOnnxCircularBuffer *buffer,
11961222 int32_t start_index, int32_t n) {
1223+ if (!buffer) return nullptr ;
11971224 std::vector<float > v = buffer->impl ->Get (start_index, n);
11981225
1199- float *p = new float [n];
1200- std::copy (v.begin (), v.end (), p);
1226+ float *p = nullptr ;
1227+ if (!v.empty ()) {
1228+ p = new float [v.size ()]();
1229+ std::copy (v.begin (), v.end (), p);
1230+ }
1231+
12011232 return p;
12021233}
12031234
@@ -1208,18 +1239,22 @@ void SherpaOnnxCircularBufferFree(const float *p) {
12081239
12091240void SherpaOnnxCircularBufferPop (const SherpaOnnxCircularBuffer *buffer,
12101241 int32_t n) {
1242+ if (!buffer) return ;
12111243 buffer->impl ->Pop (n);
12121244}
12131245
12141246int32_t SherpaOnnxCircularBufferSize (const SherpaOnnxCircularBuffer *buffer) {
1247+ if (!buffer) return 0 ;
12151248 return buffer->impl ->Size ();
12161249}
12171250
12181251int32_t SherpaOnnxCircularBufferHead (const SherpaOnnxCircularBuffer *buffer) {
1252+ if (!buffer) return 0 ;
12191253 return buffer->impl ->Head ();
12201254}
12211255
12221256void SherpaOnnxCircularBufferReset (const SherpaOnnxCircularBuffer *buffer) {
1257+ if (!buffer) return ;
12231258 buffer->impl ->Reset ();
12241259}
12251260
@@ -2686,6 +2721,7 @@ void SherpaOnnxDestroyLinearResampler(const SherpaOnnxLinearResampler *p) {
26862721const SherpaOnnxResampleOut *SherpaOnnxLinearResamplerResample (
26872722 const SherpaOnnxLinearResampler *p, const float *input, int32_t input_dim,
26882723 int32_t flush) {
2724+ if (!p) return nullptr ;
26892725 std::vector<float > o;
26902726 p->impl ->Resample (input, input_dim, flush, &o);
26912727
@@ -2707,15 +2743,18 @@ void SherpaOnnxLinearResamplerResampleFree(const SherpaOnnxResampleOut *p) {
27072743
27082744int32_t SherpaOnnxLinearResamplerResampleGetInputSampleRate (
27092745 const SherpaOnnxLinearResampler *p) {
2746+ if (!p) return 0 ;
27102747 return p->impl ->GetInputSamplingRate ();
27112748}
27122749
27132750int32_t SherpaOnnxLinearResamplerResampleGetOutputSampleRate (
27142751 const SherpaOnnxLinearResampler *p) {
2752+ if (!p) return 0 ;
27152753 return p->impl ->GetOutputSamplingRate ();
27162754}
27172755
27182756void SherpaOnnxLinearResamplerReset (const SherpaOnnxLinearResampler *p) {
2757+ if (!p) return ;
27192758 p->impl ->Reset ();
27202759}
27212760
0 commit comments