Add Swift example for generating subtitles (#318)
This commit is contained in:
@@ -9,9 +9,11 @@
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "sherpa-onnx/csrc/circular-buffer.h"
|
||||
#include "sherpa-onnx/csrc/display.h"
|
||||
#include "sherpa-onnx/csrc/offline-recognizer.h"
|
||||
#include "sherpa-onnx/csrc/online-recognizer.h"
|
||||
#include "sherpa-onnx/csrc/voice-activity-detector.h"
|
||||
|
||||
struct SherpaOnnxOnlineRecognizer {
|
||||
std::unique_ptr<sherpa_onnx::OnlineRecognizer> impl;
|
||||
@@ -127,7 +129,7 @@ void DecodeMultipleOnlineStreams(SherpaOnnxOnlineRecognizer *recognizer,
|
||||
recognizer->impl->DecodeStreams(ss.data(), n);
|
||||
}
|
||||
|
||||
SherpaOnnxOnlineRecognizerResult *GetOnlineStreamResult(
|
||||
const SherpaOnnxOnlineRecognizerResult *GetOnlineStreamResult(
|
||||
SherpaOnnxOnlineRecognizer *recognizer, SherpaOnnxOnlineStream *stream) {
|
||||
sherpa_onnx::OnlineRecognizerResult result =
|
||||
recognizer->impl->GetResult(stream->impl.get());
|
||||
@@ -340,7 +342,7 @@ void DecodeMultipleOfflineStreams(SherpaOnnxOfflineRecognizer *recognizer,
|
||||
recognizer->impl->DecodeStreams(ss.data(), n);
|
||||
}
|
||||
|
||||
SherpaOnnxOfflineRecognizerResult *GetOfflineStreamResult(
|
||||
const SherpaOnnxOfflineRecognizerResult *GetOfflineStreamResult(
|
||||
SherpaOnnxOfflineStream *stream) {
|
||||
const sherpa_onnx::OfflineRecognitionResult &result =
|
||||
stream->impl->GetResult();
|
||||
@@ -372,3 +374,128 @@ void DestroyOfflineRecognizerResult(
|
||||
delete[] r->timestamps;
|
||||
delete r;
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// For VAD
|
||||
// ============================================================
|
||||
//
|
||||
struct SherpaOnnxCircularBuffer {
|
||||
std::unique_ptr<sherpa_onnx::CircularBuffer> impl;
|
||||
};
|
||||
|
||||
SherpaOnnxCircularBuffer *SherpaOnnxCreateCircularBuffer(int32_t capacity) {
|
||||
SherpaOnnxCircularBuffer *buffer = new SherpaOnnxCircularBuffer;
|
||||
buffer->impl = std::make_unique<sherpa_onnx::CircularBuffer>(capacity);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void SherpaOnnxDestroyCircularBuffer(SherpaOnnxCircularBuffer *buffer) {
|
||||
delete buffer;
|
||||
}
|
||||
|
||||
void SherpaOnnxCircularBufferPush(SherpaOnnxCircularBuffer *buffer,
|
||||
const float *p, int32_t n) {
|
||||
buffer->impl->Push(p, n);
|
||||
}
|
||||
|
||||
const float *SherpaOnnxCircularBufferGet(SherpaOnnxCircularBuffer *buffer,
|
||||
int32_t start_index, int32_t n) {
|
||||
std::vector<float> v = buffer->impl->Get(start_index, n);
|
||||
|
||||
float *p = new float[n];
|
||||
std::copy(v.begin(), v.end(), p);
|
||||
return p;
|
||||
}
|
||||
|
||||
void SherpaOnnxCircularBufferFree(const float *p) { delete[] p; }
|
||||
|
||||
void SherpaOnnxCircularBufferPop(SherpaOnnxCircularBuffer *buffer, int32_t n) {
|
||||
buffer->impl->Pop(n);
|
||||
}
|
||||
|
||||
int32_t SherpaOnnxCircularBufferSize(SherpaOnnxCircularBuffer *buffer) {
|
||||
return buffer->impl->Size();
|
||||
}
|
||||
|
||||
void SherpaOnnxCircularBufferReset(SherpaOnnxCircularBuffer *buffer) {
|
||||
buffer->impl->Reset();
|
||||
}
|
||||
|
||||
struct SherpaOnnxVoiceActivityDetector {
|
||||
std::unique_ptr<sherpa_onnx::VoiceActivityDetector> impl;
|
||||
};
|
||||
|
||||
SherpaOnnxVoiceActivityDetector *SherpaOnnxCreateVoiceActivityDetector(
|
||||
const SherpaOnnxVadModelConfig *config, float buffer_size_in_seconds) {
|
||||
sherpa_onnx::VadModelConfig vad_config;
|
||||
|
||||
vad_config.silero_vad.model = SHERPA_ONNX_OR(config->silero_vad.model, "");
|
||||
vad_config.silero_vad.threshold =
|
||||
SHERPA_ONNX_OR(config->silero_vad.threshold, 0.5);
|
||||
|
||||
vad_config.silero_vad.min_silence_duration =
|
||||
SHERPA_ONNX_OR(config->silero_vad.min_silence_duration, 0.5);
|
||||
|
||||
vad_config.silero_vad.min_speech_duration =
|
||||
SHERPA_ONNX_OR(config->silero_vad.min_speech_duration, 0.25);
|
||||
|
||||
vad_config.silero_vad.window_size =
|
||||
SHERPA_ONNX_OR(config->silero_vad.window_size, 512);
|
||||
|
||||
vad_config.sample_rate = SHERPA_ONNX_OR(config->sample_rate, 16000);
|
||||
vad_config.num_threads = SHERPA_ONNX_OR(config->num_threads, 1);
|
||||
vad_config.provider = SHERPA_ONNX_OR(config->provider, "cpu");
|
||||
vad_config.debug = SHERPA_ONNX_OR(config->debug, false);
|
||||
|
||||
if (vad_config.debug) {
|
||||
fprintf(stderr, "%s\n", vad_config.ToString().c_str());
|
||||
}
|
||||
|
||||
SherpaOnnxVoiceActivityDetector *p = new SherpaOnnxVoiceActivityDetector;
|
||||
p->impl = std::make_unique<sherpa_onnx::VoiceActivityDetector>(
|
||||
vad_config, buffer_size_in_seconds);
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
void SherpaOnnxDestroyVoiceActivityDetector(
|
||||
SherpaOnnxVoiceActivityDetector *p) {
|
||||
delete p;
|
||||
}
|
||||
|
||||
void SherpaOnnxVoiceActivityDetectorAcceptWaveform(
|
||||
SherpaOnnxVoiceActivityDetector *p, const float *samples, int32_t n) {
|
||||
p->impl->AcceptWaveform(samples, n);
|
||||
}
|
||||
|
||||
int32_t SherpaOnnxVoiceActivityDetectorEmpty(
|
||||
SherpaOnnxVoiceActivityDetector *p) {
|
||||
return p->impl->Empty();
|
||||
}
|
||||
|
||||
SHERPA_ONNX_API void SherpaOnnxVoiceActivityDetectorPop(
|
||||
SherpaOnnxVoiceActivityDetector *p) {
|
||||
p->impl->Pop();
|
||||
}
|
||||
|
||||
SHERPA_ONNX_API const SherpaOnnxSpeechSegment *
|
||||
SherpaOnnxVoiceActivityDetectorFront(SherpaOnnxVoiceActivityDetector *p) {
|
||||
const sherpa_onnx::SpeechSegment &segment = p->impl->Front();
|
||||
|
||||
SherpaOnnxSpeechSegment *ans = new SherpaOnnxSpeechSegment;
|
||||
ans->start = segment.start;
|
||||
ans->samples = new float[segment.samples.size()];
|
||||
std::copy(segment.samples.begin(), segment.samples.end(), ans->samples);
|
||||
ans->n = segment.samples.size();
|
||||
|
||||
return ans;
|
||||
}
|
||||
|
||||
void SherpaOnnxDestroySpeechSegment(const SherpaOnnxSpeechSegment *p) {
|
||||
delete[] p->samples;
|
||||
delete p;
|
||||
}
|
||||
|
||||
void SherpaOnnxVoiceActivityDetectorReset(SherpaOnnxVoiceActivityDetector *p) {
|
||||
p->impl->Reset();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user